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

### Methods

_import aggregate_dataset complex_expression_sql_append constant_sql_append disable_insert_output disable_insert_output! full_text_search insert_select into multi_insert_sql new nolock output output! quoted_identifier_append server_version supports_group_cube? supports_group_rollup? supports_insert_select? supports_intersect_except? supports_is_true? supports_join_using? supports_modifying_joins? supports_multiple_column_in? supports_output_clause? supports_where_true? supports_window_functions?

### 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')
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

new(db, opts={})

Copy the mssql_unicode_strings option from the `db` object.

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 371
    371:       def initialize(db, opts={})
    372:         super
    373:         @mssql_unicode_strings = db.mssql_unicode_strings
    374:       end

### Public Instance methods

complex_expression_sql_append(sql, op, args)

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

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 377
    377:       def complex_expression_sql_append(sql, op, args)
    378:         case op
    379:         when '||''||'
    380:           super(sql, :+, args)
    381:         when :LIKE, "NOT LIKE""NOT LIKE"
    382:           super(sql, op, args.map{|a| LiteralString.new("(#{literal(a)} COLLATE #{CASE_SENSITIVE_COLLATION})")})
    383:         when :ILIKE, "NOT ILIKE""NOT ILIKE"
    384:           super(sql, (op == :ILIKE ? :LIKE : "NOT LIKE""NOT LIKE"), args.map{|a| LiteralString.new("(#{literal(a)} COLLATE #{CASE_INSENSITIVE_COLLATION})")})
    385:         when :<<
    386:           sql << complex_expression_arg_pairs(args){|a, b| "(#{literal(a)} * POWER(2, #{literal(b)}))"}
    387:         when :>>
    388:           sql << complex_expression_arg_pairs(args){|a, b| "(#{literal(a)} / POWER(2, #{literal(b)}))"}
    389:         when :extract
    390:           part = args.at(0)
    391:           raise(Sequel::Error, "unsupported extract argument: #{part.inspect}") unless format = EXTRACT_MAP[part]
    392:           if part == :second
    393:             expr = literal(args.at(1))
    394:             sql << DATEPART_SECOND_OPEN << format.to_s << COMMA << expr << DATEPART_SECOND_MIDDLE << expr << DATEPART_SECOND_CLOSE
    395:           else
    396:             sql << DATEPART_OPEN << format.to_s << COMMA
    397:             literal_append(sql, args.at(1))
    398:             sql << PAREN_CLOSE
    399:           end
    400:         else
    401:           super
    402:         end
    403:       end

constant_sql_append(sql, constant)

MSSQL doesn‘t support the SQL standard CURRENT_DATE or CURRENT_TIME

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 406
    406:       def constant_sql_append(sql, constant)
    407:         if c = CONSTANT_MAP[constant]
    408:           sql << c
    409:         else
    410:           super
    411:         end
    412:       end

disable_insert_output()

Disable the use of INSERT OUTPUT

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 415
    415:       def disable_insert_output
    416:         clone(:disable_insert_output=>true)
    417:       end

disable_insert_output!()

Disable the use of INSERT OUTPUT, modifying the receiver

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 420
    420:       def disable_insert_output!
    421:         mutation_method(:disable_insert_output)
    422:       end

full_text_search(cols, terms, opts = {})

MSSQL uses the CONTAINS keyword for full text search

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 425
    425:       def full_text_search(cols, terms, opts = {})
    426:         terms = "\"#{terms.join('" OR "')}\"" if terms.is_a?(Array)
    427:         filter("CONTAINS (?, ?)", cols, terms)
    428:       end

insert_select(*values)

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 431
    431:       def insert_select(*values)
    432:         return unless supports_insert_select?
    433:         naked.clone(default_server_opts(:sql=>output(nil, [SQL::ColumnAll.new(:inserted)]).insert_sql(*values))).single_record
    434:       end

into(table)

Specify a table for a SELECT … INTO query.

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 437
    437:       def into(table)
    438:         clone(:into => table)
    439:       end

multi_insert_sql(columns, values)

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

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 442
    442:       def multi_insert_sql(columns, values)
    443:         c = false
    444:         sql = LiteralString.new('')
    445:         u = UNION_ALL
    446:         values.each do |v|
    447:           sql << u if c
    448:           sql << SELECT_SPACE
    449:           expression_list_append(sql, v)
    450:           c ||= true
    451:         end
    452:         [insert_sql(columns, sql)]
    453:       end

nolock()

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

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 456
    456:       def nolock
    457:         lock_style(:dirty)
    458:       end

output(into, values)

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 472
    472:       def output(into, values)
    473:         raise(Error, "SQL Server versions 2000 and earlier do not support the OUTPUT clause") unless supports_output_clause?
    474:         output = {}
    475:         case values
    476:           when Hash
    477:             output[:column_list], output[:select_list] = values.keys, values.values
    478:           when Array
    479:             output[:select_list] = values
    480:         end
    481:         output[:into] = into
    482:         clone({:output => output})
    483:       end

output!(into, values)

An output method that modifies the receiver.

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 486
    486:       def output!(into, values)
    487:         mutation_method(:output, into, values)
    488:       end

quoted_identifier_append(sql, name)

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 492
    492:       def quoted_identifier_append(sql, name)
    493:         sql << BRACKET_OPEN << name.to_s << BRACKET_CLOSE
    494:       end

server_version()

The version of the database server.

[Source]

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

supports_group_cube?()

MSSQL 2005+ supports GROUP BY CUBE.

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 502
    502:       def supports_group_cube?
    503:         is_2005_or_later?
    504:       end

supports_group_rollup?()

MSSQL 2005+ supports GROUP BY ROLLUP

[Source]

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

supports_insert_select?()

MSSQL supports insert_select via the OUTPUT clause.

[Source]

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

supports_intersect_except?()

MSSQL 2005+ supports INTERSECT and EXCEPT

[Source]

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

supports_is_true?()

MSSQL does not support IS TRUE

[Source]

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

supports_join_using?()

MSSQL doesn‘t support JOIN USING

[Source]

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

supports_modifying_joins?()

MSSQL 2005+ supports modifying joined datasets

[Source]

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

supports_multiple_column_in?()

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

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 537
    537:       def supports_multiple_column_in?
    538:         false
    539:       end

supports_output_clause?()

MSSQL 2005+ supports the output clause.

[Source]

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

supports_where_true?()

MSSQL cannot use WHERE 1.

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 552
    552:       def supports_where_true?
    553:         false
    554:       end

supports_window_functions?()

MSSQL 2005+ supports window functions

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 547
    547:       def supports_window_functions?
    548:         true
    549:       end

### Protected Instance methods

_import(columns, values, opts={})

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 562
    562:       def _import(columns, values, opts={})
    563:         if opts[:return] == :primary_key && !@opts[:output]
    564:           output(nil, [SQL::QualifiedIdentifier.new(:inserted, first_primary_key)])._import(columns, values, opts)
    565:         elsif @opts[:output]
    566:           statements = multi_insert_sql(columns, values)
    567:           @db.transaction(opts.merge(:server=>@opts[:server])) do
    568:             statements.map{|st| with_sql(st)}
    569:           end.first.map{|v| v.length == 1 ? v.values.first : v}
    570:         else
    571:           super
    572:         end
    573:       end

aggregate_dataset()

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

[Source]

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

[Validate]