基于graalvm 开发一个cube.js jdbc driver 的思路
主要说明下具体实现方式,代码还在编写中,后边会提供出来
原理说明
直接利用graalvm 方便的访问jdbc 的能力,不需要额外的处理,而且也不需要处理callback 的问题
内部实现上参考了cube.js jdbc 的实现,但是会比官方的简单很多(运行方便,官方是基于addon 的比较费事)
- jdbc 参考代码
var url = "jdbc:mysql://docker.for.mac.localhost:2881?useSSL=false";
var username = "root";
var password = "";
var conn =null;
var rs =null;
var st = null;
try{
conn = java.sql.DriverManager.getConnection(url, username, password)
st = conn.createStatement();
var sql = "select * from test.appdemo";
rs = st.executeQuery(sql);
var metaData = rs.getMetaData();
var columnCount = metaData.getColumnCount();
for (let columnNumber = 1; columnNumber <= columnCount; columnNumber++) {
var catalogName = metaData.getCatalogName(columnNumber);
var className = metaData.getColumnClassName(columnNumber);
var label = metaData.getColumnLabel(columnNumber);
var name = metaData.getColumnName(columnNumber);
var typeName = metaData.getColumnTypeName(columnNumber);
var type = metaData.getColumnType(columnNumber);
var tableName = metaData.getTableName(columnNumber);
var schemaName = metaData.getSchemaName(columnNumber);
var isAutoIncrement = metaData.isAutoIncrement(columnNumber);
var isCaseSensitive = metaData.isCaseSensitive(columnNumber);
var isCurrency = metaData.isCurrency(columnNumber);
var isDefiniteWritable = metaData.isDefinitelyWritable(columnNumber);
var isReadOnly = metaData.isReadOnly(columnNumber);
var isSearchable = metaData.isSearchable(columnNumber);
var isReadable = metaData.isReadOnly(columnNumber);
var isSigned = metaData.isSigned(columnNumber);
var isWritable = metaData.isWritable(columnNumber);
var nullable = metaData.isNullable(columnNumber);
console.log(name,type,typeName)
}
while (rs.next()) {
var name = rs.getString(1);
console.log(name)
}
}
finally{
if(conn!=null){
conn.close();
}
if(rs!=null){
rs.close();
}
}
const Properties = java.util.Properties;
const properties = new Properties();
properties.setProperty("name","dlaong")
console.log(properties.getProperty("name"))
- docker 运行
FROM ghcr.io/graalvm/graalvm-ce:21
RUN gu install nodejs
mysql 的参考
FROM dalongrong/graalvm-ce:21-node
WORKDIR /app
ENTRYPOINT [ "node","--jvm","--vm.classpath=/app/mysql-connector-java-5.1.47.jar","app.js" ]
说明
因为我们需要提供动态的类型以及数据绑定,具体的可以参考社区已经实现好的(node-jdbc),说明目前来说基于graalvm 运行nodejs(容器环境还是有点太大了。。。。。),目前已经参考实现了部分,暂时不能运行,代码放github上了
参考资料
https://github.com/CraZySacX/node-jdbc/blob/master/lib/resultset.js
https://www.cnblogs.com/rongfengliang/p/14321619.html
https://github.com/cube-js/cube.js/blob/master/CONTRIBUTING.md
https://github.com/rongfengliang/cubejs-graalvm-jdbc-driver
https://www.npmjs.com/package/@vertx/jdbc-client
https://www.npmjs.com/package/bcray-jdbc-fork
https://www.npmjs.com/package/jdbc
https://www.graalvm.org/reference-manual/js/JavaInteroperability/