Class Index [+]

Quicksearch

Sequel::MySQL::DatasetMethods

Dataset methods shared by datasets that use MySQL databases.

Constants

BOOL_TRUE
BOOL_FALSE
COMMA_SEPARATOR
FOR_SHARE
SQL_CALC_FOUND_ROWS
DELETE_CLAUSE_METHODS
INSERT_CLAUSE_METHODS
SELECT_CLAUSE_METHODS
UPDATE_CLAUSE_METHODS
APOS
APOS_RE
DOUBLE_APOS
SPACE
PAREN_OPEN
PAREN_CLOSE
NOT_SPACE
FROM
INSERT
COMMA
LIMIT
GROUP_BY
REGEXP
LIKE
BINARY
CONCAT
CAST_BITCOMP_OPEN
CAST_BITCOMP_CLOSE
STRAIGHT_JOIN
NATURAL_LEFT_JOIN
BACKTICK
BACKTICK_RE
DOUBLE_BACKTICK
EMPTY_COLUMNS
EMPTY_VALUES
IGNORE
REPLACE
ON_DUPLICATE_KEY_UPDATE
EQ_VALUES
EQ
WITH_ROLLUP
MATCH_AGAINST
MATCH_AGAINST_BOOLEAN
EXPLAIN
EXPLAIN_EXTENDED
BACKSLASH_RE
QUAD_BACKSLASH

Public Instance Methods

calc_found_rows() click to toggle source

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
call(type, bind_arguments={}, *values, &block) click to toggle source

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
complex_expression_sql_append(sql, op, args) click to toggle source

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
distinct(*args) click to toggle source

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
explain(opts={}) click to toggle source

Return the results of an EXPLAIN query as a string. Options:

:extended

Use EXPLAIN EXPTENDED instead of EXPLAIN if true.

     # 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
for_share() click to toggle source

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

     # File lib/sequel/adapters/shared/mysql.rb, line 544
544:       def for_share
545:         lock_style(:share)
546:       end
full_text_search(cols, terms, opts = {}) click to toggle source

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
full_text_sql(cols, terms, opts = {}) click to toggle source

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
having(*cond, &block) click to toggle source

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
insert_ignore() click to toggle source

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
join_table(type, table, expr=nil, table_alias={}, &block) click to toggle source

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
join_type_sql(join_type) click to toggle source

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
multi_insert_sql(columns, values) click to toggle source

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
multi_replace(*values) click to toggle source

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
on_duplicate_key_update(*args) click to toggle source

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
prepare(type, name=nil, *values) click to toggle source

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
quoted_identifier_append(sql, c) click to toggle source

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
replace(*values) click to toggle source

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
replace_sql(*values) click to toggle source

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

     # File lib/sequel/adapters/shared/mysql.rb, line 639
639:       def replace_sql(*values)
640:         clone(:replace=>true).insert_sql(*values)
641:       end
supports_distinct_on?() click to toggle source

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
supports_group_rollup?() click to toggle source

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
supports_intersect_except?() click to toggle source

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
supports_modifying_joins?() click to toggle source

MySQL supports modifying joined datasets

     # File lib/sequel/adapters/shared/mysql.rb, line 665
665:       def supports_modifying_joins?
666:         true
667:       end
supports_ordered_distinct_on?() click to toggle source

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
supports_timestamp_usecs?() click to toggle source

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
update_ignore() click to toggle source

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

Private Instance Methods

delete_clause_methods() click to toggle source

MySQL supports the ORDER BY and LIMIT clauses for DELETE statements

     # File lib/sequel/adapters/shared/mysql.rb, line 695
695:       def delete_clause_methods
696:         DELETE_CLAUSE_METHODS
697:       end
delete_from_sql(sql) click to toggle source

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
delete_limit_sql(sql) click to toggle source
Alias for: limit_sql
insert_clause_methods() click to toggle source

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
Also aliased as: replace_clause_methods
insert_columns_sql(sql) click to toggle source

MySQL doesn’t use the SQL standard DEFAULT VALUES.

     # File lib/sequel/adapters/shared/mysql.rb, line 720
720:       def insert_columns_sql(sql)
721:         values = opts[:values]
722:         if values.is_a?(Array) && values.empty?
723:           sql << EMPTY_COLUMNS
724:         else
725:           super
726:         end
727:       end
insert_ignore_sql(sql) click to toggle source

MySQL supports INSERT IGNORE INTO

     # File lib/sequel/adapters/shared/mysql.rb, line 730
730:       def insert_ignore_sql(sql)
731:         sql << IGNORE if opts[:insert_ignore]
732:       end
insert_insert_sql(sql) click to toggle source

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
insert_on_duplicate_key_update_sql(sql) click to toggle source

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
insert_values_sql(sql) click to toggle source

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
limit_sql(sql) click to toggle source

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
literal_false() click to toggle source

Use 0 for false on MySQL

     # File lib/sequel/adapters/shared/mysql.rb, line 803
803:       def literal_false
804:         BOOL_FALSE
805:       end
literal_string_append(sql, v) click to toggle source

SQL fragment for String. Doubles \ and ’ by default.

     # File lib/sequel/adapters/shared/mysql.rb, line 808
808:       def literal_string_append(sql, v)
809:         sql << APOS << v.gsub(BACKSLASH_RE, QUAD_BACKSLASH).gsub(APOS_RE, DOUBLE_APOS) << APOS
810:       end
literal_true() click to toggle source

Use 1 for true on MySQL

     # File lib/sequel/adapters/shared/mysql.rb, line 813
813:       def literal_true
814:         BOOL_TRUE
815:       end
prepare_extend_sproc(ds) click to toggle source

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
replace_clause_methods() click to toggle source
select_calc_found_rows_sql(sql) click to toggle source

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
select_clause_methods() click to toggle source

MySQL does not support the SQL WITH clause for SELECT statements

     # File lib/sequel/adapters/shared/mysql.rb, line 818
818:       def select_clause_methods
819:         SELECT_CLAUSE_METHODS
820:       end
select_lock_sql(sql) click to toggle source

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
update_clause_methods() click to toggle source

MySQL supports the ORDER BY and LIMIT clauses for UPDATE statements

     # File lib/sequel/adapters/shared/mysql.rb, line 833
833:       def update_clause_methods
834:         UPDATE_CLAUSE_METHODS
835:       end
update_ignore_sql(sql) click to toggle source

MySQL supports UPDATE IGNORE

     # File lib/sequel/adapters/shared/mysql.rb, line 735
735:       def update_ignore_sql(sql)
736:         sql << IGNORE if opts[:update_ignore]
737:       end
update_limit_sql(sql) click to toggle source
Alias for: limit_sql
uses_with_rollup?() click to toggle source

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.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.