Package jodd.db

Examples of jodd.db.DbSession


   * Creates new database session using DB session provider.
   * Usually, this method does not belong in AppCore, but in some
   * DbCore, as it is used for low level database access.
   */
  public DbSession createDbSession() {
    return new DbSession(connectionProvider);
  }
View Full Code Here


   * Returns <code>true</code> if database exist. This method does not use
   * thread provided db session, but common db session.
   */
  public boolean checkDb() {
    log.debug("Check database.");
    DbSession dbSession = AppCore.ref.createDbSession();
    DbQuery query = new DbQuery(dbSession, "select count(1) from up_user_level");
    try {
      query.execute();
      log.debug("Database OK.");
        return true;
    } catch (Exception ignored) {
      return false;
    } finally {
      query.close();
      dbSession.closeSession();
    }
  }
View Full Code Here

      StreamUtil.close(in);
    }

    String[] queries = StringUtil.splitc(sql, ";");

    DbSession dbSession = AppCore.ref.createDbSession();
    for (int i = 0; i < queries.length; i++) {
      String q = queries[i];
      q = cleanSql(q);
      if (q.isEmpty()) {
        continue;
      }
      DbQuery query = new DbQuery(dbSession, q);
      query.executeUpdate();
      log.debug("executed query #" + (i + 1));
    }
    dbSession.closeSession();
  }
View Full Code Here

    String[] queries = StringUtil.splitc(sql, ";");
    JDateTime jdt = new JDateTime();
    jdt.subDay(61)// we have 62 pre-defined questions in total

    DbSession dbSession = AppCore.ref.createDbSession();
    for (int i = 0; i < queries.length; i++) {
      String q = queries[i];
      q = cleanSql(q);
      if (q.isEmpty()) {
        continue;
      }
     
      if (q.contains("$date")) {
        q = StringUtil.replace(q, "$date", jdt.toString("YYYYMMDD"));
        jdt.addDay(1);
      }

      DbQuery query = new DbQuery(dbSession, q);
      query.executeUpdate();
      log.debug("executed query #" + (i + 1));
    }

    dbSession.closeSession();
  }
View Full Code Here

    dbSession.closeSession();
  }


  private void insertAnswers() {
    DbSession dbSession = AppCore.ref.createDbSession();

    DbOomQuery dbQuery = new DbOomQuery(dbSession, DbSqlBuilder.sql("select $C{q.*} from $T{Question q} order by $q.id"));
    List<Question> questions =  dbQuery.list();
    dbQuery.close();
   
View Full Code Here

public class AppDaoTest extends DbHsqldbTestCase {

  @Test
  public void testAppDao1() {
    DbSession session = new DbSession(cp);
    ThreadDbSessionHolder.set(session);

    AppDao appDao = new AppDao();
    appDao.setKeysGeneratedByDatabase(false);
    DbIdGenerator didg = new DbIdGenerator();
    BeanUtil.setDeclaredProperty(appDao, "dbIdGenerator", didg);

    // store

    Girl girl = new Girl();
    girl.setName("Emma");
    girl.setSpeciality("piano");

    assertNull(girl.getId());
    appDao.store(girl);
    assertEquals(1, girl.getId().longValue());

    // update

    girl.setSpeciality("Guitar");
    appDao.store(girl);

    long count = appDao.count(Girl.class);
    assertEquals(1, count);

    Girl dbGirl = appDao.findById(Girl.class, 1);
    assertEquals("Guitar", dbGirl.getSpeciality());
    assertEquals("Emma", dbGirl.getName());

    session.closeSession();
    ThreadDbSessionHolder.remove();
  }
View Full Code Here

public class DbIdGeneratorTest extends DbHsqldbTestCase {

  @Test
  public void testIdGen() throws Exception {
    DbSession session = new DbSession(cp);
    ThreadDbSessionHolder.set(session);

    AppDao appDao = new AppDao();
    appDao.setKeysGeneratedByDatabase(false);
    appDao.dbIdGenerator = new DbIdGenerator();

    Girl girl = new Girl();
    girl.name = "One";
    girl.speciality = "Code";

    appDao.store(girl);
    assertEquals(1, girl.getId().longValue());

    girl = new Girl();
    girl.name = "Two";
    girl.speciality = "Dddd";

    appDao.store(girl);
    assertEquals(2, girl.getId().longValue());

    appDao.dbIdGenerator.reset();

    girl = new Girl();
    girl.name = "Three";
    girl.speciality = "Ssss";

    appDao.store(girl);
    assertEquals(3, girl.getId().longValue());

    assertEquals(3, appDao.count(Girl.class));

    assertNotNull(appDao.findById(Girl.class, 1));
    assertNotNull(appDao.findById(Girl.class, 2));
    assertNotNull(appDao.findById(Girl.class, 3));

    session.closeSession();
    ThreadDbSessionHolder.remove();
  }
View Full Code Here

    }
  }


  protected Tester2 insertEntry() {
    DbSession session = new DbSession();

    Tester2 tester2 = new Tester2();
    tester2.id = 1;
    tester2.name = "Hello";
    tester2.value = Integer.valueOf(123);
    tester2.time = new JDateTime(2014, 1, 30, 10, 42, 34, 0).convertToSqlTimestamp();
    tester2.time2 = new JDateTime(2014, 1, 31, 11, 41, 32, 0);

    DbOomQuery dbOomQuery = DbOomQuery.query(session, DbEntitySql.insert(tester2));
    dbOomQuery.setGeneratedKey();
    int result = dbOomQuery.executeUpdate();

    assertEquals(1, result);
    session.closeSession();

    return tester2;
  }
View Full Code Here

    return tester2;
  }

  protected void loadEntry(Tester2 tester2) {
    DbSession session = new DbSession();

    DbOomQuery dbOomQuery = DbOomQuery.query(session, DbEntitySql.findById(Tester2.class, tester2.id));
    Tester2 tester21 = dbOomQuery.find(Tester2.class);

    assertNotNull(tester21);

    assertEquals(tester2.id, tester21.id);
    assertEquals(tester2.value, tester21.value);
    assertEquals(tester2.name, tester21.name);
    assertEquals(tester2.time, tester21.time);
    assertEquals(tester2.time2, tester21.time2);

    session.closeSession();
  }
View Full Code Here

    cp.setPassword("");
    cp.init();
    dbtxm = new DbJtxTransactionManager(cp);

    // initial data
    DbSession session = new DbSession(cp);

    executeUpdate(session, "drop table BOY if exists");
    executeUpdate(session, "drop table GIRL if exists");

    String sql = "create table GIRL (" +
        "ID      integer    not null," +
        "NAME    varchar(20)  not null," +
        "SPECIALITY  varchar(20)  null," +
        "primary key (ID)" +
        ')';

    executeUpdate(session, sql);

    sql = "create table BOY (" +
        "ID      integer  not null," +
        "GIRL_ID  integer  null," +
        "NAME  varchar(20)  null," +
        "primary key (ID)," +
        "FOREIGN KEY (GIRL_ID) REFERENCES GIRL (ID)" +
        ')';

    executeUpdate(session, sql);
    session.closeSession();
  }
View Full Code Here

TOP

Related Classes of jodd.db.DbSession

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.