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

Dataset methods shared by datasets that use MySQL databases.

### Methods

calc_found_rows complex_expression_sql_append 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_append replace_sql supports_distinct_on? supports_group_rollup? supports_intersect_except? supports_modifying_joins? supports_ordered_distinct_on? supports_timestamp_usecs? update_ignore

### 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'delete from where order limit')
INSERT_CLAUSE_METHODS | = | Dataset.clause_methods(:insert, %w'insert ignore into columns values on_duplicate_key_update')
SELECT_CLAUSE_METHODS | = | Dataset.clause_methods(:select, %w'select distinct calc_found_rows columns from join where group having compounds order limit lock')
UPDATE_CLAUSE_METHODS | = | Dataset.clause_methods(:update, %w'update ignore table set where order limit')
SPACE | = | Dataset::SPACE
PAREN_OPEN | = | Dataset::PAREN_OPEN
PAREN_CLOSE | = | Dataset::PAREN_CLOSE
NOT_SPACE | = | Dataset::NOT_SPACE
FROM | = | Dataset::FROM
INSERT | = | Dataset::INSERT
COMMA | = | Dataset::COMMA
LIMIT | = | Dataset::LIMIT
GROUP_BY | = | Dataset::GROUP_BY
REGEXP | = | 'REGEXP'.freeze
LIKE | = | 'LIKE'.freeze
BINARY | = | 'BINARY '.freeze
CONCAT | = | "CONCAT".freeze
CAST_BITCOMP_OPEN | = | "CAST(~".freeze
CAST_BITCOMP_CLOSE | = | " AS SIGNED INTEGER)".freeze
STRAIGHT_JOIN | = | 'STRAIGHT_JOIN'.freeze
NATURAL_LEFT_JOIN | = | 'NATURAL LEFT JOIN'.freeze
BACKTICK | = | '`'.freeze
EMPTY_COLUMNS | = | " ()".freeze
EMPTY_VALUES | = | " VALUES ()".freeze
IGNORE | = | " IGNORE".freeze
REPLACE | = | 'REPLACE'.freeze
ON_DUPLICATE_KEY_UPDATE | = | " ON DUPLICATE KEY UPDATE ".freeze
EQ_VALUES | = | '=VALUES('.freeze
EQ | = | '='.freeze
WITH_ROLLUP | = | ' WITH ROLLUP'.freeze

### Public Instance methods

calc_found_rows()

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 418
    418:       def calc_found_rows
    419:         clone(:calc_found_rows => true)
    420:       end

complex_expression_sql_append(sql, op, args)

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

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 374
    374:       def complex_expression_sql_append(sql, op, args)
    375:         case op
    376:         when :IN, "NOT IN""NOT IN"
    377:           ds = args.at(1)
    378:           if ds.is_a?(Sequel::Dataset) && ds.opts[:limit]
    379:             super(sql, op, [args.at(0), ds.from_self])
    380:           else
    381:             super
    382:           end
    383:         when :~, '!~''!~', '~*''~*', '!~*''!~*', :LIKE, 'NOT LIKE''NOT LIKE', :ILIKE, 'NOT ILIKE''NOT ILIKE'
    384:           sql << PAREN_OPEN
    385:           literal_append(sql, args.at(0))
    386:           sql << SPACE
    387:           sql << 'NOT ' if ['NOT LIKE''NOT LIKE', 'NOT ILIKE''NOT ILIKE', '!~''!~', '!~*''!~*'].include?(op)
    388:           sql << ([:~, '!~''!~', '~*''~*', '!~*''!~*'].include?(op) ? REGEXP : LIKE)
    389:           sql << SPACE
    390:           sql << BINARY if [:~, '!~''!~', :LIKE, 'NOT LIKE''NOT LIKE'].include?(op)
    391:           literal_append(sql, args.at(1))
    392:           sql << PAREN_CLOSE
    393:         when '||''||'
    394:           if args.length > 1
    395:             sql << CONCAT
    396:             array_sql_append(sql, args)
    397:           else
    398:             literal_append(sql, args.at(0))
    399:           end
    400:         when 'B~''B~'
    401:           sql << CAST_BITCOMP_OPEN
    402:           literal_append(sql, args.at(0))
    403:           sql << CAST_BITCOMP_CLOSE
    404:         else
    405:           super
    406:         end
    407:       end

distinct(*args)

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

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 410
    410:       def distinct(*args)
    411:         args.empty? ? super : group(*args)
    412:       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 423
    423:       def for_share
    424:         lock_style(:share)
    425:       end

full_text_search(cols, terms, opts = {})

Adds full text filter

[Source]

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

full_text_sql(cols, terms, opts = {})

MySQL specific full text search syntax.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 433
    433:       def full_text_sql(cols, terms, opts = {})
    434:         terms = terms.join(' ') if terms.is_a?(Array)
    435:         SQL::PlaceholderLiteralString.new("MATCH ? AGAINST (?#{" IN BOOLEAN MODE" if opts[:boolean]})", [Array(cols), terms], true)
    436:       end

having(*cond, &block)

MySQL allows HAVING clause on ungrouped datasets.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 439
    439:       def having(*cond, &block)
    440:         _filter(:having, *cond, &block)
    441:       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 472
    472:       def insert_ignore
    473:         clone(:insert_ignore=>true)
    474:       end

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

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 445
    445:       def join_table(type, table, expr=nil, table_alias={}, &block)
    446:         type = :inner if (type == :cross) && !expr.nil?
    447:         raise(Sequel::Error, "MySQL doesn't support FULL OUTER JOIN") if type == :full_outer
    448:         super(type, table, expr, table_alias, &block)
    449:       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 453
    453:       def join_type_sql(join_type)
    454:         case join_type
    455:         when :straight
    456:           STRAIGHT_JOIN
    457:         when :natural_inner
    458:           NATURAL_LEFT_JOIN
    459:         else
    460:           super
    461:         end
    462:       end

multi_insert_sql(columns, values)

MySQL specific syntax for inserting multiple values at once.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 500
    500:       def multi_insert_sql(columns, values)
    501:         sql = LiteralString.new('VALUES ')
    502:         expression_list_append(sql, values.map{|r| Array(r)})
    503:         [insert_sql(columns, sql)]
    504:       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 495
    495:       def on_duplicate_key_update(*args)
    496:         clone(:on_duplicate_key_update => args)
    497:       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 508
    508:       def provides_accurate_rows_matched?
    509:         false
    510:       end

quoted_identifier_append(sql, c)

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

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 513
    513:       def quoted_identifier_append(sql, c)
    514:         sql << BACKTICK << c.to_s << BACKTICK
    515:       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 519
    519:       def replace_sql(*values)
    520:         clone(:replace=>true).insert_sql(*values)
    521:       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 525
    525:       def supports_distinct_on?
    526:         true
    527:       end

supports_group_rollup?()

MySQL supports GROUP BY WITH ROLLUP (but not CUBE)

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 530
    530:       def supports_group_rollup?
    531:         true
    532:       end

supports_intersect_except?()

MySQL does not support INTERSECT or EXCEPT

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 535
    535:       def supports_intersect_except?
    536:         false
    537:       end

supports_modifying_joins?()

MySQL supports modifying joined datasets

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 540
    540:       def supports_modifying_joins?
    541:         true
    542:       end

supports_ordered_distinct_on?()

MySQL‘s DISTINCT ON emulation using GROUP BY does not respect the queries ORDER BY clause.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 546
    546:       def supports_ordered_distinct_on?
    547:         false
    548:       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 553
    553:       def supports_timestamp_usecs?
    554:         false
    555:       end

update_ignore()

Sets up the update methods to use UPDATE IGNORE. Useful if you have a unique key and want to just skip updating rows that violate the unique key restriction.

      dataset.update_ignore.update({:name => 'a', :value => 1})
      # UPDATE IGNORE tablename SET name = 'a', value = 1

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 563
    563:       def update_ignore
    564:         clone(:update_ignore=>true)
    565:       end

[Validate]