Module: Sequel::MSSQL::DatasetMethods
- Included in:
- ADO::MSSQL::Dataset, JDBC::MSSQL::Dataset, ODBC::MSSQL::Dataset, TinyTDS::Dataset
- Defined in:
- lib/sequel/adapters/shared/mssql.rb
Constant Summary collapse
- BOOL_TRUE =
'1'.freeze
- BOOL_FALSE =
'0'.freeze
- COMMA_SEPARATOR =
', '.freeze
- DELETE_CLAUSE_METHODS =
Dataset.clause_methods(:delete, %w'with from output from2 where')
- INSERT_CLAUSE_METHODS =
Dataset.clause_methods(:insert, %w'with into columns output values')
- SELECT_CLAUSE_METHODS =
Dataset.clause_methods(:select, %w'with distinct limit columns into from lock join where group having order compounds')
- UPDATE_CLAUSE_METHODS =
Dataset.clause_methods(:update, %w'with table set output from where')
- NOLOCK =
' WITH (NOLOCK)'.freeze
- UPDLOCK =
' WITH (UPDLOCK)'.freeze
- WILDCARD =
LiteralString.new('*').freeze
- CONSTANT_MAP =
{:CURRENT_DATE=>'CAST(CURRENT_TIMESTAMP AS DATE)'.freeze, :CURRENT_TIME=>'CAST(CURRENT_TIMESTAMP AS TIME)'.freeze}
Instance Attribute Summary collapse
-
#mssql_unicode_strings ⇒ Object
Allow overriding of the mssql_unicode_strings option at the dataset level.
Instance Method Summary collapse
-
#complex_expression_sql(op, args) ⇒ Object
MSSQL uses + for string concatenation, and LIKE is case insensitive by default.
-
#constant_sql(constant) ⇒ Object
MSSQL doesn’t support the SQL standard CURRENT_DATE or CURRENT_TIME.
-
#disable_insert_output ⇒ Object
Disable the use of INSERT OUTPUT.
-
#disable_insert_output! ⇒ Object
Disable the use of INSERT OUTPUT, modifying the receiver.
-
#fetch_rows(sql, &block) ⇒ Object
When returning all rows, if an offset is used, delete the row_number column before yielding the row.
-
#full_text_search(cols, terms, opts = {}) ⇒ Object
MSSQL uses the CONTAINS keyword for full text search.
-
#initialize(db, opts = {}) ⇒ Object
Copy the mssql_unicode_strings option from the
db
object. -
#insert_select(*values) ⇒ Object
Use the OUTPUT clause to get the value of all columns for the newly inserted record.
-
#into(table) ⇒ Object
Specify a table for a SELECT …
-
#join_table(type, table, expr = nil, table_alias = {}, &block) ⇒ Object
SQL Server does not support CTEs on subqueries, so move any CTEs on joined datasets to the top level.
-
#multi_insert_sql(columns, values) ⇒ Object
MSSQL uses a UNION ALL statement to insert multiple values at once.
-
#nolock ⇒ Object
Allows you to do a dirty read of uncommitted data using WITH (NOLOCK).
-
#output(into, values) ⇒ Object
Include an OUTPUT clause in the eventual INSERT, UPDATE, or DELETE query.
-
#output!(into, values) ⇒ Object
An output method that modifies the receiver.
-
#quoted_identifier(name) ⇒ Object
MSSQL uses [] to quote identifiers.
-
#select_sql ⇒ Object
MSSQL Requires the use of the ROW_NUMBER window function to emulate an offset.
-
#server_version ⇒ Object
The version of the database server.
-
#supports_intersect_except? ⇒ Boolean
MSSQL 2005+ supports INTERSECT and EXCEPT.
-
#supports_is_true? ⇒ Boolean
MSSQL does not support IS TRUE.
-
#supports_join_using? ⇒ Boolean
MSSQL doesn’t support JOIN USING.
-
#supports_modifying_joins? ⇒ Boolean
MSSQL 2005+ supports modifying joined datasets.
-
#supports_multiple_column_in? ⇒ Boolean
MSSQL does not support multiple columns for the IN/NOT IN operators.
-
#supports_output_clause? ⇒ Boolean
MSSQL 2005+ supports the output clause.
-
#supports_window_functions? ⇒ Boolean
MSSQL 2005+ supports window functions.
Instance Attribute Details
#mssql_unicode_strings ⇒ Object
Allow overriding of the mssql_unicode_strings option at the dataset level.
226 227 228 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 226 def mssql_unicode_strings @mssql_unicode_strings end |
Instance Method Details
#complex_expression_sql(op, args) ⇒ Object
MSSQL uses + for string concatenation, and LIKE is case insensitive by default.
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 235 def complex_expression_sql(op, args) case op when :'||' super(:+, args) when :ILIKE super(:LIKE, args) when :"NOT ILIKE" super(:"NOT LIKE", args) when :<< "(#{literal(args[0])} * POWER(2, #{literal(args[1])}))" when :>> "(#{literal(args[0])} / POWER(2, #{literal(args[1])}))" else super(op, args) end end |
#constant_sql(constant) ⇒ Object
MSSQL doesn’t support the SQL standard CURRENT_DATE or CURRENT_TIME
253 254 255 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 253 def constant_sql(constant) CONSTANT_MAP[constant] || super end |
#disable_insert_output ⇒ Object
Disable the use of INSERT OUTPUT
258 259 260 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 258 def disable_insert_output clone(:disable_insert_output=>true) end |
#disable_insert_output! ⇒ Object
Disable the use of INSERT OUTPUT, modifying the receiver
263 264 265 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 263 def disable_insert_output! mutation_method(:disable_insert_output) end |
#fetch_rows(sql, &block) ⇒ Object
When returning all rows, if an offset is used, delete the row_number column before yielding the row.
269 270 271 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 269 def fetch_rows(sql, &block) @opts[:offset] ? super(sql){|r| r.delete(row_number_column); yield r} : super(sql, &block) end |
#full_text_search(cols, terms, opts = {}) ⇒ Object
MSSQL uses the CONTAINS keyword for full text search
274 275 276 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 274 def full_text_search(cols, terms, opts = {}) filter("CONTAINS (#{literal(cols)}, #{literal(terms)})") end |
#initialize(db, opts = {}) ⇒ Object
Copy the mssql_unicode_strings option from the db
object.
229 230 231 232 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 229 def initialize(db, opts={}) super @mssql_unicode_strings = db.mssql_unicode_strings end |
#insert_select(*values) ⇒ Object
Use the OUTPUT clause to get the value of all columns for the newly inserted record.
279 280 281 282 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 279 def insert_select(*values) return unless supports_output_clause? naked.clone(default_server_opts(:sql=>output(nil, [:inserted.*]).insert_sql(*values))).single_record unless opts[:disable_insert_output] end |
#into(table) ⇒ Object
Specify a table for a SELECT … INTO query.
285 286 287 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 285 def into(table) clone(:into => table) end |
#join_table(type, table, expr = nil, table_alias = {}, &block) ⇒ Object
SQL Server does not support CTEs on subqueries, so move any CTEs on joined datasets to the top level. The user is responsible for resolving any name clashes this may cause.
292 293 294 295 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 292 def join_table(type, table, expr=nil, table_alias={}, &block) return super unless Dataset === table && table.opts[:with] clone(:with => (opts[:with] || []) + table.opts[:with]).join_table(type, table.clone(:with => nil), expr, table_alias, &block) end |
#multi_insert_sql(columns, values) ⇒ Object
MSSQL uses a UNION ALL statement to insert multiple values at once.
298 299 300 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 298 def multi_insert_sql(columns, values) [insert_sql(columns, LiteralString.new(values.map {|r| "SELECT #{expression_list(r)}" }.join(" UNION ALL ")))] end |
#nolock ⇒ Object
Allows you to do a dirty read of uncommitted data using WITH (NOLOCK).
303 304 305 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 303 def nolock lock_style(:dirty) end |
#output(into, values) ⇒ Object
Include an OUTPUT clause in the eventual INSERT, UPDATE, or DELETE query.
The first argument is the table to output into, and the second argument is either an Array of column values to select, or a Hash which maps output column names to selected values, in the style of #insert or #update.
Output into a returned result set is not currently supported.
Examples:
dataset.output(:output_table, [:deleted__id, :deleted__name])
dataset.output(:output_table, :id => :inserted__id, :name => :inserted__name)
319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 319 def output(into, values) raise(Error, "SQL Server versions 2000 and earlier do not support the OUTPUT clause") unless supports_output_clause? output = {} case values when Hash output[:column_list], output[:select_list] = values.keys, values.values when Array output[:select_list] = values end output[:into] = into clone({:output => output}) end |
#output!(into, values) ⇒ Object
An output method that modifies the receiver.
333 334 335 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 333 def output!(into, values) mutation_method(:output, into, values) end |
#quoted_identifier(name) ⇒ Object
MSSQL uses [] to quote identifiers
338 339 340 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 338 def quoted_identifier(name) "[#{name}]" end |
#select_sql ⇒ Object
MSSQL Requires the use of the ROW_NUMBER window function to emulate an offset. This implementation requires MSSQL 2005 or greater (offset can’t be emulated well in MSSQL 2000).
The implementation is ugly, cloning the current dataset and modifying the clone to add a ROW_NUMBER window function (and some other things), then using the modified clone in a subselect which is selected from.
If offset is used, an order must be provided, because the use of ROW_NUMBER requires an order.
352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 352 def select_sql return super unless o = @opts[:offset] raise(Error, 'MSSQL requires an order be provided if using an offset') unless order = @opts[:order] dsa1 = dataset_alias(1) rn = row_number_column subselect_sql(unlimited. unordered. select_append{ROW_NUMBER(:over, :order=>order){}.as(rn)}. from_self(:alias=>dsa1). limit(@opts[:limit]). where(SQL::Identifier.new(rn) > o)) end |
#server_version ⇒ Object
The version of the database server.
366 367 368 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 366 def server_version db.server_version(@opts[:server]) end |
#supports_intersect_except? ⇒ Boolean
MSSQL 2005+ supports INTERSECT and EXCEPT
371 372 373 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 371 def supports_intersect_except? is_2005_or_later? end |
#supports_is_true? ⇒ Boolean
MSSQL does not support IS TRUE
376 377 378 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 376 def supports_is_true? false end |
#supports_join_using? ⇒ Boolean
MSSQL doesn’t support JOIN USING
381 382 383 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 381 def supports_join_using? false end |
#supports_modifying_joins? ⇒ Boolean
MSSQL 2005+ supports modifying joined datasets
386 387 388 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 386 def is_2005_or_later? end |
#supports_multiple_column_in? ⇒ Boolean
MSSQL does not support multiple columns for the IN/NOT IN operators
391 392 393 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 391 def supports_multiple_column_in? false end |
#supports_output_clause? ⇒ Boolean
MSSQL 2005+ supports the output clause.
396 397 398 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 396 def supports_output_clause? is_2005_or_later? end |
#supports_window_functions? ⇒ Boolean
MSSQL 2005+ supports window functions
401 402 403 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 401 def supports_window_functions? true end |