Module | Sequel::MSSQL |
In: |
lib/sequel/extensions/mssql_emulate_lateral_with_apply.rb
lib/sequel/adapters/shared/mssql.rb |
CONSTANT_MAP | = | {:CURRENT_DATE=>'CAST(CURRENT_TIMESTAMP AS DATE)'.freeze, :CURRENT_TIME=>'CAST(CURRENT_TIMESTAMP AS TIME)'.freeze}.freeze |
EXTRACT_MAP | = | {:year=>"yy", :month=>"m", :day=>"d", :hour=>"hh", :minute=>"n", :second=>"s"}.freeze |
LIMIT_ALL | = | Object.new.freeze |
# File lib/sequel/adapters/shared/mssql.rb, line 10 10: def self.mock_adapter_setup(db) 11: db.instance_exec do 12: @server_version = 11000000 13: end 14: end
# File lib/sequel/adapters/shared/mssql.rb, line 523 523: def complex_expression_sql_append(sql, op, args) 524: case op 525: when '||''||' 526: super(sql, :+, args) 527: when :LIKE, "NOT LIKE""NOT LIKE" 528: super(sql, op, args.map{|a| Sequel.lit(["(", " COLLATE Latin1_General_CS_AS)"], a)}) 529: when :ILIKE, "NOT ILIKE""NOT ILIKE" 530: super(sql, (op == :ILIKE ? :LIKE : "NOT LIKE""NOT LIKE"), args.map{|a| Sequel.lit(["(", " COLLATE Latin1_General_CI_AS)"], a)}) 531: when :<<, :>> 532: complex_expression_emulate_append(sql, op, args) 533: when :extract 534: part = args[0] 535: raise(Sequel::Error, "unsupported extract argument: #{part.inspect}") unless format = EXTRACT_MAP[part] 536: if part == :second 537: expr = args[1] 538: sql << "CAST((datepart(" << format.to_s << ', ' 539: literal_append(sql, expr) 540: sql << ') + datepart(ns, ' 541: literal_append(sql, expr) 542: sql << ")/1000000000.0) AS double precision)" 543: else 544: sql << "datepart(" << format.to_s << ', ' 545: literal_append(sql, args[1]) 546: sql << ')' 547: end 548: else 549: super 550: end 551: end
Disable the use of INSERT OUTPUT
# File lib/sequel/adapters/shared/mssql.rb, line 568 568: def disable_insert_output 569: clone(:disable_insert_output=>true) 570: 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 584 584: def insert_select(*values) 585: return unless supports_insert_select? 586: with_sql_first(insert_select_sql(*values)) 587: end
Add OUTPUT clause unless there is already an existing output clause, then return the SQL to insert.
# File lib/sequel/adapters/shared/mssql.rb, line 591 591: def insert_select_sql(*values) 592: ds = (opts[:output] || opts[:returning]) ? self : output(nil, [SQL::ColumnAll.new(:inserted)]) 593: ds.insert_sql(*values) 594: end
Specify a table for a SELECT … INTO query.
# File lib/sequel/adapters/shared/mssql.rb, line 597 597: def into(table) 598: clone(:into => table) 599: end
Use the database‘s mssql_unicode_strings setting if the dataset hasn‘t overridden it.
# File lib/sequel/adapters/shared/mssql.rb, line 514 514: def mssql_unicode_strings 515: opts.has_key?(:mssql_unicode_strings) ? opts[:mssql_unicode_strings] : db.mssql_unicode_strings 516: end
Allows you to do a dirty read of uncommitted data using WITH (NOLOCK).
# File lib/sequel/adapters/shared/mssql.rb, line 602 602: def nolock 603: lock_style(:dirty) 604: 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, [Sequel[:deleted][:id], Sequel[:deleted][:name]]) dataset.output(:output_table, id: Sequel[:inserted][:id], name: Sequel[:inserted][:name])
# File lib/sequel/adapters/shared/mssql.rb, line 623 623: def output(into, values) 624: raise(Error, "SQL Server versions 2000 and earlier do not support the OUTPUT clause") unless supports_output_clause? 625: output = {} 626: case values 627: when Hash 628: output[:column_list], output[:select_list] = values.keys, values.values 629: when Array 630: output[:select_list] = values 631: end 632: output[:into] = into 633: clone(:output => output) 634: end
Emulate RETURNING using the output clause. This only handles values that are simple column references.
# File lib/sequel/adapters/shared/mssql.rb, line 642 642: def returning(*values) 643: values = values.map do |v| 644: unless r = unqualified_column_for(v) 645: raise(Error, "cannot emulate RETURNING via OUTPUT for value: #{v.inspect}") 646: end 647: r 648: end 649: clone(:returning=>values) 650: end
On MSSQL 2012+ add a default order to the current dataset if an offset is used. The default offset emulation using a subquery would be used in the unordered case by default, and that also adds a default order, so it‘s better to just avoid the subquery.
# File lib/sequel/adapters/shared/mssql.rb, line 656 656: def select_sql 657: if @opts[:offset] && !@opts[:order] && is_2012_or_later? 658: order(1).select_sql 659: else 660: super 661: end 662: end
The version of the database server.
# File lib/sequel/adapters/shared/mssql.rb, line 665 665: def server_version 666: db.server_version(@opts[:server]) 667: end
# File lib/sequel/adapters/shared/mssql.rb, line 669 669: def supports_cte?(type=:select) 670: is_2005_or_later? 671: end
MSSQL supports insert_select via the OUTPUT clause.
# File lib/sequel/adapters/shared/mssql.rb, line 689 689: def supports_insert_select? 690: supports_output_clause? && !opts[:disable_insert_output] 691: end
Return a cloned dataset with the mssql_unicode_strings option set.
# File lib/sequel/adapters/shared/mssql.rb, line 519 519: def with_mssql_unicode_strings(v) 520: clone(:mssql_unicode_strings=>v) 521: 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 754 754: def _import(columns, values, opts=OPTS) 755: if opts[:return] == :primary_key && !@opts[:output] 756: output(nil, [SQL::QualifiedIdentifier.new(:inserted, first_primary_key)])._import(columns, values, opts) 757: elsif @opts[:output] 758: statements = multi_insert_sql(columns, values) 759: @db.transaction(opts.merge(:server=>@opts[:server])) do 760: statements.map{|st| with_sql(st)} 761: end.first.map{|v| v.length == 1 ? v.values.first : v} 762: else 763: super 764: end 765: end
If the dataset using a order without a limit or offset or custom SQL, remove the order. Compounds on Microsoft SQL Server have undefined order unless the result is specifically ordered. Applying the current order before the compound doesn‘t work in all cases, such as when qualified identifiers are used. If you want to ensure a order for a compound dataset, apply the order after all compounds have been added.
# File lib/sequel/adapters/shared/mssql.rb, line 779 779: def compound_from_self 780: if @opts[:offset] && !@opts[:limit] && !is_2012_or_later? 781: clone(:limit=>LIMIT_ALL).from_self 782: elsif @opts[:order] && !(@opts[:sql] || @opts[:limit] || @opts[:offset]) 783: unordered 784: else 785: super 786: end 787: end