Module Sequel::MSSQL
In: lib/sequel/adapters/shared/mssql.rb
lib/sequel/extensions/mssql_emulate_lateral_with_apply.rb

Methods

Included Modules

EmulateOffsetWithRowNumber

Classes and Modules

Module Sequel::MSSQL::DatabaseMethods
Module Sequel::MSSQL::DatasetMethods
Module Sequel::MSSQL::EmulateLateralWithApply

Constants

BOOL_TRUE = '1'.freeze
BOOL_FALSE = '0'.freeze
COMMA_SEPARATOR = ', '.freeze
TABLE_HINT = " WITH (".freeze
READPAST = "READPAST".freeze
NOLOCK = 'NOLOCK'.freeze
UPDLOCK = '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
DOUBLE_BRACKET_CLOSE = ']]'.freeze
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
OUTPUT_INSERTED = " OUTPUT INSERTED.*".freeze
HEX_START = '0x'.freeze
UNICODE_STRING_START = "N'".freeze
BACKSLASH_CRLF_RE = /\\((?:\r\n)|\n)/.freeze
BACKSLASH_CRLF_REPLACE = '\\\\\\\\\\1\\1'.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
DEFAULT_TIMESTAMP_FORMAT = "'%Y-%m-%dT%H:%M:%S%N%z'".freeze
FORMAT_DATE = "'%Y%m%d'".freeze
CROSS_APPLY = 'CROSS APPLY'.freeze
OUTER_APPLY = 'OUTER APPLY'.freeze
OFFSET = " OFFSET ".freeze
ROWS = " ROWS".freeze
ROWS_ONLY = " ROWS ONLY".freeze
FETCH_NEXT = " FETCH NEXT ".freeze

Attributes

mssql_unicode_strings  [W]  Allow overriding of the mssql_unicode_strings option at the dataset level.

Public Class methods

[Source]

    # File lib/sequel/adapters/shared/mssql.rb, line 10
10:     def self.mock_adapter_setup(db)
11:       db.instance_eval do
12:         @server_version = 11000000
13:       end
14:     end

Public Instance methods

MSSQL uses + for string concatenation, and LIKE is case insensitive by default.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 585
585:       def complex_expression_sql_append(sql, op, args)
586:         case op
587:         when '||''||'
588:           super(sql, :+, args)
589:         when :LIKE, "NOT LIKE""NOT LIKE"
590:           super(sql, op, args.map{|a| Sequel.lit(["(", " COLLATE #{CASE_SENSITIVE_COLLATION})"], a)})
591:         when :ILIKE, "NOT ILIKE""NOT ILIKE"
592:           super(sql, (op == :ILIKE ? :LIKE : "NOT LIKE""NOT LIKE"), args.map{|a| Sequel.lit(["(", " COLLATE #{CASE_INSENSITIVE_COLLATION})"], a)})
593:         when :<<, :>>
594:           complex_expression_emulate_append(sql, op, args)
595:         when :extract
596:           part = args.at(0)
597:           raise(Sequel::Error, "unsupported extract argument: #{part.inspect}") unless format = EXTRACT_MAP[part]
598:           if part == :second
599:             expr = args.at(1)
600:             sql << DATEPART_SECOND_OPEN << format.to_s << COMMA
601:             literal_append(sql, expr)
602:             sql << DATEPART_SECOND_MIDDLE
603:             literal_append(sql, expr)
604:             sql << DATEPART_SECOND_CLOSE
605:           else
606:             sql << DATEPART_OPEN << format.to_s << COMMA
607:             literal_append(sql, args.at(1))
608:             sql << PAREN_CLOSE
609:           end
610:         else
611:           super
612:         end
613:       end

MSSQL doesn‘t support the SQL standard CURRENT_DATE or CURRENT_TIME

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 616
616:       def constant_sql_append(sql, constant)
617:         if c = CONSTANT_MAP[constant]
618:           sql << c
619:         else
620:           super
621:         end
622:       end

Uses CROSS APPLY to join the given table into the current dataset.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 625
625:       def cross_apply(table)
626:         join_table(:cross_apply, table)
627:       end

Disable the use of INSERT OUTPUT

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 630
630:       def disable_insert_output
631:         clone(:disable_insert_output=>true)
632:       end

MSSQL treats [] as a metacharacter in LIKE expresions.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 635
635:       def escape_like(string)
636:         string.gsub(/[\\%_\[\]]/){|m| "\\#{m}"}
637:       end

MSSQL uses the CONTAINS keyword for full text search

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 640
640:       def full_text_search(cols, terms, opts = OPTS)
641:         terms = "\"#{terms.join('" OR "')}\"" if terms.is_a?(Array)
642:         filter("CONTAINS (?, ?)", cols, terms)
643:       end

Use the OUTPUT clause to get the value of all columns for the newly inserted record.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 646
646:       def insert_select(*values)
647:         return unless supports_insert_select?
648:         with_sql_first(insert_select_sql(*values))
649:       end

Add OUTPUT clause unless there is already an existing output clause, then return the SQL to insert.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 653
653:       def insert_select_sql(*values)
654:         ds = (opts[:output] || opts[:returning]) ? self : output(nil, [SQL::ColumnAll.new(:inserted)])
655:         ds.insert_sql(*values)
656:       end

Specify a table for a SELECT … INTO query.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 659
659:       def into(table)
660:         clone(:into => table)
661:       end

Use the database‘s mssql_unicode_strings setting if the dataset hasn‘t overridden it.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 580
580:       def mssql_unicode_strings
581:         defined?(@mssql_unicode_strings) ? @mssql_unicode_strings : (@mssql_unicode_strings = db.mssql_unicode_strings)
582:       end

Allows you to do a dirty read of uncommitted data using WITH (NOLOCK).

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 664
664:       def nolock
665:         lock_style(:dirty)
666:       end

Uses OUTER APPLY to join the given table into the current dataset.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 669
669:       def outer_apply(table)
670:         join_table(:outer_apply, table)
671:       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)

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 685
685:       def output(into, values)
686:         raise(Error, "SQL Server versions 2000 and earlier do not support the OUTPUT clause") unless supports_output_clause?
687:         output = {}
688:         case values
689:         when Hash
690:           output[:column_list], output[:select_list] = values.keys, values.values
691:         when Array
692:           output[:select_list] = values
693:         end
694:         output[:into] = into
695:         clone(:output => output)
696:       end

MSSQL uses [] to quote identifiers.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 699
699:       def quoted_identifier_append(sql, name)
700:         sql << BRACKET_OPEN << name.to_s.gsub(/\]/, DOUBLE_BRACKET_CLOSE) << BRACKET_CLOSE
701:       end

Emulate RETURNING using the output clause. This only handles values that are simple column references.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 704
704:       def returning(*values)
705:         values = values.map do |v|
706:           unless r = unqualified_column_for(v)
707:             raise(Error, "cannot emulate RETURNING via OUTPUT for value: #{v.inspect}")
708:           end
709:           r
710:         end
711:         clone(:returning=>values)
712:       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.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 718
718:       def select_sql
719:         if @opts[:offset] && !@opts[:order] && is_2012_or_later?
720:           order(1).select_sql
721:         else
722:           super
723:         end
724:       end

The version of the database server.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 727
727:       def server_version
728:         db.server_version(@opts[:server])
729:       end

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 731
731:       def supports_cte?(type=:select)
732:         is_2005_or_later?
733:       end

MSSQL 2005+ supports GROUP BY CUBE.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 736
736:       def supports_group_cube?
737:         is_2005_or_later?
738:       end

MSSQL 2005+ supports GROUP BY ROLLUP

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 741
741:       def supports_group_rollup?
742:         is_2005_or_later?
743:       end

MSSQL 2008+ supports GROUPING SETS

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 746
746:       def supports_grouping_sets?
747:         is_2008_or_later?
748:       end

MSSQL supports insert_select via the OUTPUT clause.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 751
751:       def supports_insert_select?
752:         supports_output_clause? && !opts[:disable_insert_output]
753:       end

MSSQL 2005+ supports INTERSECT and EXCEPT

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 756
756:       def supports_intersect_except?
757:         is_2005_or_later?
758:       end

MSSQL does not support IS TRUE

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 761
761:       def supports_is_true?
762:         false
763:       end

MSSQL doesn‘t support JOIN USING

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 766
766:       def supports_join_using?
767:         false
768:       end

MSSQL 2005+ supports modifying joined datasets

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 771
771:       def supports_modifying_joins?
772:         is_2005_or_later?
773:       end

MSSQL does not support multiple columns for the IN/NOT IN operators

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 776
776:       def supports_multiple_column_in?
777:         false
778:       end

MSSQL 2012+ supports offsets in correlated subqueries.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 781
781:       def supports_offsets_in_correlated_subqueries?
782:         is_2012_or_later?
783:       end

MSSQL 2005+ supports the OUTPUT clause.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 786
786:       def supports_output_clause?
787:         is_2005_or_later?
788:       end

MSSQL 2005+ can emulate RETURNING via the OUTPUT clause.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 791
791:       def supports_returning?(type)
792:         supports_insert_select?
793:       end

MSSQL uses READPAST to skip locked rows.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 796
796:       def supports_skip_locked?
797:         true
798:       end

MSSQL cannot use WHERE 1.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 806
806:       def supports_where_true?
807:         false
808:       end

MSSQL 2005+ supports window functions

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 801
801:       def supports_window_functions?
802:         true
803:       end

Protected Instance methods

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.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 816
816:       def _import(columns, values, opts=OPTS)
817:         if opts[:return] == :primary_key && !@opts[:output]
818:           output(nil, [SQL::QualifiedIdentifier.new(:inserted, first_primary_key)])._import(columns, values, opts)
819:         elsif @opts[:output]
820:           statements = multi_insert_sql(columns, values)
821:           @db.transaction(opts.merge(:server=>@opts[:server])) do
822:             statements.map{|st| with_sql(st)}
823:           end.first.map{|v| v.length == 1 ? v.values.first : v}
824:         else
825:           super
826:         end
827:       end

MSSQL does not allow ordering in sub-clauses unless ‘top’ (limit) is specified

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 830
830:       def aggregate_dataset
831:         (options_overlap(Sequel::Dataset::COUNT_FROM_SELF_OPTS) && !options_overlap([:limit])) ? unordered.from_self : super
832:       end

[Validate]