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

Methods

Included Modules

EmulateOffsetWithRowNumber

Constants

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 limit table set output from where')
UPDATE_CLAUSE_METHODS_2000 = Dataset.clause_methods(:update, %w'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
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
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
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

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 459
459:       def complex_expression_sql_append(sql, op, args)
460:         case op
461:         when '||''||'
462:           super(sql, :+, args)
463:         when :LIKE, "NOT LIKE""NOT LIKE"
464:           super(sql, op, args.map{|a| LiteralString.new("(#{literal(a)} COLLATE #{CASE_SENSITIVE_COLLATION})")})
465:         when :ILIKE, "NOT ILIKE""NOT ILIKE"
466:           super(sql, (op == :ILIKE ? :LIKE : "NOT LIKE""NOT LIKE"), args.map{|a| LiteralString.new("(#{literal(a)} COLLATE #{CASE_INSENSITIVE_COLLATION})")})
467:         when :<<
468:           sql << complex_expression_arg_pairs(args){|a, b| "(#{literal(a)} * POWER(2, #{literal(b)}))"}
469:         when :>>
470:           sql << complex_expression_arg_pairs(args){|a, b| "(#{literal(a)} / POWER(2, #{literal(b)}))"}
471:         when :extract
472:           part = args.at(0)
473:           raise(Sequel::Error, "unsupported extract argument: #{part.inspect}") unless format = EXTRACT_MAP[part]
474:           if part == :second
475:             expr = literal(args.at(1))
476:             sql << DATEPART_SECOND_OPEN << format.to_s << COMMA << expr << DATEPART_SECOND_MIDDLE << expr << DATEPART_SECOND_CLOSE
477:           else
478:             sql << DATEPART_OPEN << format.to_s << COMMA
479:             literal_append(sql, args.at(1))
480:             sql << PAREN_CLOSE
481:           end
482:         else
483:           super
484:         end
485:       end

MSSQL doesn‘t support the SQL standard CURRENT_DATE or CURRENT_TIME

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 488
488:       def constant_sql_append(sql, constant)
489:         if c = CONSTANT_MAP[constant]
490:           sql << c
491:         else
492:           super
493:         end
494:       end

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

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 497
497:       def cross_apply(table)
498:         join_table(:cross_apply, table)
499:       end

Disable the use of INSERT OUTPUT

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 502
502:       def disable_insert_output
503:         clone(:disable_insert_output=>true)
504:       end

There is no function on Microsoft SQL Server that does character length and respects trailing spaces (datalength respects trailing spaces, but counts bytes instead of characters). Use a hack to work around the trailing spaces issue.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 515
515:       def emulated_function_sql_append(sql, f)
516:         case f.f
517:         when :char_length
518:           literal_append(sql, SQL::Function.new(:len, Sequel.join([f.args.first, 'x'])) - 1)
519:         when :trim
520:           literal_append(sql, SQL::Function.new(:ltrim, SQL::Function.new(:rtrim, f.args.first)))
521:         else
522:           super
523:         end
524:       end

MSSQL treats [] as a metacharacter in LIKE expresions.

[Source]

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

MSSQL uses the CONTAINS keyword for full text search

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 527
527:       def full_text_search(cols, terms, opts = OPTS)
528:         terms = "\"#{terms.join('" OR "')}\"" if terms.is_a?(Array)
529:         filter("CONTAINS (?, ?)", cols, terms)
530:       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 533
533:       def insert_select(*values)
534:         return unless supports_insert_select?
535:         naked.clone(default_server_opts(:sql=>output(nil, [SQL::ColumnAll.new(:inserted)]).insert_sql(*values))).single_record
536:       end

Specify a table for a SELECT … INTO query.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 539
539:       def into(table)
540:         clone(:into => table)
541:       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 454
454:       def mssql_unicode_strings
455:         defined?(@mssql_unicode_strings) ? @mssql_unicode_strings : (@mssql_unicode_strings = db.mssql_unicode_strings)
456:       end

MSSQL uses a UNION ALL statement to insert multiple values at once.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 544
544:       def multi_insert_sql(columns, values)
545:         c = false
546:         sql = LiteralString.new('')
547:         u = UNION_ALL
548:         values.each do |v|
549:           sql << u if c
550:           sql << SELECT_SPACE
551:           expression_list_append(sql, v)
552:           c ||= true
553:         end
554:         [insert_sql(columns, sql)]
555:       end

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

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 558
558:       def nolock
559:         lock_style(:dirty)
560:       end

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

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 563
563:       def outer_apply(table)
564:         join_table(:outer_apply, table)
565:       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 579
579:       def output(into, values)
580:         raise(Error, "SQL Server versions 2000 and earlier do not support the OUTPUT clause") unless supports_output_clause?
581:         output = {}
582:         case values
583:           when Hash
584:             output[:column_list], output[:select_list] = values.keys, values.values
585:           when Array
586:             output[:select_list] = values
587:         end
588:         output[:into] = into
589:         clone({:output => output})
590:       end

MSSQL uses [] to quote identifiers.

[Source]

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

The version of the database server.

[Source]

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

MSSQL 2005+ supports GROUP BY CUBE.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 603
603:       def supports_group_cube?
604:         is_2005_or_later?
605:       end

MSSQL 2005+ supports GROUP BY ROLLUP

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 608
608:       def supports_group_rollup?
609:         is_2005_or_later?
610:       end

MSSQL supports insert_select via the OUTPUT clause.

[Source]

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

MSSQL 2005+ supports INTERSECT and EXCEPT

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 618
618:       def supports_intersect_except?
619:         is_2005_or_later?
620:       end

MSSQL does not support IS TRUE

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 623
623:       def supports_is_true?
624:         false
625:       end

MSSQL doesn‘t support JOIN USING

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 628
628:       def supports_join_using?
629:         false
630:       end

MSSQL 2005+ supports modifying joined datasets

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 633
633:       def supports_modifying_joins?
634:         is_2005_or_later?
635:       end

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

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 638
638:       def supports_multiple_column_in?
639:         false
640:       end

MSSQL 2005+ supports the output clause.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 643
643:       def supports_output_clause?
644:         is_2005_or_later?
645:       end

MSSQL cannot use WHERE 1.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 653
653:       def supports_where_true?
654:         false
655:       end

MSSQL 2005+ supports window functions

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 648
648:       def supports_window_functions?
649:         true
650:       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 663
663:       def _import(columns, values, opts=OPTS)
664:         if opts[:return] == :primary_key && !@opts[:output]
665:           output(nil, [SQL::QualifiedIdentifier.new(:inserted, first_primary_key)])._import(columns, values, opts)
666:         elsif @opts[:output]
667:           statements = multi_insert_sql(columns, values)
668:           @db.transaction(opts.merge(:server=>@opts[:server])) do
669:             statements.map{|st| with_sql(st)}
670:           end.first.map{|v| v.length == 1 ? v.values.first : v}
671:         else
672:           super
673:         end
674:       end

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

[Source]

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

[Validate]