Package org.nutz.dao.sql

Examples of org.nutz.dao.sql.Sql


        }
        return pager;
    }

    public static <T> List<T> queryList(Dao dao, Class<T> klass, String sql_str) {
        Sql sql = Sqls.create(sql_str)
                        .setCallback(Sqls.callback.entities())
                        .setEntity(dao.getEntity(klass));
        dao.execute(sql);
        return sql.getList(klass);
    }
View Full Code Here


        dao.execute(sql);
        return sql.getList(klass);
    }

    public static Object query(Dao dao, String sql_str, SqlCallback callback) {
        Sql sql = Sqls.create(sql_str).setCallback(callback);
        dao.execute(sql);
        return sql.getResult();
    }
View Full Code Here

   
    /**
     * 查询sql并把结果放入传入的class组成的List中
     */
    public static <T> List<T> query(Dao dao, Class<T> classOfT, String sql, Condition cnd, Pager pager) {
        Sql sql2 = Sqls.queryEntity(sql);
        sql2.setEntity(dao.getEntity(classOfT));
        sql2.setCondition(cnd);
        sql2.setPager(pager);
        dao.execute(sql2);
        return sql2.getList(classOfT);
    }
View Full Code Here

   
    /**
     * 查询某sql的结果条数
     */
    public static int queryCount(Dao dao, String sql) {
        Sql sql2 = Sqls.fetchLong("select count(1) FROM ("+sql+")");
        dao.execute(sql2);
        return sql2.getInt();
    }
View Full Code Here

                pet.setNickName("AA_" + pet.getName().toUpperCase());
                dao.update(pet);
            }
        });
        Entity<Pet> en = dao.getEntity(Pet.class);
        Sql sql = Sqls.queryRecord("SELECT * FROM t_pet");
        dao.execute(sql);
        List<Record> recs = sql.getList(Record.class);
        Pet[] pets = new Pet[recs.size()];
        int i = 0;
        for (Record rec : recs)
            pets[i++] = rec.toEntity(en);

View Full Code Here

    public void createTableTimestampFieldDefaultNullInMySQL() throws Exception {
        boolean isMySql = dao.meta().isMySql();
        // 这个仅仅测试MySQL数据库
        if (isMySql) {
            dao.create(TableWithTimestampInMySql.class, true);
            Sql descTable = Sqls.create(DESC_TABLE_SQL);
            descTable.params().set("tableName", "t_ts");
            descTable.setCallback(new SqlCallback() {
                public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                    boolean ts1 = false;
                    boolean ts2 = false;
                    boolean ts3 = false;
                    boolean ts4 = false;
                    while (rs.next()) {
                        String columnName = rs.getString(1);
                        String isNullable = rs.getString(2);
                        String defaultValue = rs.getString(3);
                        String extra = rs.getString(4);
                        if ("ts1".equals(columnName)) {
                            if ((null == defaultValue || "NULL".equalsIgnoreCase(defaultValue))
                                && "YES".equalsIgnoreCase(isNullable)) {
                                ts1 = true;
                            }
                        }
                        if ("ts2".equals(columnName)) {
                            if (null != defaultValue
                                && "0000-00-00 00:00:00".equals(defaultValue)
                                && "YES".equalsIgnoreCase(isNullable)) {
                                ts2 = true;
                            }
                        }
                        if ("ts3".equals(columnName)) {
                            if (null != defaultValue
                                && "0000-00-00 00:00:00".equals(defaultValue)
                                && "NO".equalsIgnoreCase(isNullable)) {
                                ts3 = true;
                            }
                        }
                        if ("ts4".equals(columnName)) {
                            if (null != defaultValue
                                && "CURRENT_TIMESTAMP".equalsIgnoreCase(defaultValue)
                                && "NO".equalsIgnoreCase(isNullable)
                                && "ON UPDATE CURRENT_TIMESTAMP".equalsIgnoreCase(extra)) {
                                ts4 = true;
                            }
                        }
                    }
                    return ts1 && ts2 && ts3 && ts4;
                }
            });
            dao.execute(descTable);
            boolean isTrue = descTable.getObject(Boolean.class);
            Assert.assertTrue(isTrue);
        }
    }
View Full Code Here

    }

    @Test
    public void run_2_sqls_with_error() {
        assertEquals(0, dao.count(Pet.class));
        Sql sql1 = Sqls.create("INSERT INTO t_pet (name) VALUES ('A')");
        Sql sql2 = Sqls.create("INSERT INTO t_pet (nocol) VALUES ('B')");
        try {
            dao.execute(sql1, sql2);
            fail();
        }
        catch (DaoException e) {}
View Full Code Here

        for (int i = 0; i < 100; i++) {
            dao.insert(Pet.class, Chain.make("name", "record" + i).add("nickName", "Time="+System.currentTimeMillis()));
        }
        Pager pager = dao.createPager(5, 5);
        pager.setRecordCount(dao.count(Pet.class));
        Sql sql = Sqls.queryEntity("select * from t_pet");
        sql.setEntity(dao.getEntity(Pet.class));
        sql.setPager(pager);
        dao.execute(sql);
       
        List<Pet> pets = sql.getList(Pet.class);
        assertNotNull(pets);
        assertEquals(5, pets.size());
        assertEquals("record20", pets.get(0).getName());
        assertEquals("record21", pets.get(1).getName());
        assertEquals("record22", pets.get(2).getName());
View Full Code Here

        boolean isMySql = dao.meta().isMySql();
        // 这个仅仅测试MySQL数据库
        if (isMySql) {
            dao.create(TableWithComment.class, true);
            // 表注释
            Sql findTableComment = Sqls.create(FIND_TABLE_COMMENT_MYSQL);
            findTableComment.params().set("tableName", "t_twc");
            findTableComment.setCallback(Sqls.callback.str());
            dao.execute(findTableComment);
            String tableComment = findTableComment.getString();
            Assert.assertTrue(!Strings.isBlank(tableComment) && "测试表".equals(tableComment));
            // 字段注释
            Sql findIdComment = Sqls.create(FIND_COLUMN_COMMENT_MYSQL);
            findIdComment.params().set("tableName", "t_twc").set("columnName", "id");
            findIdComment.setCallback(Sqls.callback.str());
            dao.execute(findIdComment);
            String idComment = findIdComment.getString();
            Assert.assertTrue(!Strings.isBlank(idComment) && "唯一主键".equals(idComment));

            Sql findNameComment = Sqls.create(FIND_COLUMN_COMMENT_MYSQL);
            findNameComment.params().set("tableName", "t_twc").set("columnName", "nm");
            findNameComment.setCallback(Sqls.callback.str());
            dao.execute(findNameComment);
            String nameComment = findNameComment.getString();
            Assert.assertTrue(!Strings.isBlank(nameComment) && "name".equals(nameComment));

            Sql findNumComment = Sqls.create(FIND_COLUMN_COMMENT_MYSQL);
            findNumComment.params().set("tableName", "t_twc").set("columnName", "num");
            findNumComment.setCallback(Sqls.callback.str());
            dao.execute(findNumComment);
            String numComment = findNumComment.getString();
            Assert.assertTrue(Strings.isBlank(numComment));
        }
    }
View Full Code Here

        // 字段注释
        if (en.hasColumnComment()) {
            List<Sql> sqls = new ArrayList<Sql>();
            for (MappingField mf : en.getMappingFields()) {
                if (mf.hasColumnComment()) {
                    Sql columnCommentSQL = Sqls.create(commentColumn);
                    columnCommentSQL.vars()
                                    .set("table", en.getTableName())
                                    .set("column", mf.getColumnName())
                                    .set("columnComment", mf.getColumnComment());
                    sqls.add(columnCommentSQL);
                }
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.