Included Modules

Class Index [+]

Quicksearch

Sequel::MSSQL::DatasetMethods

Constants

BOOL_TRUE
BOOL_FALSE
COMMA_SEPARATOR
DELETE_CLAUSE_METHODS
INSERT_CLAUSE_METHODS
SELECT_CLAUSE_METHODS
UPDATE_CLAUSE_METHODS
NOLOCK
UPDLOCK
WILDCARD
CONSTANT_MAP
EXTRACT_MAP
BRACKET_CLOSE
BRACKET_OPEN
COMMA
PAREN_CLOSE
PAREN_SPACE_OPEN
SPACE
FROM
APOS
APOS_RE
DOUBLE_APOS
INTO
DATEPART_SECOND_OPEN
DATEPART_SECOND_MIDDLE
DATEPART_SECOND_CLOSE
DATEPART_OPEN
UNION_ALL
SELECT_SPACE
TIMESTAMP_USEC_FORMAT
OUTPUT_INSERTED
HEX_START
UNICODE_STRING_START
BACKSLASH_CRLF_RE
BACKSLASH_CRLF_REPLACE
TOP_PAREN
TOP
OUTPUT
HSTAR
CASE_SENSITIVE_COLLATION
CASE_INSENSITIVE_COLLATION
DEFAULT_TIMESTAMP_FORMAT
FORMAT_DATE

Attributes

mssql_unicode_strings[RW]

Allow overriding of the mssql_unicode_strings option at the dataset level.

Public Class Methods

new(db, opts={}) click to toggle source

Copy the mssql_unicode_strings option from the db object.

     # File lib/sequel/adapters/shared/mssql.rb, line 361
361:       def initialize(db, opts={})
362:         super
363:         @mssql_unicode_strings = db.mssql_unicode_strings
364:       end

Public Instance Methods

complex_expression_sql_append(sql, op, args) click to toggle source

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

     # File lib/sequel/adapters/shared/mssql.rb, line 367
367:       def complex_expression_sql_append(sql, op, args)
368:         case op
369:         when :'||'
370:           super(sql, :+, args)
371:         when :LIKE, :"NOT LIKE"
372:           super(sql, op, args.map{|a| LiteralString.new("(#{literal(a)} COLLATE #{CASE_SENSITIVE_COLLATION})")})
373:         when :ILIKE, :"NOT ILIKE"
374:           super(sql, (op == :ILIKE ? :LIKE : :"NOT LIKE"), args.map{|a| LiteralString.new("(#{literal(a)} COLLATE #{CASE_INSENSITIVE_COLLATION})")})
375:         when :<<
376:           sql << complex_expression_arg_pairs(args){|a, b| "(#{literal(a)} * POWER(2, #{literal(b)}))"}
377:         when :>>
378:           sql << complex_expression_arg_pairs(args){|a, b| "(#{literal(a)} / POWER(2, #{literal(b)}))"}
379:         when :extract
380:           part = args.at(0)
381:           raise(Sequel::Error, "unsupported extract argument: #{part.inspect}") unless format = EXTRACT_MAP[part]
382:           if part == :second
383:             expr = literal(args.at(1))
384:             sql << DATEPART_SECOND_OPEN << format.to_s << COMMA << expr << DATEPART_SECOND_MIDDLE << expr << DATEPART_SECOND_CLOSE
385:           else
386:             sql << DATEPART_OPEN << format.to_s << COMMA
387:             literal_append(sql, args.at(1))
388:             sql << PAREN_CLOSE
389:           end
390:         else
391:           super
392:         end
393:       end
constant_sql_append(sql, constant) click to toggle source

MSSQL doesn’t support the SQL standard CURRENT_DATE or CURRENT_TIME

     # File lib/sequel/adapters/shared/mssql.rb, line 396
396:       def constant_sql_append(sql, constant)
397:         if c = CONSTANT_MAP[constant]
398:           sql << c
399:         else
400:           super
401:         end
402:       end
disable_insert_output() click to toggle source

Disable the use of INSERT OUTPUT

     # File lib/sequel/adapters/shared/mssql.rb, line 405
405:       def disable_insert_output
406:         clone(:disable_insert_output=>true)
407:       end
disable_insert_output!() click to toggle source

Disable the use of INSERT OUTPUT, modifying the receiver

     # File lib/sequel/adapters/shared/mssql.rb, line 410
410:       def disable_insert_output!
411:         mutation_method(:disable_insert_output)
412:       end
full_text_search(cols, terms, opts = {}) click to toggle source

MSSQL uses the CONTAINS keyword for full text search

     # File lib/sequel/adapters/shared/mssql.rb, line 415
415:       def full_text_search(cols, terms, opts = {})
416:         terms = "\"#{terms.join('" OR "')}\"" if terms.is_a?(Array)
417:         filter("CONTAINS (?, ?)", cols, terms)
418:       end
insert_select(*values) click to toggle source

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

     # File lib/sequel/adapters/shared/mssql.rb, line 421
421:       def insert_select(*values)
422:         return unless supports_insert_select?
423:         naked.clone(default_server_opts(:sql=>output(nil, [SQL::ColumnAll.new(:inserted)]).insert_sql(*values))).single_record
424:       end
into(table) click to toggle source

Specify a table for a SELECT … INTO query.

     # File lib/sequel/adapters/shared/mssql.rb, line 427
427:       def into(table)
428:         clone(:into => table)
429:       end
multi_insert_sql(columns, values) click to toggle source

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

     # File lib/sequel/adapters/shared/mssql.rb, line 432
432:       def multi_insert_sql(columns, values)
433:         c = false
434:         sql = LiteralString.new('')
435:         u = UNION_ALL
436:         values.each do |v|
437:           sql << u if c
438:           sql << SELECT_SPACE
439:           expression_list_append(sql, v)
440:           c ||= true
441:         end
442:         [insert_sql(columns, sql)]
443:       end
nolock() click to toggle source

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

     # File lib/sequel/adapters/shared/mssql.rb, line 446
446:       def nolock
447:         lock_style(:dirty)
448:       end
output(into, values) click to toggle source

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 # or #.

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 462
462:       def output(into, values)
463:         raise(Error, "SQL Server versions 2000 and earlier do not support the OUTPUT clause") unless supports_output_clause?
464:         output = {}
465:         case values
466:           when Hash
467:             output[:column_list], output[:select_list] = values.keys, values.values
468:           when Array
469:             output[:select_list] = values
470:         end
471:         output[:into] = into
472:         clone({:output => output})
473:       end
output!(into, values) click to toggle source

An output method that modifies the receiver.

     # File lib/sequel/adapters/shared/mssql.rb, line 476
476:       def output!(into, values)
477:         mutation_method(:output, into, values)
478:       end
quoted_identifier_append(sql, name) click to toggle source

MSSQL uses [] to quote identifiers. MSSQL does not support escaping of ], so you cannot use that character in an identifier.

     # File lib/sequel/adapters/shared/mssql.rb, line 482
482:       def quoted_identifier_append(sql, name)
483:         sql << BRACKET_OPEN << name.to_s << BRACKET_CLOSE
484:       end
server_version() click to toggle source

The version of the database server.

     # File lib/sequel/adapters/shared/mssql.rb, line 487
487:       def server_version
488:         db.server_version(@opts[:server])
489:       end
supports_group_cube?() click to toggle source

MSSQL 2005+ supports GROUP BY CUBE.

     # File lib/sequel/adapters/shared/mssql.rb, line 492
492:       def supports_group_cube?
493:         is_2005_or_later?
494:       end
supports_group_rollup?() click to toggle source

MSSQL 2005+ supports GROUP BY ROLLUP

     # File lib/sequel/adapters/shared/mssql.rb, line 497
497:       def supports_group_rollup?
498:         is_2005_or_later?
499:       end
supports_insert_select?() click to toggle source

MSSQL supports insert_select via the OUTPUT clause.

     # File lib/sequel/adapters/shared/mssql.rb, line 502
502:       def supports_insert_select?
503:         supports_output_clause? && !opts[:disable_insert_output]
504:       end
supports_intersect_except?() click to toggle source

MSSQL 2005+ supports INTERSECT and EXCEPT

     # File lib/sequel/adapters/shared/mssql.rb, line 507
507:       def supports_intersect_except?
508:         is_2005_or_later?
509:       end
supports_is_true?() click to toggle source

MSSQL does not support IS TRUE

     # File lib/sequel/adapters/shared/mssql.rb, line 512
512:       def supports_is_true?
513:         false
514:       end
supports_join_using?() click to toggle source

MSSQL doesn’t support JOIN USING

     # File lib/sequel/adapters/shared/mssql.rb, line 517
517:       def supports_join_using?
518:         false
519:       end
supports_modifying_joins?() click to toggle source

MSSQL 2005+ supports modifying joined datasets

     # File lib/sequel/adapters/shared/mssql.rb, line 522
522:       def supports_modifying_joins?
523:         is_2005_or_later?
524:       end
supports_multiple_column_in?() click to toggle source

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

     # File lib/sequel/adapters/shared/mssql.rb, line 527
527:       def supports_multiple_column_in?
528:         false
529:       end
supports_output_clause?() click to toggle source

MSSQL 2005+ supports the output clause.

     # File lib/sequel/adapters/shared/mssql.rb, line 532
532:       def supports_output_clause?
533:         is_2005_or_later?
534:       end
supports_where_true?() click to toggle source

MSSQL cannot use WHERE 1.

     # File lib/sequel/adapters/shared/mssql.rb, line 542
542:       def supports_where_true?
543:         false
544:       end
supports_window_functions?() click to toggle source

MSSQL 2005+ supports window functions

     # File lib/sequel/adapters/shared/mssql.rb, line 537
537:       def supports_window_functions?
538:         true
539:       end

Protected Instance Methods

_import(columns, values, opts={}) click to toggle source

If returned primary keys are requested, use OUTPUT unless already set on the dataset. If OUTPUT is already set, use existing returning values. If OUTPUT is only set to return a single columns, return an array of just that column. Otherwise, return an array of hashes.

     # File lib/sequel/adapters/shared/mssql.rb, line 552
552:       def _import(columns, values, opts={})
553:         if opts[:return] == :primary_key && !@opts[:output]
554:           output(nil, [SQL::QualifiedIdentifier.new(:inserted, first_primary_key)])._import(columns, values, opts)
555:         elsif @opts[:output]
556:           statements = multi_insert_sql(columns, values)
557:           @db.transaction(opts.merge(:server=>@opts[:server])) do
558:             statements.map{|st| with_sql(st)}
559:           end.first.map{|v| v.length == 1 ? v.values.first : v}
560:         else
561:           super
562:         end
563:       end
aggregate_dataset() click to toggle source

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

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

Private Instance Methods

default_timestamp_format() click to toggle source

Use strict ISO-8601 format with T between date and time, since that is the format that is multilanguage and not DATEFORMAT dependent.

     # File lib/sequel/adapters/shared/mssql.rb, line 585
585:       def default_timestamp_format
586:         DEFAULT_TIMESTAMP_FORMAT
587:       end
delete_clause_methods() click to toggle source

MSSQL supports the OUTPUT clause for DELETE statements. It also allows prepending a WITH clause.

     # File lib/sequel/adapters/shared/mssql.rb, line 591
591:       def delete_clause_methods
592:         DELETE_CLAUSE_METHODS
593:       end
delete_from2_sql(sql) click to toggle source

MSSQL supports FROM clauses in DELETE and UPDATE statements.

     # File lib/sequel/adapters/shared/mssql.rb, line 602
602:       def delete_from2_sql(sql)
603:         if joined_dataset?
604:           select_from_sql(sql)
605:           select_join_sql(sql)
606:         end
607:       end
Also aliased as: update_from_sql
delete_from_sql(sql) click to toggle source

Only include the primary table in the main delete clause

     # File lib/sequel/adapters/shared/mssql.rb, line 596
596:       def delete_from_sql(sql)
597:         sql << FROM
598:         source_list_append(sql, @opts[:from][0..0])
599:       end
delete_output_sql(sql) click to toggle source
Alias for: output_sql
first_primary_key() click to toggle source

Return the first primary key for the current table. If this table has multiple primary keys, this will only return one of them. Used by #_import.

     # File lib/sequel/adapters/shared/mssql.rb, line 612
612:       def first_primary_key
613:         @db.schema(self).map{|k, v| k if v[:primary_key] == true}.compact.first
614:       end
format_timestamp_usec(usec) click to toggle source

MSSQL raises an error if you try to provide more than 3 decimal places for a fractional timestamp. This probably doesn’t work for smalldatetime fields.

     # File lib/sequel/adapters/shared/mssql.rb, line 619
619:       def format_timestamp_usec(usec)
620:         sprintf(TIMESTAMP_USEC_FORMAT, usec/1000)
621:       end
insert_clause_methods() click to toggle source

MSSQL supports the OUTPUT clause for INSERT statements. It also allows prepending a WITH clause.

     # File lib/sequel/adapters/shared/mssql.rb, line 625
625:       def insert_clause_methods
626:         INSERT_CLAUSE_METHODS
627:       end
insert_output_sql(sql) click to toggle source

Use OUTPUT INSERTED.* to return all columns of the inserted row, for use with the prepared statement code.

     # File lib/sequel/adapters/shared/mssql.rb, line 631
631:       def insert_output_sql(sql)
632:         if @opts.has_key?(:returning)
633:           sql << OUTPUT_INSERTED
634:         else
635:           output_sql(sql)
636:         end
637:       end
is_2005_or_later?() click to toggle source

Whether we are using SQL Server 2005 or later.

     # File lib/sequel/adapters/shared/mssql.rb, line 573
573:       def is_2005_or_later?
574:         server_version >= 9000000
575:       end
is_2008_or_later?() click to toggle source

Whether we are using SQL Server 2008 or later.

     # File lib/sequel/adapters/shared/mssql.rb, line 578
578:       def is_2008_or_later?
579:         server_version >= 10000000
580:       end
literal_blob_append(sql, v) click to toggle source

MSSQL uses a literal hexidecimal number for blob strings

     # File lib/sequel/adapters/shared/mssql.rb, line 640
640:       def literal_blob_append(sql, v)
641:         sql << HEX_START << v.unpack(HSTAR).first
642:       end
literal_date(v) click to toggle source

Use YYYYmmdd format, since that’s the only want that is multilanguage and not DATEFORMAT dependent.

     # File lib/sequel/adapters/shared/mssql.rb, line 646
646:       def literal_date(v)
647:         v.strftime(FORMAT_DATE)
648:       end
literal_false() click to toggle source

Use 0 for false on MSSQL

     # File lib/sequel/adapters/shared/mssql.rb, line 651
651:       def literal_false
652:         BOOL_FALSE
653:       end
literal_string_append(sql, v) click to toggle source

Optionally use unicode string syntax for all strings. Don’t double backslashes.

     # File lib/sequel/adapters/shared/mssql.rb, line 657
657:       def literal_string_append(sql, v)
658:         sql << (mssql_unicode_strings ? UNICODE_STRING_START : APOS)
659:         sql << v.gsub(APOS_RE, DOUBLE_APOS).gsub(BACKSLASH_CRLF_RE, BACKSLASH_CRLF_REPLACE) << APOS
660:       end
literal_true() click to toggle source

Use 1 for true on MSSQL

     # File lib/sequel/adapters/shared/mssql.rb, line 663
663:       def literal_true
664:         BOOL_TRUE
665:       end
output_sql(sql) click to toggle source

SQL fragment for MSSQL’s OUTPUT clause.

     # File lib/sequel/adapters/shared/mssql.rb, line 707
707:       def output_sql(sql)
708:         return unless supports_output_clause?
709:         return unless output = @opts[:output]
710:         sql << OUTPUT
711:         column_list_append(sql, output[:select_list])
712:         if into = output[:into]
713:           sql << INTO
714:           identifier_append(sql, into)
715:           if column_list = output[:column_list]
716:             sql << PAREN_SPACE_OPEN
717:             source_list_append(sql, column_list)
718:             sql << PAREN_CLOSE
719:           end
720:         end
721:       end
select_clause_methods() click to toggle source

MSSQL adds the limit before the columns

     # File lib/sequel/adapters/shared/mssql.rb, line 668
668:       def select_clause_methods
669:         SELECT_CLAUSE_METHODS
670:       end
select_into_sql(sql) click to toggle source
     # File lib/sequel/adapters/shared/mssql.rb, line 672
672:       def select_into_sql(sql)
673:         if i = @opts[:into]
674:           sql << INTO
675:           identifier_append(sql, i)
676:         end
677:       end
select_limit_sql(sql) click to toggle source

MSSQL uses TOP N for limit. For MSSQL 2005+ TOP (N) is used to allow the limit to be a bound variable.

     # File lib/sequel/adapters/shared/mssql.rb, line 681
681:       def select_limit_sql(sql)
682:         if l = @opts[:limit]
683:           if is_2005_or_later?
684:             sql << TOP_PAREN
685:             literal_append(sql, l)
686:             sql << PAREN_CLOSE
687:           else
688:             sql << TOP
689:             literal_append(sql, l)
690:           end
691:         end
692:       end
select_lock_sql(sql) click to toggle source

Support different types of locking styles

     # File lib/sequel/adapters/shared/mssql.rb, line 695
695:       def select_lock_sql(sql)
696:         case @opts[:lock]
697:         when :update
698:           sql << UPDLOCK
699:         when :dirty
700:           sql << NOLOCK
701:         else
702:           super
703:         end
704:       end
update_clause_methods() click to toggle source

MSSQL supports the OUTPUT clause for UPDATE statements. It also allows prepending a WITH clause.

     # File lib/sequel/adapters/shared/mssql.rb, line 727
727:       def update_clause_methods
728:         UPDATE_CLAUSE_METHODS
729:       end
update_from_sql(sql) click to toggle source
Alias for: delete_from2_sql
update_output_sql(sql) click to toggle source
Alias for: output_sql
update_table_sql(sql) click to toggle source

Only include the primary table in the main update clause

     # File lib/sequel/adapters/shared/mssql.rb, line 732
732:       def update_table_sql(sql)
733:         sql << SPACE
734:         source_list_append(sql, @opts[:from][0..0])
735:       end
uses_with_rollup?() click to toggle source
     # File lib/sequel/adapters/shared/mssql.rb, line 737
737:       def uses_with_rollup?
738:         !is_2008_or_later?
739:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.