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(); }
public int delete() { SQLStatement stat = new SQLStatement(db); stat.appendSQL("DELETE FROM "); from.appendSQL(stat); appendWhere(stat); StatementLogger.delete(stat.getSQL()); return stat.executeUpdate(); }
public int update() { if (updateColumnDeclarations.size() == 0) { throw new RuntimeException("Missing set or increment call."); } SQLStatement stat = new SQLStatement(db); stat.appendSQL("UPDATE "); from.appendSQL(stat); stat.appendSQL(" SET "); int i = 0; for (UpdateColumn declaration : updateColumnDeclarations) { if (i++ > 0) { stat.appendSQL(", "); } declaration.appendSQL(stat); } appendWhere(stat); StatementLogger.update(stat.getSQL()); return stat.executeUpdate(); }
void merge(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("MERGE INTO "); buff.append(db.getDialect().getTableName(schemaName, tableName)).append(" ("); buff.resetCount(); for (FieldDefinition field : fields) { buff.appendExceptFirst(", "); buff.append(field.columnName); } buff.append(") KEY("); buff.resetCount(); for (FieldDefinition field : fields) { if (field.isPrimaryKey) { buff.appendExceptFirst(", "); buff.append(field.columnName); } } buff.append(") "); buff.resetCount(); buff.append("VALUES ("); for (FieldDefinition field : fields) { buff.appendExceptFirst(", "); buff.append('?'); Object value = getValue(obj, field); stat.addParameter(value); } buff.append(')'); stat.setSQL(buff.toString()); StatementLogger.merge(stat.getSQL()); stat.executeUpdate(); }
@SuppressWarnings("unchecked") void appendFromWhere(SQLStatement stat) { stat.appendSQL(" FROM "); from.appendSQL(stat); for (SelectTable join : joins) { join.appendSQLAsJoin(stat, this); } appendWhere(stat); if (groupByExpressions != null) { stat.appendSQL(" GROUP BY "); int i = 0; for (Object obj : groupByExpressions) { if (i++ > 0) { stat.appendSQL(", "); } appendSQL(stat, obj); stat.appendSQL(" "); } } if (!orderByList.isEmpty()) { stat.appendSQL(" ORDER BY "); int i = 0; for (OrderExpression<T> o : orderByList) { if (i++ > 0) { stat.appendSQL(", "); } o.appendSQL(stat); stat.appendSQL(" "); } } if (limit > 0) { db.getDialect().appendLimit(stat, limit); } if (offset > 0) { db.getDialect().appendOffset(stat, offset); } StatementLogger.select(stat.getSQL()); }
long insert(Db db, Object obj, boolean returnKey) { SQLStatement stat = new SQLStatement(db); StatementBuilder buff = new StatementBuilder("INSERT INTO "); buff.append(db.getDialect().getTableName(schemaName, tableName)).append('('); for (FieldDefinition field : fields) { buff.appendExceptFirst(", "); buff.append(field.columnName); } buff.append(") VALUES("); buff.resetCount(); for (FieldDefinition field : fields) { buff.appendExceptFirst(", "); buff.append('?'); Object value = getValue(obj, field); stat.addParameter(value); } buff.append(')'); stat.setSQL(buff.toString()); StatementLogger.insert(stat.getSQL()); if (returnKey) { return stat.executeInsert(); } return stat.executeUpdate(); }
TableDefinition<T> createTableIfRequired(Db db) { if (!createTableIfRequired) { // skip table and index creation // but still check for upgrades db.upgradeTable(this); return this; } SQLDialect dialect = db.getDialect(); SQLStatement stat = new SQLStatement(db); StatementBuilder buff; if (memoryTable && dialect.supportsMemoryTables()) { buff = new StatementBuilder("CREATE MEMORY TABLE IF NOT EXISTS "); } else { buff = new StatementBuilder("CREATE TABLE IF NOT EXISTS "); } buff.append(dialect.getTableName(schemaName, tableName)).append('('); for (FieldDefinition field : fields) { buff.appendExceptFirst(", "); buff.append(field.columnName).append(' ').append(field.dataType); if (field.maxLength > 0) { buff.append('(').append(field.maxLength).append(')'); } if (field.isAutoIncrement) { buff.append(" AUTO_INCREMENT"); } if (!field.allowNull) { buff.append(" NOT NULL"); } // default values if (!field.isAutoIncrement && !field.isPrimaryKey) { String dv = field.defaultValue; if (!StringUtils.isNullOrEmpty(dv)) { if (ModelUtils.isProperlyFormattedDefaultValue(dv) && ModelUtils.isValidDefaultValue(field.field.getType(), dv)) { buff.append(" DEFAULT " + dv); } } } } // primary key if (primaryKeyColumnNames != null && primaryKeyColumnNames.size() > 0) { buff.append(", PRIMARY KEY("); buff.resetCount(); for (String n : primaryKeyColumnNames) { buff.appendExceptFirst(", "); buff.append(n); } buff.append(')'); } buff.append(')'); stat.setSQL(buff.toString()); StatementLogger.create(stat.getSQL()); stat.executeUpdate(); // create indexes for (IndexDefinition index : indexes) { String sql = db.getDialect().getCreateIndex(schemaName, tableName, index); stat.setSQL(sql); StatementLogger.create(stat.getSQL()); stat.executeUpdate(); } // tables are created using IF NOT EXISTS // but we may still need to upgrade db.upgradeTable(this); return this; }