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 523 523: def calc_found_rows 524: clone(:calc_found_rows => true) 525: end
MySQL specific syntax for LIKE/REGEXP searches, as well as string concatenation.
# 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
Use GROUP BY instead of DISTINCT ON if arguments are provided.
# File lib/sequel/adapters/shared/mysql.rb, line 515 515: def distinct(*args) 516: args.empty? ? super : group(*args) 517: 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 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
Return a cloned dataset which will use LOCK IN SHARE MODE to lock returned rows.
# File lib/sequel/adapters/shared/mysql.rb, line 539 539: def for_share 540: lock_style(:share) 541: end
Adds full text filter
# 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
MySQL specific full text search syntax.
# 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
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 588 588: def insert_ignore 589: clone(:insert_ignore=>true) 590: 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 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
Transforms :natural_inner to NATURAL LEFT JOIN and straight to STRAIGHT_JOIN.
# 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
MySQL specific syntax for inserting multiple values at once.
# 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
Replace multiple rows in a single query.
# File lib/sequel/adapters/shared/mysql.rb, line 639 639: def multi_replace(*values) 640: clone(:replace=>true).multi_insert(*values) 641: 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 611 611: def on_duplicate_key_update(*args) 612: clone(:on_duplicate_key_update => args) 613: end
Execute a REPLACE statement on the database.
# File lib/sequel/adapters/shared/mysql.rb, line 628 628: def replace(*values) 629: execute_insert(replace_sql(*values)) 630: 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 673 673: def supports_timestamp_usecs? 674: false 675: 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 683 683: def update_ignore 684: clone(:update_ignore=>true) 685: end