| 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'from where order limit') |
| INSERT_CLAUSE_METHODS | = | Dataset.clause_methods(:insert, %w'ignore into columns values on_duplicate_key_update') |
| SELECT_CLAUSE_METHODS | = | Dataset.clause_methods(:select, %w'distinct calc_found_rows columns from join where group having compounds order limit lock') |
| UPDATE_CLAUSE_METHODS | = | Dataset.clause_methods(:update, %w'table set where order limit') |
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 357
357: def calc_found_rows
358: clone(:calc_found_rows => true)
359: end
MySQL specific syntax for LIKE/REGEXP searches, as well as string concatenation.
# File lib/sequel/adapters/shared/mysql.rb, line 331
331: def complex_expression_sql(op, args)
332: case op
333: when :~, '!~''!~', '~*''~*', '!~*''!~*', :LIKE, 'NOT LIKE''NOT LIKE', :ILIKE, 'NOT ILIKE''NOT ILIKE'
334: "(#{literal(args.at(0))} #{'NOT ' if [:'NOT LIKE', :'NOT ILIKE', :'!~', :'!~*'].include?(op)}#{[:~, :'!~', :'~*', :'!~*'].include?(op) ? 'REGEXP' : 'LIKE'} #{'BINARY ' if [:~, :'!~', :LIKE, :'NOT LIKE'].include?(op)}#{literal(args.at(1))})"
335: when '||''||'
336: if args.length > 1
337: "CONCAT(#{args.collect{|a| literal(a)}.join(', ')})"
338: else
339: literal(args.at(0))
340: end
341: when 'B~''B~'
342: "CAST(~#{literal(args.at(0))} AS SIGNED INTEGER)"
343: else
344: super(op, args)
345: end
346: end
Use GROUP BY instead of DISTINCT ON if arguments are provided.
# File lib/sequel/adapters/shared/mysql.rb, line 349
349: def distinct(*args)
350: args.empty? ? super : group(*args)
351: end
Return a cloned dataset which will use LOCK IN SHARE MODE to lock returned rows.
# File lib/sequel/adapters/shared/mysql.rb, line 362
362: def for_share
363: lock_style(:share)
364: end
Adds full text filter
# File lib/sequel/adapters/shared/mysql.rb, line 367
367: def full_text_search(cols, terms, opts = {})
368: filter(full_text_sql(cols, terms, opts))
369: 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 407
407: def insert_ignore
408: clone(:insert_ignore=>true)
409: 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 383
383: def join_table(type, table, expr=nil, table_alias={}, &block)
384: type = :inner if (type == :cross) && !expr.nil?
385: raise(Sequel::Error, "MySQL doesn't support FULL OUTER JOIN") if type == :full_outer
386: super(type, table, expr, table_alias, &block)
387: end
Transforms :natural_inner to NATURAL LEFT JOIN and straight to STRAIGHT_JOIN.
# File lib/sequel/adapters/shared/mysql.rb, line 391
391: def join_type_sql(join_type)
392: case join_type
393: when :straight then 'STRAIGHT_JOIN'
394: when :natural_inner then 'NATURAL LEFT JOIN'
395: else super
396: end
397: 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 430
430: def on_duplicate_key_update(*args)
431: clone(:on_duplicate_key_update => args)
432: 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 475
475: def supports_timestamp_usecs?
476: false
477: end