Commit b0c90e81 authored by Olivier Bertrand's avatar Olivier Bertrand

- New version of java wrappers as the "wrappers" package

  deleted:    storage/connect/JdbcApacheInterface.class
  deleted:    storage/connect/JdbcApacheInterface.java
  deleted:    storage/connect/JdbcDSInterface.class
  deleted:    storage/connect/JdbcDSInterface.java
  modified:   storage/connect/JdbcInterface.java
  modified:   storage/connect/ha_connect.cc
  modified:   storage/connect/jdbconn.cpp
  modified:   storage/connect/jdbconn.h
  modified:   storage/connect/tabjdbc.cpp
  modified:   storage/connect/tabjdbc.h
  added:      storage/connect/ApacheInterface.java
  added:      storage/connect/Client.java
  added:      storage/connect/JdbcInterface.jar
  added:      storage/connect/MariadbInterface.java
  added:      storage/connect/MysqlInterface.java
  added:      storage/connect/OracleInterface.java
  added:      storage/connect/PostgresqlInterface.java
  added:      storage/connect/wrappers/ApacheInterface.class
  added:      storage/connect/wrappers/Client.class
  added:      storage/connect/wrappers/JdbcInterface.class
  added:      storage/connect/wrappers/MariadbInterface.class
  added:      storage/connect/wrappers/MysqlInterface.class
  added:      storage/connect/wrappers/OracleInterface.class
  added:      storage/connect/wrappers/PostgresqlInterface.class
parent 92dbe32d
package wrappers;
import java.sql.*;
import java.util.Hashtable;
import org.apache.commons.dbcp2.BasicDataSource;
public class ApacheInterface extends JdbcInterface {
static Hashtable<String,BasicDataSource> pool = new Hashtable<String, BasicDataSource>();
public ApacheInterface() {
this(true);
} // end of default constructor
public ApacheInterface(boolean b) {
super(b);
} // end of constructor
@Override
public int JdbcConnect(String[] parms, int fsize, boolean scrollable) {
int rc = 0;
String url = parms[1];
BasicDataSource ds = null;
if (DEBUG)
System.out.println("Connecting to Apache data source");
try {
if (url == null)
throw new Exception("URL cannot be null");
if ((ds = pool.get(url)) == null) {
ds = new BasicDataSource();
ds.setDriverClassName(parms[0]);
ds.setUrl(url);
ds.setUsername(parms[2]);
ds.setPassword(parms[3]);
pool.put(url, ds);
} // endif ds
// Get a connection from the data source
conn = ds.getConnection();
// Get the data base meta data object
dbmd = conn.getMetaData();
// Get a statement from the connection
stmt = GetStmt(fsize, scrollable);
} catch (SQLException se) {
SetErrmsg(se);
rc = -2;
} catch (Exception e) {
SetErrmsg(e);
rc = -3;
} // end try/catch
return rc;
} // end of JdbcConnect
} // end of class ApacheInterface
package wrappers;
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
public class Client {
static boolean DEBUG = true;
static final Console c = System.console();
static JdbcInterface jdi = null;
public static void main(String[] args) {
int rc, n, ncol, i = 0, fsize = 0;
boolean scrollable = false;
String s;
String[] parms = new String[4];
if (args.length > 0)
try {
i = Integer.parseInt(args[i]);
} catch (NumberFormatException e) {
i = 0;
} // end try/catch
switch (i) {
case 1:
jdi = new ApacheInterface(DEBUG);
break;
case 2:
jdi = new MysqlInterface(DEBUG);
break;
case 3:
jdi = new MariadbInterface(DEBUG);
break;
case 4:
jdi = new OracleInterface(DEBUG);
break;
case 5:
jdi = new PostgresqlInterface(DEBUG);
break;
default:
jdi = new JdbcInterface(DEBUG);
} // endswitch i
parms[0] = getLine("Driver: ", false);
parms[1] = getLine("URL: ", false);
parms[2] = getLine("User: ", false);
parms[3] = getLine("Password: ", true);
s = getLine("Fsize: ", false);
fsize = (s != null) ? Integer.parseInt(s) : 0;
s = getLine("Scrollable: ", false);
scrollable = (s != null) ? s.toLowerCase().charAt(0) != 'n' : false;
rc = jdi.JdbcConnect(parms, fsize, scrollable);
if (rc == 0) {
String query;
System.out.println("Successfully connected to " + parms[1]);
while ((query = getLine("Query: ", false)) != null) {
n = jdi.Execute(query);
System.out.println("Returned n = " + n);
if ((ncol = jdi.GetResult()) > 0)
PrintResult(ncol);
else
System.out.println("Affected rows = " + n);
} // endwhile
rc = jdi.JdbcDisconnect();
System.out.println("Disconnect returned " + rc);
} else
System.out.println(jdi.GetErrmsg() + " rc=" + rc);
} // end of main
private static void PrintResult(int ncol) {
// Get result set meta data
int i;
String columnName;
// Get the column names; column indices start from 1
for (i = 1; i <= ncol; i++) {
columnName = jdi.ColumnName(i);
if (columnName == null)
return;
// Get the name of the column's table name
//String tableName = rsmd.getTableName(i);
if (i > 1)
System.out.print("\t");
System.out.print(columnName);
} // endfor i
System.out.println();
// Loop through the result set
while (jdi.ReadNext() > 0) {
for (i = 1; i <= ncol; i++) {
if (i > 1)
System.out.print("\t");
if (DEBUG)
System.out.print("(" + jdi.ColumnType(i, null) + ")");
switch (jdi.ColumnType(i, null)) {
case java.sql.Types.VARCHAR:
case java.sql.Types.LONGVARCHAR:
case java.sql.Types.CHAR:
System.out.print(jdi.StringField(i, null));
break;
case java.sql.Types.INTEGER:
System.out.print(jdi.IntField(i, null));
break;
case java.sql.Types.BIGINT:
System.out.print(jdi.BigintField(i, null));
break;
case java.sql.Types.TIMESTAMP:
System.out.print(jdi.TimestampField(i, null));
break;
case java.sql.Types.TIME:
System.out.print(jdi.TimeField(i, null));
break;
case java.sql.Types.DATE:
System.out.print(jdi.DateField(i, null));
break;
case java.sql.Types.SMALLINT:
System.out.print(jdi.IntField(i, null));
break;
case java.sql.Types.DOUBLE:
case java.sql.Types.REAL:
case java.sql.Types.FLOAT:
case java.sql.Types.DECIMAL:
System.out.print(jdi.DoubleField(i, null));
break;
case java.sql.Types.BOOLEAN:
System.out.print(jdi.BooleanField(i, null));
default:
break;
} // endswitch Type
} // endfor i
System.out.println();
} // end while rs
} // end of PrintResult
// ==================================================================
private static String getLine(String p, boolean b) {
String response;
if (c != null) {
// Standard console mode
if (b) {
response = new String(c.readPassword(p));
} else
response = c.readLine(p);
} else {
// For instance when testing from Eclipse
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print(p);
try {
// Cannot suppress echo for password entry
response = in.readLine();
} catch (IOException e) {
response = "";
} // end of try/catch
} // endif c
return (response.isEmpty()) ? null : response;
} // end of getLine
} // end of class Client
This diff was suppressed by a .gitattributes entry.
This diff is collapsed.
This diff was suppressed by a .gitattributes entry.
This diff is collapsed.
This diff was suppressed by a .gitattributes entry.
package wrappers;
import java.math.*;
import java.sql.*;
//import java.util.Arrays;
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;
//import java.io.File;
//import java.lang.reflect.Field;
import javax.sql.DataSource;
public class JdbcInterface {
// This is used by DS classes
static Hashtable<String,DataSource> dst = null;
boolean DEBUG = false;
String Errmsg = "No error";
Connection conn = null;
......@@ -25,7 +30,7 @@ public class JdbcInterface {
DEBUG = b;
} // end of constructor
private void SetErrmsg(Exception e) {
protected void SetErrmsg(Exception e) {
if (DEBUG)
System.out.println(e.getMessage());
......@@ -74,27 +79,7 @@ public class JdbcInterface {
dbmd = conn.getMetaData();
// Get a statement from the connection
if (scrollable)
stmt = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);
else
stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
if (DEBUG)
System.out.println("Statement type = " + stmt.getResultSetType()
+ " concurrency = " + stmt.getResultSetConcurrency());
if (DEBUG) // Get the fetch size of a statement
System.out.println("Default fetch size = " + stmt.getFetchSize());
if (fsize != 0) {
// Set the fetch size
stmt.setFetchSize(fsize);
if (DEBUG)
System.out.println("New fetch size = " + stmt.getFetchSize());
} // endif fsize
stmt = GetStmt(fsize, scrollable);
} catch(ClassNotFoundException e) {
SetErrmsg(e);
rc = -1;
......@@ -109,6 +94,34 @@ public class JdbcInterface {
return rc;
} // end of JdbcConnect
protected Statement GetStmt(int fsize, boolean scrollable) throws SQLException, Exception {
Statement stmt = null;
if (scrollable)
stmt = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);
else
stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
if (DEBUG)
System.out.println("Statement type = " + stmt.getResultSetType()
+ " concurrency = " + stmt.getResultSetConcurrency());
if (DEBUG) // Get the fetch size of a statement
System.out.println("Default fetch size = " + stmt.getFetchSize());
if (fsize != 0) {
// Set the fetch size
stmt.setFetchSize(fsize);
if (DEBUG)
System.out.println("New fetch size = " + stmt.getFetchSize());
} // endif fsize
return stmt;
} // end of GetStmt
public int CreatePrepStmt(String sql) {
int rc = 0;
......@@ -227,7 +240,9 @@ public class JdbcInterface {
// Cancel pending statement
if (stmt != null)
try {
System.out.println("Cancelling statement");
if (DEBUG)
System.out.println("Cancelling statement");
stmt.cancel();
} catch(SQLException se) {
SetErrmsg(se);
......@@ -710,3 +725,4 @@ public class JdbcInterface {
*/
} // end of class JdbcInterface
package wrappers;
import java.sql.*;
import java.util.Hashtable;
import javax.sql.DataSource;
import org.mariadb.jdbc.MariaDbDataSource;
public class MariadbInterface extends JdbcInterface {
public MariadbInterface() {
this(true);
} // end of default constructor
public MariadbInterface(boolean b) {
super(b);
if (dst == null)
dst = new Hashtable<String, DataSource>();
} // end of default constructor
@Override
public int JdbcConnect(String[] parms, int fsize, boolean scrollable) {
int rc = 0;
String url = parms[1];
DataSource ds = null;
MariaDbDataSource ads = null;
if (DEBUG)
System.out.println("Connecting to MariaDB data source");
try {
if (url == null)
throw new Exception("URL cannot be null");
if ((ds = dst.get(url)) == null) {
ads = new MariaDbDataSource();
ads.setUrl(url);
if (parms[2] != null)
ads.setUser(parms[2]);
if (parms[3] != null)
ads.setPassword(parms[3]);
ds = ads;
dst.put(url, ds);
} // endif ds
// Get a connection from the data source
conn = ds.getConnection();
// Get the data base meta data object
dbmd = conn.getMetaData();
// Get a statement from the connection
stmt = GetStmt(fsize, scrollable);
} catch (SQLException se) {
SetErrmsg(se);
rc = -2;
} catch( Exception e ) {
SetErrmsg(e);
rc = -3;
} // end try/catch
return rc;
} // end of JdbcConnect
}
package wrappers;
import java.sql.*;
import java.util.Hashtable;
import javax.sql.DataSource;
import com.mysql.cj.jdbc.MysqlDataSource;
public class MysqlInterface extends JdbcInterface {
public MysqlInterface() {
this(true);
} // end of default constructor
public MysqlInterface(boolean b) {
super(b);
if (dst == null)
dst = new Hashtable<String, DataSource>();
} // end of default constructor
@Override
public int JdbcConnect(String[] parms, int fsize, boolean scrollable) {
int rc = 0;
String url = parms[1];
DataSource ds = null;
MysqlDataSource mds = null;
if (DEBUG)
System.out.println("Connecting to MySQL data source");
try {
if (url == null)
throw new Exception("URL cannot be null");
if ((ds = dst.get(url)) == null) {
mds = new MysqlDataSource();
mds.setUrl(url);
if (parms[2] != null)
mds.setUser(parms[2]);
if (parms[3] != null)
mds.setPassword(parms[3]);
ds = mds;
dst.put(url, ds);
} // endif ds
// Get a connection from the data source
conn = ds.getConnection();
// Get the data base meta data object
dbmd = conn.getMetaData();
// Get a statement from the connection
stmt = GetStmt(fsize, scrollable);
} catch (SQLException se) {
SetErrmsg(se);
rc = -2;
} catch( Exception e ) {
SetErrmsg(e);
rc = -3;
} // end try/catch
return rc;
} // end of JdbcConnect
} // end of class MysqlInterface
package wrappers;
import java.sql.*;
import java.util.Hashtable;
import javax.sql.DataSource;
import oracle.jdbc.pool.OracleDataSource;
public class OracleInterface extends JdbcInterface {
public OracleInterface() {
this(true);
} // end of OracleInterface constructor
public OracleInterface(boolean b) {
super(b);
if (dst == null)
dst = new Hashtable<String, DataSource>();
} // end of OracleInterface constructor
@Override
public int JdbcConnect(String[] parms, int fsize, boolean scrollable) {
int rc = 0;
String url = parms[1];
DataSource ds = null;
OracleDataSource ods = null;
if (DEBUG)
System.out.println("Connecting to Oracle data source");
try {
if (url == null)
throw new Exception("URL cannot be null");
if ((ds = dst.get(url)) == null) {
ods = new OracleDataSource();
ods.setURL(url);
if (parms[2] != null)
ods.setUser(parms[2]);
if (parms[3] != null)
ods.setPassword(parms[3]);
ds = ods;
dst.put(url, ds);
} // endif ds
// Get a connection from the data source
conn = ds.getConnection();
// Get the data base meta data object
dbmd = conn.getMetaData();
// Get a statement from the connection
stmt = GetStmt(fsize, scrollable);
} catch (SQLException se) {
SetErrmsg(se);
rc = -2;
} catch( Exception e ) {
SetErrmsg(e);
rc = -3;
} // end try/catch
return rc;
} // end of JdbcConnect
} // end of class OracleInterface
package wrappers;
import java.sql.*;
import java.util.Hashtable;
import javax.sql.DataSource;
import org.postgresql.jdbc2.optional.PoolingDataSource;
public class PostgresqlInterface extends JdbcInterface {
public PostgresqlInterface() {
this(true);
} // end of constructor
public PostgresqlInterface(boolean b) {
super(b);
if (dst == null)
dst = new Hashtable<String, DataSource>();
} // end of constructor
@Override
public int JdbcConnect(String[] parms, int fsize, boolean scrollable) {
int rc = 0;
String url = parms[1];
DataSource ds = null;
PoolingDataSource pds = null;
if (DEBUG)
System.out.println("Connecting to Postgresql data source");
try {
if (url == null)
throw new Exception("URL cannot be null");
if ((ds = dst.get(url)) == null) {
pds = new PoolingDataSource();
pds.setUrl(url);
if (parms[2] != null)
pds.setUser(parms[2]);
if (parms[3] != null)
pds.setPassword(parms[3]);
ds = pds;
dst.put(url, ds);
} // endif ds
// Get a connection from the data source
conn = ds.getConnection();
// Get the data base meta data object
dbmd = conn.getMetaData();
// Get a statement from the connection
stmt = GetStmt(fsize, scrollable);
} catch (SQLException se) {
SetErrmsg(se);
rc = -2;
} catch( Exception e ) {
SetErrmsg(e);
rc = -3;
} // end try/catch
return rc;
} // end of JdbcConnect
} // end of class PostgresqlInterface
......@@ -6880,7 +6880,7 @@ static MYSQL_SYSVAR_STR(java_wrapper, Wrapper,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_MEMALLOC,
"Java wrapper class",
// check_class_path, update_class_path,
NULL, NULL, "JdbcInterface");
NULL, NULL, "wrappers/JdbcInterface");
#endif // JDBC_SUPPORT
......
......@@ -616,7 +616,7 @@ PQRYRES JDBCPrimaryKeys(PGLOBAL g, JDBConn *op, char *dsn, char *table)
} // end of JDBCPrimaryKeys
#endif // 0
/***********************************************************************/
/************************************************Wrapper***********************/
/* JDBConn construction/destruction. */
/***********************************************************************/
JDBConn::JDBConn(PGLOBAL g, TDBJDBC *tdbp)
......@@ -634,6 +634,14 @@ JDBConn::JDBConn(PGLOBAL g, TDBJDBC *tdbp)
//m_QueryTimeout = DEFAULT_QUERY_TIMEOUT;
//m_UpdateOptions = 0;
Msg = NULL;
m_Wrap = (tdbp && tdbp->WrapName) ? tdbp->WrapName : Wrapper;
if (!strchr(m_Wrap, '/')) {
// Add the wrapper package name
char *wn = (char*)PlugSubAlloc(g, NULL, strlen(m_Wrap) + 10);
m_Wrap = strcat(strcpy(wn, "wrappers/"), m_Wrap);
} // endif m_Wrap
m_Driver = NULL;
m_Url = NULL;
m_User = NULL;
......@@ -890,7 +898,9 @@ bool JDBConn::GetJVM(PGLOBAL g)
/***********************************************************************/
int JDBConn::Open(PJPARM sop)
{
bool err = false;
jboolean jt = (trace > 0);
PGLOBAL& g = m_G;
// Link or check whether jvm library was linked
......@@ -1000,19 +1010,16 @@ int JDBConn::Open(PJPARM sop)
return RC_FX;
} // endswitch rc
//=============== Display JVM version ===============
jint ver = env->GetVersion();
printf("JVM Version %d.%d\n", ((ver>>16)&0x0f), (ver&0x0f));
} // endif rc
//=============== Display JVM version =======================================
#if defined(_DEBUG)
jint ver = env->GetVersion();
printf("JVM Version %d.%d\n", ((ver>>16)&0x0f), (ver&0x0f));
#endif //_DEBUG
// try to find the java wrapper class
jdi = env->FindClass(Wrapper);
jdi = env->FindClass(m_Wrap);
if (jdi == nullptr) {
sprintf(g->Message, "ERROR: class %s not found!", Wrapper);
sprintf(g->Message, "ERROR: class %s not found!", m_Wrap);
return RC_FX;
} // endif jdi
......@@ -1055,19 +1062,19 @@ int JDBConn::Open(PJPARM sop)
#endif // 0
// if class found, continue
jmethodID ctor = env->GetMethodID(jdi, "<init>", "()V");
jmethodID ctor = env->GetMethodID(jdi, "<init>", "(Z)V");
if (ctor == nullptr) {
sprintf(g->Message, "ERROR: %s constructor not found!", Wrapper);
sprintf(g->Message, "ERROR: %s constructor not found!", m_Wrap);
return RC_FX;
} else
job = env->NewObject(jdi, ctor);
job = env->NewObject(jdi, ctor, jt);
// If the object is successfully constructed,
// we can then search for the method we want to call,
// and invoke it for the object:
if (job == nullptr) {
sprintf(g->Message, "%s class object not constructed!", Wrapper);
sprintf(g->Message, "%s class object not constructed!", m_Wrap);
return RC_FX;
} // endif job
......
......@@ -175,6 +175,7 @@ class JDBConn : public BLOCK {
//DWORD m_QueryTimeout;
//DWORD m_UpdateOptions;
char *Msg;
char *m_Wrap;
char m_IDQuoteChar[2];
PSZ m_Driver;
PSZ m_Url;
......
......@@ -96,7 +96,7 @@ bool ExactInfo(void);
/***********************************************************************/
JDBCDEF::JDBCDEF(void)
{
Driver = Url = Tabname = Tabschema = Username = Colpat = NULL;
Driver = Url = Wrapname =Tabname = Tabschema = Username = Colpat = NULL;
Password = Tabcat = Tabtype = Srcdef = Qchar = Qrystr = Sep = NULL;
Options = Quoted = Maxerr = Maxres = Memory = 0;
Scrollable = Xsrc = false;
......@@ -233,6 +233,7 @@ bool JDBCDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff)
if ((Srcdef = GetStringCatInfo(g, "Srcdef", NULL)))
Read_Only = true;
Wrapname = GetStringCatInfo(g, "Wrapper", NULL);
Tabcat = GetStringCatInfo(g, "Qualifier", NULL);
Tabcat = GetStringCatInfo(g, "Catalog", Tabcat);
Tabschema = GetStringCatInfo(g, "Dbname", NULL);
......@@ -331,6 +332,7 @@ TDBJDBC::TDBJDBC(PJDBCDEF tdp) : TDBASE(tdp)
if (tdp) {
Ops.Driver = tdp->Driver;
Ops.Url = tdp->Url;
WrapName = tdp->Wrapname;
TableName = tdp->Tabname;
Schema = tdp->Tabschema;
Ops.User = tdp->Username;
......@@ -347,6 +349,7 @@ TDBJDBC::TDBJDBC(PJDBCDEF tdp) : TDBASE(tdp)
Memory = tdp->Memory;
Ops.Scrollable = tdp->Scrollable;
} else {
WrapName = NULL;
TableName = NULL;
Schema = NULL;
Ops.Driver = NULL;
......@@ -392,6 +395,7 @@ TDBJDBC::TDBJDBC(PTDBJDBC tdbp) : TDBASE(tdbp)
{
Jcp = tdbp->Jcp; // is that right ?
Cnp = tdbp->Cnp;
WrapName = tdbp->WrapName;
TableName = tdbp->TableName;
Schema = tdbp->Schema;
Ops = tdbp->Ops;
......
......@@ -54,6 +54,7 @@ class DllExport JDBCDEF : public TABDEF { /* Logical table description */
PSZ Driver; /* JDBC driver */
PSZ Url; /* JDBC driver URL */
PSZ Tabname; /* External table name */
PSZ Wrapname; /* Java wrapper name */
PSZ Tabschema; /* External table schema */
PSZ Username; /* User connect name */
PSZ Password; /* Password connect info */
......@@ -133,6 +134,7 @@ class TDBJDBC : public TDBASE {
JDBCCOL *Cnp; // Points to count(*) column
JDBCPARM Ops; // Additional parameters
PSTRG Query; // Constructed SQL query
char *WrapName; // Points to Java wrapper name
char *TableName; // Points to JDBC table name
char *Schema; // Points to JDBC table Schema
char *User; // User connect info
......
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment