Ejemplo n.º 1
0
 void delete(Db db, Object obj) {
   if (primaryKeyColumnNames == null || primaryKeyColumnNames.size() == 0) {
     throw new IllegalStateException(
         "No primary key columns defined "
             + "for table "
             + obj.getClass()
             + " - no update possible");
   }
   SQLStatement stat = new SQLStatement(db);
   StatementBuilder buff = new StatementBuilder("DELETE FROM ");
   buff.append(db.getDialect().getTableName(schemaName, tableName));
   buff.resetCount();
   Object alias = ClassUtils.newObject(obj.getClass());
   Query<Object> query = Query.from(db, alias);
   boolean firstCondition = true;
   for (FieldDefinition field : fields) {
     if (field.isPrimaryKey) {
       Object aliasValue = field.getValue(alias);
       Object value = field.getValue(obj);
       if (!firstCondition) {
         query.addConditionToken(ConditionAndOr.AND);
       }
       firstCondition = false;
       query.addConditionToken(new Condition<Object>(aliasValue, value, CompareType.EQUAL));
     }
   }
   stat.setSQL(buff.toString());
   query.appendWhere(stat);
   StatementLogger.delete(stat.getSQL());
   stat.executeUpdate();
 }
Ejemplo n.º 2
0
 public static Boolean not(Boolean x) {
   return Db.registerToken(
       ClassUtils.newObject(Boolean.class),
       new Function("", x) {
         public <T> void appendSQL(SQLStatement stat, Query<T> query) {
           stat.appendSQL("NOT ");
           query.appendSQL(stat, x[0]);
         }
       });
 }
Ejemplo n.º 3
0
 public static Boolean isNotNull(Object x) {
   return Db.registerToken(
       ClassUtils.newObject(Boolean.class),
       new Function("", x) {
         public <T> void appendSQL(SQLStatement stat, Query<T> query) {
           query.appendSQL(stat, x[0]);
           stat.appendSQL(" IS NOT NULL");
         }
       });
 }
Ejemplo n.º 4
0
 public static Boolean like(String x, String pattern) {
   Boolean o = ClassUtils.newObject(Boolean.class);
   return Db.registerToken(
       o,
       new Function("LIKE", x, pattern) {
         public <T> void appendSQL(SQLStatement stat, Query<T> query) {
           stat.appendSQL("(");
           query.appendSQL(stat, x[0]);
           stat.appendSQL(" LIKE ");
           query.appendSQL(stat, x[1]);
           stat.appendSQL(")");
         }
       });
 }
Ejemplo n.º 5
0
 public static Boolean or(Boolean... x) {
   return Db.registerToken(
       ClassUtils.newObject(Boolean.class),
       new Function("", (Object[]) x) {
         public <T> void appendSQL(SQLStatement stat, Query<T> query) {
           int i = 0;
           for (Object o : x) {
             if (i++ > 0) {
               stat.appendSQL(" OR ");
             }
             query.appendSQL(stat, o);
           }
         }
       });
 }
Ejemplo n.º 6
0
 private <X> List<X> select(Class<X> clazz, X x, boolean distinct) {
   List<X> result = New.arrayList();
   TableDefinition<X> def = db.define(clazz);
   SQLStatement stat = getSelectStatement(distinct);
   def.appendSelectList(stat, this, x);
   appendFromWhere(stat);
   ResultSet rs = stat.executeQuery();
   Statement s = null;
   try {
     s = rs.getStatement();
     while (rs.next()) {
       X row = ClassUtils.newObject(clazz);
       def.readRow(row, rs);
       result.add(row);
     }
   } catch (SQLException e) {
     throw new RuntimeException(e);
   } finally {
     JdbcUtils.closeSilently(rs);
     JdbcUtils.closeSilently(s);
   }
   return result;
 }
Ejemplo n.º 7
0
 public static Long count(Object x) {
   return Db.registerToken(ClassUtils.newObject(Long.class), new Function("COUNT", x));
 }
Ejemplo n.º 8
0
 @SuppressWarnings("unchecked")
 public static <T extends Number> T sum(T x) {
   return (T) Db.registerToken(ClassUtils.newObject(x.getClass()), new Function("SUM", x));
 }
Ejemplo n.º 9
0
 public static Integer length(Object x) {
   return Db.registerToken(ClassUtils.newObject(Integer.class), new Function("LENGTH", x));
 }
Ejemplo n.º 10
0
 @SuppressWarnings("unchecked")
 public static <X> X max(X x) {
   Class<X> clazz = (Class<X>) x.getClass();
   X o = ClassUtils.newObject(clazz);
   return Db.registerToken(o, new Function("MAX", x));
 }
Ejemplo n.º 11
0
 void initWithNewObject(Object obj) {
   Object o = ClassUtils.newObject(field.getType());
   setValue(obj, o);
 }