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

Methods

Constants

BOOL_TRUE = '1'.freeze
BOOL_FALSE = '0'.freeze
COMMA_SEPARATOR = ', '.freeze
DELETE_CLAUSE_METHODS = Dataset.clause_methods(:delete, %w'with from output from2 where')
INSERT_CLAUSE_METHODS = Dataset.clause_methods(:insert, %w'with into columns output values')
SELECT_CLAUSE_METHODS = Dataset.clause_methods(:select, %w'with distinct limit columns into from lock join where group having order compounds')
UPDATE_CLAUSE_METHODS = Dataset.clause_methods(:update, %w'with 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}

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 250
250:       def initialize(db, opts={})
251:         super
252:         @mssql_unicode_strings = db.mssql_unicode_strings
253:       end

Public Instance methods

Ugly hack. While MSSQL supports TRUE and FALSE values, you can‘t actually specify them directly in SQL. Unfortunately, you also cannot use an integer value when a boolean is required. Also unforunately, you cannot use an expression that yields a boolean type in cases where in an integer type is needed, such as inserting into a bit field (the closest thing MSSQL has to a boolean).

In filters, SQL::BooleanConstants are used more, while in other places the ruby true/false values are used more, so use expressions that return booleans for SQL::BooleanConstants, and 1/0 for other places. The correct fix for this would require separate literalization paths for filters compared to other values, but that‘s more work than I want to do right now.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 267
267:       def boolean_constant_sql(constant)
268:         case constant
269:         when true
270:           '(1 = 1)'
271:         when false
272:           '(1 = 0)'
273:         else
274:           super
275:         end
276:       end

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

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 279
279:       def complex_expression_sql(op, args)
280:         case op
281:         when '||''||'
282:           super(:+, args)
283:         when :ILIKE
284:           super(:LIKE, args)
285:         when "NOT ILIKE""NOT ILIKE"
286:           super("NOT LIKE""NOT LIKE", args)
287:         when :<<
288:           "(#{literal(args[0])} * POWER(2, #{literal(args[1])}))"
289:         when :>>
290:           "(#{literal(args[0])} / POWER(2, #{literal(args[1])}))"
291:         else
292:           super(op, args)
293:         end
294:       end

MSSQL doesn‘t support the SQL standard CURRENT_DATE or CURRENT_TIME

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 297
297:       def constant_sql(constant)
298:         CONSTANT_MAP[constant] || super
299:       end

Disable the use of INSERT OUTPUT

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 302
302:       def disable_insert_output
303:         clone(:disable_insert_output=>true)
304:       end

Disable the use of INSERT OUTPUT, modifying the receiver

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 307
307:       def disable_insert_output!
308:         mutation_method(:disable_insert_output)
309:       end

When returning all rows, if an offset is used, delete the row_number column before yielding the row.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 313
313:       def fetch_rows(sql, &block)
314:         @opts[:offset] ? super(sql){|r| r.delete(row_number_column); yield r} : super(sql, &block)
315:       end

MSSQL uses the CONTAINS keyword for full text search

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 318
318:       def full_text_search(cols, terms, opts = {})
319:         filter("CONTAINS (#{literal(cols)}, #{literal(terms)})")
320:       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 323
323:       def insert_select(*values)
324:         return unless supports_insert_select?
325:         naked.clone(default_server_opts(:sql=>output(nil, [SQL::ColumnAll.new(:inserted)]).insert_sql(*values))).single_record
326:       end

Specify a table for a SELECT … INTO query.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 329
329:       def into(table)
330:         clone(:into => table)
331:       end

SQL Server does not support CTEs on subqueries, so move any CTEs on joined datasets to the top level. The user is responsible for resolving any name clashes this may cause.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 336
336:       def join_table(type, table, expr=nil, table_alias={}, &block)
337:         return super unless Dataset === table && table.opts[:with]
338:         clone(:with => (opts[:with] || []) + table.opts[:with]).join_table(type, table.clone(:with => nil), expr, table_alias, &block)
339:       end

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

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 342
342:       def multi_insert_sql(columns, values)
343:         [insert_sql(columns, LiteralString.new(values.map {|r| "SELECT #{expression_list(r)}" }.join(" UNION ALL ")))]
344:       end

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

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 347
347:       def nolock
348:         lock_style(:dirty)
349:       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 363
363:       def output(into, values)
364:         raise(Error, "SQL Server versions 2000 and earlier do not support the OUTPUT clause") unless supports_output_clause?
365:         output = {}
366:         case values
367:           when Hash
368:             output[:column_list], output[:select_list] = values.keys, values.values
369:           when Array
370:             output[:select_list] = values
371:         end
372:         output[:into] = into
373:         clone({:output => output})
374:       end

An output method that modifies the receiver.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 377
377:       def output!(into, values)
378:         mutation_method(:output, into, values)
379:       end

MSSQL uses [] to quote identifiers

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 382
382:       def quoted_identifier(name)
383:         "[#{name}]"
384:       end

MSSQL Requires the use of the ROW_NUMBER window function to emulate an offset. This implementation requires MSSQL 2005 or greater (offset can‘t be emulated well in MSSQL 2000).

The implementation is ugly, cloning the current dataset and modifying the clone to add a ROW_NUMBER window function (and some other things), then using the modified clone in a subselect which is selected from.

If offset is used, an order must be provided, because the use of ROW_NUMBER requires an order.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 396
396:       def select_sql
397:         return super unless o = @opts[:offset]
398:         raise(Error, 'MSSQL requires an order be provided if using an offset') unless order = @opts[:order]
399:         dsa1 = dataset_alias(1)
400:         rn = row_number_column
401:         subselect_sql(unlimited.
402:           unordered.
403:           select_append{ROW_NUMBER(:over, :order=>order){}.as(rn)}.
404:           from_self(:alias=>dsa1).
405:           limit(@opts[:limit]).
406:           where(SQL::Identifier.new(rn) > o))
407:       end

The version of the database server.

[Source]

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

MSSQL supports insert_select via the OUTPUT clause.

[Source]

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

MSSQL 2005+ supports INTERSECT and EXCEPT

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 420
420:       def supports_intersect_except?
421:         is_2005_or_later?
422:       end

MSSQL does not support IS TRUE

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 425
425:       def supports_is_true?
426:         false
427:       end

MSSQL doesn‘t support JOIN USING

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 430
430:       def supports_join_using?
431:         false
432:       end

MSSQL 2005+ supports modifying joined datasets

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 435
435:       def supports_modifying_joins?
436:         is_2005_or_later?
437:       end

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

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 440
440:       def supports_multiple_column_in?
441:         false
442:       end

MSSQL 2005+ supports the output clause.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 445
445:       def supports_output_clause?
446:         is_2005_or_later?
447:       end

MSSQL 2005+ supports window functions

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 450
450:       def supports_window_functions?
451:         true
452:       end

Protected Instance methods

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

[Source]

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

[Validate]