Skip to content

Commit

Permalink
use client(not query) to get meta data
Browse files Browse the repository at this point in the history
  • Loading branch information
vagetablechicken committed Mar 30, 2022
1 parent 3d35d2d commit 5c5c108
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@

package com._4paradigm.openmldb.jdbc;

import com._4paradigm.openmldb.sdk.Column;
import com._4paradigm.openmldb.sdk.Schema;

import java.sql.Connection;
import java.sql.JDBCType;
import java.sql.ResultSet;
import java.sql.RowIdLifetime;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com._4paradigm.openmldb.sdk.Column;
import com._4paradigm.openmldb.sdk.Schema;

public class DatabaseMetaData implements java.sql.DatabaseMetaData {
public static final String DRIVER_NAME = "OpenMLDB Connector/J";
private final SQLConnection connection;
Expand All @@ -36,11 +35,6 @@ public DatabaseMetaData(SQLConnection connection) {
this.connection = connection;
}

private ResultSet executeQuery(String sql) throws SQLException {
Statement statement = connection.createStatement();
return statement.executeQuery(sql);
}

@Override
public boolean allProceduresAreCallable() throws SQLException {
return false;
Expand Down Expand Up @@ -668,8 +662,8 @@ public ResultSet getProcedureColumns(String catalog, String schemaPattern, Strin
* </OL>
*
* <P><B>Note:</B> OpenMLDB only has columns:
* TABLE_CAT(DB NAME)
* TABLE_SCHEM
* TABLE_CAT='null'
* TABLE_SCHEM='null'
* TABLE_NAME
* TABLE_TYPE='TABLE'
*
Expand Down Expand Up @@ -698,11 +692,11 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
throw new SQLException("unsupported");
}

ResultSet allTables = executeQuery("SHOW TABLES");
List<String> allTables = connection.getClient().getTableNames(connection.getDefaultDatabase());

List<List<String>> table = new ArrayList<>();
while (allTables.next()) {
if (tableNamePattern != null && !allTables.getString(1).equals(tableNamePattern)) {
for (String tableName : allTables) {
if (tableNamePattern != null && !tableName.equals(tableNamePattern)) {
continue;
}

Expand All @@ -712,7 +706,7 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
// TABLE_SCHEM schema name
row.add("null");
// TABLE_NAME table name
row.add(allTables.getString(1));
row.add(tableName);
// table type
row.add("TABLE");
// extra 6 columns
Expand Down Expand Up @@ -743,15 +737,15 @@ public ResultSet getTableTypes() throws SQLException {
@Override
public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException {
// hard to impl when 'SHOW xxx WHERE'
if (!schemaPattern.equals("null") || columnNamePattern != null) {
if (!catalog.equals("null") || !schemaPattern.equals("null") || columnNamePattern != null) {
throw new SQLException("unsupported");
}

// must use this to get database name, and then get table schema.
ResultSet allTables = executeQuery("SHOW TABLE STATUS");
List<String> allTables = connection.getClient().getTableNames(connection.getDefaultDatabase());
List<List<String>> table = new ArrayList<>();
while (allTables.next()) {
if (tableNamePattern != null && !allTables.getString(2).equals(tableNamePattern)) {
for (String tableName : allTables) {
if (tableNamePattern != null && !tableName.equals(tableNamePattern)) {
continue;
}

Expand All @@ -762,11 +756,10 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
// TABLE_SCHEM schema name
rowFirstPart.add("null");

String dbName = allTables.getString(3);
String tableName = allTables.getString(2);
String dbName = connection.getDefaultDatabase();
// TABLE_NAME table name
rowFirstPart.add(tableName);
Schema schema = connection.client.getTableSchema(dbName, tableName);
Schema schema = connection.getClient().getTableSchema(dbName, tableName);
List<Column> cols = schema.getColumnList();
for (int i = 0; i < cols.size(); i++) {
Column col = cols.get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
import java.util.concurrent.Executor;

public class SQLConnection implements Connection {
// TODO(hw): public this until we can get INFORMATION_SCHEMA from sql
public SqlExecutor client;
// TODO(hw): DatabaseMetaData use this directly until we can get INFORMATION_SCHEMA from sql
private SqlExecutor client;
private final Properties props;
private final String defaultDatabase;

Expand All @@ -48,12 +48,21 @@ public SQLConnection(SqlExecutor client, Properties props) throws SQLException {
this.defaultDatabase = props.getProperty("dbName", "");
java.sql.Statement stmt = client.getStatement();
// TODO(hw): offline job result is not good now, use online by default
// it only effects Statement
stmt.execute("set @@execute_mode='online'");
if (!this.defaultDatabase.isEmpty()) {
stmt.execute("USE " + this.defaultDatabase);
}
}

public SqlExecutor getClient(){
return client;
}

public String getDefaultDatabase(){
return defaultDatabase;
}

@Override
public java.sql.Statement createStatement() throws SQLException {
return client.getStatement();
Expand All @@ -62,6 +71,7 @@ public java.sql.Statement createStatement() throws SQLException {

/**
* WARNING: insert prepared statement only can insert into <B>online storage</B><P>
* database will use the db config set by jdbcUrl
* <p>
* select can visit both offline and online
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,7 @@ PreparedStatement getBatchRequestPreparedStmt(String db, String sql,

NS.TableInfo getTableInfo(String db, String table) throws SQLException;

List<String> getTableNames(String db);

void close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,17 @@ private SQLInsertRow getSQLInsertRow() throws SQLException {
}
throw new SQLException("getSQLInsertRow failed, " + msg);
}
status.delete();
return row;
}

private void clearSQLInsertRowList(){
for (SQLInsertRow row : currentRows) {
row.delete();
}
currentRows.clear();
}

@Override
@Deprecated
public ResultSet executeQuery() throws SQLException {
Expand Down Expand Up @@ -361,7 +369,7 @@ public boolean execute() throws SQLException {
boolean ok = router.ExecuteInsert(db, sql, currentRows.get(0), status);
// cleanup rows even if insert failed
// we can't execute() again without set new row, so we must clean up here
currentRows.clear();
clearSQLInsertRowList();
if (!ok) {
logger.error("getInsertRow fail: {}", status.getMsg());
status.delete();
Expand Down Expand Up @@ -589,9 +597,7 @@ public void close() throws SQLException {
if (closed) {
return;
}
for (SQLInsertRow row : currentRows) {
row.delete();
}
clearSQLInsertRowList();
if (currentSchema != null) {
currentSchema.delete();
currentSchema = null;
Expand Down Expand Up @@ -751,6 +757,7 @@ public int[] executeBatch() throws SQLException {
result[i] = ok ? 0 : -1;
}
status.delete();
clearSQLInsertRowList();
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ public List<String> showDatabases() {
return databases;
}

@Override
public List<String> getTableNames(String db) {
VectorString names = sqlRouter.GetTableNames(db);
List<String> tableNames = new ArrayList<>(names);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ public void testInsertPreparedStateBatch() {
while (rs2.next()) {
recordCnt++;
}
Assert.assertEquals(datas1.length + datas2.length, recordCnt);
Assert.assertEquals(datas1.length, recordCnt);
rs2.close();
// drop table
String drop = "drop table tsql1010;";
Expand Down

0 comments on commit 5c5c108

Please sign in to comment.