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_intersect_except? supports_modifying_joins? supports_ordered_distinct_on? supports_timestamp_usecs?

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

### 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 411
    411:       def calc_found_rows
    412:         clone(:calc_found_rows => true)
    413:       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 367
    367:       def complex_expression_sql_append(sql, op, args)
    368:         case op
    369:         when :IN, "NOT IN""NOT IN"
    370:           ds = args.at(1)
    371:           if ds.is_a?(Sequel::Dataset) && ds.opts[:limit]
    372:             super(sql, op, [args.at(0), ds.from_self])
    373:           else
    374:             super
    375:           end
    376:         when :~, '!~''!~', '~*''~*', '!~*''!~*', :LIKE, 'NOT LIKE''NOT LIKE', :ILIKE, 'NOT ILIKE''NOT ILIKE'
    377:           sql << PAREN_OPEN
    378:           literal_append(sql, args.at(0))
    379:           sql << SPACE
    380:           sql << 'NOT ' if ['NOT LIKE''NOT LIKE', 'NOT ILIKE''NOT ILIKE', '!~''!~', '!~*''!~*'].include?(op)
    381:           sql << ([:~, '!~''!~', '~*''~*', '!~*''!~*'].include?(op) ? REGEXP : LIKE)
    382:           sql << SPACE
    383:           sql << BINARY if [:~, '!~''!~', :LIKE, 'NOT LIKE''NOT LIKE'].include?(op)
    384:           literal_append(sql, args.at(1))
    385:           sql << PAREN_CLOSE
    386:         when '||''||'
    387:           if args.length > 1
    388:             sql << CONCAT
    389:             array_sql_append(sql, args)
    390:           else
    391:             literal_append(sql, args.at(0))
    392:           end
    393:         when 'B~''B~'
    394:           sql << CAST_BITCOMP_OPEN
    395:           literal_append(sql, args.at(0))
    396:           sql << CAST_BITCOMP_CLOSE
    397:         else
    398:           super
    399:         end
    400:       end

distinct(*args)

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

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 403
    403:       def distinct(*args)
    404:         args.empty? ? super : group(*args)
    405:       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 416
    416:       def for_share
    417:         lock_style(:share)
    418:       end

full_text_search(cols, terms, opts = {})

Adds full text filter

[Source]

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

full_text_sql(cols, term, opts = {})

MySQL specific full text search syntax.

[Source]

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

having(*cond, &block)

MySQL allows HAVING clause on ungrouped datasets.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 431
    431:       def having(*cond, &block)
    432:         _filter(:having, *cond, &block)
    433:       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 464
    464:       def insert_ignore
    465:         clone(:insert_ignore=>true)
    466:       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 437
    437:       def join_table(type, table, expr=nil, table_alias={}, &block)
    438:         type = :inner if (type == :cross) && !expr.nil?
    439:         raise(Sequel::Error, "MySQL doesn't support FULL OUTER JOIN") if type == :full_outer
    440:         super(type, table, expr, table_alias, &block)
    441:       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 445
    445:       def join_type_sql(join_type)
    446:         case join_type
    447:         when :straight
    448:           STRAIGHT_JOIN
    449:         when :natural_inner
    450:           NATURAL_LEFT_JOIN
    451:         else
    452:           super
    453:         end
    454:       end

multi_insert_sql(columns, values)

MySQL specific syntax for inserting multiple values at once.

[Source]

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

quoted_identifier_append(sql, c)

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

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 505
    505:       def quoted_identifier_append(sql, c)
    506:         sql << BACKTICK << c.to_s << BACKTICK
    507:       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 511
    511:       def replace_sql(*values)
    512:         clone(:replace=>true).insert_sql(*values)
    513:       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 517
    517:       def supports_distinct_on?
    518:         true
    519:       end

supports_intersect_except?()

MySQL does not support INTERSECT or EXCEPT

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 522
    522:       def supports_intersect_except?
    523:         false
    524:       end

supports_modifying_joins?()

MySQL supports modifying joined datasets

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 527
    527:       def supports_modifying_joins?
    528:         true
    529:       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 533
    533:       def supports_ordered_distinct_on?
    534:         false
    535:       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 540
    540:       def supports_timestamp_usecs?
    541:         false
    542:       end

[Validate]