Module | Sequel::MSSQL::DatasetMethods |
In: |
lib/sequel/adapters/shared/mssql.rb
|
BOOL_TRUE | = | '1'.freeze |
BOOL_FALSE | = | '0'.freeze |
COMMA_SEPARATOR | = | ', '.freeze |
DELETE_CLAUSE_METHODS | = | Dataset.clause_methods(:delete, %w'with delete from output from2 where') |
INSERT_CLAUSE_METHODS | = | Dataset.clause_methods(:insert, %w'with insert into columns output values') |
SELECT_CLAUSE_METHODS | = | Dataset.clause_methods(:select, %w'with select distinct limit columns into from lock join where group having order compounds') |
UPDATE_CLAUSE_METHODS | = | Dataset.clause_methods(:update, %w'with update 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"} |
BRACKET_CLOSE | = | Dataset::BRACKET_CLOSE |
BRACKET_OPEN | = | Dataset::BRACKET_OPEN |
COMMA | = | Dataset::COMMA |
PAREN_CLOSE | = | Dataset::PAREN_CLOSE |
PAREN_SPACE_OPEN | = | Dataset::PAREN_SPACE_OPEN |
SPACE | = | Dataset::SPACE |
FROM | = | Dataset::FROM |
APOS | = | Dataset::APOS |
APOS_RE | = | Dataset::APOS_RE |
DOUBLE_APOS | = | Dataset::DOUBLE_APOS |
INTO | = | Dataset::INTO |
DATEPART_SECOND_OPEN | = | "CAST((datepart(".freeze |
DATEPART_SECOND_MIDDLE | = | ') + datepart(ns, '.freeze |
DATEPART_SECOND_CLOSE | = | ")/1000000000.0) AS double precision)".freeze |
DATEPART_OPEN | = | "datepart(".freeze |
UNION_ALL | = | ' UNION ALL '.freeze |
SELECT_SPACE | = | 'SELECT '.freeze |
TIMESTAMP_USEC_FORMAT | = | ".%03d".freeze |
OUTPUT_INSERTED | = | " OUTPUT INSERTED.*".freeze |
HEX_START | = | '0x'.freeze |
UNICODE_STRING_START | = | "N'".freeze |
TOP_PAREN | = | " TOP (".freeze |
TOP | = | " TOP ".freeze |
OUTPUT | = | " OUTPUT ".freeze |
HSTAR | = | "H*".freeze |
CASE_SENSITIVE_COLLATION | = | 'Latin1_General_CS_AS'.freeze |
CASE_INSENSITIVE_COLLATION | = | 'Latin1_General_CI_AS'.freeze |
mssql_unicode_strings | [RW] | Allow overriding of the mssql_unicode_strings option at the dataset level. |
Copy the mssql_unicode_strings option from the db object.
# File lib/sequel/adapters/shared/mssql.rb, line 339 339: def initialize(db, opts={}) 340: super 341: @mssql_unicode_strings = db.mssql_unicode_strings 342: end
MSSQL uses + for string concatenation, and LIKE is case insensitive by default.
# File lib/sequel/adapters/shared/mssql.rb, line 345 345: def complex_expression_sql_append(sql, op, args) 346: case op 347: when '||''||' 348: super(sql, :+, args) 349: when :LIKE, "NOT LIKE""NOT LIKE" 350: super(sql, op, args.map{|a| LiteralString.new("(#{literal(a)} COLLATE #{CASE_SENSITIVE_COLLATION})")}) 351: when :ILIKE, "NOT ILIKE""NOT ILIKE" 352: super(sql, (op == :ILIKE ? :LIKE : "NOT LIKE""NOT LIKE"), args.map{|a| LiteralString.new("(#{literal(a)} COLLATE #{CASE_INSENSITIVE_COLLATION})")}) 353: when :<< 354: sql << complex_expression_arg_pairs(args){|a, b| "(#{literal(a)} * POWER(2, #{literal(b)}))"} 355: when :>> 356: sql << complex_expression_arg_pairs(args){|a, b| "(#{literal(a)} / POWER(2, #{literal(b)}))"} 357: when :extract 358: part = args.at(0) 359: raise(Sequel::Error, "unsupported extract argument: #{part.inspect}") unless format = EXTRACT_MAP[part] 360: if part == :second 361: expr = literal(args.at(1)) 362: sql << DATEPART_SECOND_OPEN << format.to_s << COMMA << expr << DATEPART_SECOND_MIDDLE << expr << DATEPART_SECOND_CLOSE 363: else 364: sql << DATEPART_OPEN << format.to_s << COMMA 365: literal_append(sql, args.at(1)) 366: sql << PAREN_CLOSE 367: end 368: else 369: super 370: end 371: end
Disable the use of INSERT OUTPUT
# File lib/sequel/adapters/shared/mssql.rb, line 383 383: def disable_insert_output 384: clone(:disable_insert_output=>true) 385: end
Disable the use of INSERT OUTPUT, modifying the receiver
# File lib/sequel/adapters/shared/mssql.rb, line 388 388: def disable_insert_output! 389: mutation_method(:disable_insert_output) 390: end
Use the OUTPUT clause to get the value of all columns for the newly inserted record.
# File lib/sequel/adapters/shared/mssql.rb, line 399 399: def insert_select(*values) 400: return unless supports_insert_select? 401: naked.clone(default_server_opts(:sql=>output(nil, [SQL::ColumnAll.new(:inserted)]).insert_sql(*values))).single_record 402: end
Specify a table for a SELECT … INTO query.
# File lib/sequel/adapters/shared/mssql.rb, line 405 405: def into(table) 406: clone(:into => table) 407: end
MSSQL uses a UNION ALL statement to insert multiple values at once.
# File lib/sequel/adapters/shared/mssql.rb, line 410 410: def multi_insert_sql(columns, values) 411: c = false 412: sql = LiteralString.new('') 413: u = UNION_ALL 414: values.each do |v| 415: sql << u if c 416: sql << SELECT_SPACE 417: expression_list_append(sql, v) 418: c ||= true 419: end 420: [insert_sql(columns, sql)] 421: end
Allows you to do a dirty read of uncommitted data using WITH (NOLOCK).
# File lib/sequel/adapters/shared/mssql.rb, line 424 424: def nolock 425: lock_style(:dirty) 426: end
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)
# File lib/sequel/adapters/shared/mssql.rb, line 440 440: def output(into, values) 441: raise(Error, "SQL Server versions 2000 and earlier do not support the OUTPUT clause") unless supports_output_clause? 442: output = {} 443: case values 444: when Hash 445: output[:column_list], output[:select_list] = values.keys, values.values 446: when Array 447: output[:select_list] = values 448: end 449: output[:into] = into 450: clone({:output => output}) 451: end
The version of the database server.
# File lib/sequel/adapters/shared/mssql.rb, line 464 464: def server_version 465: db.server_version(@opts[:server]) 466: end
MSSQL supports insert_select via the OUTPUT clause.
# File lib/sequel/adapters/shared/mssql.rb, line 479 479: def supports_insert_select? 480: supports_output_clause? && !opts[:disable_insert_output] 481: end
If returned primary keys are requested, use OUTPUT unless already set on the dataset. If OUTPUT is already set, use existing returning values. If OUTPUT is only set to return a single columns, return an array of just that column. Otherwise, return an array of hashes.
# File lib/sequel/adapters/shared/mssql.rb, line 529 529: def _import(columns, values, opts={}) 530: if opts[:return] == :primary_key && !@opts[:output] 531: output(nil, [SQL::QualifiedIdentifier.new(:inserted, first_primary_key)])._import(columns, values, opts) 532: elsif @opts[:output] 533: statements = multi_insert_sql(columns, values) 534: @db.transaction(opts.merge(:server=>@opts[:server])) do 535: statements.map{|st| with_sql(st)} 536: end.first.map{|v| v.length == 1 ? v.values.first : v} 537: else 538: super 539: end 540: end