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

Dataset methods shared by datasets that use MySQL databases.

Methods

Constants

BOOL_TRUE = '1'.freeze
BOOL_FALSE = '0'.freeze
COMMA_SEPARATOR = ', '.freeze
FOR_SHARE = ' LOCK IN SHARE MODE'.freeze
SQL_CALC_FOUND_ROWS = ' SQL_CALC_FOUND_ROWS'.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 calc_found_rows 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

Sets up the select methods to use SQL_CALC_FOUND_ROWS option.

  dataset.calc_found_rows.limit(10)
  # SELECT SQL_CALC_FOUND_ROWS * FROM table LIMIT 10

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 357
357:       def calc_found_rows
358:         clone(:calc_found_rows => true)
359:       end

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 331
331:       def complex_expression_sql(op, args)
332:         case op
333:         when :~, '!~''!~', '~*''~*', '!~*''!~*', :LIKE, 'NOT LIKE''NOT LIKE', :ILIKE, 'NOT ILIKE''NOT ILIKE'
334:           "(#{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))})"
335:         when '||''||'
336:           if args.length > 1
337:             "CONCAT(#{args.collect{|a| literal(a)}.join(', ')})"
338:           else
339:             literal(args.at(0))
340:           end
341:         when 'B~''B~'
342:           "CAST(~#{literal(args.at(0))} AS SIGNED INTEGER)"
343:         else
344:           super(op, args)
345:         end
346:       end

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

[Source]

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

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 362
362:       def for_share
363:         lock_style(:share)
364:       end

Adds full text filter

[Source]

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

MySQL specific full text search syntax.

[Source]

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

MySQL allows HAVING clause on ungrouped datasets.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 377
377:       def having(*cond, &block)
378:         _filter(:having, *cond, &block)
379:       end

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 407
407:       def insert_ignore
408:         clone(:insert_ignore=>true)
409:       end

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 383
383:       def join_table(type, table, expr=nil, table_alias={}, &block)
384:         type = :inner if (type == :cross) && !expr.nil?
385:         raise(Sequel::Error, "MySQL doesn't support FULL OUTER JOIN") if type == :full_outer
386:         super(type, table, expr, table_alias, &block)
387:       end

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 391
391:       def join_type_sql(join_type)
392:         case join_type
393:         when :straight then 'STRAIGHT_JOIN'
394:         when :natural_inner then 'NATURAL LEFT JOIN'
395:         else super
396:         end
397:       end

MySQL specific syntax for inserting multiple values at once.

[Source]

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

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 430
430:       def on_duplicate_key_update(*args)
431:         clone(:on_duplicate_key_update => args)
432:       end

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 441
441:       def provides_accurate_rows_matched?
442:         false
443:       end

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 446
446:       def quoted_identifier(c)
447:         "`#{c}`"
448:       end

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 452
452:       def replace_sql(*values)
453:         clone(:replace=>true).insert_sql(*values)
454:       end

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 458
458:       def supports_distinct_on?
459:         true
460:       end

MySQL does not support INTERSECT or EXCEPT

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 463
463:       def supports_intersect_except?
464:         false
465:       end

MySQL supports modifying joined datasets

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 468
468:       def supports_modifying_joins?
469:         true
470:       end

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 475
475:       def supports_timestamp_usecs?
476:         false
477:       end

Protected Instance methods

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

[Source]

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

[Validate]