Module | Sequel::MSSQL::DatasetMethods |
In: |
lib/sequel/adapters/shared/mssql.rb
|
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} |
MSSQL uses + for string concatenation, and LIKE is case insensitive by default.
# File lib/sequel/adapters/shared/mssql.rb, line 210 210: def complex_expression_sql(op, args) 211: case op 212: when '||''||' 213: super(:+, args) 214: when :ILIKE 215: super(:LIKE, args) 216: when "NOT ILIKE""NOT ILIKE" 217: super("NOT LIKE""NOT LIKE", args) 218: else 219: super(op, args) 220: end 221: end
Disable the use of INSERT OUTPUT
# File lib/sequel/adapters/shared/mssql.rb, line 229 229: def disable_insert_output 230: clone(:disable_insert_output=>true) 231: end
Disable the use of INSERT OUTPUT, modifying the receiver
# File lib/sequel/adapters/shared/mssql.rb, line 234 234: def disable_insert_output! 235: mutation_method(:disable_insert_output) 236: end
When returning all rows, if an offset is used, delete the row_number column before yielding the row.
# File lib/sequel/adapters/shared/mssql.rb, line 240 240: def fetch_rows(sql, &block) 241: @opts[:offset] ? super(sql){|r| r.delete(row_number_column); yield r} : super(sql, &block) 242: end
Use the OUTPUT clause to get the value of all columns for the newly inserted record.
# File lib/sequel/adapters/shared/mssql.rb, line 250 250: def insert_select(*values) 251: return unless supports_output_clause? 252: naked.clone(default_server_opts(:sql=>output(nil, [:inserted.*]).insert_sql(*values))).single_record unless opts[:disable_insert_output] 253: end
Specify a table for a SELECT … INTO query.
# File lib/sequel/adapters/shared/mssql.rb, line 256 256: def into(table) 257: clone(:into => table) 258: end
Allows you to do a dirty read of uncommitted data using WITH (NOLOCK).
# File lib/sequel/adapters/shared/mssql.rb, line 266 266: def nolock 267: lock_style(:dirty) 268: 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)
# File lib/sequel/adapters/shared/mssql.rb, line 282 282: def output(into, values) 283: raise(Error, "SQL Server versions 2000 and earlier do not support the OUTPUT clause") unless supports_output_clause? 284: output = {} 285: case values 286: when Hash 287: output[:column_list], output[:select_list] = values.keys, values.values 288: when Array 289: output[:select_list] = values 290: end 291: output[:into] = into 292: clone({:output => output}) 293: 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.
# File lib/sequel/adapters/shared/mssql.rb, line 315 315: def select_sql 316: return super unless o = @opts[:offset] 317: raise(Error, 'MSSQL requires an order be provided if using an offset') unless order = @opts[:order] 318: dsa1 = dataset_alias(1) 319: rn = row_number_column 320: subselect_sql(unlimited. 321: unordered. 322: select_append{ROW_NUMBER(:over, :order=>order){}.as(rn)}. 323: from_self(:alias=>dsa1). 324: limit(@opts[:limit]). 325: where(SQL::Identifier.new(rn) > o)) 326: end
The version of the database server.
# File lib/sequel/adapters/shared/mssql.rb, line 329 329: def server_version 330: db.server_version(@opts[:server]) 331: end