Skip to content

Commit f9f748a

Browse files
authored
Merge pull request #997 from dr-itz/50-dev
50.x fixes
2 parents ebea905 + 0a3ddb8 commit f9f748a

File tree

9 files changed

+27
-99
lines changed

9 files changed

+27
-99
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ rvm:
4242
- jruby-9.1.16.0
4343
jdk:
4444
- openjdk8
45+
addons:
46+
postgresql: "9.4"
4547
env:
4648
- DB=mysql2 PREPARED_STATEMENTS=false
4749
- DB=mysql2 PREPARED_STATEMENTS=true

lib/arjdbc/abstract/database_statements.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def exec_query(sql, name = nil, binds = NO_BINDS, prepare: false)
2626
log(sql, name) { @connection.execute_query(sql) }
2727
else
2828
log(sql, name, binds) do
29-
# It seems that #supports_statement_cache? is defined but isn't checked before setting "prepare" (AR 5.0)
30-
cached_statement = fetch_cached_statement(sql) if prepare && supports_statement_cache?
29+
# this is different from normal AR that always caches
30+
cached_statement = fetch_cached_statement(sql) if prepare && @jdbc_statement_cache_enabled
3131
@connection.execute_prepared_query(sql, binds, cached_statement)
3232
end
3333
end

lib/arjdbc/abstract/statement_cache.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def initialize(*args) # (connection, logger, config)
2323
# Only say we support the statement cache if we are using prepared statements
2424
# and have a max number of statements defined
2525
statement_limit = self.class.type_cast_config_to_integer(config[:statement_limit])
26-
@jdbc_statement_cache_enabled = config[:prepared_statements] && (statement_limit.nil? || statement_limit > 0)
26+
@jdbc_statement_cache_enabled = prepared_statements && (statement_limit.nil? || statement_limit > 0)
2727

2828
@statements = StatementPool.new(statement_limit) # AR (5.0) expects this to be stored as @statements
2929
end
@@ -34,11 +34,11 @@ def clear_cache!
3434
end
3535

3636
def delete_cached_statement(sql)
37-
@statements.delete(cached_statement_key(sql))
37+
@statements.delete(sql_key(sql))
3838
end
3939

4040
def fetch_cached_statement(sql)
41-
@statements[cached_statement_key(sql)] ||= @connection.connection.prepare_statement(sql)
41+
@statements[sql_key(sql)] ||= @connection.prepare_statement(sql)
4242
end
4343

4444
def supports_statement_cache?
@@ -49,7 +49,7 @@ def supports_statement_cache?
4949

5050
# This should be overridden by the adapter if the sql itself
5151
# is not enough to make the key unique
52-
def cached_statement_key(sql)
52+
def sql_key(sql)
5353
sql
5454
end
5555

lib/arjdbc/postgresql/_bc_time_cast_patch.rb

Lines changed: 0 additions & 24 deletions
This file was deleted.

lib/arjdbc/postgresql/adapter.rb

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -522,61 +522,6 @@ def truncate(table_name, name = nil)
522522
execute "TRUNCATE TABLE #{quote_table_name(table_name)}", name
523523
end
524524

525-
# Returns an array of indexes for the given table.
526-
def indexes(table_name, name = nil)
527-
# FIXME: AR version => table = Utils.extract_schema_qualified_name(table_name.to_s)
528-
schema, table = extract_schema_and_table(table_name.to_s)
529-
530-
result = query(<<-SQL, 'SCHEMA')
531-
SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid,
532-
pg_catalog.obj_description(i.oid, 'pg_class') AS comment,
533-
(SELECT COUNT(*) FROM pg_opclass o
534-
JOIN (SELECT unnest(string_to_array(d.indclass::text, ' '))::int oid) c
535-
ON o.oid = c.oid WHERE o.opcdefault = 'f')
536-
FROM pg_class t
537-
INNER JOIN pg_index d ON t.oid = d.indrelid
538-
INNER JOIN pg_class i ON d.indexrelid = i.oid
539-
LEFT JOIN pg_namespace n ON n.oid = i.relnamespace
540-
WHERE i.relkind = 'i'
541-
AND d.indisprimary = 'f'
542-
AND t.relname = '#{table}'
543-
AND n.nspname = #{schema ? "'#{schema}'" : 'ANY (current_schemas(false))'}
544-
ORDER BY i.relname
545-
SQL
546-
547-
result.map do |row|
548-
index_name = row[0]
549-
# FIXME: These values [1,2] are returned in a different format than AR expects, maybe we could update it on the Java side to be more accurate
550-
unique = row[1].is_a?(String) ? row[1] == 't' : row[1] # JDBC gets us a boolean
551-
indkey = row[2].is_a?(Java::OrgPostgresqlUtil::PGobject) ? row[2].value : row[2]
552-
indkey = indkey.split(" ").map(&:to_i)
553-
inddef = row[3]
554-
oid = row[4]
555-
comment = row[5]
556-
opclass = row[6]
557-
558-
using, expressions, where = inddef.scan(/ USING (\w+?) \((.+?)\)(?: WHERE (.+))?\z/).flatten
559-
560-
if indkey.include?(0) || opclass > 0
561-
columns = expressions
562-
else
563-
columns = Hash[query(<<-SQL.strip_heredoc, "SCHEMA")].values_at(*indkey).compact
564-
SELECT a.attnum, a.attname
565-
FROM pg_attribute a
566-
WHERE a.attrelid = #{oid}
567-
AND a.attnum IN (#{indkey.join(",")})
568-
SQL
569-
570-
# add info on sort order for columns (only desc order is explicitly specified, asc is the default)
571-
orders = Hash[
572-
expressions.scan(/(\w+) DESC/).flatten.map { |order_column| [order_column, :desc] }
573-
]
574-
end
575-
576-
IndexDefinition.new(table_name, index_name, unique, columns, [], orders, where, nil, using.to_sym, comment.presence)
577-
end.compact
578-
end
579-
580525
# @private
581526
def column_name_for_operation(operation, node)
582527
case operation
@@ -682,8 +627,6 @@ class PostgreSQLAdapter < AbstractAdapter
682627
require 'arjdbc/postgresql/oid_types'
683628
include ::ArJdbc::PostgreSQL::OIDTypes
684629

685-
load 'arjdbc/postgresql/_bc_time_cast_patch.rb'
686-
687630
include ::ArJdbc::PostgreSQL::ColumnHelpers
688631

689632
include ::ArJdbc::Util::QuotedCache
@@ -736,7 +679,7 @@ def jdbc_connection_class(spec)
736679

737680
# Prepared statements aren't schema aware so we need to make sure we
738681
# store different PreparedStatement objects for different schemas
739-
def cached_statement_key(sql)
682+
def sql_key(sql)
740683
"#{schema_search_path}-#{sql}"
741684
end
742685

src/java/arjdbc/jdbc/RubyJdbcConnection.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,22 @@ public IRubyObject call(final Connection connection) throws SQLException {
11121112
});
11131113
}
11141114

1115+
/**
1116+
* Prepares a query, returns a wrapped PreparedStatement. This takes care of exception wrapping
1117+
* @param context which context this method is executing on.
1118+
* @param sql the query to prepare-
1119+
* @return a Ruby <code>PreparedStatement</code>
1120+
*/
1121+
@JRubyMethod(required = 1)
1122+
public IRubyObject prepare_statement(final ThreadContext context, final IRubyObject sql) {
1123+
return withConnection(context, new Callable<IRubyObject>() {
1124+
public IRubyObject call(Connection connection) throws SQLException {
1125+
final String query = sql.convertToString().getUnicodeValue();
1126+
return JavaUtil.convertJavaToRuby(context.runtime, connection.prepareStatement(query));
1127+
}
1128+
});
1129+
}
1130+
11151131
// Called from exec_query in abstract/database_statements
11161132
/**
11171133
* Executes a query and returns the (AR) result. There are three parameters:

test/db/postgresql/simple_test.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -292,17 +292,6 @@ def test_bc_timestamp
292292
time = Time.utc('0000-01-01') - 1.hour - 1.minute - 1.second
293293
db_type = DbType.create!(:sample_timestamp => time)
294294

295-
if current_connection_config[:prepared_statements].to_s == 'true'
296-
pend "Likely a JRuby/Java thing - this test is failing bad: check #516"
297-
end
298-
# if current_connection_config[:insert_returning].to_s == 'true'
299-
# pend "BC timestamps not-handled right with INSERT RETURNIG ..."
300-
# end unless ar_version('4.2')
301-
#
302-
if defined?(JRUBY_VERSION) && JRUBY_VERSION < '9.2'
303-
pend "BC timestamp handling isn't working properly through JRuby 9.1 (its to be fixed in 9.2)"
304-
end
305-
306295
assert_equal time, db_type.reload.sample_timestamp
307296

308297
date = DateTime.parse('0000-01-01T00:00:00+00:00') - 1.hour - 1.minute - 1.second
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
exclude :test_connection_options, 'Connection options not supported'
22
exclude :test_table_alias_length_logs_name, 'We reuse max identifer length for this so no query is made to be logged for the rails test'
3+
exclude :test_statement_key_is_logged, 'test uses $1 instead of ? for bind'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exclude :test_auth_with_bind, 'test uses $1 instead of ? for bind'

0 commit comments

Comments
 (0)