Module Sequel::MSSQL
In: lib/sequel/extensions/mssql_emulate_lateral_with_apply.rb
lib/sequel/adapters/shared/mssql.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
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
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 Instance methods

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

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 572
572:       def complex_expression_sql_append(sql, op, args)
573:         case op
574:         when '||''||'
575:           super(sql, :+, args)
576:         when :LIKE, "NOT LIKE""NOT LIKE"
577:           super(sql, op, args.map{|a| Sequel.lit(["(", " COLLATE #{CASE_SENSITIVE_COLLATION})"], a)})
578:         when :ILIKE, "NOT ILIKE""NOT ILIKE"
579:           super(sql, (op == :ILIKE ? :LIKE : "NOT LIKE""NOT LIKE"), args.map{|a| Sequel.lit(["(", " COLLATE #{CASE_INSENSITIVE_COLLATION})"], a)})
580:         when :<<, :>>
581:           complex_expression_emulate_append(sql, op, args)
582:         when :extract
583:           part = args.at(0)
584:           raise(Sequel::Error, "unsupported extract argument: #{part.inspect}") unless format = EXTRACT_MAP[part]
585:           if part == :second
586:             expr = args.at(1)
587:             sql << DATEPART_SECOND_OPEN << format.to_s << COMMA
588:             literal_append(sql, expr)
589:             sql << DATEPART_SECOND_MIDDLE
590:             literal_append(sql, expr)
591:             sql << DATEPART_SECOND_CLOSE
592:           else
593:             sql << DATEPART_OPEN << format.to_s << COMMA
594:             literal_append(sql, args.at(1))
595:             sql << PAREN_CLOSE
596:           end
597:         else
598:           super
599:         end
600:       end

MSSQL doesn‘t support the SQL standard CURRENT_DATE or CURRENT_TIME

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 603
603:       def constant_sql_append(sql, constant)
604:         if c = CONSTANT_MAP[constant]
605:           sql << c
606:         else
607:           super
608:         end
609:       end

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

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 612
612:       def cross_apply(table)
613:         join_table(:cross_apply, table)
614:       end

Disable the use of INSERT OUTPUT

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 617
617:       def disable_insert_output
618:         clone(:disable_insert_output=>true)
619:       end

MSSQL treats [] as a metacharacter in LIKE expresions.

[Source]

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

MSSQL uses the CONTAINS keyword for full text search

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 627
627:       def full_text_search(cols, terms, opts = OPTS)
628:         terms = "\"#{terms.join('" OR "')}\"" if terms.is_a?(Array)
629:         filter("CONTAINS (?, ?)", cols, terms)
630:       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 633
633:       def insert_select(*values)
634:         return unless supports_insert_select?
635:         with_sql_first(insert_select_sql(*values))
636:       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 640
640:       def insert_select_sql(*values)
641:         ds = (opts[:output] || opts[:returning]) ? self : output(nil, [SQL::ColumnAll.new(:inserted)])
642:         ds.insert_sql(*values)
643:       end

Specify a table for a SELECT … INTO query.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 646
646:       def into(table)
647:         clone(:into => table)
648:       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 567
567:       def mssql_unicode_strings
568:         defined?(@mssql_unicode_strings) ? @mssql_unicode_strings : (@mssql_unicode_strings = db.mssql_unicode_strings)
569:       end

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

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 651
651:       def nolock
652:         lock_style(:dirty)
653:       end

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

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 656
656:       def outer_apply(table)
657:         join_table(:outer_apply, table)
658:       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 672
672:       def output(into, values)
673:         raise(Error, "SQL Server versions 2000 and earlier do not support the OUTPUT clause") unless supports_output_clause?
674:         output = {}
675:         case values
676:         when Hash
677:           output[:column_list], output[:select_list] = values.keys, values.values
678:         when Array
679:           output[:select_list] = values
680:         end
681:         output[:into] = into
682:         clone(:output => output)
683:       end

MSSQL uses [] to quote identifiers.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 686
686:       def quoted_identifier_append(sql, name)
687:         sql << BRACKET_OPEN << name.to_s.gsub(/\]/, DOUBLE_BRACKET_CLOSE) << BRACKET_CLOSE
688:       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 691
691:       def returning(*values)
692:         values = values.map do |v|
693:           unless r = unqualified_column_for(v)
694:             raise(Error, "cannot emulate RETURNING via OUTPUT for value: #{v.inspect}")
695:           end
696:           r
697:         end
698:         clone(:returning=>values)
699:       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 705
705:       def select_sql
706:         if @opts[:offset] && !@opts[:order] && is_2012_or_later?
707:           order(1).select_sql
708:         else
709:           super
710:         end
711:       end

The version of the database server.

[Source]

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

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 718
718:       def supports_cte?(type=:select)
719:         is_2005_or_later?
720:       end

MSSQL 2005+ supports GROUP BY CUBE.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 723
723:       def supports_group_cube?
724:         is_2005_or_later?
725:       end

MSSQL 2005+ supports GROUP BY ROLLUP

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 728
728:       def supports_group_rollup?
729:         is_2005_or_later?
730:       end

MSSQL supports insert_select via the OUTPUT clause.

[Source]

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

MSSQL 2005+ supports INTERSECT and EXCEPT

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 738
738:       def supports_intersect_except?
739:         is_2005_or_later?
740:       end

MSSQL does not support IS TRUE

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 743
743:       def supports_is_true?
744:         false
745:       end

MSSQL doesn‘t support JOIN USING

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 748
748:       def supports_join_using?
749:         false
750:       end

MSSQL 2005+ supports modifying joined datasets

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 753
753:       def supports_modifying_joins?
754:         is_2005_or_later?
755:       end

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

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 758
758:       def supports_multiple_column_in?
759:         false
760:       end

MSSQL 2012+ supports offsets in correlated subqueries.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 763
763:       def supports_offsets_in_correlated_subqueries?
764:         is_2012_or_later?
765:       end

MSSQL 2005+ supports the OUTPUT clause.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 768
768:       def supports_output_clause?
769:         is_2005_or_later?
770:       end

MSSQL 2005+ can emulate RETURNING via the OUTPUT clause.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 773
773:       def supports_returning?(type)
774:         supports_insert_select?
775:       end

MSSQL cannot use WHERE 1.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 783
783:       def supports_where_true?
784:         false
785:       end

MSSQL 2005+ supports window functions

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 778
778:       def supports_window_functions?
779:         true
780:       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 793
793:       def _import(columns, values, opts=OPTS)
794:         if opts[:return] == :primary_key && !@opts[:output]
795:           output(nil, [SQL::QualifiedIdentifier.new(:inserted, first_primary_key)])._import(columns, values, opts)
796:         elsif @opts[:output]
797:           statements = multi_insert_sql(columns, values)
798:           @db.transaction(opts.merge(:server=>@opts[:server])) do
799:             statements.map{|st| with_sql(st)}
800:           end.first.map{|v| v.length == 1 ? v.values.first : v}
801:         else
802:           super
803:         end
804:       end

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

[Source]

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

[Validate]