Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make :schema__table.* work correctly

This shouldn't have been as difficult as it was, but the refactoring
done was for the best:

Move quote_schema_table and schema_and_table from Database to
Dataset, putting proxy methods in Database.

Make Database#default_schema a real attribute that can be used by
any database (but should remain nil for most).

Allow Database.new to take a :default_schema option, and document
it and other options that can be used.
  • Loading branch information...
commit 4a23245d3126e5830c8c1c1ecacc4150ad42d011 1 parent c30e8e4
@jeremyevans authored
View
5 lib/sequel_core/adapters/shared/postgres.rb
@@ -133,11 +133,6 @@ def default_schema
@default_schema ||= :public
end
- # Set a new default schema to use.
- def default_schema=(schema)
- @default_schema = schema
- end
-
# Remove the cached entries for primary keys and sequences when dropping a table.
def drop_table(*names)
names.each do |name|
View
31 lib/sequel_core/database.rb
@@ -32,6 +32,9 @@ class Database
# Whether to upcase identifiers (columns and tables) by default
@@upcase_identifiers = nil
+ # The default schema to use
+ attr_accessor :default_schema
+
# Array of SQL loggers to use for this database
attr_accessor :loggers
@@ -54,12 +57,21 @@ class Database
# options hash.
#
# Sequel::Database is an abstract class that is not useful by itself.
+ #
+ # Takes the following options:
+ # * :default_schema : The default schema to use, should generally be nil
+ # * :loggers : An array of loggers to use.
+ # * :quote_identifiers : Whether to quote identifiers
+ # * :single_threaded : Whether to use a single-threaded connection pool
+ #
+ # All options given are also passed to the ConnectionPool.
def initialize(opts = {}, &block)
@opts = opts
@quote_identifiers = opts.include?(:quote_identifiers) ? opts[:quote_identifiers] : @@quote_identifiers
@single_threaded = opts.include?(:single_threaded) ? opts[:single_threaded] : @@single_threaded
@schemas = nil
+ @default_schema = opts[:default_schema]
@prepared_statements = {}
@transactions = []
@pool = (@single_threaded ? SingleThreadedPool : ConnectionPool).new(connection_pool_default_options.merge(opts), &block)
@@ -515,11 +527,6 @@ def connection_pool_default_options
{}
end
- # Sequel doesn't use database schema's by default.
- def default_schema
- nil
- end
-
# SQL to ROLLBACK a transaction.
def rollback_transaction_sql
SQL_ROLLBACK
@@ -539,19 +546,7 @@ def raise_error(exception, opts={})
# Split the schema information from the table
def schema_and_table(table_name)
- case table_name
- when Symbol
- s, t, a = dataset.send(:split_symbol, table_name)
- [s||default_schema, t]
- when SQL::QualifiedIdentifier
- [table_name.table, table_name.column]
- when SQL::Identifier
- [default_schema, table_name.value]
- when String
- [default_schema, table_name]
- else
- raise Error, 'table_name should be a Symbol, SQL::QualifiedIdentifier, SQL::Identifier, or String'
- end
+ schema_utility_dataset.schema_and_table(table_name)
end
# Return the options for the given server by merging the generic
View
27 lib/sequel_core/dataset/sql.rb
@@ -47,7 +47,7 @@ def case_expression_sql(ce)
# SQL fragment for specifying all columns in a given table.
def column_all_sql(ca)
- "#{quote_identifier(ca.table)}.*"
+ "#{quote_schema_table(ca.table)}.*"
end
# SQL fragment for complex expressions
@@ -573,6 +573,13 @@ def quote_identifier(name)
end
alias_method :quote_column_ref, :quote_identifier
+ # Separates the schema from the table and returns a string with them
+ # quoted (if quoting identifiers)
+ def quote_schema_table(table)
+ schema, table = schema_and_table(table)
+ "#{"#{quote_identifier(schema)}." if schema}#{quote_identifier(table)}"
+ end
+
# This method quotes the given name with the SQL standard double quote.
# should be overridden by subclasses to provide quoting not matching the
# SQL standard, such as backtick (used by MySQL and SQLite).
@@ -587,6 +594,24 @@ def reverse_order(*order)
end
alias_method :reverse, :reverse_order
+ # Split the schema information from the table
+ def schema_and_table(table_name)
+ sch = db.default_schema if db
+ case table_name
+ when Symbol
+ s, t, a = split_symbol(table_name)
+ [s||sch, t]
+ when SQL::QualifiedIdentifier
+ [table_name.table, table_name.column]
+ when SQL::Identifier
+ [sch, table_name.value]
+ when String
+ [sch, table_name]
+ else
+ raise Error, 'table_name should be a Symbol, SQL::QualifiedIdentifier, SQL::Identifier, or String'
+ end
+ end
+
# Returns a copy of the dataset with the columns selected changed
# to the given columns.
def select(*columns)
View
4 lib/sequel_core/schema/sql.rb
@@ -187,9 +187,9 @@ def on_delete_clause(action)
end
end
+ # Proxy the quote_schema_table method to the dataset
def quote_schema_table(table)
- schema, table = schema_and_table(table)
- "#{"#{quote_identifier(schema)}." if schema}#{quote_identifier(table)}"
+ schema_utility_dataset.quote_schema_table(table)
end
# Proxy the quote_identifier method to the dataset, used for quoting tables and columns.
View
5 spec/sequel_core/core_sql_spec.rb
@@ -259,6 +259,11 @@ def quoted_identifier(c); "`#{c}`"; end
:xyz.*(3).to_s(@ds).should == '(xyz * 3)'
:abc.*(5).to_s(@ds).should == '(abc * 5)'
end
+
+ specify "should support qualified symbols if no argument" do
+ :xyz__abc.*.to_s(@ds).should == 'xyz.abc.*'
+ end
+
end
context "Symbol" do
Please sign in to comment.
Something went wrong with that request. Please try again.