Skip to content

Commit 4126fde

Browse files
committed
[postgres] set prepared values with array columns on AR < 4.0 correctly
1 parent bccb50e commit 4126fde

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

src/java/arjdbc/postgresql/PostgreSQLRubyJdbcConnection.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.jruby.RubyArray;
4343
import org.jruby.RubyBoolean;
4444
import org.jruby.RubyClass;
45+
import org.jruby.RubyFixnum;
4546
import org.jruby.RubyFloat;
4647
import org.jruby.RubyHash;
4748
import org.jruby.RubyIO;
@@ -55,6 +56,8 @@
5556

5657
import org.postgresql.PGConnection;
5758
import org.postgresql.PGStatement;
59+
import org.postgresql.core.BaseConnection;
60+
import org.postgresql.jdbc4.Jdbc4Array;
5861
import org.postgresql.util.PGInterval;
5962
import org.postgresql.util.PGobject;
6063

@@ -203,25 +206,50 @@ protected void setTimestampParameter(final ThreadContext context,
203206
private static final ByteList INTERVAL =
204207
new ByteList( new byte[] { 'i','n','t','e','r','v','a','l' }, false );
205208

209+
private static final ByteList ARRAY_END = new ByteList( new byte[] { '[',']' }, false );
210+
206211
@Override
207212
protected void setStringParameter(final ThreadContext context,
208213
final Connection connection, final PreparedStatement statement,
209214
final int index, final IRubyObject value,
210215
final IRubyObject column, final int type) throws SQLException {
211-
if ( value.isNil() ) statement.setNull(index, Types.VARCHAR);
216+
final RubyString sqlType;
217+
if ( column != null && ! column.isNil() ) {
218+
sqlType = (RubyString) column.callMethod(context, "sql_type");
219+
}
212220
else {
213-
if ( column != null && ! column.isNil() ) {
214-
final RubyString sqlType = column.callMethod(context, "sql_type").asString();
221+
sqlType = null;
222+
}
215223

224+
if ( value.isNil() ) {
225+
if ( rawArrayType == Boolean.TRUE ) { // array's type is :string
226+
if ( sqlType != null && sqlType.getByteList().endsWith( ARRAY_END ) ) {
227+
statement.setNull(index, Types.ARRAY); return;
228+
}
229+
statement.setNull(index, type); return;
230+
}
231+
statement.setNull(index, Types.VARCHAR);
232+
}
233+
else {
234+
final String valueStr = value.asString().toString();
235+
if ( sqlType != null ) {
236+
if ( rawArrayType == Boolean.TRUE && sqlType.getByteList().endsWith( ARRAY_END ) ) {
237+
final Array valueArr = new Jdbc4Array(connection.unwrap(BaseConnection.class), oidType(column), valueStr);
238+
statement.setArray(index, valueArr); return;
239+
}
216240
if ( sqlType.getByteList().startsWith( INTERVAL ) ) {
217-
statement.setObject( index, new PGInterval( value.asString().toString() ) );
218-
return;
241+
statement.setObject( index, new PGInterval( valueStr ) ); return;
219242
}
220243
}
221-
statement.setString( index, value.asString().toString() );
244+
statement.setString( index, valueStr );
222245
}
223246
}
224247

248+
private static int oidType(final IRubyObject column) {
249+
final IRubyObject oid_type = column.getInstanceVariables().getInstanceVariable("@oid_type");
250+
return RubyFixnum.fix2int(oid_type);
251+
}
252+
225253
@Override
226254
protected void setObjectParameter(final ThreadContext context,
227255
final Connection connection, final PreparedStatement statement,

0 commit comments

Comments
 (0)