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 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
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

Attributes

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

Public Class methods

Copy the mssql_unicode_strings option from the db object.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 361
361:       def initialize(db, opts={})
362:         super
363:         @mssql_unicode_strings = db.mssql_unicode_strings
364:       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 367
367:       def complex_expression_sql_append(sql, op, args)
368:         case op
369:         when '||''||'
370:           super(sql, :+, args)
371:         when :LIKE, "NOT LIKE""NOT LIKE"
372:           super(sql, op, args.map{|a| LiteralString.new("(#{literal(a)} COLLATE #{CASE_SENSITIVE_COLLATION})")})
373:         when :ILIKE, "NOT ILIKE""NOT ILIKE"
374:           super(sql, (op == :ILIKE ? :LIKE : "NOT LIKE""NOT LIKE"), args.map{|a| LiteralString.new("(#{literal(a)} COLLATE #{CASE_INSENSITIVE_COLLATION})")})
375:         when :<<
376:           sql << complex_expression_arg_pairs(args){|a, b| "(#{literal(a)} * POWER(2, #{literal(b)}))"}
377:         when :>>
378:           sql << complex_expression_arg_pairs(args){|a, b| "(#{literal(a)} / POWER(2, #{literal(b)}))"}
379:         when :extract
380:           part = args.at(0)
381:           raise(Sequel::Error, "unsupported extract argument: #{part.inspect}") unless format = EXTRACT_MAP[part]
382:           if part == :second
383:             expr = literal(args.at(1))
384:             sql << DATEPART_SECOND_OPEN << format.to_s << COMMA << expr << DATEPART_SECOND_MIDDLE << expr << DATEPART_SECOND_CLOSE
385:           else
386:             sql << DATEPART_OPEN << format.to_s << COMMA
387:             literal_append(sql, args.at(1))
388:             sql << PAREN_CLOSE
389:           end
390:         else
391:           super
392:         end
393:       end

MSSQL doesn‘t support the SQL standard CURRENT_DATE or CURRENT_TIME

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 396
396:       def constant_sql_append(sql, constant)
397:         if c = CONSTANT_MAP[constant]
398:           sql << c
399:         else
400:           super
401:         end
402:       end

Disable the use of INSERT OUTPUT

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 405
405:       def disable_insert_output
406:         clone(:disable_insert_output=>true)
407:       end

Disable the use of INSERT OUTPUT, modifying the receiver

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 410
410:       def disable_insert_output!
411:         mutation_method(:disable_insert_output)
412:       end

MSSQL uses the CONTAINS keyword for full text search

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 415
415:       def full_text_search(cols, terms, opts = {})
416:         terms = "\"#{terms.join('" OR "')}\"" if terms.is_a?(Array)
417:         filter("CONTAINS (?, ?)", cols, terms)
418:       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 421
421:       def insert_select(*values)
422:         return unless supports_insert_select?
423:         naked.clone(default_server_opts(:sql=>output(nil, [SQL::ColumnAll.new(:inserted)]).insert_sql(*values))).single_record
424:       end

Specify a table for a SELECT … INTO query.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 427
427:       def into(table)
428:         clone(:into => table)
429:       end

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

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 432
432:       def multi_insert_sql(columns, values)
433:         c = false
434:         sql = LiteralString.new('')
435:         u = UNION_ALL
436:         values.each do |v|
437:           sql << u if c
438:           sql << SELECT_SPACE
439:           expression_list_append(sql, v)
440:           c ||= true
441:         end
442:         [insert_sql(columns, sql)]
443:       end

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

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 446
446:       def nolock
447:         lock_style(:dirty)
448:       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 462
462:       def output(into, values)
463:         raise(Error, "SQL Server versions 2000 and earlier do not support the OUTPUT clause") unless supports_output_clause?
464:         output = {}
465:         case values
466:           when Hash
467:             output[:column_list], output[:select_list] = values.keys, values.values
468:           when Array
469:             output[:select_list] = values
470:         end
471:         output[:into] = into
472:         clone({:output => output})
473:       end

An output method that modifies the receiver.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 476
476:       def output!(into, values)
477:         mutation_method(:output, into, values)
478:       end

MSSQL uses [] to quote identifiers. MSSQL does not support escaping of ], so you cannot use that character in an identifier.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 482
482:       def quoted_identifier_append(sql, name)
483:         sql << BRACKET_OPEN << name.to_s << BRACKET_CLOSE
484:       end

The version of the database server.

[Source]

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

MSSQL 2005+ supports GROUP BY CUBE.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 492
492:       def supports_group_cube?
493:         is_2005_or_later?
494:       end

MSSQL 2005+ supports GROUP BY ROLLUP

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 497
497:       def supports_group_rollup?
498:         is_2005_or_later?
499:       end

MSSQL supports insert_select via the OUTPUT clause.

[Source]

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

MSSQL 2005+ supports INTERSECT and EXCEPT

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 507
507:       def supports_intersect_except?
508:         is_2005_or_later?
509:       end

MSSQL does not support IS TRUE

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 512
512:       def supports_is_true?
513:         false
514:       end

MSSQL doesn‘t support JOIN USING

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 517
517:       def supports_join_using?
518:         false
519:       end

MSSQL 2005+ supports modifying joined datasets

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 522
522:       def supports_modifying_joins?
523:         is_2005_or_later?
524:       end

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

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 527
527:       def supports_multiple_column_in?
528:         false
529:       end

MSSQL 2005+ supports the output clause.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 532
532:       def supports_output_clause?
533:         is_2005_or_later?
534:       end

MSSQL cannot use WHERE 1.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 542
542:       def supports_where_true?
543:         false
544:       end

MSSQL 2005+ supports window functions

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 537
537:       def supports_window_functions?
538:         true
539:       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 552
552:       def _import(columns, values, opts={})
553:         if opts[:return] == :primary_key && !@opts[:output]
554:           output(nil, [SQL::QualifiedIdentifier.new(:inserted, first_primary_key)])._import(columns, values, opts)
555:         elsif @opts[:output]
556:           statements = multi_insert_sql(columns, values)
557:           @db.transaction(opts.merge(:server=>@opts[:server])) do
558:             statements.map{|st| with_sql(st)}
559:           end.first.map{|v| v.length == 1 ? v.values.first : v}
560:         else
561:           super
562:         end
563:       end

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

[Source]

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

[Validate]