You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@empire-db.apache.org by do...@apache.org on 2008/08/06 10:47:43 UTC
svn commit: r683173 [7/10] - in /incubator/empire-db/trunk/core/Empire-db:
./ .settings/ bin/ lib/ src/ src/META-INF/ src/org/ src/org/apache/
src/org/apache/empire/ src/org/apache/empire/commons/
src/org/apache/empire/data/ src/org/apache/empire/data/...
Added: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBTableColumn.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBTableColumn.java?rev=683173&view=auto
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBTableColumn.java (added)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBTableColumn.java Wed Aug 6 01:47:37 2008
@@ -0,0 +1,350 @@
+/*
+ * ESTEAM Software GmbH
+ */
+package org.apache.empire.db;
+
+// Java
+import java.sql.Connection;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import org.apache.empire.commons.Attributes;
+import org.apache.empire.commons.DateUtils;
+import org.apache.empire.data.DataType;
+import org.apache.empire.xml.XMLUtil;
+import org.w3c.dom.Element;
+
+
+/**
+ * This class represent one column of a table.
+ * It contains all properties of this columns (e.g. the column width).
+ * <P>
+ *
+ * @author ESTEAM software <A TARGET="esteam" HREF="http://www.esteam.de">www.esteam.de </A>
+ */
+public class DBTableColumn extends DBColumn
+{
+ // Column Information
+ protected DataType type;
+ protected double size;
+ protected boolean required;
+ protected Object defValue;
+
+ /**
+ * Constructs a DBTableColumn object set the specified parameters to this object.
+ *
+ * @param type the type of the column e.g. integer, text, date
+ * @param name the column name
+ * @param size the column width
+ * @param required true if not null column
+ * @param defValue the object value
+ */
+ public DBTableColumn(DBTable table, DataType type, String name, double size, boolean required, Object defValue)
+ {
+ super(table, name);
+ this.type = type;
+ this.size = size;
+ this.required = required;
+ this.defValue = defValue;
+ // xml
+ attributes = new Attributes();
+ options = null;
+ // Add Column to Table
+ if (table != null)
+ table.addColumn(this);
+ }
+
+ /**
+ * Clone Constructor - use clone()
+ */
+ protected DBTableColumn(DBTable newTable, DBTableColumn other)
+ {
+ super(newTable, other.name);
+ // Copy
+ this.type = other.type;
+ this.size = other.size;
+ this.required = other.required;
+ this.defValue = other.defValue;
+ attributes = other.attributes;
+ options = other.options;
+ // Add Column to Table
+ if (newTable != null)
+ newTable.addColumn(this);
+ }
+
+ /**
+ * Returns the default column value.
+ * For columns of type DBDataType.AUTOINC this is assumed to be the name of a sequence
+ *
+ * @return the default column value
+ */
+ public Object getDefaultValue()
+ {
+ return defValue;
+ }
+
+ /**
+ * Sets the default column value.
+ *
+ * @param defValue the default column value
+ */
+ public void setDefaultValue(Object defValue)
+ {
+ this.defValue = defValue;
+ }
+
+ /**
+ * Returns the default column value.
+ * Unklike getDefaultValue this function is used when creating or adding records.
+ * If the column value is DBDataType AUTOIN this function will return a new sequence value for this record
+ *
+ * @param conn a valid database connection
+ * @return the default column value
+ */
+ public Object getRecordDefaultValue(Connection conn)
+ { // Check params
+ if (rowset==null)
+ return defValue;
+ // Detect default value
+ DBDatabase db = rowset.getDatabase();
+ if (type == DataType.AUTOINC)
+ { // If no connectoin is supplied defer till later
+ if (conn==null)
+ return null; // Create Later
+ // Supports sequences?
+ if (db.getDriver().isSupported(DBDriverFeature.SEQUENCES)==false)
+ return null; // Create Later
+ // Detect the Sequence Name
+ String SeqName = (defValue != null) ? defValue.toString() : this.toString();
+ return db.getNextSequenceValue(SeqName, conn);
+ }
+ // Set database systems time and date
+ if ((type==DataType.DATE || type==DataType.DATETIME) && DBDatabase.SYSDATE.equals(defValue))
+ { // If no connection is supplied defer till later
+ if (conn==null)
+ return defValue;
+ // Get database system's date and time
+ Date ts = db.getUpdateTimestamp(conn);
+ return (type==DataType.DATE ? DateUtils.getDateOnly(ts) : ts);
+ }
+ // Normal value
+ return defValue;
+ }
+
+ /**
+ * Returns the data type.
+ *
+ * @return the data type
+ */
+ @Override
+ public DataType getDataType()
+ {
+ return type;
+ }
+
+ /**
+ * Gets the the column width.
+ *
+ * @return the column width
+ */
+ @Override
+ public double getSize()
+ {
+ return size;
+ }
+
+ /**
+ * Changes the size of the table column<BR>
+ * Use for dynamic data model changes only.<BR>
+ * @param size the new column size
+ */
+ public void setSize(double size)
+ {
+ this.size = size;
+ }
+
+ /**
+ * Returns true if column is mandatory. Only for the graphic presentation.
+ *
+ * @return true if column is mandatory
+ */
+ @Override
+ public boolean isRequired()
+ {
+ return required;
+ }
+
+ /**
+ * Changes the requied property of the table column<BR>
+ * Use for dynamic data model changes only.<BR>
+ * @param required true if the column is requried or false otherwise
+ */
+ public void setRequired(boolean required)
+ {
+ this.required = required;
+ }
+
+ /**
+ * Checks whether the column is read only.
+ *
+ * @return true if the column is read only
+ */
+ @Override
+ public boolean isReadOnly()
+ {
+ if (attributes!=null &&
+ attributes.containsKey(DBCOLATTR_READONLY))
+ return true;
+ // AUTOINC's are read only
+ return (type == DataType.AUTOINC);
+ }
+
+ /**
+ * Sets the read only attribute of the column.
+ *
+ * @param readOnly true if the column should be read only or false otherwise
+ */
+ public void setReadOnly(boolean readOnly)
+ {
+ if (readOnly)
+ {
+ setAttribute(DBCOLATTR_READONLY, Boolean.TRUE);
+ }
+ else
+ { // Remove Attribute
+ if (attributes!=null)
+ attributes.remove(DBCOLATTR_READONLY);
+ }
+ }
+
+ /**
+ * Checks whether the supplied value is valid for this column.
+ * If the type of the value supplied does not match the columns
+ * data type the value will be checked for compatibility.
+ *
+ * @param value the checked to check for validity
+ * @return true if the value is valid or false otherwise.
+ */
+ @Override
+ public boolean checkValue(Object value)
+ { // Is Value valid
+ if (required && (value == null || value.toString().length() < 1))
+ return error(DBErrors.FieldNotNull, getTitle());
+ // Is value valid
+ switch (type)
+ {
+ case DATE:
+ case DATETIME:
+ // Check whether value is a valid date/time value!
+ if (value!=null && (value instanceof Date)==false && value.equals(DBDatabase.SYSDATE)==false)
+ { try
+ { // Parse date time value
+ SimpleDateFormat sdFormat = new SimpleDateFormat("dd-MM-yyyy");
+ sdFormat.setLenient(true);
+ sdFormat.parse(value.toString());
+ // OK
+ } catch (ParseException e)
+ { // Error
+ log.error("checkValue exception: " + e.toString() + " column=" + getTitle() + " value=" + String.valueOf(value));
+ return error(DBErrors.FieldInvalidDateFormat, getName());
+ }
+ }
+ break;
+
+ case DECIMAL:
+ if (value!=null && (value instanceof java.lang.Number)==false)
+ { try
+ { // Convert to String and check
+ String val = value.toString();
+ if (val.length() > 0)
+ Double.parseDouble(val);
+ // thows NumberFormatException if not a number!
+ } catch (NumberFormatException nfe)
+ {
+ log.error("checkValue exception: " + nfe.toString() + " column=" + getTitle() + " value=" + String.valueOf(value));
+ return error(DBErrors.FieldNotNumeric, getName());
+ }
+ }
+ break;
+
+ case INTEGER:
+ if (value!=null && (value instanceof java.lang.Number)==false)
+ { try
+ { // Convert to String and check
+ String val = value.toString();
+ if (val.length() > 0)
+ Integer.parseInt(val);
+ // thows NumberFormatException if not an integer!
+ } catch (NumberFormatException nfe)
+ {
+ log.error("checkValue exception: " + nfe.toString() + " column=" + getTitle() + " value=" + String.valueOf(value));
+ return error(DBErrors.FieldNotNumeric, getName());
+ }
+ }
+ break;
+
+ case TEXT:
+ case CHAR:
+ if (value!=null && value.toString().length() > size)
+ return error(DBErrors.FieldValueTooLong, getName());
+ break;
+
+ default:
+ if (log.isDebugEnabled())
+ log.debug("No column validation has been implemented for data type " + type);
+ break;
+
+ }
+ return success();
+ }
+
+ /**
+ * Creates a foreign key relation for this column.
+ *
+ * @param target the referenced primary key column
+ */
+ public DBRelation.DBReference referenceOn(DBTableColumn target)
+ {
+ return new DBRelation.DBReference(this, target);
+ }
+
+ /**
+ * Sets field elements, default attributes and all otions to
+ * the specified Element object (XML tag).
+ *
+ * @param parent the parent object
+ * @param flags a long value
+ * @return the work up Element object
+ */
+ @Override
+ public Element addXml(Element parent, long flags)
+ { // Add Field element
+ Element elem = XMLUtil.addElement(parent, "column");
+ elem.setAttribute("name", name);
+ // set default attributes
+ DBIndex primaryKey = ((DBTable) rowset).getPrimaryKey();
+ if (primaryKey != null)
+ {
+ int keyIndex;
+ if ((keyIndex = ((DBTable) rowset).getPrimaryKey().getColumnPos(this)) >= 0)
+ elem.setAttribute("key", String.valueOf(keyIndex + 1));
+ }
+ if (size > 0)
+ {
+ elem.setAttribute("size", String.valueOf((int)size));
+ if (getDataType()==DataType.DECIMAL)
+ elem.setAttribute("decimals", String.valueOf((int)(size*10)%10));
+ }
+ if (required)
+ elem.setAttribute(DBCOLATTR_MANDATORY, String.valueOf(Boolean.TRUE));
+ // add All Attributes
+ if (attributes!=null)
+ attributes.addXml(elem, flags);
+ // add All Options
+ if (options!=null)
+ options.addXml(elem, flags);
+ // done
+ return elem;
+ }
+}
\ No newline at end of file
Added: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBView.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBView.java?rev=683173&view=auto
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBView.java (added)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBView.java Wed Aug 6 01:47:37 2008
@@ -0,0 +1,382 @@
+/*
+ * ESTEAM Software GmbH
+ */
+package org.apache.empire.db;
+
+import java.sql.Connection;
+
+import org.apache.empire.commons.Errors;
+import org.apache.empire.commons.Options;
+import org.apache.empire.data.DataType;
+import org.apache.empire.db.expr.column.DBValueExpr;
+import org.apache.empire.xml.XMLUtil;
+import org.w3c.dom.Element;
+
+
+/**
+ * This class represent a view of the database.
+ * It consits methods to get and update records from the database
+ * <P>
+ *
+ * @author ESTEAM software <A TARGET="esteam" HREF="http://www.esteam.de">www.esteam.de </A>
+ */
+public abstract class DBView extends DBRowSet
+{
+ public static class DBViewColumn extends DBColumn
+ {
+ protected final DBColumnExpr expr;
+ protected final DBColumn updateColumn;
+
+ /**
+ * Constructs a DBViewColumn object set the specified parameters to this object.
+ *
+ * @param view the DBView object
+ * @param expr the DBColumnExpr of the source table
+ */
+ protected DBViewColumn(DBView view, String name, DBColumnExpr expr)
+ { // call base
+ super(view, name);
+ // set Expression
+ this.expr = expr;
+ // Update Column
+ this.updateColumn = expr.getUpdateColumn();
+ }
+
+ public DBColumnExpr getSourceColumnExpr()
+ {
+ return expr;
+ }
+
+ public DBView getView()
+ {
+ return (DBView)getRowSet();
+ }
+
+ @Override
+ public DataType getDataType()
+ {
+ return expr.getDataType();
+ }
+
+ @Override
+ public double getSize()
+ {
+ if (updateColumn==null)
+ return 0.0;
+ return updateColumn.getSize();
+ }
+
+ @Override
+ public boolean isReadOnly()
+ {
+ if (getView().isUpdateable()==false)
+ return true;
+ // Check ReadOnly attribute
+ if (attributes!=null &&
+ attributes.containsKey(DBCOLATTR_READONLY))
+ return true;
+ // AUTOINC's are read only
+ return (getDataType() == DataType.AUTOINC);
+ }
+
+ @Override
+ public boolean isRequired()
+ {
+ if (getView().isUpdateable()==false)
+ return false;
+ // Check update Column
+ if (updateColumn==null)
+ return false;
+ return updateColumn.isRequired();
+ }
+
+ @Override
+ public boolean checkValue(Object value)
+ {
+ if (updateColumn==null)
+ return true;
+ return updateColumn.checkValue(value);
+ }
+
+ @Override
+ public Object getAttribute(String name)
+ {
+ if (attributes != null && attributes.containsKey(name))
+ return attributes.get(name);
+ // Otherwise ask expression
+ if (updateColumn==null)
+ return null;
+ return updateColumn.getAttribute(name);
+ }
+
+ @Override
+ public Options getOptions()
+ {
+ if (options != null)
+ return options;
+ // Otherwise ask expression
+ if (updateColumn==null)
+ return null;
+ return updateColumn.getOptions();
+ }
+
+ @Override
+ public Element addXml(Element parent, long flags)
+ {
+ Element elem = XMLUtil.addElement(parent, "column");
+ elem.setAttribute("name", name);
+ // set default attributes
+ if (updateColumn != null)
+ elem.setAttribute("source", updateColumn.getFullName());
+ // size
+ double size = getSize();
+ if (size > 0)
+ elem.setAttribute("size", String.valueOf(size));
+ // add All Attributes
+ if (attributes != null)
+ attributes.addXml(elem, flags);
+ // add All Options
+ Options fieldOptions = getOptions();
+ if (fieldOptions != null)
+ fieldOptions.addXml(elem, flags);
+ // done
+ return elem;
+ }
+
+ }
+
+ private static int viewCount = 1;
+
+ private String name;
+ private String alias;
+ private boolean updateable; // true if the view is updateable
+
+ /**
+ * Creates a view object for a given view in the database.
+ *
+ * @param name the name of the view
+ * @param db the database this view belongs to.
+ * @param isUpdateable true if the records of this view can be updated
+ */
+ public DBView(String name, DBDatabase db, boolean isUpdateable)
+ { // Set the column expressions
+ super(db);
+ // Add View to Database
+ if (db != null && name != null)
+ db.addView(this);
+ // Set Name and Alias
+ this.name = name;
+ this.alias = "v" + String.valueOf(viewCount);
+ this.updateable = isUpdateable;
+ viewCount++;
+ }
+
+ /**
+ * Creates a view object for a given view in the database.
+ *
+ * @param name the name of the view
+ * @param db the database this view belongs to.
+ */
+ public DBView(String name, DBDatabase db)
+ { // Set the column expressions
+ this(name, db, false);
+ }
+
+ /**
+ * identifies the columns that uniquely identify a row in the view
+ * @param keyColumns list of columns that uniquely identify a row
+ */
+ protected void setKeyColumns(DBViewColumn[] keyColumns)
+ { // Set Key Columns
+ if (keyColumns != null)
+ primaryKey = new DBIndex(null, DBIndex.PRIMARYKEY, keyColumns);
+ }
+
+ /**
+ * identifies the column that uniquely identifies a row in the view
+ * @param keyColumn the column that uniquely identifies a row
+ */
+ protected void setKeyColumn(DBViewColumn keyColumn)
+ {
+ if (keyColumn != null)
+ setKeyColumns(new DBViewColumn[] { keyColumn });
+ else
+ setKeyColumns((DBViewColumn[]) null);
+ }
+
+ /**
+ * Returns the command required to create the view<br>
+ * This is function is only used for the creation of DDL statements
+ * @return a command expression that is used to create the view
+ */
+ public abstract DBCommandExpr createCommand();
+
+ /**
+ * Returns the view name of this object.
+ * @return the view name of this object
+ */
+ @Override
+ public String getName()
+ {
+ return name;
+ }
+
+ /**
+ * Returns the full qualified table name.
+ * @return the full qualified table name
+ */
+ @Override
+ public String getFullName()
+ {
+ String schema = db.getSchema();
+ return (schema != null) ? schema + "." + name : name;
+ }
+
+ /**
+ * Returns the alias name of this object.
+ * @return the alias name of this object
+ */
+ @Override
+ public String getAlias()
+ {
+ return alias;
+ }
+
+ /**
+ * Returns whether or not the view is updateable
+ * @return true if the view is updateable or false if not
+ */
+ public boolean isUpdateable()
+ {
+ return this.updateable;
+ }
+
+ /**
+ * Adds a column to the view.
+ *
+ * @param col a view column object
+ * @return true if the column was successfully added or false otherwise
+ */
+ protected boolean addColumn(DBViewColumn col)
+ { // find column by name
+ if (col == null || col.getRowSet() != this)
+ return error(Errors.InvalidArg, col, "col");
+ if (columns.contains(col) == true)
+ return error(Errors.ItemExists, col.getName());
+ // add now
+ columns.add(col);
+ return true;
+ }
+
+ /**
+ * Adds a column to the view.
+ *
+ * @param columnName name of the column in the view
+ * @param dataType the data type of the column
+ * @return true if the column was successfully added or false otherwise
+ */
+ protected final DBViewColumn addColumn(String columnName, DataType dataType)
+ { // find column by name
+ DBViewColumn vc = new DBViewColumn(this, columnName, new DBValueExpr(db, null, dataType));
+ return (addColumn(vc) ? vc : null);
+ }
+
+ /**
+ * Adds a column to the view.
+ *
+ * @param columnName name of the column in the view
+ * @param columnExpr the column expression that builds the column
+ * @return true if the column was successfully added or false otherwise
+ */
+ protected final DBViewColumn addColumn(String columnName, DBColumnExpr columnExpr)
+ { // find column by name
+ DBViewColumn vc = new DBViewColumn(this, columnName, columnExpr);
+ return (addColumn(vc) ? vc : null);
+ }
+
+ /**
+ * Adds a column to the view based on an existing column in another table or view.
+ *
+ * @param sourceColumn existing column in another table or view
+ * @return the view column object
+ */
+ protected final DBViewColumn addColumn(DBTableColumn sourceColumn)
+ { // find column by name
+ DBViewColumn vc = new DBViewColumn(this, sourceColumn.getName(), sourceColumn);
+ return (addColumn(vc) ? vc : null);
+ }
+
+ /**
+ * This function searchs for equal columns given by the specified DBColumnExpr object.
+ *
+ * @param expr the DBColumnExpr object
+ * @return the located column (only DBViewColumn onjects)
+ */
+ public DBViewColumn findViewColumn(DBColumnExpr expr)
+ {
+ for (int i = 0; i < columns.size(); i++)
+ {
+ DBViewColumn vc = (DBViewColumn) columns.get(i);
+ if (vc.expr.equals(expr))
+ return vc;
+ }
+ // not found
+ return null;
+ }
+
+ /**
+ * Creates the SQL-Command adds the alias name to the SQL-Command.
+ *
+ * @param buf the SQL-Command
+ * @param context the current SQL-Command context
+ */
+ @Override
+ public void addSQL(StringBuilder buf, long context)
+ {
+ if ((context & CTX_FULLNAME) != 0 && db != null)
+ { // Add Schema
+ db.appendQualifiedName(buf, name);
+ }
+ else
+ { // Simple Name only
+ buf.append(name);
+ }
+ // Add Alias
+ if ((context & CTX_ALIAS) != 0 && alias != null)
+ { // append alias
+ buf.append(getRenameTablePhrase());
+ buf.append(alias);
+ }
+ }
+
+ @Override
+ public boolean updateRecord(DBRecord rec, Connection conn)
+ {
+ if (updateable==false)
+ return error(Errors.NotSupported, "updateRecord");
+ // Update the record
+ return super.updateRecord(rec, conn);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.empire.db.DBRowSet#addRecord(org.apache.empire.db.DBRecord, java.sql.Connection)
+ */
+ @Override
+ public boolean createRecord(DBRecord rec, Connection conn)
+ {
+ return error(Errors.NotSupported, "addRecord");
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.empire.db.DBRowSet#deleteRecord(java.lang.Object[], java.sql.Connection, boolean)
+ */
+ @Override
+ public boolean deleteRecord(Object[] keys, Connection conn)
+ {
+ return error(Errors.NotSupported, "deleteRecord");
+ }
+}
\ No newline at end of file
Added: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBXmlDictionary.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBXmlDictionary.java?rev=683173&view=auto
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBXmlDictionary.java (added)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBXmlDictionary.java Wed Aug 6 01:47:37 2008
@@ -0,0 +1,43 @@
+/*
+ * ESTEAM Software GmbH, 22.12.2007
+ */
+package org.apache.empire.db;
+
+/**
+ * This class is used to configure XML generation as performed by the
+ * getXmlDocument Document function on DBReader and DBRecord.<BR>
+ */
+public class DBXmlDictionary
+{
+ private static DBXmlDictionary dbXmlDictonary;
+
+ public static DBXmlDictionary getInstance()
+ {
+ if (dbXmlDictonary==null)
+ {
+ // dbXmlTagDictionary has not been set. Using Default Dictionary");
+ dbXmlDictonary = new DBXmlDictionary();
+ }
+ return dbXmlDictonary;
+ }
+
+ public static void set(DBXmlDictionary xmlDictonary)
+ {
+ dbXmlDictonary = xmlDictonary;
+ }
+
+ // ------- XML Element and Attribute Names -------
+
+ public String getRowSetElementName() {
+ return "rowset";
+ }
+
+ public String getRowElementName() {
+ return "row";
+ }
+
+ public String getRowIdColumnAttribute() {
+ return "id";
+ }
+
+}
Added: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBAliasExpr.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBAliasExpr.java?rev=683173&view=auto
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBAliasExpr.java (added)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBAliasExpr.java Wed Aug 6 01:47:37 2008
@@ -0,0 +1,196 @@
+/*
+ * ESTEAM Software GmbH
+ */
+package org.apache.empire.db.expr.column;
+
+// Java
+import java.util.*;
+// XML
+import org.apache.empire.commons.StringUtils;
+import org.apache.empire.data.DataType;
+import org.apache.empire.db.DBColumn;
+import org.apache.empire.db.DBColumnExpr;
+import org.apache.empire.db.DBDatabase;
+import org.apache.empire.db.DBDatabaseDriver;
+import org.w3c.dom.*;
+
+
+/**
+ * This class allows column renaming in SQL.
+ * <P>
+ * There is no need to explicitly create instances of this class.<BR>
+ * Instead use {@link DBColumnExpr#as(String) }
+ * <P>
+ * @author ESTEAM software <A TARGET="esteam" HREF="http://www.esteam.de">www.esteam.de </A>
+ */
+public class DBAliasExpr extends DBColumnExpr
+{
+ private final DBColumnExpr expr;
+ private final String alias;
+
+ /**
+ * Constructs a DBAliasExpr object combine the DBColumnExpr object with the alias name.
+ *
+ * @param expr an DBColumnExpr object, one column
+ * @param alias the alias name of the column
+ */
+ public DBAliasExpr(DBColumnExpr expr, String alias)
+ {
+ // Check whether already a AliasExpr
+ if (expr.getClass().equals(getClass()))
+ this.expr = ((DBAliasExpr) expr).expr;
+ else
+ this.expr = expr;
+ // Set alias name
+ this.alias = alias.toUpperCase();
+ }
+
+ /**
+ * Returns the current DBDatabase object.
+ *
+ * @return the current DBDatabase object
+ */
+ @Override
+ public DBDatabase getDatabase()
+ {
+ return expr.getDatabase();
+ }
+
+ /**
+ * Returns the data type of the DBColumnExpr object.
+ *
+ * @return the data type
+ */
+ @Override
+ public DataType getDataType()
+ {
+ return expr.getDataType();
+ }
+
+ /**
+ * This helper function returns the alias name.
+ *
+ * @return the alias name
+ */
+ @Override
+ public String getName()
+ {
+ return alias;
+ }
+
+ /**
+ * This helper function returns the underlying column expression.
+ *
+ * @return the underlying column expression
+ */
+ public DBColumnExpr getExpr()
+ {
+ return expr;
+ }
+
+ /**
+ * This function set the alias name to the XML tag.
+ *
+ * @return the XML tag (with the alias name)
+ */
+ @Override
+ public Element addXml(Element parent, long flags)
+ { // Set name to Alias
+ Element field = expr.addXml(parent, flags);
+ if (field != null)
+ { // Set Name
+ if (field.hasAttribute("name"))
+ field.setAttribute("source", StringUtils.toString(field.getAttribute("name")));
+ field.setAttribute("name", alias);
+ }
+ return field;
+ }
+
+ /**
+ * Returns the DBColunm object.
+ *
+ * @return the DBColunm object
+ */
+ @Override
+ public DBColumn getUpdateColumn()
+ {
+ return expr.getUpdateColumn();
+ }
+
+ /**
+ * Always returns false since an alias expression cannot be an aggregate.
+ *
+ * @return false
+ */
+ @Override
+ public boolean isAggregate()
+ {
+ return false;
+ }
+
+ /**
+ * @see org.apache.empire.db.DBExpr#addReferencedColumns(Set)
+ */
+ @Override
+ public void addReferencedColumns(Set<DBColumn> list)
+ {
+ expr.addReferencedColumns(list);
+ }
+
+ /**
+ * Creates the SQL-Command adds the alias name to the SQL-Command.
+ *
+ * @param buf the SQL statment
+ * @param context the current SQL-Command context
+ */
+ @Override
+ public void addSQL(StringBuilder buf, long context)
+ { // Alias anhängen
+ if((context & CTX_ALIAS)!=0)
+ { // Add the column expression
+ expr.addSQL(buf, context);
+ // Rename
+ String asExpr = getDatabase().getDriver().getSQLPhrase(DBDatabaseDriver.SQL_RENAME_COLUMN);
+ if (asExpr!=null)
+ {
+ buf.append(asExpr);
+ buf.append(alias);
+ }
+ }
+ else
+ {
+ expr.addSQL(buf, context);
+ }
+ }
+
+ /**
+ * Overrides the equals method
+ *
+ * @return true if alias name and expression match
+ */
+ @Override
+ public boolean equals(Object other)
+ {
+ if (super.equals(other))
+ return true;
+ // Check for another Alias Expression
+ if (other instanceof DBAliasExpr)
+ { // Compare with another alias expression
+ DBAliasExpr otherExpr = ((DBAliasExpr)other);
+ return this.alias.equalsIgnoreCase(otherExpr.getName()) &&
+ this.expr.equals(otherExpr.getExpr());
+ }
+ return false;
+ }
+
+ /**
+ * Overrides the toString method.
+ *
+ * @return the alias name
+ */
+ @Override
+ public String toString()
+ {
+ return alias;
+ }
+}
\ No newline at end of file
Added: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBCalcExpr.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBCalcExpr.java?rev=683173&view=auto
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBCalcExpr.java (added)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBCalcExpr.java Wed Aug 6 01:47:37 2008
@@ -0,0 +1,149 @@
+/*
+ * ESTEAM Software GmbH
+ */
+package org.apache.empire.db.expr.column;
+
+// Java
+import java.util.Set;
+
+import org.apache.empire.data.DataType;
+import org.apache.empire.db.DBColumn;
+import org.apache.empire.db.DBColumnExpr;
+import org.apache.empire.db.DBDatabase;
+import org.apache.empire.db.DBExpr;
+import org.w3c.dom.Element;
+
+
+/**
+ * This class is used for performing calculations in SQL<br>
+ * It handles the mathematical operations ("+", "-", "*", "/") for the current column.
+ * <P>
+ * There is no need to explicitly create instances of this class.<BR>
+ * Instead use {@link DBColumnExpr#plus(Object) }, {@link DBColumnExpr#minus(Object) },
+ * {@link DBColumnExpr#multiplyWith(Object) }, {@link DBColumnExpr#divideBy(Object) }
+ * <P>
+ * @author ESTEAM software <A TARGET="esteam" HREF="http://www.esteam.de">www.esteam.de </A>
+ */
+public class DBCalcExpr extends DBColumnExpr
+{
+ private final DBColumnExpr expr;
+ private final String op;
+ private final Object value;
+
+ /**
+ * Constructs a DBCalcExpr object Sets the mathematical operations ("+", "-", "*", "/") <br>
+ * for the specified DBColumnExpr object and value.
+ *
+ * @param expr an DBColumnExpr object, one column
+ * @param op the mathematical operation ("+", "-", "*" or "/")
+ * @param value the multiply, divide, summate or subtract value
+ */
+ public DBCalcExpr(DBColumnExpr expr, String op, Object value)
+ {
+ this.expr = expr;
+ this.value = value;
+ this.op = op;
+ }
+
+ /**
+ * Returns the current DBDatabase object.
+ *
+ * @return the current DBDatabase object
+ */
+ @Override
+ public DBDatabase getDatabase()
+ {
+ return expr.getDatabase();
+ }
+
+ /**
+ * Returns the data type: DBDataType.DECIMAL.
+ *
+ */
+ @Override
+ public DataType getDataType()
+ {
+ return DataType.DECIMAL;
+ }
+
+ /** Returns the given expression name. */
+ @Override
+ public String getName()
+ { // Get the expression name
+ return expr.getName();
+ }
+
+ /**
+ * This function set the specified mathematical operations to the XML tag.
+ *
+ * @return the XML tag (with the mathematical operations)
+ */
+ @Override
+ public Element addXml(Element parent, long flags)
+ { // Add Expressions
+ expr.addXml(parent, flags);
+ // Add Vaue
+ if (value instanceof DBColumnExpr)
+ ((DBColumnExpr) value).addXml(parent, flags);
+ // done
+ return parent;
+ }
+
+ /** returns null */
+ @Override
+ public DBColumn getUpdateColumn()
+ {
+ return null;
+ }
+
+ /**
+ * Always returns false since CalcExpressions cannot be aggregates.
+ *
+ * @return false
+ */
+ @Override
+ public boolean isAggregate()
+ {
+ return false;
+ }
+
+ /**
+ * @see org.apache.empire.db.DBExpr#addReferencedColumns(Set)
+ */
+ @Override
+ public void addReferencedColumns(Set<DBColumn> list)
+ {
+ expr.addReferencedColumns(list);
+ // Check if value is a DBExpr
+ if (value instanceof DBExpr)
+ ((DBExpr)value).addReferencedColumns(list);
+ }
+
+ /**
+ * Creates the SQL-Command adds the mathematical operations for
+ * the specified DBColumnExpr object and value to the.
+ * SQL-Command
+ *
+ * @param buf the SQL statment
+ * @param context the current SQL-Command context
+ */
+ @Override
+ public void addSQL(StringBuilder buf, long context)
+ {
+ // Zusammenbauen
+ expr.addSQL(buf, context);
+ buf.append(op);
+ buf.append(getObjectValue(this, value, context, op));
+ }
+
+ @Override
+ public String toString()
+ { // Get a unique Name
+ String name = expr.toString();
+ name += "_X_";
+ name += value.toString();
+ // name
+ return name;
+ }
+
+}
\ No newline at end of file
Added: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBConcatExpr.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBConcatExpr.java?rev=683173&view=auto
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBConcatExpr.java (added)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBConcatExpr.java Wed Aug 6 01:47:37 2008
@@ -0,0 +1,161 @@
+/*
+ * ESTEAM Software GmbH
+ */
+package org.apache.empire.db.expr.column;
+
+// Java
+import java.text.MessageFormat;
+import java.util.Set;
+
+import org.apache.empire.data.DataType;
+import org.apache.empire.db.DBColumn;
+import org.apache.empire.db.DBColumnExpr;
+import org.apache.empire.db.DBDatabase;
+import org.apache.empire.db.DBDatabaseDriver;
+import org.apache.empire.db.DBExpr;
+import org.apache.empire.xml.XMLUtil;
+import org.w3c.dom.Element;
+
+
+/**
+ * This class is used for performing string concatenation in SQL<br>
+ * <P>
+ * There is no need to explicitly create instances of this class.<BR>
+ * Instead use {@link DBColumnExpr#append(Object) }
+ * <P>
+ * @author ESTEAM software <A TARGET="esteam" HREF="http://www.esteam.de">www.esteam.de</A>
+ */
+public class DBConcatExpr extends DBColumnExpr
+{
+ protected final DBColumnExpr left;
+ protected final Object right;
+
+ /**
+ * Constructs a new DBConcatExpr object set the specified parameters to this object.
+ */
+ public DBConcatExpr(DBColumnExpr left, Object right)
+ {
+ this.left = left;
+ this.right = right;
+ }
+
+ /**
+ * Returns the current DBDatabase object.
+ *
+ * @return the current DBDatabase object
+ */
+ @Override
+ public DBDatabase getDatabase()
+ {
+ return left.getDatabase();
+ }
+
+ /**
+ * Returns the data type: DT_TEXT.
+ *
+ * @return the data type: DT_TEXT
+ */
+ @Override
+ public DataType getDataType()
+ {
+ return DataType.TEXT;
+ }
+
+ /**
+ * This helper function returns expression name.
+ *
+ * @return the expression name
+ */
+ @Override
+ public String getName()
+ { // Get the expression name
+ String name = left.getName();
+ if (right instanceof DBColumnExpr)
+ { // add other names
+ name += "_";
+ name += ((DBColumnExpr) right).getName();
+ }
+ return name;
+ }
+
+ /** This helper function calls the DBColumnExpr.addXML(Element, long) method. */
+ @Override
+ public Element addXml(Element parent, long flags)
+ {
+ Element elem = XMLUtil.addElement(parent, "column");
+ elem.setAttribute("name", getName());
+ elem.setAttribute("function", "concat");
+ // Add Other Attributes
+ if (attributes!=null)
+ attributes.addXml(elem, flags);
+ // add All Options
+ if (options!=null)
+ options.addXml(elem, flags);
+ // Done
+ return elem;
+ }
+
+ /**
+ * returns null
+ *
+ * @return null
+ */
+ @Override
+ public DBColumn getUpdateColumn()
+ {
+ return null;
+ }
+
+ /**
+ * Always returns false since a concat expression cannot be an aggregate.
+ *
+ * @return false
+ */
+ @Override
+ public boolean isAggregate()
+ {
+ return false;
+ }
+
+ /**
+ * @see org.apache.empire.db.DBExpr#addReferencedColumns(Set)
+ */
+ @Override
+ public void addReferencedColumns(Set<DBColumn> list)
+ {
+ left.addReferencedColumns(list);
+ // Check if right object is a DBExpr
+ if (right instanceof DBExpr)
+ ((DBExpr)right).addReferencedColumns(list);
+ }
+
+ /**
+ * Creates the SQL-Command concatenate a specified column with
+ * a specified value sets the column with a specified value to
+ * the SQL-Command.
+ *
+ * @param buf the SQL statment
+ * @param context the current SQL-Command context
+ */
+ @Override
+ public void addSQL(StringBuilder buf, long context)
+ { // Zusammenbauen
+ String template = getDatabase().getDriver().getSQLPhrase(DBDatabaseDriver.SQL_CONCAT_EXPR);
+ // Find Separator
+ int sep = template.indexOf('?');
+ if (sep >= 0)
+ { // Complex Pattern with placeholder ? for this expression and {0} for the value
+ buf.append(template.substring(0, sep));
+ left.addSQL(buf, context);
+ String value = getObjectValue(this, right, context, ", ");
+ buf.append(MessageFormat.format(template.substring(sep + 1), value));
+ }
+ else
+ { // Simple Pattern without placeholders
+ left.addSQL(buf, context);
+ buf.append(template);
+ buf.append(getObjectValue(this, right, context, template));
+ }
+
+ }
+}
Added: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBCountExpr.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBCountExpr.java?rev=683173&view=auto
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBCountExpr.java (added)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBCountExpr.java Wed Aug 6 01:47:37 2008
@@ -0,0 +1,141 @@
+/*
+ * ESTEAM Software GmbH
+ */
+package org.apache.empire.db.expr.column;
+
+// Java
+import java.util.Set;
+
+import org.apache.empire.data.DataType;
+import org.apache.empire.db.DBColumn;
+import org.apache.empire.db.DBColumnExpr;
+import org.apache.empire.db.DBDatabase;
+import org.apache.empire.db.DBRowSet;
+import org.apache.empire.xml.XMLUtil;
+import org.w3c.dom.Element;
+
+
+/**
+ * This class is used to add the "count" statement to the SQL-Command.
+ * <P>
+ * There is no need to explicitly create instances of this class.<BR>
+ * Instead use {@link DBRowSet#count() }
+ * <P>
+ * @author ESTEAM software <A TARGET="esteam" HREF="http://www.esteam.de">www.esteam.de </A>
+ */
+public class DBCountExpr extends DBColumnExpr
+{
+ private final DBRowSet rowset;
+ private final DBColumnExpr expr;
+
+ /**
+ * Constructs a DBCountExpr.
+ */
+ public DBCountExpr(DBRowSet rowset, DBColumnExpr expr)
+ {
+ this.rowset = rowset;
+ this.expr = expr;
+ }
+
+ /**
+ * Returns the current DBDatabase object.
+ *
+ * @return the current DBDatabase object
+ */
+ @Override
+ public DBDatabase getDatabase()
+ {
+ return rowset.getDatabase();
+ }
+
+ /**
+ * Returns the data type: DT_INTEGER.
+ *
+ * @return the data type: DT_INTEGER
+ */
+ @Override
+ public DataType getDataType()
+ {
+ return DataType.INTEGER;
+ }
+
+ /**
+ * Returns the String "count".
+ *
+ * @return the String "count"
+ */
+ @Override
+ public String getName()
+ {
+ return "count";
+ }
+
+ /**
+ * Returns null.
+ *
+ * @return null
+ */
+ @Override
+ public DBColumn getUpdateColumn()
+ {
+ return null;
+ }
+
+ /**
+ * Returns true since the count function is an aggregate function.
+ *
+ * @return always true
+ */
+ @Override
+ public boolean isAggregate()
+ {
+ return true;
+ }
+
+ /**
+ * @see org.apache.empire.db.DBExpr#addReferencedColumns(Set)
+ */
+ @Override
+ public void addReferencedColumns(Set<DBColumn> list)
+ {
+ if (expr!=null)
+ expr.addReferencedColumns(list);
+ else
+ list.add(rowset.getColumn(0)); // select any column
+ }
+
+ /**
+ * Creates the SQL-Command adds the String "count(*)" to the SQL-Command.
+ *
+ * @param buf the SQL-Command
+ * @param context the current SQL-Command context
+ */
+ @Override
+ public void addSQL(StringBuilder buf, long context)
+ {
+ buf.append("count(");
+ if (expr!=null)
+ expr.addSQL(buf, context);
+ else
+ buf.append("*");
+ buf.append(")");
+ }
+
+ /** this helper function calls the XMLUtil.addXML(Element, String) method */
+ @Override
+ public Element addXml(Element parent, long flags)
+ { // Add Expression
+ Element elem;
+ if (expr!=null)
+ { elem = expr.addXml(parent, flags);
+ }
+ else
+ { elem = XMLUtil.addElement(parent, "column");
+ elem.setAttribute("name", getName());
+ }
+ elem.setAttribute("function", "count");
+ // done
+ return elem;
+ }
+
+}
Added: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBFuncExpr.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBFuncExpr.java?rev=683173&view=auto
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBFuncExpr.java (added)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBFuncExpr.java Wed Aug 6 01:47:37 2008
@@ -0,0 +1,202 @@
+/*
+ * ESTEAM Software GmbH
+ */
+package org.apache.empire.db.expr.column;
+
+// Java
+import java.util.Set;
+
+import org.apache.empire.data.DataType;
+import org.apache.empire.db.DBColumn;
+import org.apache.empire.db.DBColumnExpr;
+import org.apache.empire.db.DBDatabase;
+import org.apache.empire.xml.XMLUtil;
+import org.w3c.dom.Element;
+
+
+/**
+ * This class is used for performing various SQL functions on a column or column expression.
+ * <P>
+ * There is no need to explicitly create instances of this class.<BR>
+ * Instead use any of the following functions:<BR>
+ * {@link DBColumnExpr#abs() }, {@link DBColumnExpr#coalesce(Object) }, {@link DBColumnExpr#convertTo(DataType) },
+ * {@link DBColumnExpr#decode(java.util.Map, Object) }, {@link DBColumnExpr#lower() }, {@link DBColumnExpr#min() },
+ * {@link DBColumnExpr#max() }, {@link DBColumnExpr#month() }, {@link DBColumnExpr#sum() },
+ * {@link DBColumnExpr#trim() }, {@link DBColumnExpr#upper() }, {@link DBColumnExpr#year() }
+ * <P>
+ * @author ESTEAM software <A TARGET="esteam" HREF="http://www.esteam.de">www.esteam.de </A>
+ */
+public class DBFuncExpr extends DBColumnExpr
+{
+ protected final DBColumnExpr expr;
+ protected final DBColumn updateColumn; // optional
+
+ protected final String prefix;
+ protected final String postfix;
+ protected final boolean isAggregate;
+ protected final DataType dataType;
+
+ /**
+ * Constructs a new DBFuncExpr object set the specified parameters to this object.
+ * Do not use directly - use any of the DBColumnExpr.??? factory functions instead!
+ *
+ * The sql function string is built from a string template.
+ * The template string must contain a ? which is a placeholder for the column expression.
+ *
+ * @param expr the DBColumnExpr object
+ * @param template specifies a template for the expression. The template must contain a ? placeholder for the column expression
+ * @param updateColumn optional update column if any. This parameter may be null
+ * @param isAggregate indicates whether the function is an aggregate function (sum, min, max, avg, ...)
+ * @param dataType indicates the data type of the function result
+ */
+ public DBFuncExpr(DBColumnExpr expr, String template, DBColumn updateColumn, boolean isAggregate, DataType dataType)
+ {
+ this.expr = expr;
+ this.dataType = dataType;
+ this.isAggregate = isAggregate;
+ this.updateColumn = updateColumn;
+
+ int sep = template.indexOf("?");
+ if (sep >= 0)
+ {
+ prefix = template.substring(0, sep);
+ postfix = template.substring(sep + 1);
+ }
+ else
+ {
+ prefix = template;
+ postfix = "";
+ }
+ }
+
+ /**
+ * Constructs a new DBFuncExpr object set the specified parameters to this object.
+ * Do not use directly - use any of the DBColumnExpr.??? factory functions instead!
+ *
+ * The function sql string is built from a template string.
+ * The template string must contain a ? which is a placeholder for the column expression.
+ *
+ * @param expr the DBColumnExpr object
+ * @param template specifies a template for the expression. The template must contain a ? placeholder for the column expression
+ * @param updateColumn optional update column if any. This parameter may be null
+ * @param isAggregate indicates whether the function is an aggregate function (sum, min, max, avg, ...)
+ */
+ public DBFuncExpr(DBColumnExpr expr, String template, DBColumn updateColumn, boolean isAggregate)
+ {
+ this(expr, template, updateColumn, isAggregate, expr.getDataType());
+ }
+
+ /**
+ * Returns the current DBDatabase object.
+ *
+ * @return the current DBDatabase object
+ */
+ @Override
+ public DBDatabase getDatabase()
+ {
+ return expr.getDatabase();
+ }
+
+ /**
+ * Returns the data type of the DBColumnExpr object.
+ *
+ * @return the data type
+ */
+ @Override
+ public DataType getDataType()
+ {
+ return this.dataType;
+ }
+
+ /**
+ * Returns the column name.
+ *
+ * @return the column name
+ */
+ @Override
+ public String getName()
+ {
+ return expr.getName();
+ }
+
+ @Override
+ public Element addXml(Element parent, long flags)
+ {
+ Element elem;
+ if (updateColumn!=null)
+ { // Update Column
+ elem = updateColumn.addXml(parent, flags);
+ }
+ else
+ { // Add a column expression for this function
+ elem = XMLUtil.addElement(parent, "column");
+ elem.setAttribute("name", getName());
+ // Add Other Attributes
+ if (attributes!=null)
+ attributes.addXml(elem, flags);
+ // add All Options
+ if (options!=null)
+ options.addXml(elem, flags);
+ }
+ // Done
+ elem.setAttribute("function", getFunctionName());
+ return elem;
+ }
+
+ private String getFunctionName()
+ {
+ String s = prefix.trim();
+ int i=0;
+ for (; i<s.length(); i++)
+ if (s.charAt(i)<'A')
+ break;
+ // return name
+ return (i>0) ? s.substring(0,i) : postfix.trim();
+ }
+
+ /**
+ * Returns the DBColunm object.
+ *
+ * @return the DBColunm object
+ */
+ @Override
+ public DBColumn getUpdateColumn()
+ {
+ return updateColumn;
+ }
+
+ /**
+ * Returns whether the function is an aggegation function<br>
+ * that combines multiple rows to one result row.
+ *
+ * @return true if the function is an aggregate or false otherwise
+ */
+ @Override
+ public boolean isAggregate()
+ {
+ return isAggregate || expr.isAggregate();
+ }
+
+ /**
+ * @see org.apache.empire.db.DBExpr#addReferencedColumns(Set)
+ */
+ @Override
+ public void addReferencedColumns(Set<DBColumn> list)
+ {
+ expr.addReferencedColumns(list);
+ }
+
+ /**
+ * Creates the SQL-Command adds a function to the SQL-Command.
+ *
+ * @param buf the SQL-Command
+ * @param context the current SQL-Command context
+ */
+ @Override
+ public void addSQL(StringBuilder buf, long context)
+ { // Expression
+ buf.append(prefix);
+ expr.addSQL(buf, context & ~CTX_ALIAS );
+ buf.append(postfix);
+ }
+}
\ No newline at end of file
Added: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBFuncExprEx.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBFuncExprEx.java?rev=683173&view=auto
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBFuncExprEx.java (added)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBFuncExprEx.java Wed Aug 6 01:47:37 2008
@@ -0,0 +1,178 @@
+/*
+ * ESTEAM Software GmbH
+ */
+package org.apache.empire.db.expr.column;
+
+// Java
+import java.util.Set;
+
+import org.apache.empire.data.DataType;
+import org.apache.empire.db.DBColumn;
+import org.apache.empire.db.DBColumnExpr;
+import org.apache.empire.db.DBDatabase;
+import org.apache.empire.xml.XMLUtil;
+import org.w3c.dom.Element;
+
+
+/**
+ * This class is used for performing special SQL functions.
+ * <P>
+ * @author ESTEAM software <A TARGET="esteam" HREF="http://www.esteam.de">www.esteam.de </A>
+ */
+public class DBFuncExprEx extends DBColumnExpr
+{
+ protected final DBColumnExpr expr1;
+ protected final DBColumnExpr expr2;
+
+ protected final String prefix;
+ protected final String middle;
+ protected final String postfix;
+
+ /**
+ * Constructs a new DBFuncExprEx object set the specified parameters to this object.
+ *
+ * @param prefix the prefix of the function
+ * @param expr1 the DBColumnExpr object
+ * @param middle the middle of the function
+ * @param expr2 the DBColumnExpr object
+ * @param postfix the postfix of the function
+ */
+ public DBFuncExprEx(String prefix, DBColumnExpr expr1, String middle, DBColumnExpr expr2, String postfix)
+ {
+ this.prefix = prefix;
+ this.expr1 = expr1;
+ this.middle = middle;
+ this.expr2 = expr2;
+ this.postfix = postfix;
+ }
+
+ /**
+ * Constructs a new DBFuncExprEx object Same as DBFuncExpr,
+ * but you can also set a column as the update column set the
+ * specified parameters to this object.
+ *
+ * @param prefix the prefix of the function
+ * @param expr1 the DBColumnExpr object
+ * @param postfix the postfix of the function
+ */
+ public DBFuncExprEx(String prefix, DBColumnExpr expr1, String postfix)
+ {
+ this.prefix = prefix;
+ this.expr1 = expr1;
+ this.middle = null;
+ this.expr2 = null;
+ this.postfix = postfix;
+ }
+
+ /**
+ * Returns the current DBDatabase object.
+ *
+ * @return the current DBDatabase object
+ */
+ @Override
+ public DBDatabase getDatabase()
+ {
+ return expr1.getDatabase();
+ }
+
+ /**
+ * Returns the data type of the DBColumnExpr object.
+ *
+ * @return the data type
+ */
+ @Override
+ public DataType getDataType()
+ {
+ return expr1.getDataType();
+ }
+
+ /**
+ * Returns the column name.
+ *
+ * @return the column name
+ */
+ @Override
+ public String getName()
+ {
+ return (expr2!=null) ? expr1.getName() + "_" + expr2.getName() : expr1.getName();
+ }
+
+ @Override
+ public Element addXml(Element parent, long flags)
+ {
+ Element elem = XMLUtil.addElement(parent, "function");
+ if (prefix!=null)
+ elem.setAttribute("prefix", prefix);
+ if (middle!=null)
+ elem.setAttribute("middle", middle);
+ if (postfix!=null)
+ elem.setAttribute("postfix", postfix);
+ // add All Attributes
+ if (attributes!=null)
+ attributes.addXml(elem, flags);
+ // add Expressions
+ if (expr1!=null)
+ expr1.addXml(elem, flags);
+ if (expr2!=null)
+ expr2.addXml(elem, flags);
+ // add All Options
+ if (options!=null)
+ options.addXml(elem, flags);
+ return elem;
+ }
+
+ /**
+ * Returns the DBColunm object.
+ *
+ * @return the DBColunm object
+ */
+ @Override
+ public DBColumn getUpdateColumn()
+ {
+ return null;
+ }
+
+ /**
+ * Returns whether the function is an aggegation function<br>
+ * that combines multiple rows to one result row.
+ *
+ * @return true if the function is an aggregate or false otherwise
+ */
+ @Override
+ public boolean isAggregate()
+ {
+ // Aggregates are not yet supported.
+ return false;
+ }
+
+ /**
+ * @see org.apache.empire.db.DBExpr#addReferencedColumns(Set)
+ */
+ @Override
+ public void addReferencedColumns(Set<DBColumn> list)
+ {
+ if (expr1!=null)
+ expr1.addReferencedColumns(list);
+ if (expr2!=null)
+ expr2.addReferencedColumns(list);
+ }
+
+ /**
+ * Creates the SQL-Command adds a function to the SQL-Command.
+ *
+ * @param buf the SQL-Command
+ * @param context the current SQL-Command context
+ */
+ @Override
+ public void addSQL(StringBuilder buf, long context)
+ { // Expression
+ buf.append(prefix);
+ expr1.addSQL(buf, context & ~CTX_ALIAS );
+ if (middle != null)
+ {
+ buf.append(middle);
+ expr2.addSQL(buf, context & ~CTX_ALIAS );
+ }
+ buf.append(postfix);
+ }
+}
\ No newline at end of file
Added: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBValueExpr.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBValueExpr.java?rev=683173&view=auto
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBValueExpr.java (added)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBValueExpr.java Wed Aug 6 01:47:37 2008
@@ -0,0 +1,161 @@
+/*
+ * ESTEAM Software GmbH
+ */
+package org.apache.empire.db.expr.column;
+
+// Java
+import java.util.Set;
+
+import org.apache.empire.data.DataType;
+import org.apache.empire.db.DBColumn;
+import org.apache.empire.db.DBColumnExpr;
+import org.apache.empire.db.DBDatabase;
+import org.apache.empire.db.DBDatabaseDriver;
+import org.apache.empire.xml.XMLUtil;
+import org.w3c.dom.Element;
+
+
+/**
+ * This class is used for declaring constant values in SQL.
+ * <P>
+ * There is no need to explicitly create instances of this class.<BR>
+ * Instead use {@link DBDatabase#getValueExpr(String)} or one of it's overloads
+ * <P>
+ * @author ESTEAM software <A TARGET="esteam" HREF="http://www.esteam.de">www.esteam.de</A>
+ */
+public class DBValueExpr extends DBColumnExpr
+{
+ public final DBDatabase db;
+ public final Object value;
+ public final DataType type;
+ public final DBColumnExpr column;
+
+ /**
+ * Constructs a new DBValueExpr object set the specified parameters to this object.
+ */
+ public DBValueExpr(DBDatabase db, Object value, DataType type)
+ {
+ this.db = db;
+ this.value = value;
+ this.type = type;
+ this.column = null;
+ }
+
+ /**
+ * Construct a new DBValueExpr object set the specified parameters to this object.
+ */
+ public DBValueExpr(DBColumnExpr col, Object value)
+ {
+ this.column = col;
+ this.type = col.getDataType();
+ this.db = col.getDatabase();
+ this.value = value;
+ }
+
+ /**
+ * Returns the current DBDatabase object.
+ *
+ * @return the current DBDatabase object
+ */
+ @Override
+ public DBDatabase getDatabase()
+ {
+ return db;
+ }
+
+ /**
+ * Returns the data type of the DBColumnExpr object.
+ *
+ * @return the data type
+ */
+ @Override
+ public DataType getDataType()
+ {
+ return type;
+ }
+
+ /**
+ * Returns the column name.
+ *
+ * @return the column name
+ */
+ @Override
+ public String getName()
+ {
+ return (column != null) ? column.getName() : null;
+ }
+
+ /** this helper function calls the DBColumnExpr.addXML(Element, long) method */
+ @Override
+ public Element addXml(Element parent, long flags)
+ {
+ Element elem;
+ if (column!=null)
+ { // Update Column
+ elem = column.addXml(parent, flags);
+ }
+ else
+ { // Add a column expression for this function
+ elem = XMLUtil.addElement(parent, "column");
+ String name = getName();
+ if (name!=null)
+ elem.setAttribute("name", getName());
+ // Add Other Attributes
+ if (attributes!=null)
+ attributes.addXml(elem, flags);
+ // add All Options
+ if (options!=null)
+ options.addXml(elem, flags);
+ }
+ // Done
+ elem.setAttribute("function", "value");
+ return elem;
+ }
+
+ /**
+ * Returns null.
+ *
+ * @return null
+ */
+ @Override
+ public DBColumn getUpdateColumn()
+ {
+ return (column != null) ? column.getUpdateColumn() : null;
+ }
+
+ /**
+ * Always returns false since value expressions cannot be an aggregate.
+ *
+ * @return false
+ */
+ @Override
+ public boolean isAggregate()
+ {
+ return false;
+ }
+
+ /**
+ * Creates the SQL-Command.
+ *
+ * @param buf the SQL-Command
+ * @param context the current SQL-Command context
+ */
+ @Override
+ public void addSQL(StringBuilder buf, long context)
+ {
+ DBDatabaseDriver driver = db.getDriver();
+ String text = (driver!=null) ? driver.getValueString(value, getDataType()) : String.valueOf(value);
+ buf.append(text);
+ }
+
+ /**
+ * @see org.apache.empire.db.DBExpr#addReferencedColumns(Set)
+ */
+ @Override
+ public void addReferencedColumns(Set<DBColumn> list)
+ {
+ // nothing to do!
+ return;
+ }
+
+}
Added: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/package.html
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/package.html?rev=683173&view=auto
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/package.html (added)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/package.html Wed Aug 6 01:47:37 2008
@@ -0,0 +1,15 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+<html>
+<head>
+<!--
+/*
+ * ESTEAM Software GmbH, 12.12.2007
+ */
+-->
+</head>
+<body>
+
+This package contains SQL-generator classes for column expressions.
+There is no need to use these classes directly. They are internally created through functions on the DBColumnExpr object.
+
+</body></html>
\ No newline at end of file
Added: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/DBCompareAndOrExpr.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/DBCompareAndOrExpr.java?rev=683173&view=auto
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/DBCompareAndOrExpr.java (added)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/DBCompareAndOrExpr.java Wed Aug 6 01:47:37 2008
@@ -0,0 +1,135 @@
+/*
+ * ESTEAM Software GmbH
+ */
+package org.apache.empire.db.expr.compare;
+
+import java.util.Set;
+
+import org.apache.empire.db.DBColumn;
+import org.apache.empire.db.DBDatabase;
+
+
+/**
+ * This class is used for combining two filter constraints by and / or operator<br>
+ * <P>
+ * There is no need to explicitly create instances of this class.<BR>
+ * Instead use {@link DBCompareExpr#and(DBCompareExpr)} or {@link DBCompareExpr#or(DBCompareExpr)}
+ * <P>
+ * @author ESTEAM software <A TARGET="esteam" HREF="http://www.esteam.de">www.esteam.de </A>
+ */
+public class DBCompareAndOrExpr extends DBCompareExpr
+{
+ protected final DBCompareExpr left;
+ protected final DBCompareExpr right;
+ protected final boolean or;
+
+ /**
+ * Constructs a DBCompareColExpr object set the specified parameters to this object.
+ */
+ public DBCompareAndOrExpr(DBCompareExpr left, DBCompareExpr right, boolean or)
+ {
+ this.left = left;
+ this.right = right;
+ this.or = or;
+ }
+
+ public DBCompareExpr getLeft()
+ {
+ return left;
+ }
+
+ public DBCompareExpr getRight()
+ {
+ return right;
+ }
+
+ public boolean isOr()
+ {
+ return or;
+ }
+
+ /**
+ * Returns the current DBDatabase object.
+ *
+ * @return the current DBDatabase object
+ */
+ @Override
+ public DBDatabase getDatabase()
+ {
+ return left.getDatabase();
+ }
+
+ /**
+ * @see org.apache.empire.db.DBExpr#addReferencedColumns(Set)
+ */
+ @Override
+ public void addReferencedColumns(Set<DBColumn> list)
+ { // return all Columns
+ left.addReferencedColumns(list);
+ right.addReferencedColumns(list);
+ }
+
+ /**
+ * Creates the SQL-Command sets the specified compare value
+ * (the varible boolOP) between the two DBCompareExpr objects.
+ *
+ * @param buf the SQL statment
+ * @param context the current SQL-Command context
+ */
+ @Override
+ public void addSQL(StringBuilder buf, long context)
+ {
+ // Name or Value Only ?
+ if ((context & CTX_NAME )==0 ||
+ (context & CTX_VALUE)==0)
+ { // add both values separated by ","
+ left.addSQL(buf, context);
+ buf.append(",");
+ right.addSQL(buf, context);
+ return;
+ }
+ // Parenthesis
+ boolean parenthesis = ((context & CTX_NOPARENTHESES) == 0) && or;
+ boolean nested = ((left instanceof DBCompareAndOrExpr) && ((DBCompareAndOrExpr)left).or==false);
+ if (parenthesis)
+ buf.append("(");
+ if (parenthesis && nested)
+ buf.append("(");
+ // the left expression
+ left.addSQL(buf, context);
+ // Parenthesis
+ if (parenthesis && nested)
+ buf.append(")");
+ // Combine operator
+ buf.append((or ? " OR " : " AND "));
+ // Parenthesis
+ nested = ((right instanceof DBCompareAndOrExpr) && ((DBCompareAndOrExpr)right).or==false);
+ if (parenthesis && nested)
+ buf.append("(");
+ // the right expression
+ right.addSQL(buf, context);
+ if (parenthesis && nested)
+ buf.append(")");
+ // Parenthesis
+ if (parenthesis)
+ buf.append(")");
+ }
+
+ /**
+ * Returns wheter the constraint should replace another one or not.
+ *
+ * @return true it the constraints are mutually exclusive or false otherwise
+ */
+ @Override
+ public boolean isMutuallyExclusive(DBCompareExpr other)
+ {
+ if (other instanceof DBCompareAndOrExpr)
+ {
+ DBCompareAndOrExpr o = (DBCompareAndOrExpr)other;
+ if (left.equals(o.left) && right.equals(o.right))
+ return true;
+ }
+ return false;
+ }
+
+}
\ No newline at end of file
Added: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/DBCompareColExpr.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/DBCompareColExpr.java?rev=683173&view=auto
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/DBCompareColExpr.java (added)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/DBCompareColExpr.java Wed Aug 6 01:47:37 2008
@@ -0,0 +1,243 @@
+/*
+ * ESTEAM Software GmbH
+ */
+package org.apache.empire.db.expr.compare;
+
+// java
+import java.util.Set;
+
+import org.apache.empire.db.DBCmpType;
+import org.apache.empire.db.DBColumn;
+import org.apache.empire.db.DBColumnExpr;
+import org.apache.empire.db.DBDatabase;
+import org.apache.empire.db.DBExpr;
+
+
+/**
+ * This class is used for defining filter constraints based on a column expression in SQL<br>
+ * <P>
+ * There is no need to explicitly create instances of this class.<BR>
+ * Instead use any of the following functions:<BR>
+ * {@link DBColumnExpr#is(Object) }, {@link DBColumnExpr#isBetween(Object, Object) }, {@link DBColumnExpr#isGreaterThan(Object) },
+ * {@link DBColumnExpr#isLessOrEqual(Object) }, {@link DBColumnExpr#isMoreOrEqual(Object) }, {@link DBColumnExpr#isNot(Object) },
+ * {@link DBColumnExpr#isNotBetween(Object, Object) }, {@link DBColumnExpr#isSmallerThan(Object) }, {@link DBColumnExpr#like(Object) },
+ * {@link DBColumnExpr#like(String, char) }, {@link DBColumnExpr#likeLower(String) }, {@link DBColumnExpr#likeUpper(String) }
+ *
+ * @author ESTEAM software <A TARGET="esteam" HREF="http://www.esteam.de">www.esteam.de </A>
+ */
+public class DBCompareColExpr extends DBCompareExpr
+{
+ protected final DBColumnExpr expr;
+ protected final DBCmpType cmpop;
+ protected Object value;
+
+ /**
+ * Constructs a DBCompareColExpr object set the specified parameters to this object.
+ *
+ * @param expr the DBColumnExpr object
+ * @param op the comparative context e.g. (CMP_EQUAL, CMP_SMALLER)
+ * @param value the comparative value
+ */
+ public DBCompareColExpr(DBColumnExpr expr, DBCmpType op, Object value)
+ {
+ this.expr = expr;
+ this.cmpop = op;
+ this.value = value;
+ }
+
+ /**
+ * Returns the current DBDatabase object.
+ *
+ * @return the current DBDatabase object
+ */
+ @Override
+ public DBDatabase getDatabase()
+ {
+ return expr.getDatabase();
+ }
+
+ /** returns the DBColumnExpr object */
+ public DBColumnExpr getColumnExpr()
+ {
+ return expr;
+ }
+
+ /**
+ * @return the comparison operator
+ */
+ public DBCmpType getCmpop()
+ {
+ return cmpop;
+ }
+
+ /**
+ * @return the value to compare the column expression with
+ */
+ public Object getValue()
+ {
+ return value;
+ }
+
+ /**
+ * The value to compare the column expression with
+ * @param value the comparision value
+ */
+ public void setValue(Object value)
+ {
+ this.value = value;
+ }
+
+ /**
+ * @see org.apache.empire.db.DBExpr#addReferencedColumns(Set)
+ */
+ @Override
+ public void addReferencedColumns(Set<DBColumn> list)
+ { // return all Columns
+ expr.addReferencedColumns(list);
+ // Check if Value is a Column Expression
+ if (value instanceof DBExpr)
+ ((DBExpr)value).addReferencedColumns(list);
+ }
+
+ /**
+ * Add the comparison operator and value to the SQL-Command.
+ *
+ * @param buf the SQL-Command
+ * @param context the comparative context e.g. (CMP_EQUAL, CMP_SMALLER)
+ */
+ public void addCompareExpr(StringBuilder buf, long context)
+ { // Zusammenbauen
+ String arraySep = "+";
+ DBCmpType op = cmpop;
+ switch (op)
+ { // other than default:
+ case BETWEEN:
+ case NOTBETWEEN:
+ arraySep = " AND ";
+ break;
+ case IN:
+ case NOTIN:
+ arraySep = ", ";
+ break;
+ default:
+ // Nothing to do
+ break;
+ }
+ // Value
+ String valsql = getObjectValue(expr, value, context, arraySep);
+ if (valsql == null || valsql.equalsIgnoreCase("null"))
+ { // Null oder Not Null!
+ op = DBCmpType.getNullType(op);
+ }
+ // Zusammenbauen
+ switch (op)
+ {
+ case EQUAL:
+ buf.append("=");
+ break;
+ case NOTEQUAL:
+ buf.append("<>");
+ break;
+ case LESSTHAN:
+ buf.append("<");
+ break;
+ case MOREOREQUAL:
+ buf.append(">=");
+ break;
+ case GREATERTHAN:
+ buf.append(">");
+ break;
+ case LESSOREQUAL:
+ buf.append("<=");
+ break;
+ case LIKE:
+ buf.append(" LIKE ");
+ break;
+ case NOTLIKE:
+ buf.append(" NOT LIKE ");
+ break;
+ case NULL:
+ buf.append(" IS NULL");
+ valsql = null;
+ break;
+ case NOTNULL:
+ buf.append(" IS NOT NULL");
+ valsql = null;
+ break;
+ case BETWEEN:
+ buf.append(" BETWEEN ");
+ break;
+ case NOTBETWEEN:
+ buf.append(" NOT BETWEEN ");
+ break;
+ case IN:
+ buf.append(" IN (");
+ buf.append(valsql);
+ buf.append(")");
+ valsql = null;
+ break;
+ case NOTIN:
+ buf.append(" NOT IN (");
+ buf.append(valsql);
+ buf.append(")");
+ valsql = null;
+ break;
+ // Wer hat das gemacht?
+ // case LIKE_UPPER: buf.append(" LIKE upper(");buf.append(valsql);buf.append(")");valsql=null;break;
+ default:
+ // NONE
+ buf.append(" ");
+ }
+ if (valsql != null)
+ buf.append(valsql);
+ }
+
+ /**
+ * Creates the SQL-Command.
+ *
+ * @param buf the SQL-Command
+ * @param context the current SQL-Command context
+ */
+ @Override
+ public void addSQL(StringBuilder buf, long context)
+ {
+ // Name Only ?
+ if ((context & CTX_VALUE) == 0)
+ {
+ expr.addSQL(buf, context);
+ return;
+ }
+ // Value Only ?
+ if ((context & CTX_NAME) == 0)
+ {
+ String valsql = getObjectValue(expr, value, context, "+");
+ buf.append((valsql != null) ? valsql : "null");
+ return;
+ }
+ // Add Compare Expression
+ expr.addSQL(buf, context);
+ addCompareExpr(buf, context);
+ }
+
+ /**
+ * Returns wheter the constraint should replace another one or not.
+ *
+ * @return true it the constraints are mutually exclusive or false otherwise
+ */
+ @Override
+ public boolean isMutuallyExclusive(DBCompareExpr other)
+ {
+ if (other instanceof DBCompareColExpr)
+ {
+ DBCompareColExpr o = (DBCompareColExpr)other;
+ DBColumnExpr oexpr = o.getColumnExpr();
+ if (expr.equals(oexpr))
+ return true;
+ DBColumn col = expr.getUpdateColumn();
+ DBColumn ocol = oexpr.getUpdateColumn();
+ return (col!=null) ? (col.equals(ocol)) : false;
+ }
+ return false;
+ }
+
+}
\ No newline at end of file
Added: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/DBCompareExpr.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/DBCompareExpr.java?rev=683173&view=auto
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/DBCompareExpr.java (added)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/DBCompareExpr.java Wed Aug 6 01:47:37 2008
@@ -0,0 +1,45 @@
+/*
+ * ESTEAM Software GmbH
+ */
+package org.apache.empire.db.expr.compare;
+
+import org.apache.empire.db.DBExpr;
+
+/**
+ * This class is a common base class for all SQL filter constraints classes<br>
+ * <P>
+ * @author ESTEAM software <A TARGET="esteam" HREF="http://www.esteam.de">www.esteam.de</A>
+ */
+public abstract class DBCompareExpr extends DBExpr
+{
+ public abstract boolean isMutuallyExclusive(DBCompareExpr other);
+
+ /**
+ * Creates a new DBCompareORExpr object.
+ */
+ public DBCompareExpr and(DBCompareExpr expr)
+ {
+ // " AND "
+ return new DBCompareAndOrExpr(this, expr, false);
+ }
+
+ /**
+ * Create a new DBCompareORExpr object.
+ */
+ public DBCompareExpr or(DBCompareExpr expr)
+ {
+ // " OR "
+ return new DBCompareAndOrExpr(this, expr, true);
+ }
+
+ /**
+ * Creates a sql-expression for the not() function.
+ *
+ * @return the new DBCompareColExpr object
+ */
+ public DBCompareExpr not()
+ {
+ return new DBCompareNotExpr(this);
+ }
+
+}
Added: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/DBCompareNotExpr.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/DBCompareNotExpr.java?rev=683173&view=auto
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/DBCompareNotExpr.java (added)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/DBCompareNotExpr.java Wed Aug 6 01:47:37 2008
@@ -0,0 +1,85 @@
+/*
+ * ESTEAM Software GmbH
+ */
+package org.apache.empire.db.expr.compare;
+
+import java.util.Set;
+
+import org.apache.empire.db.DBColumn;
+import org.apache.empire.db.DBDatabase;
+
+
+/**
+ * This class is used for defining filter constraints based on a column expression in SQL<br>
+ * <P>
+ * There is no need to explicitly create instances of this class.<BR>
+ * Instead use {@link DBCompareExpr#not()}<BR>
+ * <P>
+ * @author ESTEAM software <A TARGET="esteam" HREF="http://www.esteam.de">www.esteam.de </A>
+ */
+public class DBCompareNotExpr extends DBCompareExpr
+{
+ public final DBCompareExpr expr;
+
+ /**
+ * constructs a DBCompareColExpr object set the specified parameters to this object
+ */
+ public DBCompareNotExpr(DBCompareExpr expr)
+ {
+ this.expr = expr;
+ }
+
+ /**
+ * Returns the current DBDatabase object.
+ *
+ * @return the current DBDatabase object
+ */
+ @Override
+ public DBDatabase getDatabase()
+ {
+ return expr.getDatabase();
+ }
+
+ /**
+ * @see org.apache.empire.db.DBExpr#addReferencedColumns(Set)
+ */
+ @Override
+ public void addReferencedColumns(Set<DBColumn> list)
+ {
+ expr.addReferencedColumns(list);
+ }
+
+ /**
+ * Creates the SQL-Command sets the specified compare value
+ * (the varible boolOP) between the two DBCompareExpr objects.
+ *
+ * @param buf the SQL statment
+ * @param context the current SQL-Command context
+ */
+ @Override
+ public void addSQL(StringBuilder buf, long context)
+ {
+ // Name Only ?
+ if ((context & CTX_VALUE)==0)
+ { // add both values separated by ","
+ expr.addSQL(buf, context);
+ return;
+ }
+ // add SQL
+ buf .append(" NOT(");
+ expr.addSQL(buf, context);
+ buf .append(" )");
+ }
+
+ /**
+ * Returns wheter the constraint should replace another one or not.
+ *
+ * @return true it the constraints are mutually exclusive or false otherwise
+ */
+ @Override
+ public boolean isMutuallyExclusive(DBCompareExpr other)
+ {
+ return false;
+ }
+
+}
\ No newline at end of file
Added: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/DBExistsExpr.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/DBExistsExpr.java?rev=683173&view=auto
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/DBExistsExpr.java (added)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/DBExistsExpr.java Wed Aug 6 01:47:37 2008
@@ -0,0 +1,124 @@
+/*
+ * ESTEAM Software GmbH
+ */
+package org.apache.empire.db.expr.compare;
+
+import java.util.Set;
+
+import org.apache.empire.db.DBColumn;
+import org.apache.empire.db.DBCommand;
+import org.apache.empire.db.DBCommandExpr;
+import org.apache.empire.db.DBDatabase;
+
+
+/**
+ * This class is used for building up the SQL-Command for the EXISTS syntax.
+ * <P>
+ *
+ * @author ESTEAM software <A TARGET="esteam" HREF="http://www.esteam.de">www.esteam.de </A>
+ */
+public class DBExistsExpr extends DBCompareExpr
+{
+ public final DBCommandExpr cmd;
+ public final DBCompareExpr compareExpr;
+
+ /**
+ * Constructs a DBExistsExpr object set the specified parameters to this object.
+ *
+ * @param cmd the DBCommandExpr object
+ */
+ public DBExistsExpr(DBCommandExpr cmd)
+ {
+ this(cmd, null);
+ }
+
+ /**
+ * Constructs a DBExistsExpr object set the specified parameters to this object.
+ *
+ * @param cmd the DBCommandExpr object
+ * @param compareExpr The expression to append to the end of the exists statement
+ */
+ public DBExistsExpr(DBCommandExpr cmd, DBCompareExpr compareExpr)
+ {
+ this.cmd = cmd;
+ this.compareExpr = compareExpr;
+ }
+
+ /**
+ * Returns the current DBDatabase object.
+ *
+ * @return the current DBDatabase object
+ */
+ @Override
+ public DBDatabase getDatabase()
+ {
+ return cmd.getDatabase();
+ }
+
+ /**
+ * @see org.apache.empire.db.DBExpr#addReferencedColumns(Set)
+ */
+ @Override
+ public void addReferencedColumns(Set<DBColumn> list)
+ {
+ // nothing to do
+ }
+
+ /**
+ * Creates the SQL-Command.
+ *
+ * @param buf the SQL-Command
+ * @param context the current SQL-Command context (CTX_DEFAULT, CTX_SELECT, CTX_NAME, CTX_VALUE)
+ */
+ @Override
+ public void addSQL(StringBuilder buf, long context)
+ {
+ // Name Only ?
+ if ((context & CTX_VALUE) == 0)
+ {
+ log.warn("cannot add name only of exits expression");
+ return;
+ }
+ // Value Only ?
+ if ((context & CTX_NAME) == 0)
+ {
+ log.warn("cannot add value only of exits expression");
+ return;
+ }
+ buf.append(" exists (");
+ cmd.getSelect(buf);
+
+ if (compareExpr != null)
+ {
+ if (cmd instanceof DBCommand && ((DBCommand) cmd).getWhereConstraints() == null)
+ {
+ buf.append(" where ");
+ }
+ else
+ {
+ buf.append(" and ");
+ }
+ buf.append("(");
+ compareExpr.addSQL(buf, context);
+ buf.append(") ");
+ }
+ buf.append(") ");
+ }
+
+ /**
+ * Returns wheter the constraint should replace another one or not.
+ * @return true it the constraints are mutually exclusive or false otherwise
+ */
+ @Override
+ public boolean isMutuallyExclusive(DBCompareExpr other)
+ {
+ if (other instanceof DBExistsExpr)
+ {
+ DBExistsExpr o = (DBExistsExpr)other;
+ if (cmd==o.cmd && compareExpr==o.compareExpr)
+ return true;
+ }
+ return false;
+ }
+
+}
\ No newline at end of file
Added: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/package.html
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/package.html?rev=683173&view=auto
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/package.html (added)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/compare/package.html Wed Aug 6 01:47:37 2008
@@ -0,0 +1,15 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+<html>
+<head>
+<!--
+/*
+ * ESTEAM Software GmbH, 12.12.2007
+ */
+-->
+</head>
+<body>
+
+This package contains SQL-generator classes for compare expressions used in the where and having clause.
+There is no need to use these classes directly. They are internally created through functions on the DBColumnExpr object.
+
+</body></html>
\ No newline at end of file