Skip to content

Commit 9736e99

Browse files
committed
JdbcConnection: over-load #table_exists? + use toString with name args
cause otherwise when symbols are passed they fail due missing #to_str - also support #indexes(table_name, name) without schema name
1 parent 0f9dd0c commit 9736e99

File tree

1 file changed

+46
-21
lines changed

1 file changed

+46
-21
lines changed

src/java/arjdbc/jdbc/RubyJdbcConnection.java

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -815,19 +815,33 @@ protected String[] getTableTypes() {
815815
return TABLE_TYPES;
816816
}
817817

818-
@JRubyMethod(name = "table_exists?", required = 1, optional = 1)
819-
public IRubyObject table_exists_p(final ThreadContext context, final IRubyObject[] args) {
820-
IRubyObject name = args[0], schema_name = args.length > 1 ? args[1] : null;
821-
if ( ! ( name instanceof RubyString ) ) {
822-
name = name.callMethod(context, "to_s");
823-
}
824-
final String tableName = ((RubyString) name).getUnicodeValue();
825-
final String tableSchema = schema_name == null ? null : schema_name.convertToString().getUnicodeValue();
826-
final Ruby runtime = context.getRuntime();
818+
@JRubyMethod(name = "table_exists?")
819+
public IRubyObject table_exists_p(final ThreadContext context, IRubyObject table) {
820+
if ( table.isNil() ) {
821+
throw context.getRuntime().newArgumentError("nil table name");
822+
}
823+
final String tableName = table.toString();
827824

825+
return tableExists(context, null, tableName);
826+
}
827+
828+
@JRubyMethod(name = "table_exists?")
829+
public IRubyObject table_exists_p(final ThreadContext context, IRubyObject table, IRubyObject schema) {
830+
if ( table.isNil() ) {
831+
throw context.getRuntime().newArgumentError("nil table name");
832+
}
833+
final String tableName = table.toString();
834+
final String defaultSchema = schema.isNil() ? null : schema.toString();
835+
836+
return tableExists(context, defaultSchema, tableName);
837+
}
838+
839+
protected IRubyObject tableExists(final ThreadContext context,
840+
final String defaultSchema, final String tableName) {
841+
final Ruby runtime = context.getRuntime();
828842
return withConnection(context, new Callable<RubyBoolean>() {
829843
public RubyBoolean call(final Connection connection) throws SQLException {
830-
final TableName components = extractTableName(connection, tableSchema, tableName);
844+
final TableName components = extractTableName(connection, defaultSchema, tableName);
831845
return runtime.newBoolean( tableExists(runtime, connection, components) );
832846
}
833847
});
@@ -840,7 +854,7 @@ public IRubyObject columns_internal(final ThreadContext context, final IRubyObje
840854
public IRubyObject call(final Connection connection) throws SQLException {
841855
ResultSet columns = null, primaryKeys = null;
842856
try {
843-
final String tableName = args[0].convertToString().getUnicodeValue();
857+
final String tableName = args[0].toString();
844858
// optionals (NOTE: catalog argumnet was never used before 1.3.0) :
845859
final String catalog = args.length > 1 ? toStringOrNull(args[1]) : null;
846860
final String defaultSchema = args.length > 2 ? toStringOrNull(args[2]) : null;
@@ -869,12 +883,17 @@ public IRubyObject call(final Connection connection) throws SQLException {
869883
}
870884
});
871885
}
886+
887+
@JRubyMethod(name = "indexes")
888+
public IRubyObject indexes(final ThreadContext context, IRubyObject tableName, IRubyObject name) {
889+
return indexes(context, toStringOrNull(tableName), toStringOrNull(name), null);
890+
}
872891

873892
@JRubyMethod(name = "indexes")
874-
public IRubyObject indexes(ThreadContext context, IRubyObject tableName, IRubyObject name, IRubyObject schemaName) {
893+
public IRubyObject indexes(final ThreadContext context, IRubyObject tableName, IRubyObject name, IRubyObject schemaName) {
875894
return indexes(context, toStringOrNull(tableName), toStringOrNull(name), toStringOrNull(schemaName));
876895
}
877-
896+
878897
// NOTE: metaData.getIndexInfo row mappings :
879898
private static final int INDEX_INFO_TABLE_NAME = 3;
880899
private static final int INDEX_INFO_NON_UNIQUE = 4;
@@ -1015,10 +1034,10 @@ public IRubyObject write_large_object(final ThreadContext context, final IRubyOb
10151034
throws SQLException {
10161035

10171036
final boolean isBinary = args[0].isTrue();
1018-
final RubyString columnName = args[1].convertToString();
1019-
final RubyString tableName = args[2].convertToString();
1020-
final RubyString idKey = args[3].convertToString();
1021-
final RubyString idVal = args[4].convertToString();
1037+
final String columnName = args[1].toString();
1038+
final String tableName = args[2].toString();
1039+
final String idKey = args[3].toString();
1040+
final String idVal = args[4].toString();
10221041
final IRubyObject lobValue = args[5];
10231042

10241043
final Ruby runtime = context.getRuntime();
@@ -1096,16 +1115,16 @@ protected IRubyObject getConfigValue(final ThreadContext context, final String k
10961115
protected IRubyObject config_value(ThreadContext context, String key) {
10971116
return getConfigValue(context, key);
10981117
}
1099-
1100-
private static String toStringOrNull(IRubyObject arg) {
1118+
1119+
private static String toStringOrNull(final IRubyObject arg) {
11011120
return arg.isNil() ? null : arg.toString();
11021121
}
11031122

1104-
protected IRubyObject getAdapter(ThreadContext context) {
1123+
protected IRubyObject getAdapter(final ThreadContext context) {
11051124
return callMethod(context, "adapter");
11061125
}
11071126

1108-
protected IRubyObject getJdbcColumnClass(ThreadContext context) {
1127+
protected IRubyObject getJdbcColumnClass(final ThreadContext context) {
11091128
return getAdapter(context).callMethod(context, "jdbc_column_class");
11101129
}
11111130

@@ -2740,6 +2759,12 @@ public TableName(String catalog, String schema, String table) {
27402759
this.schema = schema;
27412760
this.name = table;
27422761
}
2762+
2763+
@Override
2764+
public String toString() {
2765+
return getClass().getName() +
2766+
"{catalog=" + catalog + ",schema=" + schema + ",name=" + name + "}";
2767+
}
27432768

27442769
}
27452770

0 commit comments

Comments
 (0)