Skip to content

Commit

Permalink
unified Ruby SQL string to Java string conversion (improved performan…
Browse files Browse the repository at this point in the history
…ce) ...

... also reviewed a few xxx? Ruby method return values to be RubyBoolean
  • Loading branch information
kares committed Feb 17, 2015
1 parent 824b429 commit cd90361
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 40 deletions.
12 changes: 6 additions & 6 deletions src/java/arjdbc/db2/DB2RubyJdbcConnection.java
Expand Up @@ -35,6 +35,7 @@
import java.sql.SQLException;

import org.jruby.Ruby;
import org.jruby.RubyBoolean;
import org.jruby.RubyClass;
import org.jruby.RubyString;
import org.jruby.anno.JRubyMethod;
Expand Down Expand Up @@ -75,12 +76,11 @@ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
};

@JRubyMethod(name = "select?", required = 1, meta = true, frame = false)
public static IRubyObject select_p(final ThreadContext context,
public static RubyBoolean select_p(final ThreadContext context,
final IRubyObject self, final IRubyObject sql) {
if ( isValues(sql.convertToString()) ) {
return context.getRuntime().newBoolean( true );
}
return arjdbc.jdbc.RubyJdbcConnection.select_p(context, self, sql);
final RubyString sqlStr = sql.asString();
if ( isValues(sqlStr) ) return context.runtime.getTrue();
return arjdbc.jdbc.RubyJdbcConnection.select_p(context, self, sqlStr);
}

// DB2 supports 'stand-alone' VALUES expressions
Expand Down Expand Up @@ -114,7 +114,7 @@ public IRubyObject call(final Connection connection) throws SQLException {
try {
statement = connection.prepareStatement("VALUES IDENTITY_VAL_LOCAL()");
genKeys = statement.executeQuery();
return doMapGeneratedKeys(context.getRuntime(), genKeys, true);
return doMapGeneratedKeys(context.runtime, genKeys, true);
}
catch (final SQLException e) {
debugMessage(context.runtime, "failed to get generated keys: ", e.getMessage());
Expand Down
12 changes: 6 additions & 6 deletions src/java/arjdbc/derby/DerbyRubyJdbcConnection.java
Expand Up @@ -33,6 +33,7 @@
import java.sql.SQLException;

import org.jruby.Ruby;
import org.jruby.RubyBoolean;
import org.jruby.RubyClass;
import org.jruby.RubyString;
import org.jruby.anno.JRubyMethod;
Expand Down Expand Up @@ -97,16 +98,15 @@ protected DriverWrapper newDriverWrapper(final ThreadContext context, final Stri
}

@JRubyMethod(name = "select?", required = 1, meta = true, frame = false)
public static IRubyObject select_p(final ThreadContext context,
public static RubyBoolean select_p(final ThreadContext context,
final IRubyObject self, final IRubyObject sql) {
if ( isValues(sql.convertToString()) ) {
return context.runtime.newBoolean( true );
}
return arjdbc.jdbc.RubyJdbcConnection.select_p(context, self, sql);
final RubyString sqlStr = sql.asString();
if ( isValues(sqlStr) ) return context.runtime.getTrue();
return arjdbc.jdbc.RubyJdbcConnection.select_p(context, self, sqlStr);
}

// Derby supports 'stand-alone' VALUES expressions
private static final byte[] VALUES = new byte[]{ 'v','a','l','u', 'e', 's' };
private static final byte[] VALUES = new byte[]{ 'v','a','l','u','e','s' };

private static boolean isValues(final RubyString sql) {
final ByteList sqlBytes = sql.getByteList();
Expand Down
44 changes: 22 additions & 22 deletions src/java/arjdbc/jdbc/RubyJdbcConnection.java
Expand Up @@ -698,7 +698,7 @@ public IRubyObject execute(final ThreadContext context, final IRubyObject sql) {
return withConnection(context, new Callable<IRubyObject>() {
public IRubyObject call(final Connection connection) throws SQLException {
Statement statement = null;
final String query = sql.convertToString().getUnicodeValue();
final String query = sqlString(sql);
try {
statement = createStatement(context, connection);
if ( doExecute(statement, query) ) {
Expand Down Expand Up @@ -787,19 +787,17 @@ protected boolean genericExecute(final Statement statement, final String query)
@JRubyMethod(name = "execute_insert", required = 1)
public IRubyObject execute_insert(final ThreadContext context, final IRubyObject sql)
throws SQLException {
final String query = sql.convertToString().getUnicodeValue();
return executeUpdate(context, query, true);
return executeUpdate(context, sqlString(sql), true);
}

@JRubyMethod(name = "execute_insert", required = 2)
public IRubyObject execute_insert(final ThreadContext context,
final IRubyObject sql, final IRubyObject binds) throws SQLException {
final String query = sql.convertToString().getUnicodeValue();
if ( binds == null || binds.isNil() ) { // no prepared statements
return executeUpdate(context, query, true);
return executeUpdate(context, sqlString(sql), true);
}
else { // we allow prepared statements with empty binds parameters
return executePreparedUpdate(context, query, (List) binds, true);
return executePreparedUpdate(context, sqlString(sql), (List) binds, true);
}
}

Expand All @@ -813,8 +811,7 @@ public IRubyObject execute_insert(final ThreadContext context,
@JRubyMethod(name = {"execute_update", "execute_delete"}, required = 1)
public IRubyObject execute_update(final ThreadContext context, final IRubyObject sql)
throws SQLException {
final String query = sql.convertToString().getUnicodeValue();
return executeUpdate(context, query, false);
return executeUpdate(context, sqlString(sql), false);
}

/**
Expand All @@ -829,13 +826,11 @@ public IRubyObject execute_update(final ThreadContext context, final IRubyObject
@JRubyMethod(name = {"execute_update", "execute_delete"}, required = 2)
public IRubyObject execute_update(final ThreadContext context,
final IRubyObject sql, final IRubyObject binds) throws SQLException {

final String query = sql.convertToString().getUnicodeValue();
if ( binds == null || binds.isNil() ) { // no prepared statements
return executeUpdate(context, query, false);
return executeUpdate(context, sqlString(sql), false);
}
else { // we allow prepared statements with empty binds parameters
return executePreparedUpdate(context, query, (List) binds, false);
return executePreparedUpdate(context, sqlString(sql), (List) binds, false);
}
}

Expand Down Expand Up @@ -915,8 +910,7 @@ public IRubyObject call(final Connection connection) throws SQLException {
@JRubyMethod(name = "execute_query_raw", required = 1) // optional block
public IRubyObject execute_query_raw(final ThreadContext context,
final IRubyObject sql, final Block block) throws SQLException {
final String query = sql.convertToString().getUnicodeValue();
return executeQueryRaw(context, query, 0, block);
return executeQueryRaw(context, sqlString(sql), 0, block);
}

/**
Expand All @@ -932,7 +926,7 @@ public IRubyObject execute_query_raw(final ThreadContext context,
public IRubyObject execute_query_raw(final ThreadContext context,
final IRubyObject[] args, final Block block) throws SQLException {
// args: (sql), (sql, max_rows), (sql, binds), (sql, max_rows, binds)
final String query = args[0].convertToString().getUnicodeValue(); // sql
final String query = sqlString( args[0] ); // sql
IRubyObject max_rows = args.length > 1 ? args[1] : null;
IRubyObject binds = args.length > 2 ? args[2] : null;
final int maxRows;
Expand Down Expand Up @@ -1026,8 +1020,14 @@ public IRubyObject call(final Connection connection) throws SQLException {
@JRubyMethod(name = "execute_query", required = 1)
public IRubyObject execute_query(final ThreadContext context,
final IRubyObject sql) throws SQLException {
final String query = sql.convertToString().getUnicodeValue();
return executeQuery(context, query, 0);
return executeQuery(context, sqlString(sql), 0);
}

protected static String sqlString(final IRubyObject sql) {
if ( sql instanceof RubyString ) {
return ((RubyString) sql).decodeString();
}
return sql.asString().decodeString();
}

/**
Expand All @@ -1044,7 +1044,7 @@ public IRubyObject execute_query(final ThreadContext context,
public IRubyObject execute_query(final ThreadContext context,
final IRubyObject[] args) throws SQLException {
// args: (sql), (sql, max_rows), (sql, binds), (sql, max_rows, binds)
final String query = args[0].convertToString().getUnicodeValue(); // sql
final String query = sqlString( args[0] ); // sql
IRubyObject max_rows = args.length > 1 ? args[1] : null;
IRubyObject binds = args.length > 2 ? args[2] : null;
final int maxRows;
Expand Down Expand Up @@ -3751,9 +3751,9 @@ protected boolean databaseSupportsSchemas() {
private static final byte[] CALL = new byte[]{ 'c','a','l','l' };

@JRubyMethod(name = "select?", required = 1, meta = true, frame = false)
public static IRubyObject select_p(final ThreadContext context,
public static RubyBoolean select_p(final ThreadContext context,
final IRubyObject self, final IRubyObject sql) {
return context.runtime.newBoolean( isSelect(sql.convertToString()) );
return context.runtime.newBoolean( isSelect(sql.asString()) );
}

private static boolean isSelect(final RubyString sql) {
Expand All @@ -3767,9 +3767,9 @@ private static boolean isSelect(final RubyString sql) {
private static final byte[] INSERT = new byte[] { 'i','n','s','e','r','t' };

@JRubyMethod(name = "insert?", required = 1, meta = true, frame = false)
public static IRubyObject insert_p(final ThreadContext context,
public static RubyBoolean insert_p(final ThreadContext context,
final IRubyObject self, final IRubyObject sql) {
final ByteList sqlBytes = sql.convertToString().getByteList();
final ByteList sqlBytes = sql.asString().getByteList();
return context.runtime.newBoolean( startsWithIgnoreCase(sqlBytes, INSERT) );
}

Expand Down
11 changes: 6 additions & 5 deletions src/java/arjdbc/mssql/MSSQLRubyJdbcConnection.java
Expand Up @@ -36,6 +36,7 @@

import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyBoolean;
import org.jruby.RubyClass;
import org.jruby.RubyString;
import org.jruby.anno.JRubyMethod;
Expand Down Expand Up @@ -77,8 +78,8 @@ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
private static final byte[] EXEC = new byte[] { 'e', 'x', 'e', 'c' };

@JRubyMethod(name = "exec?", required = 1, meta = true, frame = false)
public static IRubyObject exec_p(ThreadContext context, IRubyObject self, IRubyObject sql) {
final ByteList sqlBytes = sql.convertToString().getByteList();
public static RubyBoolean exec_p(ThreadContext context, IRubyObject self, IRubyObject sql) {
final ByteList sqlBytes = sql.asString().getByteList();
return context.runtime.newBoolean( startsWithIgnoreCase(sqlBytes, EXEC) );
}

Expand Down Expand Up @@ -165,13 +166,13 @@ private static ColumnData[] filterRowNumFromColumns(final ColumnData[] columns)

// internal helper not meant as a "public" API - used in one place thus every
@JRubyMethod(name = "jtds_driver?")
public IRubyObject jtds_driver_p(final ThreadContext context) throws SQLException {
public RubyBoolean jtds_driver_p(final ThreadContext context) throws SQLException {
// "jTDS Type 4 JDBC Driver for MS SQL Server and Sybase"
// SQLJDBC: "Microsoft JDBC Driver 4.0 for SQL Server"
return withConnection(context, new Callable<IRubyObject>() {
return withConnection(context, new Callable<RubyBoolean>() {
// NOTE: only used in one place for now (on release_savepoint) ...
// might get optimized to only happen once since driver won't change
public IRubyObject call(final Connection connection) throws SQLException {
public RubyBoolean call(final Connection connection) throws SQLException {
final String driver = connection.getMetaData().getDriverName();
return context.runtime.newBoolean( driver.indexOf("jTDS") >= 0 );
}
Expand Down
2 changes: 1 addition & 1 deletion src/java/arjdbc/oracle/OracleRubyJdbcConnection.java
Expand Up @@ -104,7 +104,7 @@ public IRubyObject call(final Connection connection) throws SQLException {
@JRubyMethod(name = "execute_insert_returning", required = 2)
public IRubyObject execute_insert_returning(final ThreadContext context,
final IRubyObject sql, final IRubyObject binds) throws SQLException {
final String query = sql.convertToString().getUnicodeValue();
final String query = sqlString(sql);
final int outType = Types.VARCHAR;
if ( binds == null || binds.isNil() ) { // no prepared statements
return executePreparedCall(context, query, Collections.EMPTY_LIST, outType);
Expand Down

0 comments on commit cd90361

Please sign in to comment.