Module | Sequel::MySQL::DatasetMethods
In: | lib/sequel/adapters/shared/mysql.rb

Dataset methods shared by datasets that use MySQL databases.

### Methods

_insert_sql complex_expression_sql distinct for_share full_text_search full_text_sql having insert_ignore join_table join_type_sql multi_insert_sql on_duplicate_key_update provides_accurate_rows_matched? quoted_identifier replace_sql supports_distinct_on? supports_intersect_except? supports_modifying_joins? supports_timestamp_usecs?

### Constants

BOOL_TRUE | = | '1'.freeze
BOOL_FALSE | = | '0'.freeze
COMMA_SEPARATOR | = | ', '.freeze
FOR_SHARE | = | ' LOCK IN SHARE MODE'.freeze
DELETE_CLAUSE_METHODS | = | Dataset.clause_methods(:delete, %w'from where order limit')
INSERT_CLAUSE_METHODS | = | Dataset.clause_methods(:insert, %w'ignore into columns values on_duplicate_key_update')
SELECT_CLAUSE_METHODS | = | Dataset.clause_methods(:select, %w'distinct columns from join where group having compounds order limit lock')
UPDATE_CLAUSE_METHODS | = | Dataset.clause_methods(:update, %w'table set where order limit')

### Public Instance methods

complex_expression_sql(op, args)

MySQL specific syntax for LIKE/REGEXP searches, as well as string concatenation.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 302
    302:       def complex_expression_sql(op, args)
    303:         case op
    304:         when :~, '!~''!~', '~*''~*', '!~*''!~*', :LIKE, 'NOT LIKE''NOT LIKE', :ILIKE, 'NOT ILIKE''NOT ILIKE'
    305:           "(#{literal(args.at(0))} #{'NOT ' if [:'NOT LIKE', :'NOT ILIKE', :'!~', :'!~*'].include?(op)}#{[:~, :'!~', :'~*', :'!~*'].include?(op) ? 'REGEXP' : 'LIKE'} #{'BINARY ' if [:~, :'!~', :LIKE, :'NOT LIKE'].include?(op)}#{literal(args.at(1))})"
    306:         when '||''||'
    307:           if args.length > 1
    308:             "CONCAT(#{args.collect{|a| literal(a)}.join(', ')})"
    309:           else
    310:             literal(args.at(0))
    311:           end
    312:         when 'B~''B~'
    313:           "CAST(~#{literal(args.at(0))} AS SIGNED INTEGER)"
    314:         else
    315:           super(op, args)
    316:         end
    317:       end

distinct(*args)

Use GROUP BY instead of DISTINCT ON if arguments are provided.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 320
    320:       def distinct(*args)
    321:         args.empty? ? super : group(*args)
    322:       end

for_share()

Return a cloned dataset which will use LOCK IN SHARE MODE to lock returned rows.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 325
    325:       def for_share
    326:         lock_style(:share)
    327:       end

full_text_search(cols, terms, opts = {})

Adds full text filter

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 330
    330:       def full_text_search(cols, terms, opts = {})
    331:         filter(full_text_sql(cols, terms, opts))
    332:       end

full_text_sql(cols, term, opts = {})

MySQL specific full text search syntax.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 335
    335:       def full_text_sql(cols, term, opts = {})
    336:         "MATCH #{literal(Array(cols))} AGAINST (#{literal(Array(term).join(' '))}#{" IN BOOLEAN MODE" if opts[:boolean]})"
    337:       end

having(*cond, &block)

MySQL allows HAVING clause on ungrouped datasets.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 340
    340:       def having(*cond, &block)
    341:         _filter(:having, *cond, &block)
    342:       end

insert_ignore()

Sets up the insert methods to use INSERT IGNORE. Useful if you have a unique key and want to just skip inserting rows that violate the unique key restriction.

      dataset.insert_ignore.multi_insert(
       [{:name => 'a', :value => 1}, {:name => 'b', :value => 2}]
      )
      # INSERT IGNORE INTO tablename (name, value) VALUES (a, 1), (b, 2)

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 370
    370:       def insert_ignore
    371:         clone(:insert_ignore=>true)
    372:       end

join_table(type, table, expr=nil, table_alias={})

Transforms an CROSS JOIN to an INNER JOIN if the expr is not nil. Raises an error on use of :full_outer type, since MySQL doesn‘t support it.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 346
    346:       def join_table(type, table, expr=nil, table_alias={})
    347:         type = :inner if (type == :cross) && !expr.nil?
    348:         raise(Sequel::Error, "MySQL doesn't support FULL OUTER JOIN") if type == :full_outer
    349:         super(type, table, expr, table_alias)
    350:       end

join_type_sql(join_type)

Transforms :natural_inner to NATURAL LEFT JOIN and straight to STRAIGHT_JOIN.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 354
    354:       def join_type_sql(join_type)
    355:         case join_type
    356:         when :straight then 'STRAIGHT_JOIN'
    357:         when :natural_inner then 'NATURAL LEFT JOIN'
    358:         else super
    359:         end
    360:       end

multi_insert_sql(columns, values)

MySQL specific syntax for inserting multiple values at once.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 398
    398:       def multi_insert_sql(columns, values)
    399:         [insert_sql(columns, LiteralString.new('VALUES ' + values.map {|r| literal(Array(r))}.join(COMMA_SEPARATOR)))]
    400:       end

on_duplicate_key_update(*args)

Sets up the insert methods to use ON DUPLICATE KEY UPDATE If you pass no arguments, ALL fields will be updated with the new values. If you pass the fields you want then ONLY those field will be updated.

Useful if you have a unique key and want to update inserting rows that violate the unique key restriction.

      dataset.on_duplicate_key_update.multi_insert(
       [{:name => 'a', :value => 1}, {:name => 'b', :value => 2}]
      )
      # INSERT INTO tablename (name, value) VALUES (a, 1), (b, 2)
      # ON DUPLICATE KEY UPDATE name=VALUES(name), value=VALUES(value)

      dataset.on_duplicate_key_update(:value).multi_insert(
        [{:name => 'a', :value => 1}, {:name => 'b', :value => 2}]
      )
      # INSERT INTO tablename (name, value) VALUES (a, 1), (b, 2)
      # ON DUPLICATE KEY UPDATE value=VALUES(value)

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 393
    393:       def on_duplicate_key_update(*args)
    394:         clone(:on_duplicate_key_update => args)
    395:       end

provides_accurate_rows_matched?()

MySQL uses the number of rows actually modified in the update, instead of the number of matched by the filter.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 404
    404:       def provides_accurate_rows_matched?
    405:         false
    406:       end

quoted_identifier(c)

MySQL uses the nonstandard ` (backtick) for quoting identifiers.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 409
    409:       def quoted_identifier(c)
    410:         "`#{c}`"
    411:       end

replace_sql(*values)

MySQL specific syntax for REPLACE (aka UPSERT, or update if exists, insert if it doesn‘t).

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 415
    415:       def replace_sql(*values)
    416:         clone(:replace=>true).insert_sql(*values)
    417:       end

supports_distinct_on?()

MySQL can emulate DISTINCT ON with its non-standard GROUP BY implementation, though the rows returned cannot be made deterministic through ordering.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 421
    421:       def supports_distinct_on?
    422:         true
    423:       end

supports_intersect_except?()

MySQL does not support INTERSECT or EXCEPT

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 426
    426:       def supports_intersect_except?
    427:         false
    428:       end

supports_modifying_joins?()

MySQL supports modifying joined datasets

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 431
    431:       def supports_modifying_joins?
    432:         true
    433:       end

supports_timestamp_usecs?()

MySQL does support fractional timestamps in literal timestamps, but it ignores them. Also, using them seems to cause problems on 1.9. Since they are ignored anyway, not using them is probably best.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 438
    438:       def supports_timestamp_usecs?
    439:         false
    440:       end

### Protected Instance methods

_insert_sql()

If this is an replace instead of an insert, use replace instead

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 445
    445:       def _insert_sql
    446:         @opts[:replace] ? clause_sql(:replace) : super
    447:       end

[Validate]