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

Dataset methods shared by datasets that use MySQL databases.

Methods

Included Modules

Dataset::UnsupportedIntersectExcept

Constants

BOOL_TRUE = '1'.freeze
BOOL_FALSE = '0'.freeze
COMMA_SEPARATOR = ', '.freeze

Public Instance methods

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

[Source]

     # File lib/sequel_core/adapters/shared/mysql.rb, line 153
153:       def complex_expression_sql(op, args)
154:         case op
155:         when :~, '!~''!~', '~*''~*', '!~*''!~*', :LIKE, 'NOT LIKE''NOT LIKE', :ILIKE, 'NOT ILIKE''NOT ILIKE'
156:           "(#{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))})"
157:         when '||''||'
158:           if args.length > 1
159:             "CONCAT(#{args.collect{|a| literal(a)}.join(', ')})"
160:           else
161:             literal(args.at(0))
162:           end
163:         else
164:           super(op, args)
165:         end
166:       end

MySQL supports ORDER and LIMIT clauses in DELETE statements.

[Source]

     # File lib/sequel_core/adapters/shared/mysql.rb, line 169
169:       def delete_sql(opts = nil)
170:         sql = super
171:         opts = opts ? @opts.merge(opts) : @opts
172: 
173:         if order = opts[:order]
174:           sql << " ORDER BY #{expression_list(order)}"
175:         end
176:         if limit = opts[:limit]
177:           sql << " LIMIT #{limit}"
178:         end
179: 
180:         sql
181:       end

MySQL specific full text search syntax.

[Source]

     # File lib/sequel_core/adapters/shared/mysql.rb, line 184
184:       def full_text_search(cols, terms, opts = {})
185:         mode = opts[:boolean] ? " IN BOOLEAN MODE" : ""
186:         s = if Array === terms
187:           if mode.blank?
188:             "MATCH #{literal(Array(cols))} AGAINST #{literal(terms)}"
189:           else
190:             "MATCH #{literal(Array(cols))} AGAINST (#{literal(terms)[1...-1]}#{mode})"
191:           end
192:         else
193:           "MATCH #{literal(Array(cols))} AGAINST (#{literal(terms)}#{mode})"
194:         end
195:         filter(s)
196:       end

MySQL allows HAVING clause on ungrouped datasets.

[Source]

     # File lib/sequel_core/adapters/shared/mysql.rb, line 199
199:       def having(*cond, &block)
200:         @opts[:having] = {}
201:         x = filter(*cond, &block)
202:       end

MySQL doesn‘t use the SQL standard DEFAULT VALUES.

[Source]

     # File lib/sequel_core/adapters/shared/mysql.rb, line 205
205:       def insert_default_values_sql
206:         "INSERT INTO #{source_list(@opts[:from])} () VALUES ()"
207:       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_core/adapters/shared/mysql.rb, line 211
211:       def join_table(type, table, expr=nil, table_alias={})
212:         type = :inner if (type == :cross) && !expr.nil?
213:         raise(Sequel::Error, "MySQL doesn't support FULL OUTER JOIN") if type == :full_outer
214:         super(type, table, expr, table_alias)
215:       end

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

[Source]

     # File lib/sequel_core/adapters/shared/mysql.rb, line 219
219:       def join_type_sql(join_type)
220:         case join_type
221:         when :straight then 'STRAIGHT_JOIN'
222:         when :natural_inner then 'NATURAL LEFT JOIN'
223:         else super
224:         end
225:       end

Override the default boolean values.

[Source]

     # File lib/sequel_core/adapters/shared/mysql.rb, line 228
228:       def literal(v)
229:         case v
230:         when true
231:           BOOL_TRUE
232:         when false
233:           BOOL_FALSE
234:         when DateTime, Time
235:           v.strftime("'%Y-%m-%d %H:%M:%S'")
236:         else
237:           super
238:         end
239:       end

MySQL specific syntax for inserting multiple values at once.

[Source]

     # File lib/sequel_core/adapters/shared/mysql.rb, line 242
242:       def multi_insert_sql(columns, values)
243:         values = values.map {|r| literal(Array(r))}.join(COMMA_SEPARATOR)
244:         ["INSERT INTO #{source_list(@opts[:from])} (#{identifier_list(columns)}) VALUES #{values}"]
245:       end

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

[Source]

     # File lib/sequel_core/adapters/shared/mysql.rb, line 248
248:       def quoted_identifier(c)
249:         "`#{c}`"
250:       end

MySQL specific syntax for REPLACE (aka UPSERT, or update if exists, insert if it doesn‘t).

[Source]

     # File lib/sequel_core/adapters/shared/mysql.rb, line 254
254:       def replace_sql(*values)
255:         from = source_list(@opts[:from])
256:         if values.empty?
257:           "REPLACE INTO #{from} DEFAULT VALUES"
258:         else
259:           values = values[0] if values.size == 1
260:           
261:           # if hash or array with keys we need to transform the values
262:           if @transform && (values.is_a?(Hash) || (values.is_a?(Array) && values.keys))
263:             values = transform_save(values)
264:           end
265: 
266:           case values
267:           when Array
268:             if values.empty?
269:               "REPLACE INTO #{from} DEFAULT VALUES"
270:             else
271:               "REPLACE INTO #{from} VALUES #{literal(values)}"
272:             end
273:           when Hash
274:             if values.empty?
275:               "REPLACE INTO #{from} DEFAULT VALUES"
276:             else
277:               fl, vl = [], []
278:               values.each {|k, v| fl << literal(k.is_a?(String) ? k.to_sym : k); vl << literal(v)}
279:               "REPLACE INTO #{from} (#{fl.join(COMMA_SEPARATOR)}) VALUES (#{vl.join(COMMA_SEPARATOR)})"
280:             end
281:           when Dataset
282:             "REPLACE INTO #{from} #{literal(values)}"
283:           else
284:             if values.respond_to?(:values)
285:               replace_sql(values.values)
286:             else  
287:               "REPLACE INTO #{from} VALUES (#{literal(values)})"
288:             end
289:           end
290:         end
291:       end

MySQL supports ORDER and LIMIT clauses in UPDATE statements.

[Source]

     # File lib/sequel_core/adapters/shared/mysql.rb, line 294
294:       def update_sql(values, opts = nil)
295:         sql = super
296:         opts = opts ? @opts.merge(opts) : @opts
297: 
298:         if order = opts[:order]
299:           sql << " ORDER BY #{expression_list(order)}"
300:         end
301:         if limit = opts[:limit]
302:           sql << " LIMIT #{limit}"
303:         end
304: 
305:         sql
306:       end

[Validate]