Module: Sequel::MSSQL::DatasetMethods

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

### Methods

complex_expression_sql constant_sql disable_insert_output disable_insert_output! fetch_rows full_text_search insert_select multi_insert_sql nolock output output! quoted_identifier select_sql server_version supports_intersect_except? supports_is_true? supports_join_using? supports_multiple_column_in? supports_window_functions?

### 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 limit distinct columns from table_options join where group order having compounds')
UPDATE_CLAUSE_METHODS | = | Dataset.clause_methods(:update, %w'with table set output from where')
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

complex_expression_sql(op, args)

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

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 192
    192:       def complex_expression_sql(op, args)
    193:         case op
    194:         when '||''||'
    195:           super(:+, args)
    196:         when :ILIKE
    197:           super(:LIKE, args)
    198:         when "NOT ILIKE""NOT ILIKE"
    199:           super("NOT LIKE""NOT LIKE", args)
    200:         else
    201:           super(op, args)
    202:         end
    203:       end

constant_sql(constant)

MSSQL doesn‘t support the SQL standard CURRENT_DATE or CURRENT_TIME

[Source]

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

disable_insert_output()

Disable the use of INSERT OUTPUT

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 211
    211:       def disable_insert_output
    212:         clone(:disable_insert_output=>true)
    213:       end

disable_insert_output!()

Disable the use of INSERT OUTPUT, modifying the receiver

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 216
    216:       def disable_insert_output!
    217:         mutation_method(:disable_insert_output)
    218:       end

fetch_rows(sql) {|r| ...}

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 222
    222:       def fetch_rows(sql, &block)
    223:         @opts[:offset] ? super(sql){|r| r.delete(row_number_column); yield r} : super(sql, &block)
    224:       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 227
    227:       def full_text_search(cols, terms, opts = {})
    228:         filter("CONTAINS (#{literal(cols)}, #{literal(terms)})")
    229:       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 232
    232:       def insert_select(*values)
    233:         naked.clone(default_server_opts(:sql=>output(nil, [:inserted.*]).insert_sql(*values))).single_record unless opts[:disable_insert_output]
    234:       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 237
    237:       def multi_insert_sql(columns, values)
    238:         [insert_sql(columns, LiteralString.new(values.map {|r| "SELECT #{expression_list(r)}" }.join(" UNION ALL ")))]
    239:       end

nolock()

Allows you to do .nolock on a query

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 242
    242:       def nolock
    243:         clone(:table_options => "(NOLOCK)")
    244:       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 258
    258:       def output(into, values)
    259:         output = {}
    260:         case values
    261:           when Hash
    262:             output[:column_list], output[:select_list] = values.keys, values.values
    263:           when Array
    264:             output[:select_list] = values
    265:         end
    266:         output[:into] = into
    267:         clone({:output => output})
    268:       end

output!(into, values)

An output method that modifies the receiver.

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 271
    271:       def output!(into, values)
    272:         mutation_method(:output, into, values)
    273:       end

quoted_identifier(name)

MSSQL uses [] to quote identifiers

[Source]

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

select_sql()

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 CTE 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 290
    290:       def select_sql
    291:         return super unless o = @opts[:offset]
    292:         raise(Error, 'MSSQL requires an order be provided if using an offset') unless order = @opts[:order]
    293:         dsa1 = dataset_alias(1)
    294:         dsa2 = dataset_alias(2)
    295:         rn = row_number_column
    296:         unlimited.
    297:           unordered.
    298:           from_self(:alias=>dsa2).
    299:           select{[WILDCARD, ROW_NUMBER(:over, :order=>order){}.as(rn)]}.
    300:           from_self(:alias=>dsa1).
    301:           limit(@opts[:limit]).
    302:           where(rn > o).
    303:           select_sql
    304:       end

server_version()

The version of the database server.

[Source]

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

supports_intersect_except?()

Microsoft SQL Server does not support INTERSECT or EXCEPT

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 312
    312:       def supports_intersect_except?
    313:         false
    314:       end

supports_is_true?()

MSSQL does not support IS TRUE

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 317
    317:       def supports_is_true?
    318:         false
    319:       end

supports_join_using?()

MSSQL doesn‘t support JOIN USING

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 322
    322:       def supports_join_using?
    323:         false
    324:       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 327
    327:       def supports_multiple_column_in?
    328:         false
    329:       end

supports_window_functions?()

MSSQL 2005+ supports window functions

[Source]

         # File lib/sequel/adapters/shared/mssql.rb, line 332
    332:       def supports_window_functions?
    333:         true
    334:       end

[Validate]