Skip to content

Commit

Permalink
内置EXECUTE_SERVICE相关的函数
Browse files Browse the repository at this point in the history
  • Loading branch information
codefollower committed May 10, 2018
1 parent 735e338 commit e259413
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 50 deletions.
Expand Up @@ -28,14 +28,17 @@ private ServiceExecuterManager() {
private static final HashMap<String, String> serviceExecuterClassNames = new HashMap<>();

public synchronized static void registerServiceExecuter(String name, ServiceExecuter serviceExecuter) {
name = name.toUpperCase();
serviceExecuters.put(name, serviceExecuter);
}

public synchronized static void registerServiceExecuter(String name, String serviceExecuterClassName) {
name = name.toUpperCase();
serviceExecuterClassNames.put(name, serviceExecuterClassName);
}

public synchronized void deregisterServiceExecuter(String name) {
name = name.toUpperCase();
serviceExecuters.remove(name);
}

Expand All @@ -48,22 +51,24 @@ public static String executeServiceWithReturnValue(String serviceName, String js
}

private static String executeService(String serviceName, String json) {
// System.out.println("serviceName=" + serviceName + ", json=" + json);
serviceName = serviceName.toUpperCase();
int dotPos = serviceName.indexOf('.');
String methodName = serviceName.substring(dotPos + 1);
serviceName = serviceName.substring(0, dotPos);
// serviceName = "org.lealone.test.vertx.impl." + toClassName(serviceName);

ServiceExecuter serviceExecuter = serviceExecuters.get(serviceName);
if (serviceExecuter == null) {
String serviceExecuterClassName = serviceExecuterClassNames.get(serviceName);
if (serviceExecuterClassName != null) {
synchronized (serviceExecuterClassNames) {
try {
serviceExecuter = (ServiceExecuter) Class.forName(serviceExecuterClassName).newInstance();
serviceExecuters.put(serviceName, serviceExecuter);
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new RuntimeException("newInstance exception: " + serviceExecuterClassName);
serviceExecuter = serviceExecuters.get(serviceName);
if (serviceExecuter == null) {
try {
serviceExecuter = (ServiceExecuter) Class.forName(serviceExecuterClassName).newInstance();
serviceExecuters.put(serviceName, serviceExecuter);
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new RuntimeException("newInstance exception: " + serviceExecuterClassName);
}
}
}
} else {
Expand Down
10 changes: 6 additions & 4 deletions lealone-sql/src/main/java/org/lealone/sql/ddl/CreateService.java
Expand Up @@ -391,10 +391,10 @@ private void genServiceInterfaceCode() {
buff.append(" private final String url;\r\n");
if (hasNoReturnValueMethods)
buff.append(" private static final String sqlNoReturnValue "
+ "= \"{call executeServiceNoReturnValue(?,?)}\";\r\n");
+ "= \"{call EXECUTE_SERVICE_NO_RETURN_VALUE(?,?)}\";\r\n");
if (hasWithReturnValueMethods)
buff.append(" private static final String sqlWithReturnValue "
+ "= \"{? = call executeServiceWithReturnValue(?,?)}\";\r\n");
+ "= \"{? = call EXECUTE_SERVICE_WITH_RETURN_VALUE(?,?)}\";\r\n");
buff.append("\r\n");
buff.append(" private Proxy(String url) {\r\n");
buff.append(" this.url = url;\r\n");
Expand All @@ -413,7 +413,8 @@ private void genServiceInterfaceCode() {
buff.append(" return stmt.getString(1);\r\n");
buff.append(" }\r\n");
buff.append(" } catch (SQLException e) {\r\n");
buff.append(" e.printStackTrace();\r\n");
buff.append(
" throw new RuntimeException(\"Failted to execute service: \" + serviceName);\r\n");
buff.append(" }\r\n");
buff.append("\r\n");
buff.append(" return null;\r\n");
Expand All @@ -428,7 +429,8 @@ private void genServiceInterfaceCode() {
buff.append(" stmt.setString(2, json);\r\n");
buff.append(" stmt.execute();\r\n");
buff.append(" } catch (SQLException e) {\r\n");
buff.append(" e.printStackTrace();\r\n");
buff.append(
" throw new RuntimeException(\"Failted to execute service: \" + serviceName);\r\n");
buff.append(" }\r\n");
buff.append(" }\r\n");
}
Expand Down
Expand Up @@ -47,6 +47,7 @@
import org.lealone.db.expression.ExpressionVisitor;
import org.lealone.db.schema.Schema;
import org.lealone.db.schema.Sequence;
import org.lealone.db.service.ServiceExecuterManager;
import org.lealone.db.table.Column;
import org.lealone.db.table.ColumnResolver;
import org.lealone.db.table.Table;
Expand Down Expand Up @@ -80,10 +81,10 @@
*/
public class Function extends Expression implements FunctionCall {
public static final int ABS = 0, ACOS = 1, ASIN = 2, ATAN = 3, ATAN2 = 4, BITAND = 5, BITOR = 6, BITXOR = 7,
CEILING = 8, COS = 9, COT = 10, DEGREES = 11, EXP = 12, FLOOR = 13, LOG = 14, LOG10 = 15, MOD = 16,
PI = 17, POWER = 18, RADIANS = 19, RAND = 20, ROUND = 21, ROUNDMAGIC = 22, SIGN = 23, SIN = 24, SQRT = 25,
TAN = 26, TRUNCATE = 27, SECURE_RAND = 28, HASH = 29, ENCRYPT = 30, DECRYPT = 31, COMPRESS = 32,
EXPAND = 33, ZERO = 34, RANDOM_UUID = 35, COSH = 36, SINH = 37, TANH = 38, LN = 39;
CEILING = 8, COS = 9, COT = 10, DEGREES = 11, EXP = 12, FLOOR = 13, LOG = 14, LOG10 = 15, MOD = 16, PI = 17,
POWER = 18, RADIANS = 19, RAND = 20, ROUND = 21, ROUNDMAGIC = 22, SIGN = 23, SIN = 24, SQRT = 25, TAN = 26,
TRUNCATE = 27, SECURE_RAND = 28, HASH = 29, ENCRYPT = 30, DECRYPT = 31, COMPRESS = 32, EXPAND = 33,
ZERO = 34, RANDOM_UUID = 35, COSH = 36, SINH = 37, TANH = 38, LN = 39;

public static final int ASCII = 50, BIT_LENGTH = 51, CHAR = 52, CHAR_LENGTH = 53, CONCAT = 54, DIFFERENCE = 55,
HEXTORAW = 56, INSERT = 57, INSTR = 58, LCASE = 59, LEFT = 60, LENGTH = 61, LOCATE = 62, LTRIM = 63,
Expand All @@ -103,11 +104,12 @@ public class Function extends Expression implements FunctionCall {
AUTOCOMMIT = 155, READONLY = 156, DATABASE_PATH = 157, LOCK_TIMEOUT = 158, DISK_SPACE_USED = 159;

public static final int IFNULL = 200, CASEWHEN = 201, CONVERT = 202, CAST = 203, COALESCE = 204, NULLIF = 205,
CASE = 206, NEXTVAL = 207, CURRVAL = 208, ARRAY_GET = 209, CSVREAD = 210, CSVWRITE = 211,
MEMORY_FREE = 212, MEMORY_USED = 213, LOCK_MODE = 214, SCHEMA = 215, SESSION_ID = 216, ARRAY_LENGTH = 217,
LINK_SCHEMA = 218, GREATEST = 219, LEAST = 220, CANCEL_SESSION = 221, SET = 222, TABLE = 223,
TABLE_DISTINCT = 224, FILE_READ = 225, TRANSACTION_ID = 226, TRUNCATE_VALUE = 227, NVL2 = 228,
DECODE = 229, ARRAY_CONTAINS = 230;
CASE = 206, NEXTVAL = 207, CURRVAL = 208, ARRAY_GET = 209, CSVREAD = 210, CSVWRITE = 211, MEMORY_FREE = 212,
MEMORY_USED = 213, LOCK_MODE = 214, SCHEMA = 215, SESSION_ID = 216, ARRAY_LENGTH = 217, LINK_SCHEMA = 218,
GREATEST = 219, LEAST = 220, CANCEL_SESSION = 221, SET = 222, TABLE = 223, TABLE_DISTINCT = 224,
FILE_READ = 225, TRANSACTION_ID = 226, TRUNCATE_VALUE = 227, NVL2 = 228, DECODE = 229, ARRAY_CONTAINS = 230;

public static final int EXECUTE_SERVICE_NO_RETURN_VALUE = 500, EXECUTE_SERVICE_WITH_RETURN_VALUE = 501;

/**
* This is called LEALONE_VERSION() and not VERSION(), because we return a fake value
Expand Down Expand Up @@ -372,6 +374,11 @@ public class Function extends Expression implements FunctionCall {

// pseudo function
addFunctionWithNull("ROW_NUMBER", ROW_NUMBER, 0, Value.LONG);

// service function
addFunctionNotDeterministic("EXECUTE_SERVICE_NO_RETURN_VALUE", EXECUTE_SERVICE_NO_RETURN_VALUE, 2, Value.NULL);
addFunctionNotDeterministic("EXECUTE_SERVICE_WITH_RETURN_VALUE", EXECUTE_SERVICE_WITH_RETURN_VALUE, 2,
Value.STRING);
}

protected Function(Database database, FunctionInfo info) {
Expand Down Expand Up @@ -941,6 +948,18 @@ private Value getSimpleValue(ServerSession session, Value v0, Expression[] args,
result = session.getTransactionId();
break;
}
case EXECUTE_SERVICE_NO_RETURN_VALUE: {
Value v1 = getNullOrValue(session, args, values, 1);
ServiceExecuterManager.executeServiceNoReturnValue(v0.getString(), v1.getString());
result = ValueNull.INSTANCE;
break;
}
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);
break;
}
default:
result = null;
}
Expand Down Expand Up @@ -1151,12 +1170,12 @@ private Value getValueWithArgs(ServerSession session, Expression[] args) {
break;
}
case RPAD:
result = ValueString.get(StringUtils.pad(v0.getString(), v1.getInt(), v2 == null ? null : v2.getString(),
true));
result = ValueString
.get(StringUtils.pad(v0.getString(), v1.getInt(), v2 == null ? null : v2.getString(), true));
break;
case LPAD:
result = ValueString.get(StringUtils.pad(v0.getString(), v1.getInt(), v2 == null ? null : v2.getString(),
false));
result = ValueString
.get(StringUtils.pad(v0.getString(), v1.getInt(), v2 == null ? null : v2.getString(), false));
break;
case LEALONE_VERSION:
result = ValueString.get(Constants.getVersion());
Expand Down
Expand Up @@ -34,6 +34,11 @@ public static void execute(String url) {
user = userService.add(user);
System.out.println("user.id=" + user.getId());
user = userService.find(user.getId());

user.setPhone(12345678);
userService.update(user);

userService.delete(user.getId());
}

}
Expand Up @@ -54,12 +54,6 @@ public void run() {

private void createServices() {
try (Connection conn = DriverManager.getConnection(url); Statement stmt = conn.createStatement()) {
System.out.println("create ALIAS");
stmt.executeUpdate("CREATE ALIAS executeServiceNoReturnValue for " //
+ "\"org.lealone.db.service.ServiceExecuterManager.executeServiceNoReturnValue\"");
stmt.executeUpdate("CREATE ALIAS executeServiceWithReturnValue for " //
+ "\"org.lealone.db.service.ServiceExecuterManager.executeServiceWithReturnValue\"");

System.out.println("create table");
String packageName = ServiceTest.packageName + ".generated";
// 创建表: user
Expand All @@ -78,11 +72,6 @@ private void createServices() {
private void testBackendRpcServices() {
System.out.println("test backend rpc services");
ServiceConsumer.execute(url);

// user.setPhone(12345678);
// userService.update(user);
//
// userService.delete(1);
}

private void testFrontendRpcServices() {
Expand Down
Expand Up @@ -24,8 +24,8 @@ static HelloWorldService create(String url) {
static class Proxy implements HelloWorldService {

private final String url;
private static final String sqlNoReturnValue = "{call executeServiceNoReturnValue(?,?)}";
private static final String sqlWithReturnValue = "{? = call executeServiceWithReturnValue(?,?)}";
private static final String sqlNoReturnValue = "{call EXECUTE_SERVICE_NO_RETURN_VALUE(?,?)}";
private static final String sqlWithReturnValue = "{? = call EXECUTE_SERVICE_WITH_RETURN_VALUE(?,?)}";

private Proxy(String url) {
this.url = url;
Expand Down Expand Up @@ -58,7 +58,7 @@ private String executeWithReturnValue(String serviceName, String json) {
return stmt.getString(1);
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Failted to execute service: " + serviceName);
}

return null;
Expand All @@ -71,7 +71,7 @@ private void executeNoReturnValue(String serviceName, String json) {
stmt.setString(2, json);
stmt.execute();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Failted to execute service: " + serviceName);
}
}
}
Expand Down
Expand Up @@ -33,7 +33,7 @@ static UserService create(String url) {
static class Proxy implements UserService {

private final String url;
private static final String sqlWithReturnValue = "{? = call executeServiceWithReturnValue(?,?)}";
private static final String sqlWithReturnValue = "{? = call EXECUTE_SERVICE_WITH_RETURN_VALUE(?,?)}";

private Proxy(String url) {
this.url = url;
Expand Down Expand Up @@ -107,7 +107,7 @@ private String executeWithReturnValue(String serviceName, String json) {
return stmt.getString(1);
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Failted to execute service: " + serviceName);
}

return null;
Expand Down
Expand Up @@ -31,26 +31,22 @@ public User add(User user) {

@Override
public User find(Long id) {
// TODO Auto-generated method stub
return null;
return new User().setId(id);
}

@Override
public User findByDate(Date d) {
// TODO Auto-generated method stub
return null;
return new User().setNotes("date: " + d.toString());
}

@Override
public Boolean update(User user) {
// TODO Auto-generated method stub
return null;
return true;
}

@Override
public Boolean delete(Long id) {
// TODO Auto-generated method stub
return null;
return true;
}

}
Expand Up @@ -17,6 +17,7 @@
*/
package org.lealone.vertx;

import org.lealone.common.util.CamelCaseHelper;
import org.lealone.db.service.ServiceExecuterManager;

import io.vertx.core.Handler;
Expand All @@ -31,7 +32,7 @@ public void handle(SockJSSocket sockJSSocket) {
sockJSSocket.handler(buffer -> {
String a[] = buffer.getString(0, buffer.length()).split(";");
int type = Integer.parseInt(a[0]);
String serviceName = a[1];
String serviceName = CamelCaseHelper.toUnderscoreFromCamel(a[1]);
String json = null;
if (a.length >= 3) {
json = a[2];
Expand Down

0 comments on commit e259413

Please sign in to comment.