Repository: empire-db Updated Branches: refs/heads/master 7b7dce0c3 -> 6283adbbb
EMPIREDB-257 return unmodifiable lists. check database match on DBCommend object helper function to create "case when" expressions. Project: http://git-wip-us.apache.org/repos/asf/empire-db/repo Commit: http://git-wip-us.apache.org/repos/asf/empire-db/commit/6283adbb Tree: http://git-wip-us.apache.org/repos/asf/empire-db/tree/6283adbb Diff: http://git-wip-us.apache.org/repos/asf/empire-db/diff/6283adbb Branch: refs/heads/master Commit: 6283adbbb9599434dba958b852f4beed5a60e978 Parents: 7b7dce0 Author: Rainer Döbele <[email protected]> Authored: Fri Jun 30 09:26:55 2017 +0200 Committer: Rainer Döbele <[email protected]> Committed: Fri Jun 30 09:26:55 2017 +0200 ---------------------------------------------------------------------- .../org/apache/empire/data/bean/BeanClass.java | 3 +- .../org/apache/empire/data/bean/BeanDomain.java | 3 +- .../java/org/apache/empire/db/DBColumnExpr.java | 30 +------- .../java/org/apache/empire/db/DBCommand.java | 50 ++++++++----- .../java/org/apache/empire/db/DBDatabase.java | 77 +++++++++++++++++++- .../java/org/apache/empire/db/DBRowSet.java | 5 +- .../main/java/org/apache/empire/db/DBTable.java | 5 +- .../exceptions/DatabaseMismatchException.java | 38 ++++++++++ .../db/sqlite/DBDatabaseDriverSQLite.java | 9 +-- 9 files changed, 158 insertions(+), 62 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/empire-db/blob/6283adbb/empire-db/src/main/java/org/apache/empire/data/bean/BeanClass.java ---------------------------------------------------------------------- diff --git a/empire-db/src/main/java/org/apache/empire/data/bean/BeanClass.java b/empire-db/src/main/java/org/apache/empire/data/bean/BeanClass.java index 4fae288..e9d8ab7 100644 --- a/empire-db/src/main/java/org/apache/empire/data/bean/BeanClass.java +++ b/empire-db/src/main/java/org/apache/empire/data/bean/BeanClass.java @@ -19,6 +19,7 @@ package org.apache.empire.data.bean; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.apache.empire.data.Column; @@ -120,7 +121,7 @@ public abstract class BeanClass */ public List<BeanProperty> getProperties() { - return properties; + return Collections.unmodifiableList(this.properties); } /** http://git-wip-us.apache.org/repos/asf/empire-db/blob/6283adbb/empire-db/src/main/java/org/apache/empire/data/bean/BeanDomain.java ---------------------------------------------------------------------- diff --git a/empire-db/src/main/java/org/apache/empire/data/bean/BeanDomain.java b/empire-db/src/main/java/org/apache/empire/data/bean/BeanDomain.java index 9786bdc..a3964b1 100644 --- a/empire-db/src/main/java/org/apache/empire/data/bean/BeanDomain.java +++ b/empire-db/src/main/java/org/apache/empire/data/bean/BeanDomain.java @@ -19,6 +19,7 @@ package org.apache.empire.data.bean; import java.util.ArrayList; +import java.util.Collections; import java.util.List; /** @@ -52,7 +53,7 @@ public abstract class BeanDomain public List<BeanClass> getClasses() { - return classes; + return Collections.unmodifiableList(this.classes); } } http://git-wip-us.apache.org/repos/asf/empire-db/blob/6283adbb/empire-db/src/main/java/org/apache/empire/db/DBColumnExpr.java ---------------------------------------------------------------------- diff --git a/empire-db/src/main/java/org/apache/empire/db/DBColumnExpr.java b/empire-db/src/main/java/org/apache/empire/db/DBColumnExpr.java index c3a583f..99dc2cc 100644 --- a/empire-db/src/main/java/org/apache/empire/db/DBColumnExpr.java +++ b/empire-db/src/main/java/org/apache/empire/db/DBColumnExpr.java @@ -1010,32 +1010,6 @@ public abstract class DBColumnExpr extends DBExpr { return new DBCountExpr(this, true); } - - /** - * Detects the DataType of a given value. - * @param value the value to detect - * @return the DataType enum for the value - */ - protected DataType detectDataType(Object value) - { - if (value instanceof DBColumnExpr) - return ((DBColumnExpr)value).getDataType(); - if (value instanceof String) - return DataType.TEXT; - if ((value instanceof Integer) || (value instanceof Long)) - return DataType.INTEGER; - if (value instanceof Number) - return DataType.DECIMAL; - if (value instanceof Boolean) - return DataType.BOOL; - if (value instanceof Date) - return DataType.DATETIME; - if (value instanceof Character) - return DataType.CHAR; - if (value instanceof byte[]) - return DataType.BLOB; - return DataType.UNKNOWN; - } /** * Creates and returns a sql-expression that compares the current column expression with @@ -1051,13 +1025,13 @@ public abstract class DBColumnExpr extends DBExpr DataType dataType = DataType.UNKNOWN; if (otherwise!=null) { - dataType = detectDataType(otherwise); + dataType = getDatabase().detectDataType(otherwise); } if (dataType==DataType.UNKNOWN) { for (Object v : valueMap.values()) { - dataType = detectDataType(v); + dataType = getDatabase().detectDataType(v); if (dataType!=DataType.UNKNOWN) break; } http://git-wip-us.apache.org/repos/asf/empire-db/blob/6283adbb/empire-db/src/main/java/org/apache/empire/db/DBCommand.java ---------------------------------------------------------------------- diff --git a/empire-db/src/main/java/org/apache/empire/db/DBCommand.java b/empire-db/src/main/java/org/apache/empire/db/DBCommand.java index 422bfe7..b935e5e 100644 --- a/empire-db/src/main/java/org/apache/empire/db/DBCommand.java +++ b/empire-db/src/main/java/org/apache/empire/db/DBCommand.java @@ -24,6 +24,7 @@ import java.io.ObjectOutputStream; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.List; @@ -32,6 +33,7 @@ import java.util.Vector; import org.apache.empire.commons.StringUtils; import org.apache.empire.data.DataType; +import org.apache.empire.db.exceptions.DatabaseMismatchException; import org.apache.empire.db.expr.compare.DBCompareColExpr; import org.apache.empire.db.expr.compare.DBCompareExpr; import org.apache.empire.db.expr.join.DBColumnJoinExpr; @@ -284,7 +286,8 @@ public abstract class DBCommand extends DBCommandExpr * @param expr the DBColumnExpr object */ public void select(DBColumnExpr expr) - { // Select this column + { // Select this column + checkDatabase(expr); if (select == null) select = new ArrayList<DBColumnExpr>(); if (expr != null && select.contains(expr) == false) @@ -309,7 +312,7 @@ public abstract class DBCommand extends DBCommandExpr * * @param columns the column expressions to add */ - public void select(Collection<? extends DBColumnExpr> columns) + public final void select(Collection<? extends DBColumnExpr> columns) { for (DBColumnExpr expr : columns) { @@ -348,6 +351,7 @@ public abstract class DBCommand extends DBCommandExpr */ public void set(DBSetExpr expr) { + checkDatabase(expr); if (set == null) set = new ArrayList<DBSetExpr>(); for (int i = 0; i < set.size(); i++) @@ -464,6 +468,7 @@ public abstract class DBCommand extends DBCommandExpr */ public void join(DBJoinExpr join) { + checkDatabase(join); if (joins == null) joins = new ArrayList<DBJoinExpr>(); // Create a new join @@ -482,7 +487,7 @@ public abstract class DBCommand extends DBCommandExpr * @param right the right RowSet * @return the join expression */ - public DBCrossJoinExpr join(DBRowSet left, DBRowSet right) + public final DBCrossJoinExpr join(DBRowSet left, DBRowSet right) { DBCrossJoinExpr join = new DBCrossJoinExpr(left, right); join(join); @@ -498,7 +503,7 @@ public abstract class DBCommand extends DBCommandExpr * * @return the join expression */ - public DBColumnJoinExpr join(DBColumnExpr left, DBColumnExpr right, DBJoinType joinType) + public final DBColumnJoinExpr join(DBColumnExpr left, DBColumnExpr right, DBJoinType joinType) { DBColumnJoinExpr join = new DBColumnJoinExpr(left, right, joinType); join(join); @@ -513,7 +518,7 @@ public abstract class DBCommand extends DBCommandExpr * * @return the join expresion */ - public DBColumnJoinExpr join(DBColumnExpr left, DBColumn right) + public final DBColumnJoinExpr join(DBColumnExpr left, DBColumn right) { return join(left, right, DBJoinType.INNER); } @@ -527,7 +532,7 @@ public abstract class DBCommand extends DBCommandExpr * * @return the join expresion */ - public DBCompareJoinExpr join(DBRowSet rowset, DBCompareExpr cmp, DBJoinType joinType) + public final DBCompareJoinExpr join(DBRowSet rowset, DBCompareExpr cmp, DBJoinType joinType) { DBCompareJoinExpr join = new DBCompareJoinExpr(rowset, cmp, joinType); join(join); @@ -542,7 +547,7 @@ public abstract class DBCommand extends DBCommandExpr * * @return the join expresion */ - public DBCompareJoinExpr join(DBRowSet rowset, DBCompareExpr cmp) + public final DBCompareJoinExpr join(DBRowSet rowset, DBCompareExpr cmp) { return join(rowset, cmp, DBJoinType.INNER); } @@ -688,6 +693,7 @@ public abstract class DBCommand extends DBCommandExpr */ public void where(DBCompareExpr expr) { + checkDatabase(expr); if (where == null) where = new ArrayList<DBCompareExpr>(); setConstraint(where, expr); @@ -710,11 +716,7 @@ public abstract class DBCommand extends DBCommandExpr */ public List<DBCompareExpr> getWhereConstraints() { - if (where != null) - { // Return a Copy of all Where Constraints - return new ArrayList<DBCompareExpr>(where); - } - return null; + return (this.where!=null ? Collections.unmodifiableList(this.where) : null); } /** @@ -735,11 +737,7 @@ public abstract class DBCommand extends DBCommandExpr */ public List<DBJoinExpr> getJoins() { - if (joins != null) - { - return new ArrayList<DBJoinExpr>(joins); - } - return null; + return (this.joins!=null ? Collections.unmodifiableList(this.joins) : null); } /** @@ -761,6 +759,7 @@ public abstract class DBCommand extends DBCommandExpr */ public void having(DBCompareExpr expr) { + checkDatabase(expr); if (having == null) having = new ArrayList<DBCompareExpr>(); setConstraint(having, expr); @@ -786,7 +785,10 @@ public abstract class DBCommand extends DBCommandExpr { if (groupBy == null) groupBy = new ArrayList<DBColumnExpr>(); - for(DBColumnExpr expr:exprs){ + // Add all + for(DBColumnExpr expr : exprs) + { + checkDatabase(expr); if (expr.isAggregate()==false && groupBy.contains(expr)==false) groupBy.add(expr); } @@ -797,7 +799,7 @@ public abstract class DBCommand extends DBCommandExpr * * @param columns the column expressions to add */ - public void groupBy(Collection<? extends DBColumnExpr> columns) + public final void groupBy(Collection<? extends DBColumnExpr> columns) { for (DBColumnExpr expr : columns) { @@ -1263,5 +1265,13 @@ public abstract class DBCommand extends DBCommandExpr addListExpr(buf, orderBy, CTX_DEFAULT, ", "); } } - + + protected void checkDatabase(DBExpr expr) + { + if (expr.getDatabase().equals(this.getDatabase())) + return; + // not the same database + throw new DatabaseMismatchException(this, expr); + } + } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/empire-db/blob/6283adbb/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java ---------------------------------------------------------------------- diff --git a/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java b/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java index a75132c..c7ca54c 100644 --- a/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java +++ b/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java @@ -27,6 +27,8 @@ import java.sql.SQLIntegrityConstraintViolationException; import java.sql.Statement; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.Date; import java.util.HashMap; import java.util.List; @@ -42,6 +44,7 @@ import org.apache.empire.db.exceptions.QueryFailedException; import org.apache.empire.db.exceptions.QueryNoResultException; import org.apache.empire.db.exceptions.StatementFailedException; import org.apache.empire.db.expr.column.DBValueExpr; +import org.apache.empire.db.expr.compare.DBCompareExpr; import org.apache.empire.exceptions.InternalException; import org.apache.empire.exceptions.InvalidArgumentException; import org.apache.empire.exceptions.ItemExistsException; @@ -538,7 +541,7 @@ public abstract class DBDatabase extends DBObject */ public List<DBTable> getTables() { - return tables; + return Collections.unmodifiableList(this.tables); } /** @@ -643,7 +646,7 @@ public abstract class DBDatabase extends DBObject */ public List<DBRelation> getRelations() { - return relations; + return Collections.unmodifiableList(this.relations); } /** @@ -685,7 +688,7 @@ public abstract class DBDatabase extends DBObject */ public List<DBView> getViews() { - return views; + return Collections.unmodifiableList(this.views); } /** @@ -1381,6 +1384,18 @@ public abstract class DBDatabase extends DBObject } /** + * Executes an InsertInfo statement from a command object + * @param table the table into which to insert the selected data + * @param cmd the command object containing the selection command + * @param conn a valid connection to the database. + * @return the number of records that have been inserted with the supplied statement + */ + public final int executeInsertInto(DBTable table, DBCommand cmd, Connection conn) + { + return executeSQL(cmd.getInsertInto(table), cmd.getParamValues(), conn); + } + + /** * Executes an Update statement from a command object * @param cmd the command object containing the update command * @param conn a valid connection to the database. @@ -1539,5 +1554,61 @@ public abstract class DBDatabase extends DBObject throw new EmpireSQLException(this, sqle); } } + + /** + * Detects the DataType of a given value. + * @param value the value to detect + * @return the DataType enum for the value + */ + public DataType detectDataType(Object value) + { + if (value instanceof DBColumnExpr) + return ((DBColumnExpr)value).getDataType(); + if (value instanceof String) + return DataType.TEXT; + if ((value instanceof Integer) || (value instanceof Long)) + return DataType.INTEGER; + if (value instanceof Number) + return DataType.DECIMAL; + if (value instanceof Boolean) + return DataType.BOOL; + if (value instanceof Date) + return DataType.DATETIME; + if (value instanceof Character) + return DataType.CHAR; + if (value instanceof byte[]) + return DataType.BLOB; + return DataType.UNKNOWN; + } + + /** + * Creates a case column expression + * "case when <condition> then <trueValue> else <falseValue> end" + * This is a helper function to simplify client usage + * @param condition + * @param trueValue the value to select if the condition is true + * @param falseValue the value to select if the condition is false + * @return an sql case expression + */ + public DBColumnExpr caseWhen(DBCompareExpr condition, Object trueValue, Object falseValue) + { + DataType dataType = detectDataType((trueValue!=null ? trueValue : falseValue)); + DBColumnExpr trueExpr = ((trueValue instanceof DBColumnExpr) ? (DBColumnExpr)trueValue : this.getValueExpr(trueValue, dataType)); + return trueExpr.when(condition, falseValue); + } + + /** + * Creates a case column expression that check whether a column or column expression is null + * "case when <condition> is null then <trueValue> else <falseValue> end" + * This is a helper function to simplify client usage + * @param expr a column or column expression + * @param trueValue the value to select if the condition is true + * @param falseValue the value to select if the condition is false + * @return an sql case expression + */ + public DBColumnExpr caseWhenNull(DBColumnExpr expr, Object trueValue, Object falseValue) + { + return caseWhen(expr.is(null), trueValue, falseValue); + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/empire-db/blob/6283adbb/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java ---------------------------------------------------------------------- diff --git a/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java b/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java index 502c2cc..f5112bf 100644 --- a/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java +++ b/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java @@ -25,6 +25,7 @@ import java.lang.reflect.Field; import java.sql.Connection; import java.sql.Timestamp; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -37,9 +38,9 @@ import org.apache.empire.data.DataType; import org.apache.empire.db.DBRelation.DBCascadeAction; import org.apache.empire.db.DBRelation.DBReference; import org.apache.empire.db.exceptions.FieldNotNullException; +import org.apache.empire.db.exceptions.InvalidKeyException; import org.apache.empire.db.exceptions.NoPrimaryKeyException; import org.apache.empire.db.exceptions.QueryNoResultException; -import org.apache.empire.db.exceptions.InvalidKeyException; import org.apache.empire.db.exceptions.RecordNotFoundException; import org.apache.empire.db.exceptions.RecordUpdateFailedException; import org.apache.empire.db.exceptions.RecordUpdateInvalidException; @@ -260,7 +261,7 @@ public abstract class DBRowSet extends DBExpr */ public List<DBColumn> getColumns() { - return columns; + return Collections.unmodifiableList(columns); } /** http://git-wip-us.apache.org/repos/asf/empire-db/blob/6283adbb/empire-db/src/main/java/org/apache/empire/db/DBTable.java ---------------------------------------------------------------------- diff --git a/empire-db/src/main/java/org/apache/empire/db/DBTable.java b/empire-db/src/main/java/org/apache/empire/db/DBTable.java index 058c49c..b7244ec 100644 --- a/empire-db/src/main/java/org/apache/empire/db/DBTable.java +++ b/empire-db/src/main/java/org/apache/empire/db/DBTable.java @@ -22,6 +22,7 @@ package org.apache.empire.db; import java.lang.reflect.Field; import java.sql.Connection; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; @@ -291,7 +292,7 @@ public class DBTable extends DBRowSet implements Cloneable */ public List<DBIndex> getIndexes() { - return indexes; + return Collections.unmodifiableList(this.indexes); } /** @@ -538,7 +539,7 @@ public class DBTable extends DBRowSet implements Cloneable if (this.equals(r.getForeignKeyTable())) relations.add(r); } - return relations; + return Collections.unmodifiableList(relations); } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/empire-db/blob/6283adbb/empire-db/src/main/java/org/apache/empire/db/exceptions/DatabaseMismatchException.java ---------------------------------------------------------------------- diff --git a/empire-db/src/main/java/org/apache/empire/db/exceptions/DatabaseMismatchException.java b/empire-db/src/main/java/org/apache/empire/db/exceptions/DatabaseMismatchException.java new file mode 100644 index 0000000..7313e2c --- /dev/null +++ b/empire-db/src/main/java/org/apache/empire/db/exceptions/DatabaseMismatchException.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.empire.db.exceptions; + +import org.apache.empire.commons.ErrorType; +import org.apache.empire.db.DBExpr; +import org.apache.empire.exceptions.EmpireException; + +public class DatabaseMismatchException extends EmpireException +{ + /** + * Comment for <code>serialVersionUID</code> + */ + private static final long serialVersionUID = 1L; + + public static final ErrorType errorType = new ErrorType("error.db.databaseInvalid", "the databases don''t match for object of type ''{0}'' and object of type ''{1}''."); + + public DatabaseMismatchException(DBExpr expr1, DBExpr expr2) + { + super(errorType, new String[] { expr1.getClass().getSimpleName(), expr2.getClass().getSimpleName() }); + } +} http://git-wip-us.apache.org/repos/asf/empire-db/blob/6283adbb/empire-db/src/main/java/org/apache/empire/db/sqlite/DBDatabaseDriverSQLite.java ---------------------------------------------------------------------- diff --git a/empire-db/src/main/java/org/apache/empire/db/sqlite/DBDatabaseDriverSQLite.java b/empire-db/src/main/java/org/apache/empire/db/sqlite/DBDatabaseDriverSQLite.java index 3f0bacb..b8c41ea 100644 --- a/empire-db/src/main/java/org/apache/empire/db/sqlite/DBDatabaseDriverSQLite.java +++ b/empire-db/src/main/java/org/apache/empire/db/sqlite/DBDatabaseDriverSQLite.java @@ -32,7 +32,6 @@ import java.util.List; import org.apache.empire.data.DataType; import org.apache.empire.db.DBCmdType; -import org.apache.empire.db.DBColumnExpr; import org.apache.empire.db.DBCommand; import org.apache.empire.db.DBDDLGenerator; import org.apache.empire.db.DBDatabase; @@ -75,13 +74,13 @@ public class DBDatabaseDriverSQLite extends DBDatabaseDriver } @Override - public DBColumnJoinExpr join(DBColumnExpr left, DBColumnExpr right, DBJoinType joinType) + public void join(DBJoinExpr join) { // http://www.sqlite.org/omitted.html - if (joinType != DBJoinType.LEFT) { - throw new NotImplementedException(joinType, left + " join " + right); + if (join.getType() != DBJoinType.LEFT) { + throw new NotImplementedException(join.getType(), join.getLeftTable().getName() + " join " + join.getRightTable().getName()); } - return super.join(left, right, joinType); + super.join(join); } @Override
