Search in sources :

Example 31 with MappingField

use of org.nutz.dao.entity.MappingField in project nutz by nutzam.

the class ExtDaoInvocationHandler method updateBySpecialChain.

/**
     * 执行一个特殊的Chain(事实上普通Chain也能执行,但不建议使用)
     *
     * @see org.nutz.dao.Chain#addSpecial(String, Object)
     */
@SuppressWarnings({ "rawtypes" })
public static int updateBySpecialChain(Dao dao, Entity en, String tableName, Chain chain, Condition cnd) {
    if (en != null)
        tableName = en.getTableName();
    if (tableName == null)
        throw Lang.makeThrow(DaoException.class, "tableName and en is NULL !!");
    final StringBuilder sql = new StringBuilder("UPDATE ").append(tableName).append(" SET ");
    Chain head = chain.head();
    final List<Object> values = new ArrayList<Object>();
    final List<ValueAdaptor> adaptors = new ArrayList<ValueAdaptor>();
    while (head != null) {
        MappingField mf = null;
        if (en != null)
            mf = en.getField(head.name());
        String colName = head.name();
        if (mf != null)
            colName = mf.getColumnNameInSql();
        sql.append(colName).append("=");
        if (head.special()) {
            if (head.value() != null && head.value() instanceof String) {
                String str = (String) head.value();
                if (str.length() > 0) {
                    switch(str.charAt(0)) {
                        case '+':
                        case '-':
                        case '*':
                        case '/':
                        case '%':
                        case '&':
                        case '^':
                        case '|':
                            sql.append(colName);
                            break;
                    }
                }
            }
            sql.append(head.value());
        } else {
            sql.append("?");
            values.add(head.value());
            ValueAdaptor adaptor = Jdbcs.getAdaptorBy(head.value());
            if (mf != null && mf.getAdaptor() != null)
                adaptor = mf.getAdaptor();
            adaptors.add(adaptor);
        }
        sql.append(" ");
        head = head.next();
        if (head != null)
            sql.append(", ");
    }
    if (cnd != null)
        sql.append(" ").append(cnd.toSql(en));
    if (log.isDebugEnabled())
        log.debug(sql);
    final int[] ints = new int[1];
    dao.run(new ConnCallback() {

        public void invoke(Connection conn) throws Exception {
            PreparedStatement ps = conn.prepareStatement(sql.toString());
            try {
                for (int i = 0; i < values.size(); i++) adaptors.get(i).set(ps, values.get(i), i + 1);
                ints[0] = ps.executeUpdate();
            } finally {
                Daos.safeClose(ps);
            }
        }
    });
    return ints[0];
}
Also used : Chain(org.nutz.dao.Chain) ConnCallback(org.nutz.dao.ConnCallback) ValueAdaptor(org.nutz.dao.jdbc.ValueAdaptor) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) DaoException(org.nutz.dao.DaoException) MappingField(org.nutz.dao.entity.MappingField) DaoException(org.nutz.dao.DaoException) SQLException(java.sql.SQLException)

Example 32 with MappingField

use of org.nutz.dao.entity.MappingField in project nutz by nutzam.

the class ExtDaoInvocationHandler method insertBySpecialChain.

/**
     * 执行一个特殊的Chain(事实上普通Chain也能执行,但不建议使用)
     *
     * @see org.nutz.dao.Chain#addSpecial(String, Object)
     */
@SuppressWarnings({ "rawtypes" })
public static void insertBySpecialChain(Dao dao, Entity en, String tableName, Chain chain) {
    if (en != null)
        tableName = en.getTableName();
    if (tableName == null)
        throw Lang.makeThrow(DaoException.class, "tableName and en is NULL !!");
    final StringBuilder sql = new StringBuilder("INSERT INTO ").append(tableName).append(" (");
    StringBuilder _value_places = new StringBuilder(" VALUES(");
    final List<Object> values = new ArrayList<Object>();
    final List<ValueAdaptor> adaptors = new ArrayList<ValueAdaptor>();
    Chain head = chain.head();
    while (head != null) {
        String colName = head.name();
        MappingField mf = null;
        if (en != null) {
            mf = en.getField(colName);
            if (mf != null)
                colName = mf.getColumnNameInSql();
        }
        sql.append(colName);
        if (head.special()) {
            _value_places.append(head.value());
        } else {
            if (en != null)
                mf = en.getField(head.name());
            _value_places.append("?");
            values.add(head.value());
            ValueAdaptor adaptor = Jdbcs.getAdaptorBy(head.value());
            if (mf != null && mf.getAdaptor() != null)
                adaptor = mf.getAdaptor();
            adaptors.add(adaptor);
        }
        head = head.next();
        if (head != null) {
            sql.append(", ");
            _value_places.append(", ");
        }
    }
    sql.append(")");
    _value_places.append(")");
    sql.append(_value_places);
    if (log.isDebugEnabled())
        log.debug(sql);
    dao.run(new ConnCallback() {

        public void invoke(Connection conn) throws Exception {
            PreparedStatement ps = conn.prepareStatement(sql.toString());
            try {
                for (int i = 0; i < values.size(); i++) adaptors.get(i).set(ps, values.get(i), i + 1);
                ps.execute();
            } finally {
                Daos.safeClose(ps);
            }
        }
    });
}
Also used : Chain(org.nutz.dao.Chain) ConnCallback(org.nutz.dao.ConnCallback) ValueAdaptor(org.nutz.dao.jdbc.ValueAdaptor) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) DaoException(org.nutz.dao.DaoException) MappingField(org.nutz.dao.entity.MappingField) DaoException(org.nutz.dao.DaoException) SQLException(java.sql.SQLException)

Example 33 with MappingField

use of org.nutz.dao.entity.MappingField in project nutz by nutzam.

the class SimpleDaoTest method test_migration.

@Test
public void test_migration() {
    dao.execute(Sqls.create("drop table t_pet"));
    Entity<Pet> en = dao.getEntity(Pet.class);
    NutDao dao = (NutDao) this.dao;
    JdbcExpert expert = dao.getJdbcExpert();
    MappingField mf = en.getField("age");
    String str = "create table t_pet (" + mf.getColumnName() + " " + expert.evalFieldType(mf) + "," + mf.getColumnName() + "_2" + " " + expert.evalFieldType(mf) + ")";
    dao.execute(Sqls.create(str));
    Daos.migration(dao, Pet.class, !dao.meta().isSQLite(), !dao.meta().isSQLite());
}
Also used : NutDao(org.nutz.dao.impl.NutDao) JdbcExpert(org.nutz.dao.jdbc.JdbcExpert) MappingField(org.nutz.dao.entity.MappingField) Issue1163Pet(org.nutz.dao.test.meta.issue1163.Issue1163Pet) Pet(org.nutz.dao.test.meta.Pet) Test(org.junit.Test)

Aggregations

MappingField (org.nutz.dao.entity.MappingField)33 Sql (org.nutz.dao.sql.Sql)14 ArrayList (java.util.ArrayList)12 SQLException (java.sql.SQLException)5 PreparedStatement (java.sql.PreparedStatement)4 DaoException (org.nutz.dao.DaoException)4 Connection (java.sql.Connection)3 Chain (org.nutz.dao.Chain)3 ConnCallback (org.nutz.dao.ConnCallback)3 JdbcExpert (org.nutz.dao.jdbc.JdbcExpert)3 ResultSet (java.sql.ResultSet)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 Statement (java.sql.Statement)2 HashSet (java.util.HashSet)2 Test (org.junit.Test)2 ValueAdaptor (org.nutz.dao.jdbc.ValueAdaptor)2 Date (java.util.Date)1 Set (java.util.Set)1 FieldFilter (org.nutz.dao.FieldFilter)1 FieldMatcher (org.nutz.dao.FieldMatcher)1