|
42 | 42 | import org.jruby.RubyArray; |
43 | 43 | import org.jruby.RubyBoolean; |
44 | 44 | import org.jruby.RubyClass; |
| 45 | +import org.jruby.RubyFixnum; |
45 | 46 | import org.jruby.RubyFloat; |
46 | 47 | import org.jruby.RubyHash; |
47 | 48 | import org.jruby.RubyIO; |
|
55 | 56 |
|
56 | 57 | import org.postgresql.PGConnection; |
57 | 58 | import org.postgresql.PGStatement; |
| 59 | +import org.postgresql.core.BaseConnection; |
| 60 | +import org.postgresql.jdbc4.Jdbc4Array; |
58 | 61 | import org.postgresql.util.PGInterval; |
59 | 62 | import org.postgresql.util.PGobject; |
60 | 63 |
|
@@ -203,25 +206,50 @@ protected void setTimestampParameter(final ThreadContext context, |
203 | 206 | private static final ByteList INTERVAL = |
204 | 207 | new ByteList( new byte[] { 'i','n','t','e','r','v','a','l' }, false ); |
205 | 208 |
|
| 209 | + private static final ByteList ARRAY_END = new ByteList( new byte[] { '[',']' }, false ); |
| 210 | + |
206 | 211 | @Override |
207 | 212 | protected void setStringParameter(final ThreadContext context, |
208 | 213 | final Connection connection, final PreparedStatement statement, |
209 | 214 | final int index, final IRubyObject value, |
210 | 215 | 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 | + } |
212 | 220 | else { |
213 | | - if ( column != null && ! column.isNil() ) { |
214 | | - final RubyString sqlType = column.callMethod(context, "sql_type").asString(); |
| 221 | + sqlType = null; |
| 222 | + } |
215 | 223 |
|
| 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 | + } |
216 | 240 | if ( sqlType.getByteList().startsWith( INTERVAL ) ) { |
217 | | - statement.setObject( index, new PGInterval( value.asString().toString() ) ); |
218 | | - return; |
| 241 | + statement.setObject( index, new PGInterval( valueStr ) ); return; |
219 | 242 | } |
220 | 243 | } |
221 | | - statement.setString( index, value.asString().toString() ); |
| 244 | + statement.setString( index, valueStr ); |
222 | 245 | } |
223 | 246 | } |
224 | 247 |
|
| 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 | + |
225 | 253 | @Override |
226 | 254 | protected void setObjectParameter(final ThreadContext context, |
227 | 255 | final Connection connection, final PreparedStatement statement, |
|
0 commit comments