Module: Sequel::MSSQL::DatasetMethods

Included in:
ADO::MSSQL::Dataset, JDBC::MSSQL::Dataset, ODBC::MSSQL::Dataset
Defined in:
lib/sequel/adapters/shared/mssql.rb

Constant Summary collapse

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}

Instance Method Summary collapse

Instance Method Details

#complex_expression_sql(op, args) ⇒ Object

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



192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/sequel/adapters/shared/mssql.rb', line 192

def complex_expression_sql(op, args)
  case op
  when :'||'
    super(:+, args)
  when :ILIKE
    super(:LIKE, args)
  when :"NOT ILIKE"
    super(:"NOT LIKE", args)
  else
    super(op, args)
  end
end

#constant_sql(constant) ⇒ Object

MSSQL doesn’t support the SQL standard CURRENT_DATE or CURRENT_TIME



206
207
208
# File 'lib/sequel/adapters/shared/mssql.rb', line 206

def constant_sql(constant)
  CONSTANT_MAP[constant] || super
end

#disable_insert_outputObject

Disable the use of INSERT OUTPUT



211
212
213
# File 'lib/sequel/adapters/shared/mssql.rb', line 211

def disable_insert_output
  clone(:disable_insert_output=>true)
end

#disable_insert_output!Object

Disable the use of INSERT OUTPUT, modifying the receiver



216
217
218
# File 'lib/sequel/adapters/shared/mssql.rb', line 216

def disable_insert_output!
  mutation_method(:disable_insert_output)
end

#fetch_rows(sql, &block) ⇒ Object

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



222
223
224
# File 'lib/sequel/adapters/shared/mssql.rb', line 222

def fetch_rows(sql, &block)
  @opts[:offset] ? super(sql){|r| r.delete(row_number_column); yield r} : super(sql, &block)
end

#full_text_search(cols, terms, opts = {}) ⇒ Object

MSSQL uses the CONTAINS keyword for full text search



227
228
229
# File 'lib/sequel/adapters/shared/mssql.rb', line 227

def full_text_search(cols, terms, opts = {})
  filter("CONTAINS (#{literal(cols)}, #{literal(terms)})")
end

#insert_select(*values) ⇒ Object

Use the OUTPUT clause to get the value of all columns for the newly inserted record.



232
233
234
# File 'lib/sequel/adapters/shared/mssql.rb', line 232

def insert_select(*values)
  naked.clone(default_server_opts(:sql=>output(nil, [:inserted.*]).insert_sql(*values))).single_record unless opts[:disable_insert_output]
end

#multi_insert_sql(columns, values) ⇒ Object

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



237
238
239
# File 'lib/sequel/adapters/shared/mssql.rb', line 237

def multi_insert_sql(columns, values)
  [insert_sql(columns, LiteralString.new(values.map {|r| "SELECT #{expression_list(r)}" }.join(" UNION ALL ")))]
end

#nolockObject

Allows you to do .nolock on a query



242
243
244
# File 'lib/sequel/adapters/shared/mssql.rb', line 242

def nolock
  clone(:table_options => "(NOLOCK)")
end

#output(into, values) ⇒ Object

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)


258
259
260
261
262
263
264
265
266
267
268
# File 'lib/sequel/adapters/shared/mssql.rb', line 258

def output(into, values)
  output = {}
  case values
    when Hash
      output[:column_list], output[:select_list] = values.keys, values.values
    when Array
      output[:select_list] = values
  end
  output[:into] = into
  clone({:output => output})
end

#output!(into, values) ⇒ Object

An output method that modifies the receiver.



271
272
273
# File 'lib/sequel/adapters/shared/mssql.rb', line 271

def output!(into, values)
  mutation_method(:output, into, values)
end

#quoted_identifier(name) ⇒ Object

MSSQL uses [] to quote identifiers



276
277
278
# File 'lib/sequel/adapters/shared/mssql.rb', line 276

def quoted_identifier(name)
  "[#{name}]"
end

#select_sqlObject

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.

Raises:



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/sequel/adapters/shared/mssql.rb', line 290

def select_sql
  return super unless o = @opts[:offset]
  raise(Error, 'MSSQL requires an order be provided if using an offset') unless order = @opts[:order]
  dsa1 = dataset_alias(1)
  dsa2 = dataset_alias(2)
  rn = row_number_column
  unlimited.
    unordered.
    from_self(:alias=>dsa2).
    select{[WILDCARD, ROW_NUMBER(:over, :order=>order){}.as(rn)]}.
    from_self(:alias=>dsa1).
    limit(@opts[:limit]).
    where(rn > o).
    select_sql
end

#server_versionObject

The version of the database server.



307
308
309
# File 'lib/sequel/adapters/shared/mssql.rb', line 307

def server_version
  db.server_version(@opts[:server])
end

#supports_intersect_except?Boolean

Microsoft SQL Server does not support INTERSECT or EXCEPT



312
313
314
# File 'lib/sequel/adapters/shared/mssql.rb', line 312

def supports_intersect_except?
  false
end

#supports_is_true?Boolean

MSSQL does not support IS TRUE



317
318
319
# File 'lib/sequel/adapters/shared/mssql.rb', line 317

def supports_is_true?
  false
end

#supports_join_using?Boolean

MSSQL doesn’t support JOIN USING



322
323
324
# File 'lib/sequel/adapters/shared/mssql.rb', line 322

def supports_join_using?
  false
end

#supports_modifying_joins?Boolean

MSSQL 2005+ supports modifying joined datasets



327
328
329
# File 'lib/sequel/adapters/shared/mssql.rb', line 327

def supports_modifying_joins?
  true
end

#supports_multiple_column_in?Boolean

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



332
333
334
# File 'lib/sequel/adapters/shared/mssql.rb', line 332

def supports_multiple_column_in?
  false
end

#supports_window_functions?Boolean

MSSQL 2005+ supports window functions



337
338
339
# File 'lib/sequel/adapters/shared/mssql.rb', line 337

def supports_window_functions?
  true
end