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}

Public Instance methods

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

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 215
215:       def complex_expression_sql(op, args)
216:         case op
217:         when '||''||'
218:           super(:+, args)
219:         when :ILIKE
220:           super(:LIKE, args)
221:         when "NOT ILIKE""NOT ILIKE"
222:           super("NOT LIKE""NOT LIKE", args)
223:         when :<<
224:           "(#{literal(args[0])} * POWER(2, #{literal(args[1])}))"
225:         when :>>
226:           "(#{literal(args[0])} / POWER(2, #{literal(args[1])}))"
227:         else
228:           super(op, args)
229:         end
230:       end

MSSQL doesn‘t support the SQL standard CURRENT_DATE or CURRENT_TIME

[Source]

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

Disable the use of INSERT OUTPUT

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 238
238:       def disable_insert_output
239:         clone(:disable_insert_output=>true)
240:       end

Disable the use of INSERT OUTPUT, modifying the receiver

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 243
243:       def disable_insert_output!
244:         mutation_method(:disable_insert_output)
245:       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 249
249:       def fetch_rows(sql, &block)
250:         @opts[:offset] ? super(sql){|r| r.delete(row_number_column); yield r} : super(sql, &block)
251:       end

MSSQL uses the CONTAINS keyword for full text search

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 254
254:       def full_text_search(cols, terms, opts = {})
255:         filter("CONTAINS (#{literal(cols)}, #{literal(terms)})")
256:       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 259
259:       def insert_select(*values)
260:         return unless supports_output_clause?
261:         naked.clone(default_server_opts(:sql=>output(nil, [:inserted.*]).insert_sql(*values))).single_record unless opts[:disable_insert_output]
262:       end

Specify a table for a SELECT … INTO query.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 265
265:       def into(table)
266:         clone(:into => table)
267:       end

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

[Source]

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

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

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 275
275:       def nolock
276:         lock_style(:dirty)
277:       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 291
291:       def output(into, values)
292:         raise(Error, "SQL Server versions 2000 and earlier do not support the OUTPUT clause") unless supports_output_clause?
293:         output = {}
294:         case values
295:           when Hash
296:             output[:column_list], output[:select_list] = values.keys, values.values
297:           when Array
298:             output[:select_list] = values
299:         end
300:         output[:into] = into
301:         clone({:output => output})
302:       end

An output method that modifies the receiver.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 305
305:       def output!(into, values)
306:         mutation_method(:output, into, values)
307:       end

MSSQL uses [] to quote identifiers

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 310
310:       def quoted_identifier(name)
311:         "[#{name}]"
312:       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 324
324:       def select_sql
325:         return super unless o = @opts[:offset]
326:         raise(Error, 'MSSQL requires an order be provided if using an offset') unless order = @opts[:order]
327:         dsa1 = dataset_alias(1)
328:         rn = row_number_column
329:         subselect_sql(unlimited.
330:           unordered.
331:           select_append{ROW_NUMBER(:over, :order=>order){}.as(rn)}.
332:           from_self(:alias=>dsa1).
333:           limit(@opts[:limit]).
334:           where(SQL::Identifier.new(rn) > o))
335:       end

The version of the database server.

[Source]

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

Microsoft SQL Server does not support INTERSECT or EXCEPT

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 343
343:       def supports_intersect_except?
344:         false
345:       end

MSSQL does not support IS TRUE

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 348
348:       def supports_is_true?
349:         false
350:       end

MSSQL doesn‘t support JOIN USING

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 353
353:       def supports_join_using?
354:         false
355:       end

MSSQL 2005+ supports modifying joined datasets

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 358
358:       def supports_modifying_joins?
359:         true
360:       end

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

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 363
363:       def supports_multiple_column_in?
364:         false
365:       end

Only 2005+ supports the output clause.

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 368
368:       def supports_output_clause?
369:         server_version >= 9000000
370:       end

MSSQL 2005+ supports window functions

[Source]

     # File lib/sequel/adapters/shared/mssql.rb, line 373
373:       def supports_window_functions?
374:         true
375:       end

[Validate]