Browse files

Convert JoinArrayEntityCollectionImpl to Gosu

  • Loading branch information...
1 parent 1ae009e commit 2c533d309ff8e0a06079881f98600ce3859fd84f @akeefer committed Feb 15, 2012
View
85 ...a/impl/JoinArrayEntityCollectionImpl.java → ...osa/impl/JoinArrayEntityCollectionImpl.gs
@@ -1,48 +1,47 @@
-package tosa.impl;
+package tosa.impl
-import tosa.api.*;
-import tosa.loader.IDBType;
-
-import java.util.List;
+uses tosa.api.IDBObject
+uses tosa.api.IDBColumn
+uses tosa.loader.IDBType
+uses java.lang.IllegalArgumentException
/**
* Created by IntelliJ IDEA.
- * User: alan
- * Date: 5/15/11
- * Time: 10:20 PM
+ * User: Alan
+ * Date: 2/15/12
+ * Time: 11:22 PM
* To change this template use File | Settings | File Templates.
*/
-public class JoinArrayEntityCollectionImpl<T extends IDBObject> extends EntityCollectionImplBase<T> {
-
- private IDBColumn _srcColumn;
- private IDBColumn _targetColumn;
+class JoinArrayEntityCollectionImpl<T extends IDBObject> extends EntityCollectionImplBase<T> {
+
+ private var _srcColumn : IDBColumn
+ private var _targetColumn : IDBColumn
- public JoinArrayEntityCollectionImpl(IDBObject owner, IDBType fkType, IDBColumn srcColumn, IDBColumn targetColumn, QueryExecutor queryExecutor) {
+ public construct(owner : IDBObject, fkType : IDBType, srcColumn : IDBColumn, targetColumn : IDBColumn, queryExecutor : QueryExecutor) {
super(owner, fkType, queryExecutor);
_srcColumn = srcColumn;
_targetColumn = targetColumn;
}
- @Override
- protected void removeImpl(T element) {
+ protected override function removeImpl(element : T) {
if (!isAlreadyInArray(element)) {
throw new IllegalArgumentException("The element " + element.getDBTable().getName() + "(" + element.getId() +
") cannot be removed from the join array on " + _owner.getDBTable().getName() + "(" + _owner.getId() + ") as it's not currently in the array");
}
- String sql = SimpleSqlBuilder.substitute("DELETE FROM ${joinTable} WHERE ${srcFk} =? AND ${targetFk} = ?",
+ var sql = SimpleSqlBuilder.substitute("DELETE FROM \${joinTable} WHERE \${srcFk} =? AND \${targetFk} = ?",
"joinTable", _srcColumn.getTable(),
"srcFk", _srcColumn,
"targetFk", _targetColumn);
- IPreparedStatementParameter srcParam = _srcColumn.wrapParameterValue(_owner.getId());
- IPreparedStatementParameter targetParam = _targetColumn.wrapParameterValue(element.getId());
- _queryExecutor.delete("JoinArrayEntityCollectionImpl.removeImpl()", sql, srcParam, targetParam);
+ var srcParam = _srcColumn.wrapParameterValue(_owner.getId());
+ var targetParam = _targetColumn.wrapParameterValue(element.getId());
+ _queryExecutor.delete("JoinArrayEntityCollectionImpl.removeImpl()", sql, {srcParam, targetParam});
// If the results have already been loaded, we need to remove the element. We can't do just .equals() or a pointer compare,
// since the version in there might be different, so instead we want to compare ids
if (_cachedResults != null) {
- for (int i = 0; i < _cachedResults.size(); i++) {
+ for ( i in 0..|_cachedResults.size()) {
if (_cachedResults.get(i).getId().equals(element.getId())) {
_cachedResults.remove(i);
break;
@@ -51,22 +50,21 @@ protected void removeImpl(T element) {
}
}
- @Override
- protected void addImpl(T element) {
+ protected override function addImpl(element : T) {
if (!isAlreadyInArray(element)) {
// If the element hasn't yet been persisted, we have to persist it so that it has an id we can insert into the join table
if (element.isNew()) {
element.update();
}
- String sql = SimpleSqlBuilder.substitute("INSERT INTO ${joinTable} (${srcFk}, ${targetFk}) VALUES (?, ?)",
+ var sql = SimpleSqlBuilder.substitute("INSERT INTO \${joinTable} (\${srcFk}, \${targetFk}) VALUES (?, ?)",
"joinTable", _srcColumn.getTable(),
"srcFk", _srcColumn,
"targetFk", _targetColumn);
- IPreparedStatementParameter srcParam = _srcColumn.wrapParameterValue(_owner.getId());
- IPreparedStatementParameter targetParam = _targetColumn.wrapParameterValue(element.getId());
- _queryExecutor.insert("JoinArrayEntityCollectionImpl.addImpl()", sql, srcParam, targetParam);
+ var srcParam = _srcColumn.wrapParameterValue(_owner.getId());
+ var targetParam = _targetColumn.wrapParameterValue(element.getId());
+ _queryExecutor.insert("JoinArrayEntityCollectionImpl.addImpl()", sql, {srcParam, targetParam});
if (_cachedResults != null) {
_cachedResults.add(element);
@@ -77,7 +75,7 @@ protected void addImpl(T element) {
// same pointer semantics if the results have been loaded, regardless of whether or not the element is already
// in the array: in all cases, the element is now in the array
if (_cachedResults != null) {
- for (int i = 0; i < _cachedResults.size(); i++) {
+ for (i in 0..|_cachedResults.size()) {
if (_cachedResults.get(i).getId().equals(element.getId())) {
_cachedResults.set(i, element);
break; // There should only ever be one match, so stop iterating
@@ -90,46 +88,45 @@ protected void addImpl(T element) {
// TODO - AHK
}
- private boolean isAlreadyInArray(T element) {
+ private function isAlreadyInArray(element : T) : boolean {
if (_cachedResults != null) {
- for (T result : _cachedResults) {
+ for (result in _cachedResults) {
if (result.getId().equals(element.getId())) {
return true;
}
}
return false;
} else {
- String sql = SimpleSqlBuilder.substitute("SELECT count(*) as count FROM ${joinTable} WHERE ${srcFk} = ? AND ${targetFk} = ?",
+ var sql = SimpleSqlBuilder.substitute("SELECT count(*) as count FROM \${joinTable} WHERE \${srcFk} = ? AND \${targetFk} = ?",
"joinTable", _srcColumn.getTable(),
"srcFk", _srcColumn,
"targetFk", _targetColumn);
- IPreparedStatementParameter srcFkParam = _srcColumn.wrapParameterValue(_owner.getId());
- IPreparedStatementParameter targetFkParam = _srcColumn.wrapParameterValue(element.getId());
- int numResults = _queryExecutor.count("JoinArrayEntityCollectionImpl.isAlreadyInArray()", sql, srcFkParam, targetFkParam);
+ var srcFkParam = _srcColumn.wrapParameterValue(_owner.getId());
+ var targetFkParam = _srcColumn.wrapParameterValue(element.getId());
+ var numResults = _queryExecutor.count("JoinArrayEntityCollectionImpl.isAlreadyInArray()", sql, {srcFkParam, targetFkParam});
// TODO - AHK - Report an error if there's more than one result?
return numResults > 0;
}
}
- @Override
- protected List<T> loadResults() {
- String sql = SimpleSqlBuilder.substitute("SELECT * FROM ${targetTable} INNER JOIN ${joinTable} as j ON j.${targetFk} = ${targetTable}.${id} WHERE j.${srcFk} = ?",
+ override protected function loadResults() : List<T> {
+ var sql = SimpleSqlBuilder.substitute("SELECT * FROM \${targetTable} INNER JOIN \${joinTable} as j ON j.\${targetFk} = \${targetTable}.\${id} WHERE j.\${srcFk} = ?",
"targetTable", _fkType.getTable(),
"joinTable", _srcColumn.getTable(),
"id", _fkType.getTable().getColumn("id"),
"targetFk", _targetColumn,
"srcFk", _srcColumn);
- IPreparedStatementParameter param = _srcColumn.wrapParameterValue(_owner.getId());
- return (List<T>) _queryExecutor.selectEntity("JoinArrayEntityCollectionImpl.loadResultsIfNecessary()", _fkType, sql, param);
+ var param = _srcColumn.wrapParameterValue(_owner.getId());
+ return _queryExecutor.selectEntity("JoinArrayEntityCollectionImpl.loadResultsIfNecessary()", _fkType, sql, {param}) as List<T>
}
- @Override
- protected int issueCountQuery() {
- String sql = SimpleSqlBuilder.substitute("SELECT count(*) as count FROM ${joinTable} WHERE ${srcFk} = ?",
+ protected override function issueCountQuery() : int {
+ var sql = SimpleSqlBuilder.substitute("SELECT count(*) as count FROM \${joinTable} WHERE \${srcFk} = ?",
"joinTable", _srcColumn.getTable(),
"srcFk", _srcColumn);
- IPreparedStatementParameter param = _srcColumn.wrapParameterValue(_owner.getId());
- return _queryExecutor.count("JoinArrayEntityCollectionImpl.size()", sql, param);
+ var param = _srcColumn.wrapParameterValue(_owner.getId());
+ return _queryExecutor.count("JoinArrayEntityCollectionImpl.size()", sql, {param});
}
-}
+
+}

0 comments on commit 2c533d3

Please sign in to comment.