Skip to content

Commit

Permalink
Table rename to ModelTable
Browse files Browse the repository at this point in the history
  • Loading branch information
codefollower committed May 14, 2018
1 parent 3d472bf commit b777286
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 220 deletions.
64 changes: 0 additions & 64 deletions lealone-orm/src/main/java/org/lealone/orm/Database.java

This file was deleted.

Expand Up @@ -33,10 +33,9 @@


public class DefaultExpressionList<T> implements ExpressionList<T> { public class DefaultExpressionList<T> implements ExpressionList<T> {


org.lealone.db.table.Table dbTable;
Table table;
org.lealone.sql.expression.Expression expression; org.lealone.sql.expression.Expression expression;
Model<?> model; Model<?> model;
ModelTable table;


boolean isAnd = true; boolean isAnd = true;


Expand All @@ -51,24 +50,13 @@ public org.lealone.sql.expression.Expression getExpression() {
return expression; return expression;
} }


public DefaultExpressionList(Model<?> model, Table table) { public DefaultExpressionList(Model<?> model, ModelTable table) {
this.table = table; this.table = table;
if (table != null)
this.dbTable = table.getDbTable();
this.model = model; this.model = model;
} }


Table getTable() {
table = model.getTable();
dbTable = table.getDbTable();
return table;
}

private ExpressionColumn getExpressionColumn(String propertyName) { private ExpressionColumn getExpressionColumn(String propertyName) {
getTable(); return new ExpressionColumn(table.getDatabase(), table.getSchemaName(), table.getTableName(), propertyName);
return new ExpressionColumn(dbTable.getDatabase(), dbTable.getSchema().getName(), dbTable.getName(),
propertyName);
// return new ExpressionColumn(dbTable.getDatabase(), dbTable.getColumn(propertyName));
} }


private Comparison createComparison(String propertyName, Object value, int compareType) { private Comparison createComparison(String propertyName, Object value, int compareType) {
Expand Down Expand Up @@ -235,7 +223,7 @@ public ExpressionList<T> notIn(String propertyName, Collection<?> values) {
public ExpressionList<T> like(String propertyName, String value) { public ExpressionList<T> like(String propertyName, String value) {
ExpressionColumn ec = getExpressionColumn(propertyName); ExpressionColumn ec = getExpressionColumn(propertyName);
ValueExpression v = ValueExpression.get(ValueString.get(value)); ValueExpression v = ValueExpression.get(ValueString.get(value));
CompareLike like = new CompareLike(dbTable.getDatabase(), ec, v, null, false); CompareLike like = new CompareLike(table.getDatabase(), ec, v, null, false);
setRootExpression(like); setRootExpression(like);
return this; return this;
} }
Expand Down
102 changes: 38 additions & 64 deletions lealone-orm/src/main/java/org/lealone/orm/Model.java
Expand Up @@ -28,6 +28,7 @@
import org.lealone.db.index.Index; import org.lealone.db.index.Index;
import org.lealone.db.result.Result; import org.lealone.db.result.Result;
import org.lealone.db.table.Column; import org.lealone.db.table.Column;
import org.lealone.db.table.Table;
import org.lealone.db.table.TableFilter; import org.lealone.db.table.TableFilter;
import org.lealone.db.value.Value; import org.lealone.db.value.Value;
import org.lealone.db.value.ValueInt; import org.lealone.db.value.ValueInt;
Expand Down Expand Up @@ -154,8 +155,7 @@ public boolean equals(Object obj) {


private final HashSet<NVPair> nvPairs = new HashSet<>(); private final HashSet<NVPair> nvPairs = new HashSet<>();


protected Table table; protected final ModelTable modelTable;
protected final String tableName;
boolean isDao; boolean isDao;


public boolean isDao() { public boolean isDao() {
Expand All @@ -175,9 +175,8 @@ public boolean isDao() {
*/ */
private ArrayStack<ExpressionList<T>> whereStack; private ArrayStack<ExpressionList<T>> whereStack;


public Model(Table table, String tableName, boolean isDao) { public Model(ModelTable table, boolean isDao) {
this.table = table; this.modelTable = table;
this.tableName = tableName;
this.isDao = isDao; this.isDao = isDao;
reset(); reset();
} }
Expand All @@ -200,7 +199,7 @@ protected void setRoot(T root) {
private void reset() { private void reset() {
whereStack = null; whereStack = null;
selectExpressions.clear(); selectExpressions.clear();
whereExpression = new DefaultExpressionList<T>(this, table); whereExpression = new DefaultExpressionList<T>(this, modelTable);
nvPairs.clear(); nvPairs.clear();
} }


Expand Down Expand Up @@ -231,12 +230,8 @@ private void reset() {
*/ */
@SafeVarargs @SafeVarargs
public final T select(TQProperty<T>... properties) { public final T select(TQProperty<T>... properties) {
getTable();
org.lealone.db.table.Table dbTable = table.getDbTable();
for (TQProperty<T> p : properties) { for (TQProperty<T> p : properties) {
ExpressionColumn c = new ExpressionColumn(dbTable.getDatabase(), dbTable.getSchema().getName(), ExpressionColumn c = getExpressionColumn(p.getName());
dbTable.getName(), p.getName());
// c = new ExpressionColumn(dbTable.getDatabase(), dbTable.getColumn(p.propertyName()));
selectExpressions.add(c); selectExpressions.add(c);
} }
return root; return root;
Expand Down Expand Up @@ -274,21 +269,23 @@ public T orderBy() {


@SafeVarargs @SafeVarargs
public final T groupBy(TQProperty<T>... properties) { public final T groupBy(TQProperty<T>... properties) {
getTable();
groupExpressions = new ArrayList<>(); groupExpressions = new ArrayList<>();
org.lealone.db.table.Table dbTable = table.getDbTable();
for (TQProperty<T> p : properties) { for (TQProperty<T> p : properties) {
ExpressionColumn c = new ExpressionColumn(dbTable.getDatabase(), dbTable.getSchema().getName(), ExpressionColumn c = getExpressionColumn(p.getName());
dbTable.getName(), p.getName());
groupExpressions.add(c); groupExpressions.add(c);
} }


return root; return root;
} }


private ExpressionColumn getExpressionColumn(String propertyName) {
return new ExpressionColumn(modelTable.getDatabase(), modelTable.getSchemaName(), modelTable.getTableName(),
propertyName);
}

public T having() { public T having() {
whereStack.pop(); whereStack.pop();
having = new DefaultExpressionList<>(this, table); having = new DefaultExpressionList<>(this, modelTable);
pushExprList(having); pushExprList(having);
return root; return root;
} }
Expand Down Expand Up @@ -483,14 +480,13 @@ public T where() {
*/ */
public T findOne() { public T findOne() {
checkDao("findOne"); checkDao("findOne");
getTable(); Select select = new Select(modelTable.getSession());
Select select = new Select(table.getSession()); TableFilter tableFilter = new TableFilter(modelTable.getSession(), modelTable.getTable(), null, true, null);
TableFilter tableFilter = new TableFilter(table.getSession(), table.getDbTable(), null, true, null);
select.addTableFilter(tableFilter, true); select.addTableFilter(tableFilter, true);
if (selectExpressions.isEmpty()) { if (selectExpressions.isEmpty()) {
selectExpressions.add(new Wildcard(null, null)); selectExpressions.add(new Wildcard(null, null));
} }
selectExpressions.add(createRowIdExpressionColumn(table.getDbTable())); // 总是获取rowid selectExpressions.add(getExpressionColumn(Column.ROWID)); // 总是获取rowid
select.setExpressions(selectExpressions); select.setExpressions(selectExpressions);
select.addCondition(whereExpression.expression); select.addCondition(whereExpression.expression);
select.setLimit(ValueExpression.get(ValueInt.get(1))); select.setLimit(ValueExpression.get(ValueInt.get(1)));
Expand All @@ -503,11 +499,6 @@ public T findOne() {
return deserialize(result); return deserialize(result);
} }


private ExpressionColumn createRowIdExpressionColumn(org.lealone.db.table.Table dbTable) {
return new ExpressionColumn(dbTable.getDatabase(), dbTable.getSchema().getName(), dbTable.getName(),
Column.ROWID);
}

@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private T deserialize(Result result) { private T deserialize(Result result) {
Value[] row = result.currentRow(); Value[] row = result.currentRow();
Expand All @@ -520,7 +511,7 @@ private T deserialize(Result result) {
map.put(result.getColumnName(i), row[i]); map.put(result.getColumnName(i), row[i]);
} }


Model q = newInstance(table); Model q = newInstance(modelTable);
if (q != null) { if (q != null) {
for (TQProperty p : q.tqProperties) { for (TQProperty p : q.tqProperties) {
p.deserialize(map); p.deserialize(map);
Expand All @@ -530,7 +521,7 @@ private T deserialize(Result result) {
return (T) q; return (T) q;
} }


protected Model newInstance(Table t) { protected Model newInstance(ModelTable t) {
return null; return null;
} }


Expand Down Expand Up @@ -559,14 +550,13 @@ protected void deserialize(JsonNode node) {
*/ */
public List<T> findList() { public List<T> findList() {
checkDao("findList"); checkDao("findList");
getTable(); Select select = new Select(modelTable.getSession());
Select select = new Select(table.getSession()); TableFilter tableFilter = new TableFilter(modelTable.getSession(), modelTable.getTable(), null, true, null);
TableFilter tableFilter = new TableFilter(table.getSession(), table.getDbTable(), null, true, null);
select.addTableFilter(tableFilter, true); select.addTableFilter(tableFilter, true);
if (selectExpressions.isEmpty()) { if (selectExpressions.isEmpty()) {
selectExpressions.add(new Wildcard(null, null)); selectExpressions.add(new Wildcard(null, null));
} }
selectExpressions.add(createRowIdExpressionColumn(table.getDbTable())); // 总是获取rowid selectExpressions.add(getExpressionColumn(Column.ROWID)); // 总是获取rowid
select.setExpressions(selectExpressions); select.setExpressions(selectExpressions);
select.addCondition(whereExpression.expression); select.addCondition(whereExpression.expression);
if (groupExpressions != null) { if (groupExpressions != null) {
Expand Down Expand Up @@ -595,10 +585,9 @@ public List<T> findList() {
*/ */
public int findCount() { public int findCount() {
checkDao("findCount"); checkDao("findCount");
getTable(); Select select = new Select(modelTable.getSession());
Select select = new Select(table.getSession());
select.setGroupQuery(); select.setGroupQuery();
TableFilter tableFilter = new TableFilter(table.getSession(), table.getDbTable(), null, true, null); TableFilter tableFilter = new TableFilter(modelTable.getSession(), modelTable.getTable(), null, true, null);
select.addTableFilter(tableFilter, true); select.addTableFilter(tableFilter, true);
selectExpressions.clear(); selectExpressions.clear();
Aggregate a = new Aggregate(Aggregate.COUNT_ALL, null, select, false); Aggregate a = new Aggregate(Aggregate.COUNT_ALL, null, select, false);
Expand All @@ -625,10 +614,9 @@ public int findCount() {
* @return the number of beans/rows that were deleted. * @return the number of beans/rows that were deleted.
*/ */
public int delete() { public int delete() {
getTable(); Table dbTable = modelTable.getTable();
org.lealone.db.table.Table dbTable = table.getDbTable(); Delete delete = new Delete(modelTable.getSession());
Delete delete = new Delete(table.getSession()); TableFilter tableFilter = new TableFilter(modelTable.getSession(), dbTable, null, true, null);
TableFilter tableFilter = new TableFilter(table.getSession(), dbTable, null, true, null);
delete.setTableFilter(tableFilter); delete.setTableFilter(tableFilter);
if (whereExpression.expression == null) { if (whereExpression.expression == null) {
maybeCreateWhereExpression(dbTable); maybeCreateWhereExpression(dbTable);
Expand All @@ -648,10 +636,9 @@ public int delete() {
} }


public int update() { public int update() {
getTable(); Table dbTable = modelTable.getTable();
org.lealone.db.table.Table dbTable = table.getDbTable(); Update update = new Update(modelTable.getSession());
Update update = new Update(table.getSession()); TableFilter tableFilter = new TableFilter(modelTable.getSession(), dbTable, null, true, null);
TableFilter tableFilter = new TableFilter(table.getSession(), dbTable, null, true, null);
update.setTableFilter(tableFilter); update.setTableFilter(tableFilter);
if (whereExpression.expression == null) { if (whereExpression.expression == null) {
maybeCreateWhereExpression(dbTable); maybeCreateWhereExpression(dbTable);
Expand All @@ -673,7 +660,7 @@ public int update() {
return count; return count;
} }


private void maybeCreateWhereExpression(org.lealone.db.table.Table dbTable) { private void maybeCreateWhereExpression(Table dbTable) {
// 没有指定where条件时,如果存在ROWID,则用ROWID当where条件 // 没有指定where条件时,如果存在ROWID,则用ROWID当where条件
if (_rowid_.get() != 0) { if (_rowid_.get() != 0) {
peekExprList().eq(Column.ROWID, _rowid_.get()); peekExprList().eq(Column.ROWID, _rowid_.get());
Expand All @@ -697,20 +684,8 @@ private void maybeCreateWhereExpression(org.lealone.db.table.Table dbTable) {
} }
} }


// 可能是延迟关联到Table
Table getTable() {
if (table == null) {
String url = System.getProperty("lealone.jdbc.url");
if (url == null) {
throw new RuntimeException("'lealone.jdbc.url' must be set");
}
table = new Table(url, tableName);
}
return table;
}

private void commit() { private void commit() {
table.getSession().commit(); modelTable.getSession().commit();
} }


public long insert() { public long insert() {
Expand All @@ -720,9 +695,8 @@ public long insert() {
throw new UnsupportedOperationException("The insert operation is not allowed for " + name throw new UnsupportedOperationException("The insert operation is not allowed for " + name
+ ".dao, please use new " + name + "().insert() instead."); + ".dao, please use new " + name + "().insert() instead.");
} }
getTable(); Table dbTable = modelTable.getTable();
org.lealone.db.table.Table dbTable = table.getDbTable(); Insert insert = new Insert(modelTable.getSession());
Insert insert = new Insert(table.getSession());
int size = nvPairs.size(); int size = nvPairs.size();
Column[] columns = new Column[size]; Column[] columns = new Column[size];
Expression[] expressions = new Expression[size]; Expression[] expressions = new Expression[size];
Expand All @@ -738,7 +712,7 @@ public long insert() {
insert.prepare(); insert.prepare();
logger.info("execute sql: " + insert.getPlanSQL()); logger.info("execute sql: " + insert.getPlanSQL());
insert.executeUpdate(); insert.executeUpdate();
long rowId = table.getSession().getLastRowKey(); long rowId = modelTable.getSession().getLastRowKey();
_rowid_.set(rowId); _rowid_.set(rowId);
commit(); commit();
reset(); reset();
Expand All @@ -765,13 +739,13 @@ public ExpressionList<T> peekExprList() {
} }


public boolean databaseToUpper() { public boolean databaseToUpper() {
if (table == null) if (modelTable == null)
return false; return false;
return table.getSession().getDatabase().getSettings().databaseToUpper; return modelTable.getSession().getDatabase().getSettings().databaseToUpper;
} }


public boolean isReady() { public boolean isReady() {
return table != null; return modelTable != null;
} }


@Override @Override
Expand All @@ -781,7 +755,7 @@ public String toString() {
} }


public T leftParenthesis() { public T leftParenthesis() {
DefaultExpressionList<T> e = new DefaultExpressionList<>(this, table); DefaultExpressionList<T> e = new DefaultExpressionList<>(this, modelTable);
pushExprList(e); pushExprList(e);
return root; return root;
} }
Expand Down

0 comments on commit b777286

Please sign in to comment.