Package org.nutz.dao.sql

Examples of org.nutz.dao.sql.Sql


    // For mysql only
    if (dao.meta().isMySql()) {
      pojos.initPet();
      dao.insert(Pet.create(8));
      assertEquals(8, dao.count(Pet.class));
      Sql sql = Sqls.queryEntity("SELECT * FROM t_pet $condition LIMIT @off,@size ");
      sql.setEntity(dao.getEntity(Pet.class));
      sql.params().set("off", 2).set("size", 2);
      sql.setCondition(Cnd.orderBy().asc("name"));
      dao.execute(sql);
      List<Pet> pets = sql.getList(Pet.class);
      assertEquals(2, pets.size());
      assertEquals("pet_02", pets.get(0).getName());
      assertEquals("pet_03", pets.get(1).getName());

    } else {
View Full Code Here


  @Test
  public void test_query_without_entity() {
    pojos.initPet();
    dao.insert(Pet.create(4));
    Sql sql = Sqls.create("SELECT * FROM t_pet $condition");
    sql.setCondition(Cnd.where("name", "like", "pet_%").asc("name"));
    sql.setCallback(new SqlCallback() {
      public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
        List<Pet> pets = new ArrayList<Pet>(4);
        while (rs.next())
          pets.add(dao.getObject(Pet.class, rs, null));
        return pets;
      }
    });
    dao.execute(sql);
    List<Pet> pets = sql.getList(Pet.class);
    assertEquals(4, pets.size());
    assertEquals("pet_00", pets.get(0).getName());
    assertEquals("pet_01", pets.get(1).getName());
    assertEquals("pet_02", pets.get(2).getName());
    assertEquals("pet_03", pets.get(3).getName());
View Full Code Here

    pojos.init();
    ((NutDao) dao).setSqlManager(new FileSqlManager("org/nutz/dao/test/sqls/exec.sqls"));
    int platoonId = 23;
    try {
      pojos.initPlatoon(platoonId);
      Sql sql = dao.sqls().create("tank.insert");
      sql.vars().set("id", platoonId);
      sql.params().set("code", "T1").set("weight", 12);
      dao.execute(sql);

      sql = dao.sqls().create("tank.insert");
      sql.vars().set("id", platoonId);
      sql.params().set("code", "T2").set("weight", 13);
      dao.execute(sql);

      sql = dao.sqls().create("tank.insert");
      sql.vars().set("id", platoonId);
      sql.params().set("code", "T3").set("weight", 14);
      dao.execute(sql);

      sql = dao.sqls().create("tank.insert");
      sql.vars().set("id", platoonId);
      sql.params().set("code", "T4").set("weight", 15);
      dao.execute(sql);

      TableName.run(platoonId, new Atom() {
        public void run() {
          assertEquals(4, dao.count(Tank.class));
View Full Code Here

  }

  public boolean drop(String tableName) {
    if (!exists(tableName))
      return false;
    Sql sql = Sqls.createf("DROP TABLE %s", tableName);
    _exec(sql);
    return true;
  }
View Full Code Here

  @Test
  public void test_dynamic_query() {
    pojos.init();
    Platoon p = pojos.create4Platoon(Base.make("xyz"), "GG");
    Sql sql = dao.sqls().create("tank.query").setEntity(dao.getEntity(Tank.class));
    sql.vars().set("id", p.getId());
    sql.setCallback(Sqls.callback.entities());
    dao.execute(sql);
    assertEquals(2, sql.getList(Tank.class).size());

    pojos.dropPlatoon(p.getId());
  }
View Full Code Here

  }

  @Test
  public void test_statice_null_field() {
    pojos.init();
    Sql sql = Sqls.create("INSERT INTO dao_country (name,detail) VALUES(@name,@detail)");
    sql.params().set("name", "ABC").set("detail", "haha");
    dao.execute(sql);
    assertEquals(1, dao.count("dao_country"));

    sql = Sqls.create("UPDATE dao_country SET detail=@detail WHERE name=@name");
    sql.params().set("name", "ABC").set("detail", null);
    dao.execute(sql);
    Country c = dao.fetch(Country.class, "ABC");
    assertNull(c.getDetail());
  }
View Full Code Here

  public List<Sql> createCombo(String... keys) {
    if (null == keys || keys.length == 0)
      keys = this.keys();
    List<Sql> list = new ArrayList<Sql>(keys.length);
    for (String key : keys) {
      Sql sql = create(key);
      list.add(sql);
    }
    return list;
  }
View Full Code Here

            return;
        }
        List<Sql> sqls = new ArrayList<Sql>();
        // 表注释
        if (en.hasTableComment()) {
            Sql tableCommentSQL = Sqls.create(Strings.isBlank(commentTable)    ? DEFAULT_COMMENT_TABLE
                                                                            : commentTable);
            tableCommentSQL.vars()
                            .set("table", en.getTableName())
                            .set("tableComment", en.getTableComment());
            sqls.add(tableCommentSQL);
        }
        // 字段注释
        if (en.hasColumnComment()) {
            for (MappingField mf : en.getMappingFields()) {
                if (mf.hasColumnComment()) {
                    Sql columnCommentSQL = Sqls.create(Strings.isBlank(commentColumn)    ? DEFAULT_COMMENT_COLUMN
                                                                                        : commentColumn);
                    columnCommentSQL.vars()
                                    .set("table", en.getTableName())
                                    .set("column", mf.getColumnName())
                                    .set("columnComment", mf.getColumnComment());
                    sqls.add(columnCommentSQL);
                }
View Full Code Here

    }

    public boolean drop(String tableName) {
        if (!exists(tableName))
            return false;
        Sql sql = Sqls.createf("DROP TABLE %s", tableName);
        _exec(sql);
        return true;
    }
View Full Code Here

    public List<Sql> createCombo(String... keys) {
        if (null == keys || keys.length == 0)
            keys = this.keys();
        List<Sql> list = new ArrayList<Sql>(keys.length);
        for (String key : keys) {
            Sql sql = create(key);
            list.add(sql);
        }
        return list;
    }
View Full Code Here

TOP

Related Classes of org.nutz.dao.sql.Sql

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.