Skip to content

Commit

Permalink
Merge pull request #15088 from kares/shareable-modules
Browse files Browse the repository at this point in the history
[postgres] remove (outer) sub-class declarations with nested modules/classes
  • Loading branch information
rafaelfranca committed May 15, 2014
2 parents a11e2fc + c0bfc3f commit 095c42e
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ActiveRecord
module ConnectionAdapters
class PostgreSQLColumn < Column
module PostgreSQL
module ArrayParser

DOUBLE_QUOTE = '"'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ActiveRecord
module ConnectionAdapters
class PostgreSQLColumn < Column
module PostgreSQL
module Cast
def point_to_string(point)
"(#{point[0]},#{point[1]})"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
require 'active_record/connection_adapters/postgresql/cast'

module ActiveRecord
module ConnectionAdapters
# PostgreSQL-specific extensions to column definitions in a table.
class PostgreSQLColumn < Column #:nodoc:

attr_accessor :array

def initialize(name, default, oid_type, sql_type = nil, null = true)
Expand Down Expand Up @@ -29,7 +32,7 @@ def text?

# :stopdoc:
class << self
include PostgreSQLColumn::Cast
include PostgreSQL::Cast

# Loads pg_array_parser if available. String parsing can be
# performed quicker by a native extension, which will not create
Expand All @@ -40,7 +43,7 @@ class << self
include PgArrayParser
rescue LoadError
require 'active_record/connection_adapters/postgresql/array_parser'
include PostgreSQLColumn::ArrayParser
include PostgreSQL::ArrayParser
end

attr_accessor :money_precision
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ActiveRecord
module ConnectionAdapters
class PostgreSQLAdapter < AbstractAdapter
module PostgreSQL
module DatabaseStatements
def explain(arel, binds = [])
sql = "EXPLAIN #{to_sql(arel, binds)}"
Expand Down Expand Up @@ -94,6 +94,11 @@ def create
super.insert
end

# The internal PostgreSQL identifier of the money data type.
MONEY_COLUMN_TYPE_OID = 790 #:nodoc:
# The internal PostgreSQL identifier of the BYTEA data type.
BYTEA_COLUMN_TYPE_OID = 17 #:nodoc:

# create a 2D array representing the result set
def result_as_array(res) #:nodoc:
# check if we have any binary column and if they need escaping
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require 'active_record/connection_adapters/abstract_adapter'

module ActiveRecord
module ConnectionAdapters
class PostgreSQLAdapter < AbstractAdapter
module PostgreSQL
module OID
class Type
def type; end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ActiveRecord
module ConnectionAdapters
class PostgreSQLAdapter < AbstractAdapter
module PostgreSQL
module Quoting
# Escapes binary strings for bytea input to the database.
def escape_bytea(value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ActiveRecord
module ConnectionAdapters
class PostgreSQLAdapter < AbstractAdapter
module PostgreSQL
module ReferentialIntegrity
def supports_disable_referential_integrity? #:nodoc:
true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ActiveRecord
module ConnectionAdapters
class PostgreSQLAdapter < AbstractAdapter
module PostgreSQL
class SchemaCreation < AbstractAdapter::SchemaCreation
private

Expand Down Expand Up @@ -33,10 +33,6 @@ def add_column_options!(sql, options)
end
end

def schema_creation
SchemaCreation.new self
end

module SchemaStatements
# Drops the database specified on the +name+ attribute
# and creates it again using the provided +options+.
Expand Down Expand Up @@ -101,7 +97,7 @@ def tables(name = nil)
# If the schema is not specified as part of +name+ then it will only find tables within
# the current schema search path (regardless of permissions to access tables in other schemas)
def table_exists?(name)
schema, table = Utils.extract_schema_and_table(name.to_s)
schema, table = extract_schema_and_table(name.to_s)
return false unless table

exec_query(<<-SQL, 'SCHEMA').rows.first[0].to_i > 0
Expand Down Expand Up @@ -492,6 +488,24 @@ def columns_for_distinct(columns, orders) #:nodoc:

[super, *order_columns].join(', ')
end

private

# Returns an array of <tt>[schema_name, table_name]</tt> extracted from +name+.
# +schema_name+ is nil if not specified in +name+.
# +schema_name+ and +table_name+ exclude surrounding quotes (regardless of whether provided in +name+)
# +name+ supports the range of schema/table references understood by PostgreSQL, for example:
#
# * <tt>table_name</tt>
# * <tt>"table.name"</tt>
# * <tt>schema_name.table_name</tt>
# * <tt>schema_name."table.name"</tt>
# * <tt>"schema.name"."table name"</tt>
def extract_schema_and_table(name)
table, schema = name.scan(/[^".\s]+|"[^"]*"/)[0..1].collect{|m| m.gsub(/(^"|"$)/,'') }.reverse
[schema, table]
end

end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
require 'active_record/connection_adapters/abstract_adapter'
require 'active_record/connection_adapters/statement_pool'

require 'active_record/connection_adapters/postgresql/column'
require 'active_record/connection_adapters/postgresql/oid'
require 'active_record/connection_adapters/postgresql/cast'
require 'active_record/connection_adapters/postgresql/array_parser'
require 'active_record/connection_adapters/postgresql/quoting'
require 'active_record/connection_adapters/postgresql/referential_integrity'
require 'active_record/connection_adapters/postgresql/schema_statements'
require 'active_record/connection_adapters/postgresql/database_statements'
require 'active_record/connection_adapters/postgresql/referential_integrity'
require 'active_record/connection_adapters/postgresql/column'

require 'arel/visitors/bind_visitor'

# Make sure we're using pg high enough for PGResult#values
Expand Down Expand Up @@ -44,6 +44,7 @@ def postgresql_connection(config)
end

module ConnectionAdapters

# The PostgreSQL adapter works with the native C (https://bitbucket.org/ged/ruby-pg) driver.
#
# Options:
Expand Down Expand Up @@ -72,6 +73,7 @@ module ConnectionAdapters
# In addition, default connection parameters of libpq can be set per environment variables.
# See http://www.postgresql.org/docs/9.1/static/libpq-envars.html .
class PostgreSQLAdapter < AbstractAdapter

class ColumnDefinition < ActiveRecord::ConnectionAdapters::ColumnDefinition
attr_accessor :array
end
Expand Down Expand Up @@ -238,17 +240,23 @@ class Table < ActiveRecord::ConnectionAdapters::Table
citext: { name: "citext" }
}

include Quoting
include ReferentialIntegrity
include SchemaStatements
include DatabaseStatements
OID = PostgreSQL::OID #:nodoc:

include PostgreSQL::Quoting
include PostgreSQL::ReferentialIntegrity
include PostgreSQL::SchemaStatements
include PostgreSQL::DatabaseStatements
include Savepoints

# Returns 'PostgreSQL' as adapter name for identification purposes.
def adapter_name
ADAPTER_NAME
end

def schema_creation
PostgreSQL::SchemaCreation.new self
end

# Adds `:array` option to the default set provided by the
# AbstractAdapter
def prepare_column_options(column, types)
Expand Down Expand Up @@ -494,25 +502,6 @@ def session_auth=(user)
exec_query "SET SESSION AUTHORIZATION #{user}"
end

module Utils
extend self

# Returns an array of <tt>[schema_name, table_name]</tt> extracted from +name+.
# +schema_name+ is nil if not specified in +name+.
# +schema_name+ and +table_name+ exclude surrounding quotes (regardless of whether provided in +name+)
# +name+ supports the range of schema/table references understood by PostgreSQL, for example:
#
# * <tt>table_name</tt>
# * <tt>"table.name"</tt>
# * <tt>schema_name.table_name</tt>
# * <tt>schema_name."table.name"</tt>
# * <tt>"schema.name"."table name"</tt>
def extract_schema_and_table(name)
table, schema = name.scan(/[^".\s]+|"[^"]*"/)[0..1].collect{|m| m.gsub(/(^"|"$)/,'') }.reverse
[schema, table]
end
end

def use_insert_returning?
@use_insert_returning
end
Expand Down Expand Up @@ -663,11 +652,6 @@ def prepare_statement(sql)
@statements[sql_key]
end

# The internal PostgreSQL identifier of the money data type.
MONEY_COLUMN_TYPE_OID = 790 #:nodoc:
# The internal PostgreSQL identifier of the BYTEA data type.
BYTEA_COLUMN_TYPE_OID = 17 #:nodoc:

# Connects to a PostgreSQL server and sets up the adapter depending on the
# connected server's characteristics.
def connect
Expand Down
15 changes: 15 additions & 0 deletions activerecord/test/cases/adapters/postgresql/schema_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,21 @@ def test_schema_exists?
end
end

def test_extract_schema_and_table
{
%(table_name) => [nil,'table_name'],
%("table.name") => [nil,'table.name'],
%(schema.table_name) => %w{schema table_name},
%("schema".table_name) => %w{schema table_name},
%(schema."table_name") => %w{schema table_name},
%("schema"."table_name") => %w{schema table_name},
%("even spaces".table) => ['even spaces','table'],
%(schema."table.name") => ['schema', 'table.name']
}.each do |given, expect|
assert_equal expect, @connection.send(:extract_schema_and_table, given)
end
end

private
def columns(table_name)
@connection.send(:column_definitions, table_name).map do |name, type, default|
Expand Down
20 changes: 0 additions & 20 deletions activerecord/test/cases/adapters/postgresql/utils_test.rb

This file was deleted.

0 comments on commit 095c42e

Please sign in to comment.