List of usage examples for org.hibernate SQLQuery uniqueResult
R uniqueResult();
From source file:org.openmrs.module.usagestatistics.db.hibernate.HibernateUsageStatisticsDAO.java
License:Open Source License
/** * Calculates the total usage events with the given "found by" option * @param from the start date/*from w w w. j a va 2s . com*/ * @param until the end date * @param location the location (if null then all locations) * @param filter the types of actions to include * @param foundBy the found by option * @return the total number of events */ protected int getFoundByTotal(Date from, Date until, Location location, ActionCriteria filter, int foundBy) { int locationAttrTypeId = Options.getInstance().getLocationAttributeTypeId(); StringBuffer sb = new StringBuffer(); sb.append("SELECT COUNT(*) "); sb.append("FROM " + TABLE_USAGE + " e "); // Check user location if (location != null) { sb.append("INNER JOIN users us ON e.user_id = us.user_id "); sb.append("INNER JOIN person_attribute pa ON (pa.person_id = us.person_id) AND pa.voided = 0 "); sb.append(" AND pa.person_attribute_type_id = " + locationAttrTypeId + " AND pa.value = \"" + location.getLocationId() + "\" "); } sb.append("WHERE e.found_by = " + foundBy + " "); if (filter == ActionCriteria.CREATED) sb.append("AND e.created = 1 "); else if (filter == ActionCriteria.ENCOUNTER) sb.append("AND e.usage_id IN (SELECT usage_id FROM " + TABLE_ENCOUNTER + ") "); else if (filter == ActionCriteria.UPDATED) sb.append("AND e.updated = 1 "); else if (filter == ActionCriteria.VOIDED) sb.append("AND e.voided = 1 "); appendDateRange(sb, "e", from, until); SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sb.toString()); return ((BigInteger) query.uniqueResult()).intValue(); }
From source file:org.openxdata.server.dao.hibernate.HibernateEditableDAO.java
License:Apache License
@Override @Secured("Perm_View_Form_Data") public BigInteger getNumberOfResponses(String formBinding) { SQLQuery countQuery = getSession().createSQLQuery("select count(*) from " + formBinding); BigInteger count = (BigInteger) countQuery.uniqueResult(); return count; }
From source file:org.openxdata.server.dao.hibernate.HibernateFormDownloadDAO.java
License:Apache License
@Override public String getXformLocaleText(Integer formId, String locale) { Session session = getSessionFactory().getCurrentSession(); SQLQuery query = session .createSQLQuery("select xform_text from form_definition_version_text where locale_key='" + locale + "' and form_definition_version_id=" + formId); query.addScalar("xform_text", Hibernate.STRING); String text = (String) query.uniqueResult(); return text;//from ww w . ja v a2 s . c o m }
From source file:org.photovault.common.DbInfo.java
License:Open Source License
/** * Get the schema version of current database using SQL query. This is * useful in cases in which schema has not been upgraded to latest version, * and {@link #getDbInfo() } method cannot construct the DbInfo object. * @return Current schema version./*from w w w . java2 s .c o m*/ */ static int querySchemaVersion() { Session session = HibernateUtil.getSessionFactory().openSession(); SQLQuery q = session.createSQLQuery("select schema_version from database_info"); int v = (Integer) q.uniqueResult(); session.close(); return v; }
From source file:org.rhq.server.metrics.migrator.workers.AggregateDataMigratorTest.java
License:Open Source License
@Test public void testEstimateTask() throws Exception { //tell the method story as it happens: mock or create dependencies and configure //those dependencies to get the method under test to completion DatabaseType databaseType = DatabaseType.Oracle; DataMigratorConfiguration mockConfig = mock(DataMigratorConfiguration.class); when(mockConfig.getDatabaseType()).thenReturn(databaseType); MetricsIndexMigrator mockMetricsIndexUpdateAccumulator = mock(MetricsIndexMigrator.class); PowerMockito.whenNew(MetricsIndexMigrator.class).withArguments(eq(MigrationTable.ONE_HOUR), eq(mockConfig)) .thenReturn(mockMetricsIndexUpdateAccumulator); EntityManager mockEntityManager = mock(EntityManager.class); when(mockConfig.getEntityManager()).thenReturn(mockEntityManager); org.hibernate.Session mockHibernateSession = mock(org.hibernate.Session.class); when(mockEntityManager.getDelegate()).thenReturn(mockHibernateSession); SessionFactory mockSessionFactory = mock(SessionFactory.class); when(mockHibernateSession.getSessionFactory()).thenReturn(mockSessionFactory); StatelessSession mockStatelessSession = mock(StatelessSession.class); when(mockSessionFactory.openStatelessSession()).thenReturn(mockStatelessSession); org.hibernate.SQLQuery mockQuery = mock(org.hibernate.SQLQuery.class); when(mockStatelessSession.createSQLQuery(any(String.class))).thenReturn(mockQuery); when(mockQuery.uniqueResult()).thenReturn("1000"); ScrollableDataSource mockDataSource = mock(ScrollableDataSource.class); PowerMockito.whenNew(ScrollableDataSource.class) .withArguments(eq(mockEntityManager), eq(databaseType), any(), anyInt()).thenReturn(mockDataSource); when(mockDataSource.getData(eq(0), anyInt())).thenReturn(new ArrayList<Object[]>()); //create object to test and inject required dependencies AggregateDataMigrator objectUnderTest = new AggregateDataMigrator(MigrationTable.ONE_HOUR, mockConfig); //run code under test long estimateActual = objectUnderTest.estimate(); //verify the results (assert and mock verification) PowerMockito.verifyNew(MetricsIndexMigrator.class).withArguments(eq(MigrationTable.ONE_HOUR), eq(mockConfig));//from w w w .j a v a 2 s . c om PowerMockito.verifyNew(ScrollableDataSource.class, times(1)).withArguments(eq(mockEntityManager), eq(databaseType), any(), anyInt()); verify(mockStatelessSession, times(1)).createSQLQuery(any(String.class)); verify(mockDataSource, times(1)).initialize(); verify(mockDataSource, times(1)).getData(eq(0), anyInt()); verify(mockDataSource, times(1)).close(); verify(mockMetricsIndexUpdateAccumulator, times(1)).drain(); verifyNoMoreInteractions(mockDataSource); verifyNoMoreInteractions(mockMetricsIndexUpdateAccumulator); Assert.assertNotEquals(estimateActual, 0); }
From source file:org.rhq.server.metrics.migrator.workers.AggregateDataMigratorTest.java
License:Open Source License
@Test public void testMigrateTask() throws Exception { //tell the method story as it happens: mock or create dependencies and configure //those dependencies to get the method under test to completion DatabaseType databaseType = DatabaseType.Oracle; DataMigratorConfiguration mockConfig = mock(DataMigratorConfiguration.class); when(mockConfig.getDatabaseType()).thenReturn(databaseType); Session mockCassandraSession = mock(Session.class); when(mockConfig.getSession()).thenReturn(mockCassandraSession); MetricsIndexMigrator mockMetricsIndexUpdateAccumulator = mock(MetricsIndexMigrator.class); PowerMockito.whenNew(MetricsIndexMigrator.class).withArguments(eq(MigrationTable.ONE_HOUR), eq(mockConfig)) .thenReturn(mockMetricsIndexUpdateAccumulator); EntityManager mockEntityManager = mock(EntityManager.class); when(mockConfig.getEntityManager()).thenReturn(mockEntityManager); org.hibernate.Session mockHibernateSession = mock(org.hibernate.Session.class); when(mockEntityManager.getDelegate()).thenReturn(mockHibernateSession); SessionFactory mockSessionFactory = mock(SessionFactory.class); when(mockHibernateSession.getSessionFactory()).thenReturn(mockSessionFactory); StatelessSession mockStatelessSession = mock(StatelessSession.class); when(mockSessionFactory.openStatelessSession()).thenReturn(mockStatelessSession); org.hibernate.SQLQuery mockQuery = mock(org.hibernate.SQLQuery.class); when(mockStatelessSession.createSQLQuery(any(String.class))).thenReturn(mockQuery); when(mockQuery.uniqueResult()).thenReturn("1000"); ScrollableDataSource mockDataSource = mock(ScrollableDataSource.class); PowerMockito.whenNew(ScrollableDataSource.class) .withArguments(eq(mockEntityManager), eq(databaseType), any()).thenReturn(mockDataSource); List<Object[]> resultList = new ArrayList<Object[]>(); resultList.add(new Object[] { 100, 100, 100, 100, 100 }); resultList.add(new Object[] { 100, System.currentTimeMillis() - 100l, 100, 100, 100 }); for (int index = 0; index < 15; index++) { when(mockDataSource.getData(eq(0), anyInt())).thenReturn(resultList); when(mockDataSource.getData(eq(2), anyInt())).thenReturn(new ArrayList<Object[]>()); }//ww w. j a v a2 s .c om ResultSetFuture mockResultSetFuture = mock(ResultSetFuture.class); when(mockCassandraSession.executeAsync(any(Query.class))).thenReturn(mockResultSetFuture); //create object to test and inject required dependencies AggregateDataMigrator objectUnderTest = new AggregateDataMigrator(MigrationTable.ONE_HOUR, mockConfig); //run code under test objectUnderTest.migrate(); //verify the results (assert and mock verification) PowerMockito.verifyNew(MetricsIndexMigrator.class).withArguments(eq(MigrationTable.ONE_HOUR), eq(mockConfig)); PowerMockito.verifyNew(ScrollableDataSource.class, times(1)).withArguments(eq(mockEntityManager), eq(databaseType), any()); verify(mockDataSource, times(1)).initialize(); verify(mockDataSource, times(1)).getData(eq(0), anyInt()); verify(mockDataSource, times(1)).getData(eq(2), anyInt()); verify(mockDataSource, times(1)).close(); verify(mockMetricsIndexUpdateAccumulator, times(1)).add(eq(100), anyInt()); verify(mockMetricsIndexUpdateAccumulator, times(1)).drain(); verify(mockCassandraSession, times(1)).executeAsync(any(Query.class)); verify(mockResultSetFuture, times(1)).get(); verifyNoMoreInteractions(mockDataSource); verifyNoMoreInteractions(mockCassandraSession); verifyNoMoreInteractions(mockResultSetFuture); verifyNoMoreInteractions(mockMetricsIndexUpdateAccumulator); }
From source file:org.rhq.server.metrics.migrator.workers.RawDataMigratorTest.java
License:Open Source License
@Test public void testEstimateTask() throws Exception { //tell the method story as it happens: mock or create dependencies and configure //those dependencies to get the method under test to completion DatabaseType databaseType = DatabaseType.Oracle; DataMigratorConfiguration mockConfig = mock(DataMigratorConfiguration.class); when(mockConfig.getDatabaseType()).thenReturn(databaseType); MetricsIndexMigrator mockMetricsIndexUpdateAccumulator = mock(MetricsIndexMigrator.class); PowerMockito.whenNew(MetricsIndexMigrator.class).withArguments(eq(MigrationTable.RAW), eq(mockConfig)) .thenReturn(mockMetricsIndexUpdateAccumulator); EntityManager mockEntityManager = mock(EntityManager.class); when(mockConfig.getEntityManager()).thenReturn(mockEntityManager); org.hibernate.Session mockHibernateSession = mock(org.hibernate.Session.class); when(mockEntityManager.getDelegate()).thenReturn(mockHibernateSession); SessionFactory mockSessionFactory = mock(SessionFactory.class); when(mockHibernateSession.getSessionFactory()).thenReturn(mockSessionFactory); StatelessSession mockStatelessSession = mock(StatelessSession.class); when(mockSessionFactory.openStatelessSession()).thenReturn(mockStatelessSession); org.hibernate.SQLQuery mockQuery = mock(org.hibernate.SQLQuery.class); when(mockStatelessSession.createSQLQuery(any(String.class))).thenReturn(mockQuery); when(mockQuery.uniqueResult()).thenReturn("1000"); ScrollableDataSource mockDataSource = mock(ScrollableDataSource.class); PowerMockito.whenNew(ScrollableDataSource.class) .withArguments(eq(mockEntityManager), eq(databaseType), any(), anyInt()).thenReturn(mockDataSource); when(mockDataSource.getData(eq(0), anyInt())).thenReturn(new ArrayList<Object[]>()); //create object to test and inject required dependencies RawDataMigrator objectUnderTest = new RawDataMigrator(mockConfig); //run code under test long estimateActual = objectUnderTest.estimate(); //verify the results (assert and mock verification) PowerMockito.verifyNew(MetricsIndexMigrator.class).withArguments(eq(MigrationTable.RAW), eq(mockConfig)); PowerMockito.verifyNew(ScrollableDataSource.class, times(15)).withArguments(eq(mockEntityManager), eq(databaseType), any(), anyInt()); verify(mockStatelessSession, times(15)).createSQLQuery(any(String.class)); verify(mockDataSource, times(15)).initialize(); verify(mockDataSource, times(15)).getData(eq(0), anyInt()); verify(mockDataSource, times(15)).close(); verify(mockMetricsIndexUpdateAccumulator, times(1)).drain(); verifyNoMoreInteractions(mockDataSource); verifyNoMoreInteractions(mockMetricsIndexUpdateAccumulator); Assert.assertNotEquals(estimateActual, 0); }
From source file:org.rhq.server.metrics.migrator.workers.RawDataMigratorTest.java
License:Open Source License
@Test public void testMigrateTask() throws Exception { //tell the method story as it happens: mock or create dependencies and configure //those dependencies to get the method under test to completion DatabaseType databaseType = DatabaseType.Oracle; DataMigratorConfiguration mockConfig = mock(DataMigratorConfiguration.class); when(mockConfig.getDatabaseType()).thenReturn(databaseType); Session mockCassandraSession = mock(Session.class); when(mockConfig.getSession()).thenReturn(mockCassandraSession); MetricsIndexMigrator mockMetricsIndexUpdateAccumulator = mock(MetricsIndexMigrator.class); PowerMockito.whenNew(MetricsIndexMigrator.class).withArguments(eq(MigrationTable.RAW), eq(mockConfig)) .thenReturn(mockMetricsIndexUpdateAccumulator); EntityManager mockEntityManager = mock(EntityManager.class); when(mockConfig.getEntityManager()).thenReturn(mockEntityManager); org.hibernate.Session mockHibernateSession = mock(org.hibernate.Session.class); when(mockEntityManager.getDelegate()).thenReturn(mockHibernateSession); SessionFactory mockSessionFactory = mock(SessionFactory.class); when(mockHibernateSession.getSessionFactory()).thenReturn(mockSessionFactory); StatelessSession mockStatelessSession = mock(StatelessSession.class); when(mockSessionFactory.openStatelessSession()).thenReturn(mockStatelessSession); org.hibernate.SQLQuery mockQuery = mock(org.hibernate.SQLQuery.class); when(mockStatelessSession.createSQLQuery(any(String.class))).thenReturn(mockQuery); when(mockQuery.uniqueResult()).thenReturn("1000"); ScrollableDataSource mockDataSource = mock(ScrollableDataSource.class); PowerMockito.whenNew(ScrollableDataSource.class) .withArguments(eq(mockEntityManager), eq(databaseType), any()).thenReturn(mockDataSource); List<Object[]> resultList = new ArrayList<Object[]>(); resultList.add(new Object[] { 100, 100, 100 }); resultList.add(new Object[] { 100, System.currentTimeMillis() - 100l, 100 }); for (int index = 0; index < 15; index++) { when(mockDataSource.getData(eq(0), anyInt())).thenReturn(resultList); when(mockDataSource.getData(eq(2), anyInt())).thenReturn(new ArrayList<Object[]>()); }//www .j a v a 2 s . c om ResultSetFuture mockResultSetFuture = mock(ResultSetFuture.class); when(mockCassandraSession.executeAsync(any(Query.class))).thenReturn(mockResultSetFuture); //create object to test and inject required dependencies RawDataMigrator objectUnderTest = new RawDataMigrator(mockConfig); //run code under test objectUnderTest.migrate(); //verify the results (assert and mock verification) PowerMockito.verifyNew(MetricsIndexMigrator.class).withArguments(eq(MigrationTable.RAW), eq(mockConfig)); PowerMockito.verifyNew(ScrollableDataSource.class, times(15)).withArguments(eq(mockEntityManager), eq(databaseType), any()); verify(mockDataSource, times(15)).initialize(); verify(mockDataSource, times(15)).getData(eq(0), anyInt()); verify(mockDataSource, times(15)).getData(eq(2), anyInt()); verify(mockDataSource, times(15)).close(); verify(mockMetricsIndexUpdateAccumulator, times(15)).add(eq(100), anyInt()); verify(mockMetricsIndexUpdateAccumulator, times(1)).drain(); verify(mockCassandraSession, times(15)).executeAsync(any(Query.class)); verify(mockResultSetFuture, times(15)).get(); verifyNoMoreInteractions(mockDataSource); verifyNoMoreInteractions(mockCassandraSession); verifyNoMoreInteractions(mockResultSetFuture); verifyNoMoreInteractions(mockMetricsIndexUpdateAccumulator); }
From source file:org.smallmind.persistence.orm.hibernate.HibernateDao.java
License:Open Source License
public <T> T findBySQLQuery(Class<T> returnType, SQLQueryDetails sqlQueryDetails) { SQLQuery sqlQuery; sqlQuery = constructSQLQuery(sqlQueryDetails); if (Durable.class.isAssignableFrom(returnType)) { return returnType.cast(sqlQuery.addEntity(returnType).uniqueResult()); } else if (!SqlType.isKnownType(returnType)) { return returnType .cast(sqlQuery.setResultTransformer(Transformers.aliasToBean(returnType)).uniqueResult()); } else {/*from w ww . j a va 2s .com*/ Object obj; if ((obj = sqlQuery.uniqueResult()) != null) { return returnType.cast(obj); } return null; } }
From source file:org.snaker.engine.access.hibernate.HibernateAccess.java
License:Apache License
public Integer getLatestProcessVersion(String name) { SQLQuery query = getSession().createSQLQuery(QUERY_VERSION + " where name = ?"); query.setParameter(0, name);/*from w ww . ja v a 2 s .co m*/ Object result = query.uniqueResult(); return new Long(ClassHelper.castLong(result)).intValue(); }