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 695
695:       def calc_found_rows
696:         clone(:calc_found_rows => true)
697:       end

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 635
635:       def complex_expression_sql_append(sql, op, args)
636:         case op
637:         when :IN, "NOT IN""NOT IN"
638:           ds = args.at(1)
639:           if ds.is_a?(Sequel::Dataset) && ds.opts[:limit]
640:             super(sql, op, [args.at(0), ds.from_self])
641:           else
642:             super
643:           end
644:         when :~, '!~''!~', '~*''~*', '!~*''!~*', :LIKE, 'NOT LIKE''NOT LIKE', :ILIKE, 'NOT ILIKE''NOT ILIKE'
645:           sql << PAREN_OPEN
646:           literal_append(sql, args.at(0))
647:           sql << SPACE
648:           sql << 'NOT ' if ['NOT LIKE''NOT LIKE', 'NOT ILIKE''NOT ILIKE', '!~''!~', '!~*''!~*'].include?(op)
649:           sql << ([:~, '!~''!~', '~*''~*', '!~*''!~*'].include?(op) ? REGEXP : LIKE)
650:           sql << SPACE
651:           sql << BINARY if [:~, '!~''!~', :LIKE, 'NOT LIKE''NOT LIKE'].include?(op)
652:           literal_append(sql, args.at(1))
653:           if [:LIKE, 'NOT LIKE''NOT LIKE', :ILIKE, 'NOT ILIKE''NOT ILIKE'].include?(op)
654:             sql << ESCAPE
655:             literal_append(sql, BACKSLASH)
656:           end
657:           sql << PAREN_CLOSE
658:         when '||''||'
659:           if args.length > 1
660:             sql << CONCAT
661:             array_sql_append(sql, args)
662:           else
663:             literal_append(sql, args.at(0))
664:           end
665:         when 'B~''B~'
666:           sql << CAST_BITCOMP_OPEN
667:           literal_append(sql, args.at(0))
668:           sql << CAST_BITCOMP_CLOSE
669:         else
670:           super
671:         end
672:       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 678
678:       def constant_sql_append(sql, constant)
679:         if constant == :CURRENT_TIMESTAMP && supports_timestamp_usecs?
680:           sql << CURRENT_TIMESTAMP_56
681:         else
682:           super
683:         end
684:       end

Sets up the select methods to delete from if deleting from a joined dataset:

  DB[:a].join(:b, :a_id=>:id).delete
  # DELETE a FROM a INNER JOIN b ON (b.a_id = a.id)

  DB[:a].join(:b, :a_id=>:id).delete_from(:a, :b).delete
  # DELETE a, b FROM a INNER JOIN b ON (b.a_id = a.id)

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 707
707:       def delete_from(*tables)
708:         clone(:delete_from=>tables)
709:       end

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 687
687:       def distinct(*args)
688:         args.empty? ? super : group(*args)
689:       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 713
713:       def explain(opts=OPTS)
714:         # Load the PrettyTable class, needed for explain output
715:         Sequel.extension(:_pretty_table) unless defined?(Sequel::PrettyTable)
716: 
717:         ds = db.send(:metadata_dataset).with_sql((opts[:extended] ? EXPLAIN_EXTENDED : EXPLAIN) + select_sql).naked
718:         rows = ds.all
719:         Sequel::PrettyTable.string(rows, ds.columns)
720:       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 723
723:       def for_share
724:         lock_style(:share)
725:       end

Adds full text filter

[Source]

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

MySQL specific full text search syntax.

[Source]

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

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 748
748:       def join_type_sql(join_type)
749:         case join_type
750:         when :straight
751:           STRAIGHT_JOIN
752:         when :natural_inner
753:           NATURAL_LEFT_JOIN
754:         else
755:           super
756:         end
757:       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 800
800:       def on_duplicate_key_update(*args)
801:         clone(:on_duplicate_key_update => args)
802:       end

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

[Source]

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

MySQL does not support derived column lists

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 810
810:       def supports_derived_column_lists?
811:         false
812:       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 816
816:       def supports_distinct_on?
817:         true
818:       end

MySQL supports GROUP BY WITH ROLLUP (but not CUBE)

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 821
821:       def supports_group_rollup?
822:         true
823:       end

MySQL does not support INTERSECT or EXCEPT

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 826
826:       def supports_intersect_except?
827:         false
828:       end

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 831
831:       def supports_limits_in_correlated_subqueries?
832:         false
833:       end

MySQL supports modifying joined datasets

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 836
836:       def supports_modifying_joins?
837:         true
838:       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 842
842:       def supports_ordered_distinct_on?
843:         false
844:       end

MySQL supports pattern matching via regular expressions

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 847
847:       def supports_regexp?
848:         true
849:       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 854
854:       def supports_timestamp_usecs?
855:         db.supports_timestamp_usecs?
856:       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 864
864:       def update_ignore
865:         clone(:update_ignore=>true)
866:       end

[Validate]