Module | Sequel::MySQL::DatasetMethods |
In: |
lib/sequel/adapters/shared/mysql.rb
|
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 |
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
# File lib/sequel/adapters/shared/mysql.rb, line 528 528: def calc_found_rows 529: clone(:calc_found_rows => true) 530: end
MySQL specific syntax for LIKE/REGEXP searches, as well as string concatenation.
# File lib/sequel/adapters/shared/mysql.rb, line 484 484: def complex_expression_sql_append(sql, op, args) 485: case op 486: when :IN, "NOT IN""NOT IN" 487: ds = args.at(1) 488: if ds.is_a?(Sequel::Dataset) && ds.opts[:limit] 489: super(sql, op, [args.at(0), ds.from_self]) 490: else 491: super 492: end 493: when :~, '!~''!~', '~*''~*', '!~*''!~*', :LIKE, 'NOT LIKE''NOT LIKE', :ILIKE, 'NOT ILIKE''NOT ILIKE' 494: sql << PAREN_OPEN 495: literal_append(sql, args.at(0)) 496: sql << SPACE 497: sql << 'NOT ' if ['NOT LIKE''NOT LIKE', 'NOT ILIKE''NOT ILIKE', '!~''!~', '!~*''!~*'].include?(op) 498: sql << ([:~, '!~''!~', '~*''~*', '!~*''!~*'].include?(op) ? REGEXP : LIKE) 499: sql << SPACE 500: sql << BINARY if [:~, '!~''!~', :LIKE, 'NOT LIKE''NOT LIKE'].include?(op) 501: literal_append(sql, args.at(1)) 502: sql << PAREN_CLOSE 503: when '||''||' 504: if args.length > 1 505: sql << CONCAT 506: array_sql_append(sql, args) 507: else 508: literal_append(sql, args.at(0)) 509: end 510: when 'B~''B~' 511: sql << CAST_BITCOMP_OPEN 512: literal_append(sql, args.at(0)) 513: sql << CAST_BITCOMP_CLOSE 514: else 515: super 516: end 517: end
Use GROUP BY instead of DISTINCT ON if arguments are provided.
# File lib/sequel/adapters/shared/mysql.rb, line 520 520: def distinct(*args) 521: args.empty? ? super : group(*args) 522: end
Return the results of an EXPLAIN query as a string. Options:
:extended : | Use EXPLAIN EXPTENDED instead of EXPLAIN if true. |
# File lib/sequel/adapters/shared/mysql.rb, line 534 534: def explain(opts={}) 535: # Load the PrettyTable class, needed for explain output 536: Sequel.extension(:_pretty_table) unless defined?(Sequel::PrettyTable) 537: 538: ds = db.send(:metadata_dataset).with_sql((opts[:extended] ? EXPLAIN_EXTENDED : EXPLAIN) + select_sql).naked 539: rows = ds.all 540: Sequel::PrettyTable.string(rows, ds.columns) 541: end
Return a cloned dataset which will use LOCK IN SHARE MODE to lock returned rows.
# File lib/sequel/adapters/shared/mysql.rb, line 544 544: def for_share 545: lock_style(:share) 546: end
Adds full text filter
# File lib/sequel/adapters/shared/mysql.rb, line 549 549: def full_text_search(cols, terms, opts = {}) 550: filter(full_text_sql(cols, terms, opts)) 551: end
MySQL specific full text search syntax.
# File lib/sequel/adapters/shared/mysql.rb, line 554 554: def full_text_sql(cols, terms, opts = {}) 555: terms = terms.join(' ') if terms.is_a?(Array) 556: SQL::PlaceholderLiteralString.new((opts[:boolean] ? MATCH_AGAINST_BOOLEAN : MATCH_AGAINST), [Array(cols), terms]) 557: 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)
# File lib/sequel/adapters/shared/mysql.rb, line 593 593: def insert_ignore 594: clone(:insert_ignore=>true) 595: 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.
# File lib/sequel/adapters/shared/mysql.rb, line 566 566: def join_table(type, table, expr=nil, table_alias={}, &block) 567: type = :inner if (type == :cross) && !expr.nil? 568: raise(Sequel::Error, "MySQL doesn't support FULL OUTER JOIN") if type == :full_outer 569: super(type, table, expr, table_alias, &block) 570: end
Transforms :natural_inner to NATURAL LEFT JOIN and straight to STRAIGHT_JOIN.
# File lib/sequel/adapters/shared/mysql.rb, line 574 574: def join_type_sql(join_type) 575: case join_type 576: when :straight 577: STRAIGHT_JOIN 578: when :natural_inner 579: NATURAL_LEFT_JOIN 580: else 581: super 582: end 583: end
MySQL specific syntax for inserting multiple values at once.
# File lib/sequel/adapters/shared/mysql.rb, line 621 621: def multi_insert_sql(columns, values) 622: sql = LiteralString.new('VALUES ') 623: expression_list_append(sql, values.map{|r| Array(r)}) 624: [insert_sql(columns, sql)] 625: end
Replace multiple rows in a single query.
# File lib/sequel/adapters/shared/mysql.rb, line 644 644: def multi_replace(*values) 645: clone(:replace=>true).multi_insert(*values) 646: 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)
# File lib/sequel/adapters/shared/mysql.rb, line 616 616: def on_duplicate_key_update(*args) 617: clone(:on_duplicate_key_update => args) 618: end
Execute a REPLACE statement on the database.
# File lib/sequel/adapters/shared/mysql.rb, line 633 633: def replace(*values) 634: execute_insert(replace_sql(*values)) 635: 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.
# File lib/sequel/adapters/shared/mysql.rb, line 678 678: def supports_timestamp_usecs? 679: false 680: end
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
# File lib/sequel/adapters/shared/mysql.rb, line 688 688: def update_ignore 689: clone(:update_ignore=>true) 690: end