colincasey-sequel 2.10.0 → 2.10.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +7 -1
- data/doc/advanced_associations.rdoc +614 -0
- data/doc/cheat_sheet.rdoc +223 -0
- data/doc/dataset_filtering.rdoc +158 -0
- data/doc/prepared_statements.rdoc +104 -0
- data/doc/release_notes/1.0.txt +38 -0
- data/doc/release_notes/1.1.txt +143 -0
- data/doc/release_notes/1.3.txt +101 -0
- data/doc/release_notes/1.4.0.txt +53 -0
- data/doc/release_notes/1.5.0.txt +155 -0
- data/doc/release_notes/2.0.0.txt +298 -0
- data/doc/release_notes/2.1.0.txt +271 -0
- data/doc/release_notes/2.10.0.txt +328 -0
- data/doc/release_notes/2.2.0.txt +253 -0
- data/doc/release_notes/2.3.0.txt +88 -0
- data/doc/release_notes/2.4.0.txt +106 -0
- data/doc/release_notes/2.5.0.txt +137 -0
- data/doc/release_notes/2.6.0.txt +157 -0
- data/doc/release_notes/2.7.0.txt +166 -0
- data/doc/release_notes/2.8.0.txt +171 -0
- data/doc/release_notes/2.9.0.txt +97 -0
- data/doc/schema.rdoc +29 -0
- data/doc/sharding.rdoc +113 -0
- data/lib/sequel.rb +1 -0
- data/lib/sequel_core/adapters/ado.rb +89 -0
- data/lib/sequel_core/adapters/db2.rb +143 -0
- data/lib/sequel_core/adapters/dbi.rb +112 -0
- data/lib/sequel_core/adapters/do/mysql.rb +38 -0
- data/lib/sequel_core/adapters/do/postgres.rb +92 -0
- data/lib/sequel_core/adapters/do/sqlite.rb +31 -0
- data/lib/sequel_core/adapters/do.rb +205 -0
- data/lib/sequel_core/adapters/firebird.rb +298 -0
- data/lib/sequel_core/adapters/informix.rb +85 -0
- data/lib/sequel_core/adapters/jdbc/h2.rb +69 -0
- data/lib/sequel_core/adapters/jdbc/mysql.rb +66 -0
- data/lib/sequel_core/adapters/jdbc/oracle.rb +23 -0
- data/lib/sequel_core/adapters/jdbc/postgresql.rb +113 -0
- data/lib/sequel_core/adapters/jdbc/sqlite.rb +43 -0
- data/lib/sequel_core/adapters/jdbc.rb +491 -0
- data/lib/sequel_core/adapters/mysql.rb +369 -0
- data/lib/sequel_core/adapters/odbc.rb +174 -0
- data/lib/sequel_core/adapters/openbase.rb +68 -0
- data/lib/sequel_core/adapters/oracle.rb +107 -0
- data/lib/sequel_core/adapters/postgres.rb +456 -0
- data/lib/sequel_core/adapters/shared/ms_access.rb +110 -0
- data/lib/sequel_core/adapters/shared/mssql.rb +102 -0
- data/lib/sequel_core/adapters/shared/mysql.rb +325 -0
- data/lib/sequel_core/adapters/shared/oracle.rb +61 -0
- data/lib/sequel_core/adapters/shared/postgres.rb +715 -0
- data/lib/sequel_core/adapters/shared/progress.rb +31 -0
- data/lib/sequel_core/adapters/shared/sqlite.rb +265 -0
- data/lib/sequel_core/adapters/sqlite.rb +248 -0
- data/lib/sequel_core/connection_pool.rb +258 -0
- data/lib/sequel_core/core_ext.rb +217 -0
- data/lib/sequel_core/core_sql.rb +202 -0
- data/lib/sequel_core/database/schema.rb +164 -0
- data/lib/sequel_core/database.rb +691 -0
- data/lib/sequel_core/dataset/callback.rb +13 -0
- data/lib/sequel_core/dataset/convenience.rb +237 -0
- data/lib/sequel_core/dataset/pagination.rb +96 -0
- data/lib/sequel_core/dataset/prepared_statements.rb +220 -0
- data/lib/sequel_core/dataset/query.rb +41 -0
- data/lib/sequel_core/dataset/schema.rb +15 -0
- data/lib/sequel_core/dataset/sql.rb +1010 -0
- data/lib/sequel_core/dataset/stored_procedures.rb +75 -0
- data/lib/sequel_core/dataset/unsupported.rb +43 -0
- data/lib/sequel_core/dataset.rb +511 -0
- data/lib/sequel_core/deprecated.rb +26 -0
- data/lib/sequel_core/exceptions.rb +44 -0
- data/lib/sequel_core/migration.rb +212 -0
- data/lib/sequel_core/object_graph.rb +230 -0
- data/lib/sequel_core/pretty_table.rb +71 -0
- data/lib/sequel_core/schema/generator.rb +320 -0
- data/lib/sequel_core/schema/sql.rb +325 -0
- data/lib/sequel_core/schema.rb +2 -0
- data/lib/sequel_core/sql.rb +887 -0
- data/lib/sequel_core/version.rb +11 -0
- data/lib/sequel_core.rb +172 -0
- data/lib/sequel_model/association_reflection.rb +267 -0
- data/lib/sequel_model/associations.rb +499 -0
- data/lib/sequel_model/base.rb +523 -0
- data/lib/sequel_model/caching.rb +82 -0
- data/lib/sequel_model/dataset_methods.rb +26 -0
- data/lib/sequel_model/eager_loading.rb +370 -0
- data/lib/sequel_model/exceptions.rb +7 -0
- data/lib/sequel_model/hooks.rb +101 -0
- data/lib/sequel_model/inflector.rb +281 -0
- data/lib/sequel_model/plugins.rb +62 -0
- data/lib/sequel_model/record.rb +568 -0
- data/lib/sequel_model/schema.rb +49 -0
- data/lib/sequel_model/validations.rb +429 -0
- data/lib/sequel_model.rb +91 -0
- data/spec/adapters/ado_spec.rb +46 -0
- data/spec/adapters/firebird_spec.rb +376 -0
- data/spec/adapters/informix_spec.rb +96 -0
- data/spec/adapters/mysql_spec.rb +881 -0
- data/spec/adapters/oracle_spec.rb +244 -0
- data/spec/adapters/postgres_spec.rb +687 -0
- data/spec/adapters/spec_helper.rb +10 -0
- data/spec/adapters/sqlite_spec.rb +555 -0
- data/spec/integration/dataset_test.rb +134 -0
- data/spec/integration/eager_loader_test.rb +696 -0
- data/spec/integration/prepared_statement_test.rb +130 -0
- data/spec/integration/schema_test.rb +180 -0
- data/spec/integration/spec_helper.rb +58 -0
- data/spec/integration/type_test.rb +96 -0
- data/spec/rcov.opts +6 -0
- data/spec/sequel_core/connection_pool_spec.rb +526 -0
- data/spec/sequel_core/core_ext_spec.rb +156 -0
- data/spec/sequel_core/core_sql_spec.rb +522 -0
- data/spec/sequel_core/database_spec.rb +1188 -0
- data/spec/sequel_core/dataset_spec.rb +3481 -0
- data/spec/sequel_core/expression_filters_spec.rb +363 -0
- data/spec/sequel_core/migration_spec.rb +261 -0
- data/spec/sequel_core/object_graph_spec.rb +272 -0
- data/spec/sequel_core/pretty_table_spec.rb +58 -0
- data/spec/sequel_core/schema_generator_spec.rb +167 -0
- data/spec/sequel_core/schema_spec.rb +780 -0
- data/spec/sequel_core/spec_helper.rb +55 -0
- data/spec/sequel_core/version_spec.rb +7 -0
- data/spec/sequel_model/association_reflection_spec.rb +93 -0
- data/spec/sequel_model/associations_spec.rb +1767 -0
- data/spec/sequel_model/base_spec.rb +419 -0
- data/spec/sequel_model/caching_spec.rb +215 -0
- data/spec/sequel_model/dataset_methods_spec.rb +78 -0
- data/spec/sequel_model/eager_loading_spec.rb +1165 -0
- data/spec/sequel_model/hooks_spec.rb +485 -0
- data/spec/sequel_model/inflector_spec.rb +119 -0
- data/spec/sequel_model/model_spec.rb +588 -0
- data/spec/sequel_model/plugins_spec.rb +80 -0
- data/spec/sequel_model/record_spec.rb +1184 -0
- data/spec/sequel_model/schema_spec.rb +90 -0
- data/spec/sequel_model/spec_helper.rb +78 -0
- data/spec/sequel_model/validations_spec.rb +1067 -0
- data/spec/spec.opts +0 -0
- data/spec/spec_config.rb.example +10 -0
- metadata +177 -3
@@ -0,0 +1,102 @@
|
|
1
|
+
module Sequel
|
2
|
+
module MSSQL
|
3
|
+
module DatabaseMethods
|
4
|
+
AUTO_INCREMENT = 'IDENTITY(1,1)'.freeze
|
5
|
+
SQL_BEGIN = "BEGIN TRANSACTION".freeze
|
6
|
+
SQL_COMMIT = "COMMIT TRANSACTION".freeze
|
7
|
+
SQL_ROLLBACK = "ROLLBACK TRANSACTION".freeze
|
8
|
+
|
9
|
+
def auto_increment_sql
|
10
|
+
AUTO_INCREMENT
|
11
|
+
end
|
12
|
+
|
13
|
+
def dataset(opts = nil)
|
14
|
+
ds = super
|
15
|
+
ds.extend(DatasetMethods)
|
16
|
+
ds
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
# SQL to BEGIN a transaction.
|
22
|
+
def begin_transaction_sql
|
23
|
+
SQL_BEGIN
|
24
|
+
end
|
25
|
+
|
26
|
+
# SQL to COMMIT a transaction.
|
27
|
+
def commit_transaction_sql
|
28
|
+
SQL_COMMIT
|
29
|
+
end
|
30
|
+
|
31
|
+
# SQL to ROLLBACK a transaction.
|
32
|
+
def rollback_transaction_sql
|
33
|
+
SQL_ROLLBACK
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
module DatasetMethods
|
38
|
+
include Dataset::UnsupportedIntersectExcept
|
39
|
+
|
40
|
+
SELECT_CLAUSE_ORDER = %w'limit distinct columns from with join where group order having compounds'.freeze
|
41
|
+
|
42
|
+
def complex_expression_sql(op, args)
|
43
|
+
case op
|
44
|
+
when :'||'
|
45
|
+
super(:+, args)
|
46
|
+
else
|
47
|
+
super(op, args)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def full_text_search(cols, terms, opts = {})
|
52
|
+
filter("CONTAINS (#{literal(cols)}, #{literal(terms)})")
|
53
|
+
end
|
54
|
+
|
55
|
+
def literal(v)
|
56
|
+
case v
|
57
|
+
when LiteralString
|
58
|
+
v
|
59
|
+
when String
|
60
|
+
"N#{super}"
|
61
|
+
when Time
|
62
|
+
literal(v.iso8601)
|
63
|
+
when Date, DateTime
|
64
|
+
literal(v.to_s)
|
65
|
+
else
|
66
|
+
super
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def multi_insert_sql(columns, values)
|
71
|
+
values = values.map {|r| "SELECT #{expression_list(r)}" }.join(" UNION ALL ")
|
72
|
+
["INSERT INTO #{source_list(@opts[:from])} (#{identifier_list(columns)}) #{values}"]
|
73
|
+
end
|
74
|
+
|
75
|
+
# Allows you to do .nolock on a query
|
76
|
+
def nolock
|
77
|
+
clone(:with => "(NOLOCK)")
|
78
|
+
end
|
79
|
+
|
80
|
+
def quoted_identifier(name)
|
81
|
+
"[#{name}]"
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
def select_clause_order
|
87
|
+
SELECT_CLAUSE_ORDER
|
88
|
+
end
|
89
|
+
|
90
|
+
# MSSQL uses TOP for limit, with no offset support
|
91
|
+
def select_limit_sql(sql, opts)
|
92
|
+
raise(Error, "OFFSET not supported") if opts[:offset]
|
93
|
+
sql << " TOP #{opts[:limit]}" if opts[:limit]
|
94
|
+
end
|
95
|
+
|
96
|
+
# MSSQL uses the WITH statement to lock tables
|
97
|
+
def select_with_sql(sql, opts)
|
98
|
+
sql << " WITH #{opts[:with]}" if opts[:with]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,325 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Schema
|
3
|
+
module SQL
|
4
|
+
# Keep default column_references_sql for add_foreign_key support
|
5
|
+
alias default_column_references_sql column_references_sql
|
6
|
+
end
|
7
|
+
end
|
8
|
+
module MySQL
|
9
|
+
# Set the default options used for CREATE TABLE
|
10
|
+
metaattr_accessor :default_charset, :default_collate, :default_engine
|
11
|
+
|
12
|
+
# Methods shared by Database instances that connect to MySQL,
|
13
|
+
# currently supported by the native and JDBC adapters.
|
14
|
+
module DatabaseMethods
|
15
|
+
AUTO_INCREMENT = 'AUTO_INCREMENT'.freeze
|
16
|
+
NOT_NULL = Sequel::Schema::SQL::NOT_NULL
|
17
|
+
NULL = Sequel::Schema::SQL::NULL
|
18
|
+
PRIMARY_KEY = Sequel::Schema::SQL::PRIMARY_KEY
|
19
|
+
TYPES = Sequel::Schema::SQL::TYPES.merge(DateTime=>'datetime', \
|
20
|
+
TrueClass=>'tinyint', FalseClass=>'tinyint')
|
21
|
+
UNIQUE = Sequel::Schema::SQL::UNIQUE
|
22
|
+
UNSIGNED = Sequel::Schema::SQL::UNSIGNED
|
23
|
+
|
24
|
+
# Use MySQL specific syntax for rename column, set column type, and
|
25
|
+
# drop index cases.
|
26
|
+
def alter_table_sql(table, op)
|
27
|
+
case op[:op]
|
28
|
+
when :add_column
|
29
|
+
if related = op.delete(:table)
|
30
|
+
sql = super(table, op)
|
31
|
+
op[:table] = related
|
32
|
+
[sql, "ALTER TABLE #{quote_schema_table(table)} ADD FOREIGN KEY (#{quote_identifier(op[:name])})#{default_column_references_sql(op)}"]
|
33
|
+
else
|
34
|
+
super(table, op)
|
35
|
+
end
|
36
|
+
when :rename_column
|
37
|
+
"ALTER TABLE #{quote_schema_table(table)} CHANGE COLUMN #{quote_identifier(op[:name])} #{quote_identifier(op[:new_name])} #{type_literal(op)}"
|
38
|
+
when :set_column_type
|
39
|
+
"ALTER TABLE #{quote_schema_table(table)} CHANGE COLUMN #{quote_identifier(op[:name])} #{quote_identifier(op[:name])} #{type_literal(op)}"
|
40
|
+
when :drop_index
|
41
|
+
"#{drop_index_sql(table, op)} ON #{quote_schema_table(table)}"
|
42
|
+
else
|
43
|
+
super(table, op)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Use MySQL specific AUTO_INCREMENT text.
|
48
|
+
def auto_increment_sql
|
49
|
+
AUTO_INCREMENT
|
50
|
+
end
|
51
|
+
|
52
|
+
# Handle MySQL specific syntax for column references
|
53
|
+
def column_references_sql(column)
|
54
|
+
"#{", FOREIGN KEY (#{quote_identifier(column[:name])})" unless column[:type] == :check}#{super(column)}"
|
55
|
+
end
|
56
|
+
|
57
|
+
# Use MySQL specific syntax for engine type and character encoding
|
58
|
+
def create_table_sql_list(name, columns, indexes = nil, options = {})
|
59
|
+
options[:engine] = Sequel::MySQL.default_engine unless options.include?(:engine)
|
60
|
+
options[:charset] = Sequel::MySQL.default_charset unless options.include?(:charset)
|
61
|
+
options[:collate] = Sequel::MySQL.default_collate unless options.include?(:collate)
|
62
|
+
sql = ["CREATE TABLE #{quote_schema_table(name)} (#{column_list_sql(columns)})#{" ENGINE=#{options[:engine]}" if options[:engine]}#{" DEFAULT CHARSET=#{options[:charset]}" if options[:charset]}#{" DEFAULT COLLATE=#{options[:collate]}" if options[:collate]}"]
|
63
|
+
sql.concat(index_list_sql_list(name, indexes)) if indexes && !indexes.empty?
|
64
|
+
sql
|
65
|
+
end
|
66
|
+
|
67
|
+
# Handle MySQL specific index SQL syntax
|
68
|
+
def index_definition_sql(table_name, index)
|
69
|
+
index_name = quote_identifier(index[:name] || default_index_name(table_name, index[:columns]))
|
70
|
+
index_type = case index[:type]
|
71
|
+
when :full_text
|
72
|
+
"FULLTEXT "
|
73
|
+
when :spatial
|
74
|
+
"SPATIAL "
|
75
|
+
else
|
76
|
+
using = " USING #{index[:type]}" unless index[:type] == nil
|
77
|
+
"UNIQUE " if index[:unique]
|
78
|
+
end
|
79
|
+
"CREATE #{index_type}INDEX #{index_name} ON #{quote_schema_table(table_name)} #{literal(index[:columns])}#{using}"
|
80
|
+
end
|
81
|
+
|
82
|
+
# Get version of MySQL server, used for determined capabilities.
|
83
|
+
def server_version
|
84
|
+
m = /(\d+)\.(\d+)\.(\d+)/.match(get(SQL::Function.new(:version)))
|
85
|
+
@server_version ||= (m[1].to_i * 10000) + (m[2].to_i * 100) + m[3].to_i
|
86
|
+
end
|
87
|
+
|
88
|
+
# Return an array of symbols specifying table names in the current database.
|
89
|
+
#
|
90
|
+
# Options:
|
91
|
+
# * :server - Set the server to use
|
92
|
+
def tables(opts={})
|
93
|
+
ds = self['SHOW TABLES'].server(opts[:server])
|
94
|
+
ds.identifier_output_method = nil
|
95
|
+
ds2 = dataset
|
96
|
+
ds.map{|r| ds2.send(:output_identifier, r.values.first)}
|
97
|
+
end
|
98
|
+
|
99
|
+
# Changes the database in use by issuing a USE statement. I would be
|
100
|
+
# very careful if I used this.
|
101
|
+
def use(db_name)
|
102
|
+
disconnect
|
103
|
+
@opts[:database] = db_name if self << "USE #{db_name}"
|
104
|
+
@schemas = nil
|
105
|
+
self
|
106
|
+
end
|
107
|
+
|
108
|
+
private
|
109
|
+
|
110
|
+
# MySQL folds unquoted identifiers to lowercase, so it shouldn't need to upcase identifiers on input.
|
111
|
+
def identifier_input_method_default
|
112
|
+
nil
|
113
|
+
end
|
114
|
+
|
115
|
+
# MySQL folds unquoted identifiers to lowercase, so it shouldn't need to upcase identifiers on output.
|
116
|
+
def identifier_output_method_default
|
117
|
+
nil
|
118
|
+
end
|
119
|
+
|
120
|
+
# Use the MySQL specific DESCRIBE syntax to get a table description.
|
121
|
+
def schema_parse_table(table_name, opts)
|
122
|
+
ds = self["DESCRIBE ?", SQL::Identifier.new(table_name)]
|
123
|
+
ds.identifier_output_method = nil
|
124
|
+
ds2 = dataset
|
125
|
+
ds.map do |row|
|
126
|
+
row.delete(:Extra)
|
127
|
+
row[:allow_null] = row.delete(:Null) == 'YES'
|
128
|
+
row[:default] = row.delete(:Default)
|
129
|
+
row[:primary_key] = row.delete(:Key) == 'PRI'
|
130
|
+
row[:default] = nil if row[:default].blank?
|
131
|
+
row[:db_type] = row.delete(:Type)
|
132
|
+
row[:type] = schema_column_type(row[:db_type])
|
133
|
+
[ds2.send(:output_identifier, row.delete(:Field)), row]
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
# Override the standard type conversions with MySQL specific ones
|
138
|
+
def type_literal_base(column)
|
139
|
+
TYPES[column[:type]]
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
# Dataset methods shared by datasets that use MySQL databases.
|
144
|
+
module DatasetMethods
|
145
|
+
include Dataset::UnsupportedIntersectExcept
|
146
|
+
|
147
|
+
BOOL_TRUE = '1'.freeze
|
148
|
+
BOOL_FALSE = '0'.freeze
|
149
|
+
CAST_TYPES = {String=>:CHAR, Integer=>:SIGNED, Time=>:DATETIME, DateTime=>:DATETIME, Numeric=>:DECIMAL, BigDecimal=>:DECIMAL, File=>:BINARY}
|
150
|
+
COMMA_SEPARATOR = ', '.freeze
|
151
|
+
|
152
|
+
# MySQL can't use the varchar type in a cast.
|
153
|
+
def cast_sql(expr, type)
|
154
|
+
"CAST(#{literal(expr)} AS #{CAST_TYPES[type] || db.send(:type_literal_base, :type=>type)})"
|
155
|
+
end
|
156
|
+
|
157
|
+
# MySQL specific syntax for LIKE/REGEXP searches, as well as
|
158
|
+
# string concatenation.
|
159
|
+
def complex_expression_sql(op, args)
|
160
|
+
case op
|
161
|
+
when :~, :'!~', :'~*', :'!~*', :LIKE, :'NOT LIKE', :ILIKE, :'NOT ILIKE'
|
162
|
+
"(#{literal(args.at(0))} #{'NOT ' if [:'NOT LIKE', :'NOT ILIKE', :'!~', :'!~*'].include?(op)}#{[:~, :'!~', :'~*', :'!~*'].include?(op) ? 'REGEXP' : 'LIKE'} #{'BINARY ' if [:~, :'!~', :LIKE, :'NOT LIKE'].include?(op)}#{literal(args.at(1))})"
|
163
|
+
when :'||'
|
164
|
+
if args.length > 1
|
165
|
+
"CONCAT(#{args.collect{|a| literal(a)}.join(', ')})"
|
166
|
+
else
|
167
|
+
literal(args.at(0))
|
168
|
+
end
|
169
|
+
else
|
170
|
+
super(op, args)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
# MySQL supports ORDER and LIMIT clauses in DELETE statements.
|
175
|
+
def delete_sql(opts = nil)
|
176
|
+
sql = super
|
177
|
+
opts = opts ? @opts.merge(opts) : @opts
|
178
|
+
|
179
|
+
if order = opts[:order]
|
180
|
+
sql << " ORDER BY #{expression_list(order)}"
|
181
|
+
end
|
182
|
+
if limit = opts[:limit]
|
183
|
+
sql << " LIMIT #{limit}"
|
184
|
+
end
|
185
|
+
|
186
|
+
sql
|
187
|
+
end
|
188
|
+
|
189
|
+
# MySQL specific full text search syntax.
|
190
|
+
def full_text_search(cols, terms, opts = {})
|
191
|
+
mode = opts[:boolean] ? " IN BOOLEAN MODE" : ""
|
192
|
+
s = if Array === terms
|
193
|
+
if mode.blank?
|
194
|
+
"MATCH #{literal(Array(cols))} AGAINST #{literal(terms)}"
|
195
|
+
else
|
196
|
+
"MATCH #{literal(Array(cols))} AGAINST (#{literal(terms)[1...-1]}#{mode})"
|
197
|
+
end
|
198
|
+
else
|
199
|
+
"MATCH #{literal(Array(cols))} AGAINST (#{literal(terms)}#{mode})"
|
200
|
+
end
|
201
|
+
filter(s)
|
202
|
+
end
|
203
|
+
|
204
|
+
# MySQL allows HAVING clause on ungrouped datasets.
|
205
|
+
def having(*cond, &block)
|
206
|
+
@opts[:having] = {}
|
207
|
+
x = filter(*cond, &block)
|
208
|
+
end
|
209
|
+
|
210
|
+
# MySQL doesn't use the SQL standard DEFAULT VALUES.
|
211
|
+
def insert_default_values_sql
|
212
|
+
"INSERT INTO #{source_list(@opts[:from])} () VALUES ()"
|
213
|
+
end
|
214
|
+
|
215
|
+
# Transforms an CROSS JOIN to an INNER JOIN if the expr is not nil.
|
216
|
+
# Raises an error on use of :full_outer type, since MySQL doesn't support it.
|
217
|
+
def join_table(type, table, expr=nil, table_alias={})
|
218
|
+
type = :inner if (type == :cross) && !expr.nil?
|
219
|
+
raise(Sequel::Error, "MySQL doesn't support FULL OUTER JOIN") if type == :full_outer
|
220
|
+
super(type, table, expr, table_alias)
|
221
|
+
end
|
222
|
+
|
223
|
+
# Transforms :natural_inner to NATURAL LEFT JOIN and straight to
|
224
|
+
# STRAIGHT_JOIN.
|
225
|
+
def join_type_sql(join_type)
|
226
|
+
case join_type
|
227
|
+
when :straight then 'STRAIGHT_JOIN'
|
228
|
+
when :natural_inner then 'NATURAL LEFT JOIN'
|
229
|
+
else super
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
# Override the default boolean values.
|
234
|
+
def literal(v)
|
235
|
+
case v
|
236
|
+
when true
|
237
|
+
BOOL_TRUE
|
238
|
+
when false
|
239
|
+
BOOL_FALSE
|
240
|
+
when DateTime, Time
|
241
|
+
v.strftime("'%Y-%m-%d %H:%M:%S'")
|
242
|
+
else
|
243
|
+
super
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
# MySQL specific syntax for inserting multiple values at once.
|
248
|
+
def multi_insert_sql(columns, values)
|
249
|
+
values = values.map {|r| literal(Array(r))}.join(COMMA_SEPARATOR)
|
250
|
+
["INSERT INTO #{source_list(@opts[:from])} (#{identifier_list(columns)}) VALUES #{values}"]
|
251
|
+
end
|
252
|
+
|
253
|
+
# MySQL uses the nonstandard ` (backtick) for quoting identifiers.
|
254
|
+
def quoted_identifier(c)
|
255
|
+
"`#{c}`"
|
256
|
+
end
|
257
|
+
|
258
|
+
# MySQL specific syntax for REPLACE (aka UPSERT, or update if exists,
|
259
|
+
# insert if it doesn't).
|
260
|
+
def replace_sql(*values)
|
261
|
+
from = source_list(@opts[:from])
|
262
|
+
if values.empty?
|
263
|
+
"REPLACE INTO #{from} DEFAULT VALUES"
|
264
|
+
else
|
265
|
+
values = values[0] if values.size == 1
|
266
|
+
|
267
|
+
# if hash or array with keys we need to transform the values
|
268
|
+
if @transform && (values.is_a?(Hash) || (values.is_a?(Array) && values.keys))
|
269
|
+
values = transform_save(values)
|
270
|
+
end
|
271
|
+
|
272
|
+
case values
|
273
|
+
when Array
|
274
|
+
if values.empty?
|
275
|
+
"REPLACE INTO #{from} DEFAULT VALUES"
|
276
|
+
else
|
277
|
+
"REPLACE INTO #{from} VALUES #{literal(values)}"
|
278
|
+
end
|
279
|
+
when Hash
|
280
|
+
if values.empty?
|
281
|
+
"REPLACE INTO #{from} DEFAULT VALUES"
|
282
|
+
else
|
283
|
+
fl, vl = [], []
|
284
|
+
values.each {|k, v| fl << literal(k.is_a?(String) ? k.to_sym : k); vl << literal(v)}
|
285
|
+
"REPLACE INTO #{from} (#{fl.join(COMMA_SEPARATOR)}) VALUES (#{vl.join(COMMA_SEPARATOR)})"
|
286
|
+
end
|
287
|
+
when Dataset
|
288
|
+
"REPLACE INTO #{from} #{literal(values)}"
|
289
|
+
else
|
290
|
+
if values.respond_to?(:values)
|
291
|
+
replace_sql(values.values)
|
292
|
+
else
|
293
|
+
"REPLACE INTO #{from} VALUES (#{literal(values)})"
|
294
|
+
end
|
295
|
+
end
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
# MySQL supports ORDER and LIMIT clauses in UPDATE statements.
|
300
|
+
def update_sql(values, opts = nil)
|
301
|
+
sql = super
|
302
|
+
opts = opts ? @opts.merge(opts) : @opts
|
303
|
+
|
304
|
+
if order = opts[:order]
|
305
|
+
sql << " ORDER BY #{expression_list(order)}"
|
306
|
+
end
|
307
|
+
if limit = opts[:limit]
|
308
|
+
sql << " LIMIT #{limit}"
|
309
|
+
end
|
310
|
+
|
311
|
+
sql
|
312
|
+
end
|
313
|
+
|
314
|
+
private
|
315
|
+
|
316
|
+
# MySQL doesn't support DISTINCT ON
|
317
|
+
def select_distinct_sql(sql, opts)
|
318
|
+
if opts[:distinct]
|
319
|
+
raise(Error, "DISTINCT ON not supported by MySQL") unless opts[:distinct].empty?
|
320
|
+
sql << " DISTINCT"
|
321
|
+
end
|
322
|
+
end
|
323
|
+
end
|
324
|
+
end
|
325
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Oracle
|
3
|
+
module DatabaseMethods
|
4
|
+
def tables(opts={})
|
5
|
+
ds = from(:tab).server(opts[:server]).select(:tname).filter(:tabtype => 'TABLE')
|
6
|
+
ds.map{|r| ds.send(:output_identifier, r[:tname])}
|
7
|
+
end
|
8
|
+
|
9
|
+
def table_exists?(name)
|
10
|
+
from(:tab).filter(:tname =>dataset.send(:input_identifier, name), :tabtype => 'TABLE').count > 0
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module DatasetMethods
|
15
|
+
include Dataset::UnsupportedIntersectExceptAll
|
16
|
+
|
17
|
+
SELECT_CLAUSE_ORDER = %w'distinct columns from join where group having compounds order limit'.freeze
|
18
|
+
|
19
|
+
# Oracle uses MINUS instead of EXCEPT, and doesn't support EXCEPT ALL
|
20
|
+
def except(dataset, all = false)
|
21
|
+
raise(Sequel::Error, "EXCEPT ALL not supported") if all
|
22
|
+
compound_clone(:minus, dataset, all)
|
23
|
+
end
|
24
|
+
|
25
|
+
def empty?
|
26
|
+
db[:dual].where(exists).get(1) == nil
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
# Oracle doesn't support the use of AS when aliasing a dataset. It doesn't require
|
32
|
+
# the use of AS anywhere, so this disables it in all cases.
|
33
|
+
def as_sql(expression, aliaz)
|
34
|
+
"#{expression} #{quote_identifier(aliaz)}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def select_clause_order
|
38
|
+
SELECT_CLAUSE_ORDER
|
39
|
+
end
|
40
|
+
|
41
|
+
# Oracle doesn't support DISTINCT ON
|
42
|
+
def select_distinct_sql(sql, opts)
|
43
|
+
if opts[:distinct]
|
44
|
+
raise(Error, "DISTINCT ON not supported by Oracle") unless opts[:distinct].empty?
|
45
|
+
sql << " DISTINCT"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Oracle requires a subselect to do limit and offset
|
50
|
+
def select_limit_sql(sql, opts)
|
51
|
+
if limit = opts[:limit]
|
52
|
+
if (offset = opts[:offset]) && (offset > 0)
|
53
|
+
sql.replace("SELECT * FROM (SELECT raw_sql_.*, ROWNUM raw_rnum_ FROM(#{sql}) raw_sql_ WHERE ROWNUM <= #{limit + offset}) WHERE raw_rnum_ > #{offset}")
|
54
|
+
else
|
55
|
+
sql.replace("SELECT * FROM (#{sql}) WHERE ROWNUM <= #{limit}")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|