summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.persistence/src/main/java/net/wotonomy/access/EOSQLExpression.java
diff options
context:
space:
mode:
Diffstat (limited to 'projects/net.wotonomy.persistence/src/main/java/net/wotonomy/access/EOSQLExpression.java')
-rw-r--r--projects/net.wotonomy.persistence/src/main/java/net/wotonomy/access/EOSQLExpression.java170
1 files changed, 92 insertions, 78 deletions
diff --git a/projects/net.wotonomy.persistence/src/main/java/net/wotonomy/access/EOSQLExpression.java b/projects/net.wotonomy.persistence/src/main/java/net/wotonomy/access/EOSQLExpression.java
index 4cb15e5..c2b6e76 100644
--- a/projects/net.wotonomy.persistence/src/main/java/net/wotonomy/access/EOSQLExpression.java
+++ b/projects/net.wotonomy.persistence/src/main/java/net/wotonomy/access/EOSQLExpression.java
@@ -37,10 +37,10 @@ import net.wotonomy.foundation.NSTimestamp;
import net.wotonomy.foundation.NSTimestampFormatter;
/**
-* @author ezamudio@nasoft.com
-* @author $Author: cgruber $
-* @version $Revision: 894 $
-*/
+ * @author ezamudio@nasoft.com
+ * @author $Author: cgruber $
+ * @version $Revision: 894 $
+ */
public abstract class EOSQLExpression {
public static final String BindVariableAttributeKey = "BindVariableAttribute";
@@ -54,9 +54,8 @@ public abstract class EOSQLExpression {
private static final int _DefaultOrderByStringLength = 128;
private static final int _DefaultPathLength = 128;
private static final int _DefaultTableListLength = 128;
- protected static final char[] _hexChars = new char[]{
- '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
- };
+ protected static final char[] _hexChars = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
+ 'C', 'D', 'E', 'F' };
private static final int _ValueLengthLimit = 40;
protected NSMutableDictionary _aliasesByRelationshipPath;
protected NSMutableDictionary _aliasesByEntityName;
@@ -77,7 +76,7 @@ public abstract class EOSQLExpression {
private EOSQLExpression() {
super();
}
-
+
public EOSQLExpression(EOEntity entity) {
super();
_entity = entity;
@@ -88,7 +87,7 @@ public abstract class EOSQLExpression {
}
public String _aliasForRelationshipPath(String path) {
- return (String)_aliasesByRelationshipPath.objectForKey(path);
+ return (String) _aliasesByRelationshipPath.objectForKey(path);
}
protected NSTimestampFormatter _defaultDateFormatter() {
@@ -115,7 +114,7 @@ public abstract class EOSQLExpression {
_entity = value;
}
- public String _sqlStringForJoinSemanticMatchSemantic(int semantic,int match) {
+ public String _sqlStringForJoinSemanticMatchSemantic(int semantic, int match) {
return null;
}
@@ -129,11 +128,13 @@ public abstract class EOSQLExpression {
return _valueListString;
}
- public void addBindVariableDictionary( NSDictionary dict ) {
+ public void addBindVariableDictionary(NSDictionary dict) {
}
- /** Adds the SQL to create the attribute to the attribute list.
- * The appended text is of the form attr_name attr_type allow_null
+ /**
+ * Adds the SQL to create the attribute to the attribute list. The appended text
+ * is of the form attr_name attr_type allow_null
+ *
* @param attr The attribute to create the SQL for.
*/
public void addCreateClauseForAttribute(EOAttribute attr) {
@@ -158,9 +159,11 @@ public abstract class EOSQLExpression {
public void addOrderByAttributeOrdering(EOSortOrdering order) {
String sql = sqlStringForAttributeNamed(order.key());
- if (order.selector().equals(EOSortOrdering.CompareCaseInsensitiveAscending) || order.selector().equals(EOSortOrdering.CompareCaseInsensitiveDescending))
+ if (order.selector().equals(EOSortOrdering.CompareCaseInsensitiveAscending)
+ || order.selector().equals(EOSortOrdering.CompareCaseInsensitiveDescending))
sql = "UPPER(" + sql + ")";
- if (order.selector().equals(EOSortOrdering.CompareCaseInsensitiveAscending) || order.selector().equals(EOSortOrdering.CompareAscending))
+ if (order.selector().equals(EOSortOrdering.CompareCaseInsensitiveAscending)
+ || order.selector().equals(EOSortOrdering.CompareAscending))
sql += " ASC";
else
sql += " DESC";
@@ -205,8 +208,8 @@ public abstract class EOSQLExpression {
return s;
}
- public String assembleInsertStatementWithRow(NSDictionary row,
- String tableList, String columnList, String valueList) {
+ public String assembleInsertStatementWithRow(NSDictionary row, String tableList, String columnList,
+ String valueList) {
String sql = "INSERT INTO " + tableList;
if (columnList != null)
sql += " (" + columnList + ")";
@@ -223,9 +226,9 @@ public abstract class EOSQLExpression {
return leftName + op + rightName;
}
- public String assembleSelectStatementWithAttributes(NSArray attributes,
- boolean lock, EOQualifier q, NSArray fetchOrder, String selectString, String columnList,
- String tableList, String whereClause, String joinClause, String orderByClause, String lockClause) {
+ public String assembleSelectStatementWithAttributes(NSArray attributes, boolean lock, EOQualifier q,
+ NSArray fetchOrder, String selectString, String columnList, String tableList, String whereClause,
+ String joinClause, String orderByClause, String lockClause) {
String sql = selectString + " " + columnList + " FROM " + tableList;
if (lockClause != null)
sql += " " + lockClause;
@@ -242,7 +245,8 @@ public abstract class EOSQLExpression {
return sql;
}
- public String assembleUpdateStatementWithRow(NSDictionary row, EOQualifier q, String tableList, String updateList, String whereClause) {
+ public String assembleUpdateStatementWithRow(NSDictionary row, EOQualifier q, String tableList, String updateList,
+ String whereClause) {
String s = "UPDATE " + tableList + " SET " + updateList;
if (whereClause != null && whereClause.length() > 0)
s += " WHERE " + whereClause;
@@ -280,14 +284,14 @@ public abstract class EOSQLExpression {
}
/**
- * Returns the received string wrapped in single quotes,
- * with any quotes or escape chars found inside it
- * properly escaped.
+ * Returns the received string wrapped in single quotes, with any quotes or
+ * escape chars found inside it properly escaped.
+ *
* @param s The string to format.
*/
public String formatStringValue(String s) {
StringBuffer buf = new StringBuffer(s);
- for (int i = buf.length()-1; i >= 0; i--) {
+ for (int i = buf.length() - 1; i >= 0; i--) {
if (buf.charAt(i) == sqlEscapeChar()) {
buf.insert(i, sqlEscapeChar());
i++;
@@ -306,10 +310,10 @@ public abstract class EOSQLExpression {
if (value == null || value == NSKeyValueCoding.NullValue)
return "NULL";
if (value instanceof String)
- return formatStringValue((String)value);
+ return formatStringValue((String) value);
if (value instanceof Number)
- return sqlStringForNumber((Number)value);
- //TODO: format timestamps
+ return sqlStringForNumber((Number) value);
+ // TODO: format timestamps
return value.toString();
}
@@ -338,7 +342,7 @@ public abstract class EOSQLExpression {
}
public String orderByString() {
- if (_orderByString == null)
+ if (_orderByString == null)
return null;
return _orderByString.toString();
}
@@ -356,7 +360,7 @@ public abstract class EOSQLExpression {
StringBuffer values = new StringBuffer("(");
Enumeration enumeration = row.keyEnumerator();
while (enumeration.hasMoreElements()) {
- String key = (String)enumeration.nextElement();
+ String key = (String) enumeration.nextElement();
EOAttribute a = _entity.attributeNamed(key);
cols.append(a.columnName());
values.append(formatValueForAttribute(row.objectForKey(key), a));
@@ -379,30 +383,31 @@ public abstract class EOSQLExpression {
q = fspec.qualifier();
order = fspec.sortOrderings();
}
- //Assemble the column list (this yields the alias list)
+ // Assemble the column list (this yields the alias list)
for (int i = 0; i < atts.count(); i++)
- addSelectListAttribute((EOAttribute)atts.objectAtIndex(i));
- //assemble the where string
+ addSelectListAttribute((EOAttribute) atts.objectAtIndex(i));
+ // assemble the where string
if (q != null) {
if (q instanceof EOQualifierSQLGeneration)
- setWhereClauseString(sqlStringForQualifier((EOQualifierSQLGeneration)q));
+ setWhereClauseString(sqlStringForQualifier((EOQualifierSQLGeneration) q));
else {
EOQualifierSQLGeneration.Support sup = EOQualifierSQLGeneration.Support.supportForClass(q.getClass());
setWhereClauseString(sup.sqlStringForSQLExpression(q, this));
}
}
- //assemble the join string
+ // assemble the join string
joinExpression();
- //assemble the order by string
+ // assemble the order by string
if (order != null && order.count() > 0) {
for (int i = 0; i < order.count(); i++) {
- EOSortOrdering so = (EOSortOrdering)order.objectAtIndex(i);
+ EOSortOrdering so = (EOSortOrdering) order.objectAtIndex(i);
addOrderByAttributeOrdering(so);
}
}
- //create the statement
+ // create the statement
setStatement(assembleSelectStatementWithAttributes(atts, lock, q, order, "SELECT", listString(),
- tableListWithRootEntity(_entity), whereClauseString(), joinClauseString(), orderByString(), lockClause()));
+ tableListWithRootEntity(_entity), whereClauseString(), joinClauseString(), orderByString(),
+ lockClause()));
}
/** Build an UPDATE statement with the given information. */
@@ -410,10 +415,11 @@ public abstract class EOSQLExpression {
StringBuffer buf = new StringBuffer();
Enumeration enumeration = row.keyEnumerator();
while (enumeration.hasMoreElements()) {
- String key = (String)enumeration.nextElement();
+ String key = (String) enumeration.nextElement();
EOAttribute a = _entity.attributeNamed(key);
if (a == null)
- throw new EOGeneralAdaptorException("Cannot find attribute named " + key + " in entity " + _entity.name());
+ throw new EOGeneralAdaptorException(
+ "Cannot find attribute named " + key + " in entity " + _entity.name());
buf.append(a.columnName());
buf.append('=');
buf.append(formatValueForAttribute(row.objectForKey(key), a));
@@ -423,7 +429,8 @@ public abstract class EOSQLExpression {
if (q != null) {
setWhereClauseString(sqlStringForQualifier(null));
}
- setStatement(assembleUpdateStatementWithRow(row, q, _entity.externalName(), buf.toString(), whereClauseString()));
+ setStatement(
+ assembleUpdateStatementWithRow(row, q, _entity.externalName(), buf.toString(), whereClauseString()));
}
public void setStatement(String statement) {
@@ -453,6 +460,7 @@ public abstract class EOSQLExpression {
public static void setUseQuotedExternalNames(boolean flag) {
_quoteExternalNames = flag;
}
+
/** @deprecated Use the instance method externalNameQuoteCharacter instead. */
public static boolean useQuotedExternalNames() {
return _quoteExternalNames;
@@ -481,33 +489,33 @@ public abstract class EOSQLExpression {
public String sqlPatternFromShellPatternWithEscapeCharacter(String pattern, char escape) {
StringBuffer buf = new StringBuffer(pattern);
int idx = 0;
- //escape all '%'
+ // escape all '%'
do {
idx = buf.indexOf("%");
if (idx == 0)
buf.insert(escape, 0);
- else if (idx > 0 && buf.charAt(idx-1) != escape)
+ else if (idx > 0 && buf.charAt(idx - 1) != escape)
buf.insert(escape, idx);
} while (idx >= 0);
- //escape all '_'
+ // escape all '_'
do {
idx = buf.indexOf("_");
if (idx == 0)
buf.insert(escape, 0);
- else if (idx > 0 && buf.charAt(idx-1) != escape)
+ else if (idx > 0 && buf.charAt(idx - 1) != escape)
buf.insert(escape, idx);
} while (idx >= 0);
- //substitute all '*'
+ // substitute all '*'
do {
idx = buf.indexOf("*");
if (idx >= 0)
- buf.replace(idx, idx+1, "%");
+ buf.replace(idx, idx + 1, "%");
} while (idx >= 0);
- //substitute all '?'
+ // substitute all '?'
do {
idx = buf.indexOf("?");
if (idx >= 0)
- buf.replace(idx, idx+1, "_");
+ buf.replace(idx, idx + 1, "_");
} while (idx >= 0);
return buf.toString();
}
@@ -515,7 +523,7 @@ public abstract class EOSQLExpression {
public String sqlStringForAttribute(EOAttribute attr) {
if (_aliasesByEntityName == null)
_aliasesByEntityName = new NSMutableDictionary();
- String alias = (String)_aliasesByEntityName.objectForKey(attr.entity().name());
+ String alias = (String) _aliasesByEntityName.objectForKey(attr.entity().name());
if (alias == null) {
alias = "t" + (_aliasesByEntityName.count() + 1);
_aliasesByEntityName.setObjectForKey(alias, attr.entity().name());
@@ -531,20 +539,21 @@ public abstract class EOSQLExpression {
return sqlStringForAttribute(_entity._attributeForPath(name));
}
return sqlStringForAttribute(_entity.attributeNamed(name));
-
+
}
/**
- * Returns a string representing the path from the first
- * relationship in the array to the last one.
+ * Returns a string representing the path from the first relationship in the
+ * array to the last one.
+ *
* @param path An array of EORelationship objects.
- * @return A string consisting of the names of the relationships
- * separated by dots.
+ * @return A string consisting of the names of the relationships separated by
+ * dots.
*/
public String sqlStringForAttributePath(NSArray path) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < path.count(); i++) {
- EORelationship rel = (EORelationship)path.objectAtIndex(i);
+ EORelationship rel = (EORelationship) path.objectAtIndex(i);
if (i > 0)
buf.append('.');
buf.append(rel.name());
@@ -558,7 +567,8 @@ public abstract class EOSQLExpression {
public String sqlStringForConjoinedQualifiers(NSArray qualifiers) {
EOAndQualifier q = new EOAndQualifier(qualifiers);
- return EOQualifierSQLGeneration.Support.supportForClass(EOAndQualifier.class).sqlStringForSQLExpression(q, this);
+ return EOQualifierSQLGeneration.Support.supportForClass(EOAndQualifier.class).sqlStringForSQLExpression(q,
+ this);
}
public String sqlStringForData(NSData data) {
@@ -580,11 +590,13 @@ public abstract class EOSQLExpression {
}
public String sqlStringForKeyComparisonQualifier(EOKeyComparisonQualifier q) {
- return EOQualifierSQLGeneration.Support.supportForClass(EOKeyComparisonQualifier.class).sqlStringForSQLExpression(q, this);
+ return EOQualifierSQLGeneration.Support.supportForClass(EOKeyComparisonQualifier.class)
+ .sqlStringForSQLExpression(q, this);
}
public String sqlStringForKeyValueQualifier(EOKeyValueQualifier q) {
- return EOQualifierSQLGeneration.Support.supportForClass(EOKeyValueQualifier.class).sqlStringForSQLExpression(q, this);
+ return EOQualifierSQLGeneration.Support.supportForClass(EOKeyValueQualifier.class).sqlStringForSQLExpression(q,
+ this);
}
public String sqlStringForNegatedQualifier(EOQualifier q) {
@@ -622,7 +634,8 @@ public abstract class EOSQLExpression {
return "<=";
} else if (sel == EOQualifier.QualifierOperatorGreaterThanOrEqualTo) {
return ">=";
- } else if (sel == EOQualifier.QualifierOperatorLike || sel == EOQualifier.QualifierOperatorCaseInsensitiveLike) {
+ } else if (sel == EOQualifier.QualifierOperatorLike
+ || sel == EOQualifier.QualifierOperatorCaseInsensitiveLike) {
return " like ";
}
return sel.name();
@@ -646,7 +659,7 @@ public abstract class EOSQLExpression {
if (_aliasesByEntityName.count() > 0) {
Enumeration enumeration = _aliasesByEntityName.keyEnumerator();
while (enumeration.hasMoreElements()) {
- String key = (String)enumeration.nextElement();
+ String key = (String) enumeration.nextElement();
if (!key.equals(root.name())) {
buf.append(", ");
buf.append(key);
@@ -668,30 +681,31 @@ public abstract class EOSQLExpression {
}
/*
- * $Log$
- * Revision 1.2 2006/02/16 16:47:14 cgruber
- * Move some classes in to "internal" packages and re-work imports, etc.
+ * $Log$ Revision 1.2 2006/02/16 16:47:14 cgruber Move some classes in to
+ * "internal" packages and re-work imports, etc.
*
- * Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
+ * Also use UnsupportedOperationExceptions where appropriate, instead of
+ * WotonomyExceptions.
*
- * Revision 1.1 2006/02/16 13:19:57 cgruber
- * Check in all sources in eclipse-friendly maven-enabled packages.
+ * Revision 1.1 2006/02/16 13:19:57 cgruber Check in all sources in
+ * eclipse-friendly maven-enabled packages.
*
- * Revision 1.5 2005/05/11 15:21:53 cgruber
- * Change enum to enumeration, since enum is now a keyword as of Java 5.0
+ * Revision 1.5 2005/05/11 15:21:53 cgruber Change enum to enumeration, since
+ * enum is now a keyword as of Java 5.0
*
* A few other comments in the code.
*
- * Revision 1.4 2003/08/29 21:14:18 chochos
- * fix the algorithm in formatStringValue.
+ * Revision 1.4 2003/08/29 21:14:18 chochos fix the algorithm in
+ * formatStringValue.
*
- * Revision 1.3 2003/08/14 02:12:32 chochos
- * implemented use of (simple) qualifiers
+ * Revision 1.3 2003/08/14 02:12:32 chochos implemented use of (simple)
+ * qualifiers
*
- * Revision 1.2 2003/08/13 22:59:39 chochos
- * Can now generate simple one-entity select statements.
+ * Revision 1.2 2003/08/13 22:59:39 chochos Can now generate simple one-entity
+ * select statements.
*
- * Revision 1.1 2003/08/13 01:04:32 chochos
- * the SQL generation classes. EOSQLExpression still needs a lot of work, but the factory is pretty much done.
+ * Revision 1.1 2003/08/13 01:04:32 chochos the SQL generation classes.
+ * EOSQLExpression still needs a lot of work, but the factory is pretty much
+ * done.
*
*/ \ No newline at end of file