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 explain for_share full_text_search full_text_sql having insert_ignore join_table join_type_sql multi_insert_sql multi_replace on_duplicate_key_update quoted_identifier_append replace 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')
APOS | = | Dataset::APOS
APOS_RE | = | Dataset::APOS_RE
DOUBLE_APOS | = | Dataset::DOUBLE_APOS
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
BACKTICK_RE | = | /`/.freeze
DOUBLE_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
MATCH_AGAINST | = | ["(MATCH ".freeze, " AGAINST (".freeze, "))".freeze].freeze
MATCH_AGAINST_BOOLEAN | = | ["(MATCH ".freeze, " AGAINST (".freeze, " IN BOOLEAN MODE))".freeze].freeze
EXPLAIN | = | 'EXPLAIN '.freeze
EXPLAIN_EXTENDED | = | 'EXPLAIN EXTENDED '.freeze
BACKSLASH_RE | = | /\\/.freeze
QUAD_BACKSLASH | = | "\\\\\\\\".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 523
    523:       def calc_found_rows
    524:         clone(:calc_found_rows => true)
    525:       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 479
    479:       def complex_expression_sql_append(sql, op, args)
    480:         case op
    481:         when :IN, "NOT IN""NOT IN"
    482:           ds = args.at(1)
    483:           if ds.is_a?(Sequel::Dataset) && ds.opts[:limit]
    484:             super(sql, op, [args.at(0), ds.from_self])
    485:           else
    486:             super
    487:           end
    488:         when :~, '!~''!~', '~*''~*', '!~*''!~*', :LIKE, 'NOT LIKE''NOT LIKE', :ILIKE, 'NOT ILIKE''NOT ILIKE'
    489:           sql << PAREN_OPEN
    490:           literal_append(sql, args.at(0))
    491:           sql << SPACE
    492:           sql << 'NOT ' if ['NOT LIKE''NOT LIKE', 'NOT ILIKE''NOT ILIKE', '!~''!~', '!~*''!~*'].include?(op)
    493:           sql << ([:~, '!~''!~', '~*''~*', '!~*''!~*'].include?(op) ? REGEXP : LIKE)
    494:           sql << SPACE
    495:           sql << BINARY if [:~, '!~''!~', :LIKE, 'NOT LIKE''NOT LIKE'].include?(op)
    496:           literal_append(sql, args.at(1))
    497:           sql << PAREN_CLOSE
    498:         when '||''||'
    499:           if args.length > 1
    500:             sql << CONCAT
    501:             array_sql_append(sql, args)
    502:           else
    503:             literal_append(sql, args.at(0))
    504:           end
    505:         when 'B~''B~'
    506:           sql << CAST_BITCOMP_OPEN
    507:           literal_append(sql, args.at(0))
    508:           sql << CAST_BITCOMP_CLOSE
    509:         else
    510:           super
    511:         end
    512:       end

distinct(*args)

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

[Source]

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

explain(opts={})

Return the results of an EXPLAIN query as a string. Options:

:extended : | Use EXPLAIN EXPTENDED instead of EXPLAIN if true.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 529
    529:       def explain(opts={})
    530:         # Load the PrettyTable class, needed for explain output
    531:         Sequel.extension(:_pretty_table) unless defined?(Sequel::PrettyTable)
    532:
    533:         ds = db.send(:metadata_dataset).with_sql((opts[:extended] ? EXPLAIN_EXTENDED : EXPLAIN) + select_sql).naked
    534:         rows = ds.all
    535:         Sequel::PrettyTable.string(rows, ds.columns)
    536:       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 539
    539:       def for_share
    540:         lock_style(:share)
    541:       end

full_text_search(cols, terms, opts = {})

Adds full text filter

[Source]

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

full_text_sql(cols, terms, opts = {})

MySQL specific full text search syntax.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 549
    549:       def full_text_sql(cols, terms, opts = {})
    550:         terms = terms.join(' ') if terms.is_a?(Array)
    551:         SQL::PlaceholderLiteralString.new((opts[:boolean] ? MATCH_AGAINST_BOOLEAN : MATCH_AGAINST), [Array(cols), terms])
    552:       end

having(*cond, &block)

MySQL allows HAVING clause on ungrouped datasets.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 555
    555:       def having(*cond, &block)
    556:         _filter(:having, *cond, &block)
    557:       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 588
    588:       def insert_ignore
    589:         clone(:insert_ignore=>true)
    590:       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 561
    561:       def join_table(type, table, expr=nil, table_alias={}, &block)
    562:         type = :inner if (type == :cross) && !expr.nil?
    563:         raise(Sequel::Error, "MySQL doesn't support FULL OUTER JOIN") if type == :full_outer
    564:         super(type, table, expr, table_alias, &block)
    565:       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 569
    569:       def join_type_sql(join_type)
    570:         case join_type
    571:         when :straight
    572:           STRAIGHT_JOIN
    573:         when :natural_inner
    574:           NATURAL_LEFT_JOIN
    575:         else
    576:           super
    577:         end
    578:       end

multi_insert_sql(columns, values)

MySQL specific syntax for inserting multiple values at once.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 616
    616:       def multi_insert_sql(columns, values)
    617:         sql = LiteralString.new('VALUES ')
    618:         expression_list_append(sql, values.map{|r| Array(r)})
    619:         [insert_sql(columns, sql)]
    620:       end

multi_replace(*values)

Replace multiple rows in a single query.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 639
    639:       def multi_replace(*values)
    640:         clone(:replace=>true).multi_insert(*values)
    641:       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 611
    611:       def on_duplicate_key_update(*args)
    612:         clone(:on_duplicate_key_update => args)
    613:       end

quoted_identifier_append(sql, c)

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

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 623
    623:       def quoted_identifier_append(sql, c)
    624:         sql << BACKTICK << c.to_s.gsub(BACKTICK_RE, DOUBLE_BACKTICK) << BACKTICK
    625:       end

replace(*values)

Execute a REPLACE statement on the database.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 628
    628:       def replace(*values)
    629:         execute_insert(replace_sql(*values))
    630:       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 634
    634:       def replace_sql(*values)
    635:         clone(:replace=>true).insert_sql(*values)
    636:       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 645
    645:       def supports_distinct_on?
    646:         true
    647:       end

supports_group_rollup?()

MySQL supports GROUP BY WITH ROLLUP (but not CUBE)

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 650
    650:       def supports_group_rollup?
    651:         true
    652:       end

supports_intersect_except?()

MySQL does not support INTERSECT or EXCEPT

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 655
    655:       def supports_intersect_except?
    656:         false
    657:       end

supports_modifying_joins?()

MySQL supports modifying joined datasets

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 660
    660:       def supports_modifying_joins?
    661:         true
    662:       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 666
    666:       def supports_ordered_distinct_on?
    667:         false
    668:       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 673
    673:       def supports_timestamp_usecs?
    674:         false
    675:       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 683
    683:       def update_ignore
    684:         clone(:update_ignore=>true)
    685:       end

[Validate]