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

Dataset methods shared by datasets that use MySQL databases.

### Methods

_insert_sql complex_expression_sql full_text_search full_text_sql having insert_ignore join_table join_type_sql multi_insert_sql on_duplicate_key_update quoted_identifier replace_sql supports_distinct_on? supports_intersect_except? supports_timestamp_usecs?

### Constants

BOOL_TRUE | = | '1'.freeze
BOOL_FALSE | = | '0'.freeze
COMMA_SEPARATOR | = | ', '.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')
UPDATE_CLAUSE_METHODS | = | Dataset.clause_methods(:update, %w'table set where order limit')

### Public Instance methods

complex_expression_sql(op, args)

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

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 212
    212:       def complex_expression_sql(op, args)
    213:         case op
    214:         when :~, '!~''!~', '~*''~*', '!~*''!~*', :LIKE, 'NOT LIKE''NOT LIKE', :ILIKE, 'NOT ILIKE''NOT ILIKE'
    215:           "(#{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))})"
    216:         when '||''||'
    217:           if args.length > 1
    218:             "CONCAT(#{args.collect{|a| literal(a)}.join(', ')})"
    219:           else
    220:             literal(args.at(0))
    221:           end
    222:         else
    223:           super(op, args)
    224:         end
    225:       end

full_text_search(cols, terms, opts = {})

Adds full text filter

[Source]

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

full_text_sql(cols, term, opts = {})

MySQL specific full text search syntax.

[Source]

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

having(*cond, &block)

MySQL allows HAVING clause on ungrouped datasets.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 238
    238:       def having(*cond, &block)
    239:         _filter(:having, *cond, &block)
    240:       end

insert_ignore()

Sets up multi_insert or import to use INSERT IGNORE. Useful if you have a unique key and want to just skip inserting rows that violate the unique key restriction.

Example:

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 272
    272:       def insert_ignore
    273:         clone(:insert_ignore=>true)
    274:       end

join_table(type, table, expr=nil, table_alias={})

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 244
    244:       def join_table(type, table, expr=nil, table_alias={})
    245:         type = :inner if (type == :cross) && !expr.nil?
    246:         raise(Sequel::Error, "MySQL doesn't support FULL OUTER JOIN") if type == :full_outer
    247:         super(type, table, expr, table_alias)
    248:       end

join_type_sql(join_type)

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

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 252
    252:       def join_type_sql(join_type)
    253:         case join_type
    254:         when :straight then 'STRAIGHT_JOIN'
    255:         when :natural_inner then 'NATURAL LEFT JOIN'
    256:         else super
    257:         end
    258:       end

multi_insert_sql(columns, values)

MySQL specific syntax for inserting multiple values at once.

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 305
    305:       def multi_insert_sql(columns, values)
    306:         [insert_sql(columns, LiteralString.new('VALUES ' + values.map {|r| literal(Array(r))}.join(COMMA_SEPARATOR)))]
    307:       end

on_duplicate_key_update(*args)

Sets up multi_insert or import 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.

Examples:

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 300
    300:       def on_duplicate_key_update(*args)
    301:         clone(:on_duplicate_key_update => args)
    302:       end

quoted_identifier(c)

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

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 310
    310:       def quoted_identifier(c)
    311:         "`#{c}`"
    312:       end

replace_sql(*values)

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 316
    316:       def replace_sql(*values)
    317:         clone(:replace=>true).insert_sql(*values)
    318:       end

supports_distinct_on?()

does not support DISTINCT ON

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 321
    321:       def supports_distinct_on?
    322:         false
    323:       end

supports_intersect_except?()

MySQL does not support INTERSECT or EXCEPT

[Source]

         # File lib/sequel/adapters/shared/mysql.rb, line 326
    326:       def supports_intersect_except?
    327:         false
    328:       end

supports_timestamp_usecs?()

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 333
    333:       def supports_timestamp_usecs?
    334:         false
    335:       end

### Protected Instance methods

_insert_sql()

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

[Source]

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

[Validate]