Module: Sequel::MSSQL::DatasetMethods
- Includes:
- EmulateOffsetWithRowNumber
- Included in:
- ADO::MSSQL::Dataset, JDBC::JTDS::Dataset, JDBC::SQLServer::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}
- EXTRACT_MAP =
{:year=>"yy", :month=>"m", :day=>"d", :hour=>"hh", :minute=>"n", :second=>"s"}
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.
-
#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 …
-
#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.
-
#server_version ⇒ Object
The version of the database server.
-
#supports_insert_select? ⇒ Boolean
MSSQL supports insert_select via the OUTPUT clause.
-
#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_where_true? ⇒ Boolean
MSSQL cannot use WHERE 1.
-
#supports_window_functions? ⇒ Boolean
MSSQL 2005+ supports window functions.
Methods included from EmulateOffsetWithRowNumber
Instance Attribute Details
#mssql_unicode_strings ⇒ Object
Allow overriding of the mssql_unicode_strings option at the dataset level.
263 264 265 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 263 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.
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 272 def complex_expression_sql(op, args) case op when :'||' super(:+, args) when :LIKE, :"NOT LIKE" super(op, args.map{|a| LiteralString.new("(#{literal(a)} COLLATE Latin1_General_CS_AS)")}) when :ILIKE, :"NOT ILIKE" super((op == :ILIKE ? :LIKE : :"NOT LIKE"), args) when :<< complex_expression_arg_pairs(args){|a, b| "(#{literal(a)} * POWER(2, #{literal(b)}))"} when :>> complex_expression_arg_pairs(args){|a, b| "(#{literal(a)} / POWER(2, #{literal(b)}))"} when :extract part = args.at(0) raise(Sequel::Error, "unsupported extract argument: #{part.inspect}") unless format = EXTRACT_MAP[part] expr = literal(args.at(1)) s = "datepart(#{format}, #{expr})" s = "CAST((#{s} + datepart(ns, #{expr})/1000000000.0) AS double precision)" if part == :second s else super(op, args) end end |
#constant_sql(constant) ⇒ Object
MSSQL doesn’t support the SQL standard CURRENT_DATE or CURRENT_TIME
297 298 299 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 297 def constant_sql(constant) CONSTANT_MAP[constant] || super end |
#disable_insert_output ⇒ Object
Disable the use of INSERT OUTPUT
302 303 304 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 302 def disable_insert_output clone(:disable_insert_output=>true) end |
#disable_insert_output! ⇒ Object
Disable the use of INSERT OUTPUT, modifying the receiver
307 308 309 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 307 def disable_insert_output! mutation_method(:disable_insert_output) end |
#full_text_search(cols, terms, opts = {}) ⇒ Object
MSSQL uses the CONTAINS keyword for full text search
312 313 314 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 312 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.
266 267 268 269 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 266 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.
317 318 319 320 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 317 def insert_select(*values) return unless supports_insert_select? naked.clone(default_server_opts(:sql=>output(nil, [SQL::ColumnAll.new(:inserted)]).insert_sql(*values))).single_record end |
#into(table) ⇒ Object
Specify a table for a SELECT … INTO query.
323 324 325 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 323 def into(table) clone(:into => table) end |
#multi_insert_sql(columns, values) ⇒ Object
MSSQL uses a UNION ALL statement to insert multiple values at once.
328 329 330 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 328 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).
333 334 335 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 333 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)
349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 349 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.
363 364 365 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 363 def output!(into, values) mutation_method(:output, into, values) end |
#quoted_identifier(name) ⇒ Object
MSSQL uses [] to quote identifiers
368 369 370 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 368 def quoted_identifier(name) "[#{name}]" end |
#server_version ⇒ Object
The version of the database server.
373 374 375 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 373 def server_version db.server_version(@opts[:server]) end |
#supports_insert_select? ⇒ Boolean
MSSQL supports insert_select via the OUTPUT clause.
378 379 380 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 378 def supports_insert_select? supports_output_clause? && !opts[:disable_insert_output] end |
#supports_intersect_except? ⇒ Boolean
MSSQL 2005+ supports INTERSECT and EXCEPT
383 384 385 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 383 def supports_intersect_except? is_2005_or_later? end |
#supports_is_true? ⇒ Boolean
MSSQL does not support IS TRUE
388 389 390 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 388 def supports_is_true? false end |
#supports_join_using? ⇒ Boolean
MSSQL doesn’t support JOIN USING
393 394 395 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 393 def supports_join_using? false end |
#supports_modifying_joins? ⇒ Boolean
MSSQL 2005+ supports modifying joined datasets
398 399 400 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 398 def is_2005_or_later? end |
#supports_multiple_column_in? ⇒ Boolean
MSSQL does not support multiple columns for the IN/NOT IN operators
403 404 405 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 403 def supports_multiple_column_in? false end |
#supports_output_clause? ⇒ Boolean
MSSQL 2005+ supports the output clause.
408 409 410 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 408 def supports_output_clause? is_2005_or_later? end |
#supports_where_true? ⇒ Boolean
MSSQL cannot use WHERE 1.
418 419 420 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 418 def supports_where_true? false end |
#supports_window_functions? ⇒ Boolean
MSSQL 2005+ supports window functions
413 414 415 |
# File 'lib/sequel/adapters/shared/mssql.rb', line 413 def supports_window_functions? true end |