Example usage for org.hibernate SQLQuery uniqueResult

List of usage examples for org.hibernate SQLQuery uniqueResult

Introduction

In this page you can find the example usage for org.hibernate SQLQuery uniqueResult.

Prototype

R uniqueResult();

Source Link

Document

Convenience method to return a single instance that matches the query, or null if the query returns no results.

Usage

From source file:org.openmrs.api.db.hibernate.HibernateEncounterDAO.java

License:Mozilla Public License

/**
 * @see org.openmrs.api.db.EncounterDAO#getSavedEncounterDatetime(org.openmrs.Encounter)
 *///www.java2 s .  c  o  m
public Date getSavedEncounterDatetime(Encounter encounter) {
    SQLQuery sql = sessionFactory.getCurrentSession()
            .createSQLQuery("select encounter_datetime from encounter where encounter_id = :encounterId");
    sql.setInteger("encounterId", encounter.getEncounterId());
    return (Date) sql.uniqueResult();
}

From source file:org.openmrs.api.db.hibernate.HibernateEncounterDAO.java

License:Mozilla Public License

/**
 * @see org.openmrs.api.db.EncounterDAO#getSavedEncounterLocation(org.openmrs.Encounter)
 *//*from ww  w .  j  ava  2s .  com*/
public Location getSavedEncounterLocation(Encounter encounter) {
    SQLQuery sql = sessionFactory.getCurrentSession()
            .createSQLQuery("select location_id from encounter where encounter_id = :encounterId");
    sql.setInteger("encounterId", encounter.getEncounterId());
    return Context.getLocationService().getLocation((Integer) sql.uniqueResult());
}

From source file:org.openmrs.api.db.hibernate.HibernatePersonDAO.java

License:Mozilla Public License

/**
 * @see org.openmrs.api.db.PersonDAO#getSavedPersonAttributeTypeName(org.openmrs.PersonAttributeType)
 *//*from  w  w  w  . ja v a  2 s.  co  m*/
public String getSavedPersonAttributeTypeName(PersonAttributeType personAttributeType) {
    SQLQuery sql = sessionFactory.getCurrentSession().createSQLQuery(
            "select name from person_attribute_type where person_attribute_type_id = :personAttributeTypeId");
    sql.setInteger("personAttributeTypeId", personAttributeType.getId());
    return (String) sql.uniqueResult();
}

From source file:org.openmrs.module.camerwa.db.hibernate.HibernateCamerwaDAO.java

License:Open Source License

public Date getTreeMonthBefore(String date) {
    Session session = getSessionFactory().getCurrentSession();
    SQLQuery query = session.createSQLQuery("SELECT DATE_SUB(CAST('" + date + "' AS DATE), INTERVAL 3 MONTH);");
    return (Date) query.uniqueResult();

}

From source file:org.openmrs.module.logmanager.db.hibernate.HibernateLogManagerDAO.java

License:Open Source License

/**
 * @see LogManagerDAO#getMySQLVersion()//from w w w .j  ava  2 s. c o m
 */
public String getMySQLVersion() throws DAOException {
    Session session = sessionFactory.getCurrentSession();
    SQLQuery query = session.createSQLQuery("SELECT VERSION();");
    return query.uniqueResult().toString();
}

From source file:org.openmrs.module.muzima.api.db.hibernate.HibernateCoreDao.java

License:Open Source License

@Override
@Transactional(readOnly = true)//from  w  w  w.j  a  v  a2  s. c  o m
public Number countPatients(final String cohortUuid, final Date syncDate) throws DAOException {
    String hqlQuery = " select count(p.patient_id) as total from patient p, cohort c, cohort_member m "
            + " where c.uuid = :uuid and p.patient_id = m.patient_id " + " and c.cohort_id = m.cohort_id "
            + " and c.voided = false and p.voided = false ";
    if (syncDate != null) {
        hqlQuery = hqlQuery
                + " and ( (c.date_created is not null and c.date_changed is null and c.date_voided is null and c.date_created >= :syncDate) or "
                + "       (c.date_created is not null and c.date_changed is not null and c.date_voided is null and c.date_changed >= :syncDate) or "
                + "       (c.date_created is not null and c.date_changed is not null and c.date_voided is not null and c.date_voided >= :syncDate) ) "
                + " and ( (p.date_created is not null and p.date_changed is null and p.date_voided is null and p.date_created >= :syncDate) or "
                + "       (p.date_created is not null and p.date_changed is not null and p.date_voided is null and p.date_changed >= :syncDate) or "
                + "       (p.date_created is not null and p.date_changed is not null and p.date_voided is not null and p.date_voided >= :syncDate) ) ";
    }
    SQLQuery query = getSessionFactory().getCurrentSession().createSQLQuery(hqlQuery);
    query.addScalar("total");
    query.setParameter("uuid", cohortUuid);
    if (syncDate != null) {
        query.setParameter("syncDate", syncDate);
    }
    return (Number) query.uniqueResult();
}

From source file:org.openmrs.module.orderextension.api.db.HibernateOrderExtensionDAO.java

License:Open Source License

/**
  * @see org.openmrs.module.orderextension.api.db.OrderExtensionDAO#getMaxNumberOfCyclesForRegimen(org.openmrs.module.orderextension.DrugRegimen)
  *//*from  w  w w  . j a  v a  2s.  c  o m*/
@Override
public Integer getMaxNumberOfCyclesForRegimen(Patient patient, DrugRegimen regimen) {

    SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(
            "select MAX(og.cycle_number) from orderextension_order_group og, orderextension_order er, orders o where og.id = er.group_id and er.order_id = o.order_id and o.voided = 0 and og.voided = 0 and og.order_set_id = :orderSetId and o.patient_id = :patientId and o.start_date >= :startDate");
    query.setInteger("patientId", patient.getId());
    query.setInteger("orderSetId", regimen.getOrderSet().getId());
    query.setDate("startDate", regimen.getFirstDrugOrderStartDate());

    return (Integer) query.uniqueResult();
}

From source file:org.openmrs.module.programOver.db.hibernate.ProgramOverviewDAOimpl.java

License:Open Source License

private String maxDateForNextVisit(int patientId) throws ParseException {

    Date maxDate;/*from   w w  w .  ja  va  2  s.  c om*/
    String returnedDate = "";

    DbSession session = getSessionFactory().getCurrentSession();
    SQLQuery query = session
            .createSQLQuery("select cast(MAX(obs.value_datetime) as DATE) from obs where obs.concept_id = 5096 "
                    + " and obs.person_id = " + patientId);

    Date record = (Date) query.uniqueResult();

    DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

    if (record != null) {

        maxDate = record;
        returnedDate = sdf.format(maxDate);
    }

    String returnValue = returnedDate;

    return returnValue;

}

From source file:org.openmrs.module.tracnetreporting.impl.TracNetIndicatorServiceImpl.java

License:Open Source License

private String maxDateForNextVisit(int patientId) throws ParseException {

    Date maxDate;/*from  www.ja  v  a2  s.  c  o  m*/
    String returnedDate = "";
    String returnValue = "";

    try {

        Session session = getSessionFactory().getCurrentSession();
        SQLQuery query = session
                .createSQLQuery("select  cast(MAX(obs.value_datetime) as DATE) from obs where obs.concept_id = "
                        + ConstantValues.NEXT_SCHEDULED_VISIT + " and obs.person_id = " + patientId
                        + " group by obs.person_id");

        Date record = (Date) query.uniqueResult();

        DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

        if (record != null) {

            maxDate = record;
            returnedDate = sdf.format(maxDate);
        }

        returnValue = returnedDate;
    } catch (Exception e) {
        // TODO: handle exception
    }

    return returnValue;
}

From source file:org.openmrs.module.usagestatistics.api.db.hibernate.HibernateUsageStatisticsDAO.java

License:Open Source License

protected int getFoundByTotal(Date from, Date until, String filter, int foundBy) {

    StringBuffer sb = new StringBuffer();
    sb.append("SELECT COUNT(*) ");
    sb.append("FROM " + TABLE_PATIENT + " e ");
    sb.append("INNER JOIN usagestatistics_usage u ON e.usage_id = u.usage_id ");

    sb.append("WHERE e.found_by = " + foundBy + " ");

    if (filter.equalsIgnoreCase("Created")) {
        sb.append("AND e.created = 1 ");
    } else if (filter.equalsIgnoreCase("Updated")) {
        sb.append("AND e.updated = 1 ");
    } else if (filter.equalsIgnoreCase("Voided")) {
        sb.append("AND e.voided = 1 ");
    } else if (filter.equalsIgnoreCase("Encounter")) {
        sb.append("AND e.usage_id IN (SELECT usage_id FROM " + TABLE_ENCOUNTER + ") ");
    } else if (filter.equalsIgnoreCase("Visit")) {
        sb.append("AND e.usage_id IN (SELECT usage_id FROM " + TABLE_VISIT + ") ");
    } else if (filter.equalsIgnoreCase("Order")) {
        sb.append("AND e.usage_id IN (SELECT usage_id FROM " + TABLE_ORDER + ") ");
    }/*from  w ww.j  a  v a  2s.co  m*/

    appendDateRange(sb, "u", from, until);

    SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sb.toString());
    return ((BigInteger) query.uniqueResult()).intValue();
}