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 528 528: def calc_found_rows 529: clone(:calc_found_rows => true) 530: end
MySQL is different in that it supports prepared statements but not bound variables outside of prepared statements. The default implementation breaks the use of subselects in prepared statements, so extend the temporary prepared statement that this creates with a module that fixes it.
# File lib/sequel/adapters/shared/mysql_prepared_statements.rb, line 127 127: def call(type, bind_arguments={}, *values, &block) 128: ps = to_prepared_statement(type, values) 129: ps.extend(CallableStatementMethods) 130: ps.call(bind_arguments, &block) 131: end
MySQL specific syntax for LIKE/REGEXP searches, as well as string concatenation.
# File lib/sequel/adapters/shared/mysql.rb, line 484 484: def complex_expression_sql_append(sql, op, args) 485: case op 486: when :IN, :"NOT IN" 487: ds = args.at(1) 488: if ds.is_a?(Sequel::Dataset) && ds.opts[:limit] 489: super(sql, op, [args.at(0), ds.from_self]) 490: else 491: super 492: end 493: when :~, :'!~', :'~*', :'!~*', :LIKE, :'NOT LIKE', :ILIKE, :'NOT ILIKE' 494: sql << PAREN_OPEN 495: literal_append(sql, args.at(0)) 496: sql << SPACE 497: sql << 'NOT ' if [:'NOT LIKE', :'NOT ILIKE', :'!~', :'!~*'].include?(op) 498: sql << ([:~, :'!~', :'~*', :'!~*'].include?(op) ? REGEXP : LIKE) 499: sql << SPACE 500: sql << BINARY if [:~, :'!~', :LIKE, :'NOT LIKE'].include?(op) 501: literal_append(sql, args.at(1)) 502: sql << PAREN_CLOSE 503: when :'||' 504: if args.length > 1 505: sql << CONCAT 506: array_sql_append(sql, args) 507: else 508: literal_append(sql, args.at(0)) 509: end 510: when :'B~' 511: sql << CAST_BITCOMP_OPEN 512: literal_append(sql, args.at(0)) 513: sql << CAST_BITCOMP_CLOSE 514: else 515: super 516: end 517: end
Use GROUP BY instead of DISTINCT ON if arguments are provided.
# File lib/sequel/adapters/shared/mysql.rb, line 520 520: def distinct(*args) 521: args.empty? ? super : group(*args) 522: end
Return the results of an EXPLAIN query as a string. Options:
:extended |
# File lib/sequel/adapters/shared/mysql.rb, line 534 534: def explain(opts={}) 535: # Load the PrettyTable class, needed for explain output 536: Sequel.extension(:_pretty_table) unless defined?(Sequel::PrettyTable) 537: 538: ds = db.send(:metadata_dataset).with_sql((opts[:extended] ? EXPLAIN_EXTENDED : EXPLAIN) + select_sql).naked 539: rows = ds.all 540: Sequel::PrettyTable.string(rows, ds.columns) 541: end
Adds full text filter
# File lib/sequel/adapters/shared/mysql.rb, line 549 549: def full_text_search(cols, terms, opts = {}) 550: filter(full_text_sql(cols, terms, opts)) 551: end
MySQL specific full text search syntax.
# File lib/sequel/adapters/shared/mysql.rb, line 554 554: def full_text_sql(cols, terms, opts = {}) 555: terms = terms.join(' ') if terms.is_a?(Array) 556: SQL::PlaceholderLiteralString.new((opts[:boolean] ? MATCH_AGAINST_BOOLEAN : MATCH_AGAINST), [Array(cols), terms]) 557: end
MySQL allows HAVING clause on ungrouped datasets.
# File lib/sequel/adapters/shared/mysql.rb, line 560 560: def having(*cond, &block) 561: _filter(:having, *cond, &block) 562: 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 593 593: def insert_ignore 594: clone(:insert_ignore=>true) 595: 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 566 566: def join_table(type, table, expr=nil, table_alias={}, &block) 567: type = :inner if (type == :cross) && !expr.nil? 568: raise(Sequel::Error, "MySQL doesn't support FULL OUTER JOIN") if type == :full_outer 569: super(type, table, expr, table_alias, &block) 570: end
Transforms :natural_inner to NATURAL LEFT JOIN and straight to STRAIGHT_JOIN.
# File lib/sequel/adapters/shared/mysql.rb, line 574 574: def join_type_sql(join_type) 575: case join_type 576: when :straight 577: STRAIGHT_JOIN 578: when :natural_inner 579: NATURAL_LEFT_JOIN 580: else 581: super 582: end 583: end
MySQL specific syntax for inserting multiple values at once.
# File lib/sequel/adapters/shared/mysql.rb, line 621 621: def multi_insert_sql(columns, values) 622: sql = LiteralString.new('VALUES ') 623: expression_list_append(sql, values.map{|r| Array(r)}) 624: [insert_sql(columns, sql)] 625: end
Replace multiple rows in a single query.
# File lib/sequel/adapters/shared/mysql.rb, line 644 644: def multi_replace(*values) 645: clone(:replace=>true).multi_insert(*values) 646: 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 616 616: def on_duplicate_key_update(*args) 617: clone(:on_duplicate_key_update => args) 618: end
Store the given type of prepared statement in the associated database with the given name.
# File lib/sequel/adapters/shared/mysql_prepared_statements.rb, line 135 135: def prepare(type, name=nil, *values) 136: ps = to_prepared_statement(type, values) 137: ps.extend(PreparedStatementMethods) 138: if name 139: ps.prepared_statement_name = name 140: db.set_prepared_statement(name, ps) 141: end 142: ps 143: end
MySQL uses the nonstandard ` (backtick) for quoting identifiers.
# File lib/sequel/adapters/shared/mysql.rb, line 628 628: def quoted_identifier_append(sql, c) 629: sql << BACKTICK << c.to_s.gsub(BACKTICK_RE, DOUBLE_BACKTICK) << BACKTICK 630: end
Execute a REPLACE statement on the database.
# File lib/sequel/adapters/shared/mysql.rb, line 633 633: def replace(*values) 634: execute_insert(replace_sql(*values)) 635: end
MySQL can emulate DISTINCT ON with its non-standard GROUP BY implementation, though the rows returned cannot be made deterministic through ordering.
# File lib/sequel/adapters/shared/mysql.rb, line 650 650: def supports_distinct_on? 651: true 652: end
MySQL supports GROUP BY WITH ROLLUP (but not CUBE)
# File lib/sequel/adapters/shared/mysql.rb, line 655 655: def supports_group_rollup? 656: true 657: end
MySQL does not support INTERSECT or EXCEPT
# File lib/sequel/adapters/shared/mysql.rb, line 660 660: def supports_intersect_except? 661: false 662: end
MySQL supports modifying joined datasets
# File lib/sequel/adapters/shared/mysql.rb, line 665 665: def supports_modifying_joins? 666: true 667: end
MySQL’s DISTINCT ON emulation using GROUP BY does not respect the queries ORDER BY clause.
# File lib/sequel/adapters/shared/mysql.rb, line 671 671: def supports_ordered_distinct_on? 672: false 673: 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 678 678: def supports_timestamp_usecs? 679: false 680: 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 688 688: def update_ignore 689: clone(:update_ignore=>true) 690: end
Consider the first table in the joined dataset is the table to delete from, but include the others for the purposes of selecting rows.
# File lib/sequel/adapters/shared/mysql.rb, line 701 701: def delete_from_sql(sql) 702: if joined_dataset? 703: sql << SPACE 704: source_list_append(sql, @opts[:from][0..0]) 705: sql << FROM 706: source_list_append(sql, @opts[:from]) 707: select_join_sql(sql) 708: else 709: super 710: end 711: end
MySQL supports the IGNORE and ON DUPLICATE KEY UPDATE clauses for INSERT statements
# File lib/sequel/adapters/shared/mysql.rb, line 714 714: def insert_clause_methods 715: INSERT_CLAUSE_METHODS 716: end
If this is an replace instead of an insert, use replace instead
# File lib/sequel/adapters/shared/mysql.rb, line 740 740: def insert_insert_sql(sql) 741: sql << (@opts[:replace] ? REPLACE : INSERT) 742: end
MySQL supports INSERT … ON DUPLICATE KEY UPDATE
# File lib/sequel/adapters/shared/mysql.rb, line 745 745: def insert_on_duplicate_key_update_sql(sql) 746: if update_cols = opts[:on_duplicate_key_update] 747: update_vals = nil 748: 749: if update_cols.empty? 750: update_cols = columns 751: elsif update_cols.last.is_a?(Hash) 752: update_vals = update_cols.last 753: update_cols = update_cols[0..2] 754: end 755: 756: sql << ON_DUPLICATE_KEY_UPDATE 757: c = false 758: co = COMMA 759: values = EQ_VALUES 760: endp = PAREN_CLOSE 761: update_cols.each do |col| 762: sql << co if c 763: quote_identifier_append(sql, col) 764: sql << values 765: quote_identifier_append(sql, col) 766: sql << endp 767: c ||= true 768: end 769: if update_vals 770: eq = EQ 771: update_vals.map do |col,v| 772: sql << co if c 773: quote_identifier_append(sql, col) 774: sql << eq 775: literal_append(sql, v) 776: c ||= true 777: end 778: end 779: end 780: end
MySQL doesn’t use the standard DEFAULT VALUES for empty values.
# File lib/sequel/adapters/shared/mysql.rb, line 783 783: def insert_values_sql(sql) 784: values = opts[:values] 785: if values.is_a?(Array) && values.empty? 786: sql << EMPTY_VALUES 787: else 788: super 789: end 790: end
MySQL allows a LIMIT in DELETE and UPDATE statements.
# File lib/sequel/adapters/shared/mysql.rb, line 793 793: def limit_sql(sql) 794: if l = @opts[:limit] 795: sql << LIMIT 796: literal_append(sql, @opts[:limit]) 797: end 798: end
Use 0 for false on MySQL
# File lib/sequel/adapters/shared/mysql.rb, line 803 803: def literal_false 804: BOOL_FALSE 805: end
Use 1 for true on MySQL
# File lib/sequel/adapters/shared/mysql.rb, line 813 813: def literal_true 814: BOOL_TRUE 815: end
Extend the dataset with the MySQL stored procedure methods.
# File lib/sequel/adapters/shared/mysql_prepared_statements.rb, line 148 148: def prepare_extend_sproc(ds) 149: ds.extend(StoredProcedureMethods) 150: end
MySQL specific SQL_CALC_FOUND_ROWS option
# File lib/sequel/adapters/shared/mysql.rb, line 828 828: def select_calc_found_rows_sql(sql) 829: sql << SQL_CALC_FOUND_ROWS if opts[:calc_found_rows] 830: end
Support FOR SHARE locking when using the :share lock style.
# File lib/sequel/adapters/shared/mysql.rb, line 823 823: def select_lock_sql(sql) 824: @opts[:lock] == :share ? (sql << FOR_SHARE) : super 825: end
MySQL uses WITH ROLLUP syntax.
# File lib/sequel/adapters/shared/mysql.rb, line 838 838: def uses_with_rollup? 839: true 840: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.