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

Dataset methods shared by datasets that use MySQL databases.

Methods

Included Modules

Sequel::Dataset::Replace

Constants

BOOL_TRUE = '1'.freeze
BOOL_FALSE = '0'.freeze
COMMA_SEPARATOR = ', '.freeze
FOR_SHARE = ' LOCK IN SHARE MODE'.freeze
SQL_CALC_FOUND_ROWS = ' SQL_CALC_FOUND_ROWS'.freeze
APOS = Dataset::APOS
APOS_RE = Dataset::APOS_RE
DOUBLE_APOS = Dataset::DOUBLE_APOS
SPACE = Dataset::SPACE
PAREN_OPEN = Dataset::PAREN_OPEN
PAREN_CLOSE = Dataset::PAREN_CLOSE
NOT_SPACE = Dataset::NOT_SPACE
FROM = Dataset::FROM
COMMA = Dataset::COMMA
LIMIT = Dataset::LIMIT
GROUP_BY = Dataset::GROUP_BY
ESCAPE = Dataset::ESCAPE
BACKSLASH = Dataset::BACKSLASH
REGEXP = 'REGEXP'.freeze
LIKE = 'LIKE'.freeze
BINARY = 'BINARY '.freeze
CONCAT = "CONCAT".freeze
CAST_BITCOMP_OPEN = "CAST(~".freeze
CAST_BITCOMP_CLOSE = " AS SIGNED INTEGER)".freeze
STRAIGHT_JOIN = 'STRAIGHT_JOIN'.freeze
NATURAL_LEFT_JOIN = 'NATURAL LEFT JOIN'.freeze
BACKTICK = '`'.freeze
BACKTICK_RE = /`/.freeze
DOUBLE_BACKTICK = '``'.freeze
EMPTY_COLUMNS = " ()".freeze
EMPTY_VALUES = " VALUES ()".freeze
IGNORE = " IGNORE".freeze
ON_DUPLICATE_KEY_UPDATE = " ON DUPLICATE KEY UPDATE ".freeze
EQ_VALUES = '=VALUES('.freeze
EQ = '='.freeze
WITH_ROLLUP = ' WITH ROLLUP'.freeze
MATCH_AGAINST = ["(MATCH ".freeze, " AGAINST (".freeze, "))".freeze].freeze
MATCH_AGAINST_BOOLEAN = ["(MATCH ".freeze, " AGAINST (".freeze, " IN BOOLEAN MODE))".freeze].freeze
EXPLAIN = 'EXPLAIN '.freeze
EXPLAIN_EXTENDED = 'EXPLAIN EXTENDED '.freeze
BACKSLASH_RE = /\\/.freeze
QUAD_BACKSLASH = "\\\\\\\\".freeze
BLOB_START = "0x".freeze
EMPTY_BLOB = "''".freeze
HSTAR = "H*".freeze
CURRENT_TIMESTAMP_56 = 'CURRENT_TIMESTAMP(6)'.freeze
ONLY_OFFSET = ",18446744073709551615".freeze   Comes directly from MySQL‘s documentation, used for queries with limits without offsets

Public Instance methods

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 674
674:       def calc_found_rows
675:         clone(:calc_found_rows => true)
676:       end

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 614
614:       def complex_expression_sql_append(sql, op, args)
615:         case op
616:         when :IN, "NOT IN""NOT IN"
617:           ds = args.at(1)
618:           if ds.is_a?(Sequel::Dataset) && ds.opts[:limit]
619:             super(sql, op, [args.at(0), ds.from_self])
620:           else
621:             super
622:           end
623:         when :~, '!~''!~', '~*''~*', '!~*''!~*', :LIKE, 'NOT LIKE''NOT LIKE', :ILIKE, 'NOT ILIKE''NOT ILIKE'
624:           sql << PAREN_OPEN
625:           literal_append(sql, args.at(0))
626:           sql << SPACE
627:           sql << 'NOT ' if ['NOT LIKE''NOT LIKE', 'NOT ILIKE''NOT ILIKE', '!~''!~', '!~*''!~*'].include?(op)
628:           sql << ([:~, '!~''!~', '~*''~*', '!~*''!~*'].include?(op) ? REGEXP : LIKE)
629:           sql << SPACE
630:           sql << BINARY if [:~, '!~''!~', :LIKE, 'NOT LIKE''NOT LIKE'].include?(op)
631:           literal_append(sql, args.at(1))
632:           if [:LIKE, 'NOT LIKE''NOT LIKE', :ILIKE, 'NOT ILIKE''NOT ILIKE'].include?(op)
633:             sql << ESCAPE
634:             literal_append(sql, BACKSLASH)
635:           end
636:           sql << PAREN_CLOSE
637:         when '||''||'
638:           if args.length > 1
639:             sql << CONCAT
640:             array_sql_append(sql, args)
641:           else
642:             literal_append(sql, args.at(0))
643:           end
644:         when 'B~''B~'
645:           sql << CAST_BITCOMP_OPEN
646:           literal_append(sql, args.at(0))
647:           sql << CAST_BITCOMP_CLOSE
648:         else
649:           super
650:         end
651:       end

MySQL‘s CURRENT_TIMESTAMP does not use fractional seconds, even if the database itself supports fractional seconds. If MySQL 5.6.4+ is being used, use a value that will return fractional seconds.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 657
657:       def constant_sql_append(sql, constant)
658:         if constant == :CURRENT_TIMESTAMP && supports_timestamp_usecs?
659:           sql << CURRENT_TIMESTAMP_56
660:         else
661:           super
662:         end
663:       end

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

[Source]

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

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

:extended :Use EXPLAIN EXPTENDED instead of EXPLAIN if true.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 680
680:       def explain(opts=OPTS)
681:         # Load the PrettyTable class, needed for explain output
682:         Sequel.extension(:_pretty_table) unless defined?(Sequel::PrettyTable)
683: 
684:         ds = db.send(:metadata_dataset).with_sql((opts[:extended] ? EXPLAIN_EXTENDED : EXPLAIN) + select_sql).naked
685:         rows = ds.all
686:         Sequel::PrettyTable.string(rows, ds.columns)
687:       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 690
690:       def for_share
691:         lock_style(:share)
692:       end

Adds full text filter

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 695
695:       def full_text_search(cols, terms, opts = OPTS)
696:         filter(full_text_sql(cols, terms, opts))
697:       end

MySQL specific full text search syntax.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 700
700:       def full_text_sql(cols, terms, opts = OPTS)
701:         terms = terms.join(' ') if terms.is_a?(Array)
702:         SQL::PlaceholderLiteralString.new((opts[:boolean] ? MATCH_AGAINST_BOOLEAN : MATCH_AGAINST), [Array(cols), terms])
703:       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 734
734:       def insert_ignore
735:         clone(:insert_ignore=>true)
736:       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 707
707:       def join_table(type, table, expr=nil, opts=OPTS, &block)
708:         type = :inner if (type == :cross) && !expr.nil?
709:         raise(Sequel::Error, "MySQL doesn't support FULL OUTER JOIN") if type == :full_outer
710:         super(type, table, expr, opts, &block)
711:       end

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 715
715:       def join_type_sql(join_type)
716:         case join_type
717:         when :straight
718:           STRAIGHT_JOIN
719:         when :natural_inner
720:           NATURAL_LEFT_JOIN
721:         else
722:           super
723:         end
724:       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. If you pass a hash you can customize the values (for example, to increment a numeric field).

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)

  dataset.on_duplicate_key_update(
    :value => Sequel.lit('value + VALUES(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=value + VALUES(value)

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 767
767:       def on_duplicate_key_update(*args)
768:         clone(:on_duplicate_key_update => args)
769:       end

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 772
772:       def quoted_identifier_append(sql, c)
773:         sql << BACKTICK << c.to_s.gsub(BACKTICK_RE, DOUBLE_BACKTICK) << BACKTICK
774:       end

MySQL does not support derived column lists

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 777
777:       def supports_derived_column_lists?
778:         false
779:       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 783
783:       def supports_distinct_on?
784:         true
785:       end

MySQL supports GROUP BY WITH ROLLUP (but not CUBE)

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 788
788:       def supports_group_rollup?
789:         true
790:       end

MySQL does not support INTERSECT or EXCEPT

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 793
793:       def supports_intersect_except?
794:         false
795:       end

MySQL does not support limits in correlated subqueries (or any subqueries that use IN).

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 798
798:       def supports_limits_in_correlated_subqueries?
799:         false
800:       end

MySQL supports modifying joined datasets

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 803
803:       def supports_modifying_joins?
804:         true
805:       end

MySQL‘s DISTINCT ON emulation using GROUP BY does not respect the queries ORDER BY clause.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 809
809:       def supports_ordered_distinct_on?
810:         false
811:       end

MySQL supports pattern matching via regular expressions

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 814
814:       def supports_regexp?
815:         true
816:       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 821
821:       def supports_timestamp_usecs?
822:         db.supports_timestamp_usecs?
823:       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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 831
831:       def update_ignore
832:         clone(:update_ignore=>true)
833:       end

[Validate]