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 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 |
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 413 413: def calc_found_rows 414: clone(:calc_found_rows => true) 415: end
MySQL specific syntax for LIKE/REGEXP searches, as well as string concatenation.
# File lib/sequel/adapters/shared/mysql.rb, line 369 369: def complex_expression_sql_append(sql, op, args) 370: case op 371: when :IN, "NOT IN""NOT IN" 372: ds = args.at(1) 373: if ds.is_a?(Sequel::Dataset) && ds.opts[:limit] 374: super(sql, op, [args.at(0), ds.from_self]) 375: else 376: super 377: end 378: when :~, '!~''!~', '~*''~*', '!~*''!~*', :LIKE, 'NOT LIKE''NOT LIKE', :ILIKE, 'NOT ILIKE''NOT ILIKE' 379: sql << PAREN_OPEN 380: literal_append(sql, args.at(0)) 381: sql << SPACE 382: sql << 'NOT ' if ['NOT LIKE''NOT LIKE', 'NOT ILIKE''NOT ILIKE', '!~''!~', '!~*''!~*'].include?(op) 383: sql << ([:~, '!~''!~', '~*''~*', '!~*''!~*'].include?(op) ? REGEXP : LIKE) 384: sql << SPACE 385: sql << BINARY if [:~, '!~''!~', :LIKE, 'NOT LIKE''NOT LIKE'].include?(op) 386: literal_append(sql, args.at(1)) 387: sql << PAREN_CLOSE 388: when '||''||' 389: if args.length > 1 390: sql << CONCAT 391: array_sql_append(sql, args) 392: else 393: literal_append(sql, args.at(0)) 394: end 395: when 'B~''B~' 396: sql << CAST_BITCOMP_OPEN 397: literal_append(sql, args.at(0)) 398: sql << CAST_BITCOMP_CLOSE 399: else 400: super 401: end 402: end
Use GROUP BY instead of DISTINCT ON if arguments are provided.
# File lib/sequel/adapters/shared/mysql.rb, line 405 405: def distinct(*args) 406: args.empty? ? super : group(*args) 407: end
Return a cloned dataset which will use LOCK IN SHARE MODE to lock returned rows.
# File lib/sequel/adapters/shared/mysql.rb, line 418 418: def for_share 419: lock_style(:share) 420: end
Adds full text filter
# File lib/sequel/adapters/shared/mysql.rb, line 423 423: def full_text_search(cols, terms, opts = {}) 424: filter(full_text_sql(cols, terms, opts)) 425: end
MySQL specific full text search syntax.
# File lib/sequel/adapters/shared/mysql.rb, line 428 428: def full_text_sql(cols, terms, opts = {}) 429: terms = terms.join(' ') if terms.is_a?(Array) 430: SQL::PlaceholderLiteralString.new("MATCH ? AGAINST (?#{" IN BOOLEAN MODE" if opts[:boolean]})", [Array(cols), terms], true) 431: 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 467 467: def insert_ignore 468: clone(:insert_ignore=>true) 469: 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 440 440: def join_table(type, table, expr=nil, table_alias={}, &block) 441: type = :inner if (type == :cross) && !expr.nil? 442: raise(Sequel::Error, "MySQL doesn't support FULL OUTER JOIN") if type == :full_outer 443: super(type, table, expr, table_alias, &block) 444: end
Transforms :natural_inner to NATURAL LEFT JOIN and straight to STRAIGHT_JOIN.
# File lib/sequel/adapters/shared/mysql.rb, line 448 448: def join_type_sql(join_type) 449: case join_type 450: when :straight 451: STRAIGHT_JOIN 452: when :natural_inner 453: NATURAL_LEFT_JOIN 454: else 455: super 456: end 457: end
MySQL specific syntax for inserting multiple values at once.
# File lib/sequel/adapters/shared/mysql.rb, line 495 495: def multi_insert_sql(columns, values) 496: sql = LiteralString.new('VALUES ') 497: expression_list_append(sql, values.map{|r| Array(r)}) 498: [insert_sql(columns, sql)] 499: 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 490 490: def on_duplicate_key_update(*args) 491: clone(:on_duplicate_key_update => args) 492: 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 548 548: def supports_timestamp_usecs? 549: false 550: end