Module Sequel::MySQL::DatasetMethods
In: lib/sequel/adapters/shared/mysql.rb

Dataset methods shared by datasets that use MySQL databases.

Methods

Constants

BOOL_TRUE = '1'.freeze
BOOL_FALSE = '0'.freeze
COMMA_SEPARATOR = ', '.freeze
FOR_SHARE = ' LOCK IN SHARE MODE'.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 columns from join where group having compounds order limit lock')
UPDATE_CLAUSE_METHODS = Dataset.clause_methods(:update, %w'table set where order limit')

Public Instance methods

MySQL specific syntax for LIKE/REGEXP searches, as well as string concatenation.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 233
233:       def complex_expression_sql(op, args)
234:         case op
235:         when :~, '!~''!~', '~*''~*', '!~*''!~*', :LIKE, 'NOT LIKE''NOT LIKE', :ILIKE, 'NOT ILIKE''NOT ILIKE'
236:           "(#{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))})"
237:         when '||''||'
238:           if args.length > 1
239:             "CONCAT(#{args.collect{|a| literal(a)}.join(', ')})"
240:           else
241:             literal(args.at(0))
242:           end
243:         else
244:           super(op, args)
245:         end
246:       end

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

[Source]

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

Return a cloned dataset which will use LOCK IN SHARE MODE to lock returned rows.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 254
254:       def for_share
255:         lock_style(:share)
256:       end

Adds full text filter

[Source]

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

MySQL specific full text search syntax.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 264
264:       def full_text_sql(cols, term, opts = {})
265:         "MATCH #{literal(Array(cols))} AGAINST (#{literal(Array(term).join(' '))}#{" IN BOOLEAN MODE" if opts[:boolean]})"
266:       end

MySQL allows HAVING clause on ungrouped datasets.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 269
269:       def having(*cond, &block)
270:         _filter(:having, *cond, &block)
271:       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)

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 299
299:       def insert_ignore
300:         clone(:insert_ignore=>true)
301:       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.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 275
275:       def join_table(type, table, expr=nil, table_alias={})
276:         type = :inner if (type == :cross) && !expr.nil?
277:         raise(Sequel::Error, "MySQL doesn't support FULL OUTER JOIN") if type == :full_outer
278:         super(type, table, expr, table_alias)
279:       end

Transforms :natural_inner to NATURAL LEFT JOIN and straight to STRAIGHT_JOIN.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 283
283:       def join_type_sql(join_type)
284:         case join_type
285:         when :straight then 'STRAIGHT_JOIN'
286:         when :natural_inner then 'NATURAL LEFT JOIN'
287:         else super
288:         end
289:       end

MySQL specific syntax for inserting multiple values at once.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 327
327:       def multi_insert_sql(columns, values)
328:         [insert_sql(columns, LiteralString.new('VALUES ' + values.map {|r| literal(Array(r))}.join(COMMA_SEPARATOR)))]
329:       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)

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 322
322:       def on_duplicate_key_update(*args)
323:         clone(:on_duplicate_key_update => args)
324:       end

MySQL uses the number of rows actually modified in the update, instead of the number of matched by the filter.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 333
333:       def provides_accurate_rows_matched?
334:         false
335:       end

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 338
338:       def quoted_identifier(c)
339:         "`#{c}`"
340:       end

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 344
344:       def replace_sql(*values)
345:         clone(:replace=>true).insert_sql(*values)
346:       end

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 350
350:       def supports_distinct_on?
351:         true
352:       end

MySQL does not support INTERSECT or EXCEPT

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 355
355:       def supports_intersect_except?
356:         false
357:       end

MySQL supports modifying joined datasets

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 360
360:       def supports_modifying_joins?
361:         true
362:       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.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 367
367:       def supports_timestamp_usecs?
368:         false
369:       end

Protected Instance methods

If this is an replace instead of an insert, use replace instead

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 374
374:       def _insert_sql
375:         @opts[:replace] ? clause_sql(:replace) : super
376:       end

[Validate]