Skip to content

Commit

Permalink
从lealone.jdbc.url参数获取url,增加全栈测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
codefollower committed May 12, 2018
1 parent 8e2e8e3 commit d15d8e2
Show file tree
Hide file tree
Showing 29 changed files with 554 additions and 134 deletions.
19 changes: 19 additions & 0 deletions lealone-orm/pom.xml
Expand Up @@ -35,5 +35,24 @@
<artifactId>lealone-sql</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
</project>
Expand Up @@ -42,7 +42,14 @@ public DefaultExpressionList(Query<?> query, Table table) {
this.query = query;
}

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

private ExpressionColumn getExpressionColumn(String propertyName) {
getTable();
return new ExpressionColumn(dbTable.getDatabase(), dbTable.getSchema().getName(), dbTable.getName(),
propertyName);
// return new ExpressionColumn(dbTable.getDatabase(), dbTable.getColumn(propertyName));
Expand Down
46 changes: 42 additions & 4 deletions lealone-orm/src/main/java/org/lealone/orm/Query.java
Expand Up @@ -46,6 +46,8 @@

import com.fasterxml.jackson.databind.JsonNode;

import io.vertx.core.json.JsonObject;

/**
* Base root query bean.
* <p>
Expand Down Expand Up @@ -196,7 +198,8 @@ public boolean equals(Object obj) {

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

protected final Table table;
protected Table table;
protected final String tableName;

private final ArrayList<Expression> selectExpressions = new ArrayList<>();

Expand All @@ -211,8 +214,9 @@ public boolean equals(Object obj) {
*/
private ArrayStack<ExpressionList<T>> whereStack;

public Query(Table table) {
public Query(Table table, String tableName) {
this.table = table;
this.tableName = tableName;
reset();
}

Expand Down Expand Up @@ -514,6 +518,7 @@ public T where() {
* }</pre>
*/
public T findOne() {
getTable();
Select select = new Select(table.getSession());
TableFilter tableFilter = new TableFilter(table.getSession(), table.getDbTable(), null, true, null);
select.addTableFilter(tableFilter, true);
Expand All @@ -526,6 +531,7 @@ public T findOne() {
select.setLimit(ValueExpression.get(ValueInt.get(1)));
select.init();
select.prepare();
logger.info("execute sql: " + select.getPlanSQL());
Result result = select.executeQuery(1);
result.next();
reset();
Expand Down Expand Up @@ -587,6 +593,7 @@ protected void deserialize(JsonNode node) {
* @see EbeanServer#findList(Query, Transaction)
*/
public List<T> findList() {
getTable();
Select select = new Select(table.getSession());
TableFilter tableFilter = new TableFilter(table.getSession(), table.getDbTable(), null, true, null);
select.addTableFilter(tableFilter, true);
Expand Down Expand Up @@ -615,6 +622,7 @@ public List<T> findList() {
* </p>
*/
public int findCount() {
getTable();
Select select = new Select(table.getSession());
select.setGroupQuery();
TableFilter tableFilter = new TableFilter(table.getSession(), table.getDbTable(), null, true, null);
Expand Down Expand Up @@ -644,6 +652,7 @@ public int findCount() {
* @return the number of beans/rows that were deleted.
*/
public int delete() {
getTable();
org.lealone.db.table.Table dbTable = table.getDbTable();
Delete delete = new Delete(table.getSession());
TableFilter tableFilter = new TableFilter(table.getSession(), dbTable, null, true, null);
Expand All @@ -655,10 +664,13 @@ public int delete() {
delete.prepare();
reset();
logger.info("execute sql: " + delete.getPlanSQL());
return delete.executeUpdate();
int count = delete.executeUpdate();
commit();
return count;
}

public int update() {
getTable();
org.lealone.db.table.Table dbTable = table.getDbTable();
Update update = new Update(table.getSession());
TableFilter tableFilter = new TableFilter(table.getSession(), dbTable, null, true, null);
Expand All @@ -673,10 +685,29 @@ public int update() {
update.prepare();
reset();
logger.info("execute sql: " + update.getPlanSQL());
return update.executeUpdate();
int count = update.executeUpdate();
commit();
return count;
}

// 可能是延迟关联到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() {
table.getSession().commit();
}

public long insert() {
getTable();
org.lealone.db.table.Table dbTable = table.getDbTable();
Insert insert = new Insert(table.getSession());
int size = nvPairs.size();
Expand All @@ -696,6 +727,7 @@ public long insert() {
insert.executeUpdate();
long rowId = table.getSession().getLastRowKey();
_rowid_.set(rowId);
commit();
reset();
return rowId;
}
Expand Down Expand Up @@ -729,6 +761,12 @@ public boolean isReady() {
return table != null;
}

@Override
public String toString() {
JsonObject json = JsonObject.mapFrom(this);
return json.encode();
}

/**
* Stack based on ArrayList.
*
Expand Down
1 change: 0 additions & 1 deletion lealone-orm/src/main/java/org/lealone/orm/Table.java
Expand Up @@ -27,7 +27,6 @@ public class Table {
public Table(String url, String tableName) {
db = new Database(url);
dbTable = db.getDbTable(tableName);
dbTable.getTemplateRow();
}

Table(Database db, org.lealone.db.table.Table dbTable) {
Expand Down
Expand Up @@ -57,9 +57,9 @@ public final R set(int value) {
if (!areEqual(this.value, value)) {
this.value = value;
changed = true;
if (isReady()) {
expr().set(name, ValueInt.get(value));
}
// if (isReady()) {
expr().set(name, ValueInt.get(value));
// }
}
return root;
}
Expand All @@ -76,7 +76,11 @@ public R serialize(JsonGenerator jgen) throws IOException {

@Override
public R deserialize(JsonNode node) {
value = ((NumericNode) node.get(propertyName())).asInt();
node = getJsonNode(node);
if (node == null) {
return root;
}
set(((NumericNode) node).asInt());
return root;
}

Expand Down
12 changes: 8 additions & 4 deletions lealone-orm/src/main/java/org/lealone/orm/typequery/PLong.java
Expand Up @@ -57,9 +57,9 @@ public R set(long value) {
if (!areEqual(this.value, value)) {
this.value = value;
changed = true;
if (isReady()) {
expr().set(name, ValueLong.get(value));
}
// if (isReady()) {
expr().set(name, ValueLong.get(value));
// }
}
return root;
}
Expand All @@ -76,7 +76,11 @@ public R serialize(JsonGenerator jgen) throws IOException {

@Override
public R deserialize(JsonNode node) {
value = ((NumericNode) node.get(propertyName())).asLong();
node = getJsonNode(node);
if (node == null) {
return root;
}
set(((NumericNode) node).asLong());
return root;
}

Expand Down
12 changes: 8 additions & 4 deletions lealone-orm/src/main/java/org/lealone/orm/typequery/PString.java
Expand Up @@ -179,9 +179,9 @@ public final R set(String value) {
if (!areEqual(this.value, value)) {
this.value = value;
changed = true;
if (isReady()) {
expr().set(name, ValueString.get(value));
}
// if (isReady()) {
expr().set(name, ValueString.get(value));
// }
}
return root;
}
Expand All @@ -198,7 +198,11 @@ public R serialize(JsonGenerator jgen) throws IOException {

@Override
public R deserialize(JsonNode node) {
value = node.get(propertyName()).asText();
node = getJsonNode(node);
if (node == null) {
return root;
}
set(node.asText());
return root;
}

Expand Down
Expand Up @@ -132,6 +132,14 @@ public R deserialize(HashMap<String, Value> map) {
return root;
}

protected JsonNode getJsonNode(JsonNode node) {
JsonNode n = node.get(name);
if (n == null) {
n = node.get(name.toLowerCase());
}
return n;
}

/**
* Helper method to check if two objects are equal.
*/
Expand Down
Expand Up @@ -414,7 +414,7 @@ private void genServiceInterfaceCode() {
buff.append(" }\r\n");
buff.append(" } catch (SQLException e) {\r\n");
buff.append(
" throw new RuntimeException(\"Failted to execute service: \" + serviceName);\r\n");
" throw new RuntimeException(\"Failted to execute service: \" + serviceName, e);\r\n");
buff.append(" }\r\n");
buff.append("\r\n");
buff.append(" return null;\r\n");
Expand All @@ -430,7 +430,7 @@ private void genServiceInterfaceCode() {
buff.append(" stmt.execute();\r\n");
buff.append(" } catch (SQLException e) {\r\n");
buff.append(
" throw new RuntimeException(\"Failted to execute service: \" + serviceName);\r\n");
" throw new RuntimeException(\"Failted to execute service: \" + serviceName, e);\r\n");
buff.append(" }\r\n");
buff.append(" }\r\n");
}
Expand Down Expand Up @@ -536,8 +536,9 @@ private void genServiceExecuterCode() {
buff.append(returnType).append(" ").append(resultVarName).append(" = ");
}
buff.append("this.s.").append(methodName).append("(").append(argsBuff).append(");\r\n");

if (!isVoid) {
buff.append(" if (").append(resultVarName).append(" == null)\r\n");
buff.append(" return null;\r\n");
if (returnColumn.getTable() != null) {
importSet.add("io.vertx.core.json.JsonObject");
buff.append(" return JsonObject.mapFrom(").append(resultVarName)
Expand Down Expand Up @@ -649,7 +650,7 @@ private static String getResultMethodName(String type) {
return "Byte.valueOf(result)";
case "SHORT":
return "Short.valueOf(result)";
case "INT":
case "INTEGER":
return "Integer.valueOf(result)";
case "LONG":
return "Long.valueOf(result)";
Expand Down
Expand Up @@ -400,7 +400,7 @@ private void genCode() {
buff.append(" }\r\n");
buff.append("\r\n");
buff.append(" public ").append(className).append("(Table t) {\r\n");
buff.append(" super(t);\r\n");
buff.append(" super(t, \"").append(data.tableName).append("\");\r\n");
buff.append(" super.setRoot(this);\r\n");
buff.append("\r\n");
buff.append(init);
Expand Down
Expand Up @@ -957,7 +957,10 @@ private Value getSimpleValue(ServerSession session, Value v0, Expression[] args,
case EXECUTE_SERVICE_WITH_RETURN_VALUE: {
Value v1 = getNullOrValue(session, args, values, 1);
String r = ServiceExecuterManager.executeServiceWithReturnValue(v0.getString(), v1.getString());
result = ValueString.get(r);
if (r == null)
result = ValueNull.INSTANCE;
else
result = ValueString.get(r);
break;
}
default:
Expand Down
12 changes: 11 additions & 1 deletion lealone-test/src/test/java/org/lealone/test/UnitTestBase.java
Expand Up @@ -38,12 +38,22 @@ public void execute(String sql) {
}

public void runTest() {
runTest(true, true);
}

public void runTest(boolean isEmbeddedMemoryMode, boolean closeTransactionEngine) {
if (isEmbeddedMemoryMode) {
setEmbedded(true);
setInMemory(true);
}

try {
test();
} catch (Exception e) {
e.printStackTrace();
} finally {
closeTransactionEngine();
if (closeTransactionEngine)
closeTransactionEngine();
}
}

Expand Down

0 comments on commit d15d8e2

Please sign in to comment.