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 UnmodifiedIdentifiers::DatasetMethods

Constants

MATCH_AGAINST = ["MATCH ".freeze, " AGAINST (".freeze, ")".freeze].freeze
MATCH_AGAINST_BOOLEAN = ["MATCH ".freeze, " AGAINST (".freeze, " IN BOOLEAN MODE)".freeze].freeze

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 666
666:       def calc_found_rows
667:         clone(:calc_found_rows => true)
668:       end

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 600
600:       def complex_expression_sql_append(sql, op, args)
601:         case op
602:         when :IN, "NOT IN""NOT IN"
603:           ds = args[1]
604:           if ds.is_a?(Sequel::Dataset) && ds.opts[:limit]
605:             super(sql, op, [args[0], ds.from_self])
606:           else
607:             super
608:           end
609:         when :~, '!~''!~', '~*''~*', '!~*''!~*', :LIKE, 'NOT LIKE''NOT LIKE', :ILIKE, 'NOT ILIKE''NOT ILIKE'
610:           if !db.mariadb? && db.server_version >= 80000 && [:~, '!~''!~'].include?(op)
611:             func = Sequel.function(:REGEXP_LIKE, args[0], args[1], 'c')
612:             func = ~func if op == '!~''!~'
613:             return literal_append(sql, func)
614:           end
615: 
616:           sql << '('
617:           literal_append(sql, args[0])
618:           sql << ' '
619:           sql << 'NOT ' if ['NOT LIKE''NOT LIKE', 'NOT ILIKE''NOT ILIKE', '!~''!~', '!~*''!~*'].include?(op)
620:           sql << ([:~, '!~''!~', '~*''~*', '!~*''!~*'].include?(op) ? 'REGEXP' : 'LIKE')
621:           sql << ' '
622:           sql << 'BINARY ' if [:~, '!~''!~', :LIKE, 'NOT LIKE''NOT LIKE'].include?(op)
623:           literal_append(sql, args[1])
624:           if [:LIKE, 'NOT LIKE''NOT LIKE', :ILIKE, 'NOT ILIKE''NOT ILIKE'].include?(op)
625:             sql << " ESCAPE "
626:             literal_append(sql, "\\")
627:           end
628:           sql << ')'
629:         when '||''||'
630:           if args.length > 1
631:             sql << "CONCAT"
632:             array_sql_append(sql, args)
633:           else
634:             literal_append(sql, args[0])
635:           end
636:         when 'B~''B~'
637:           sql << "CAST(~"
638:           literal_append(sql, args[0])
639:           sql << " AS SIGNED INTEGER)"
640:         else
641:           super
642:         end
643:       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 649
649:       def constant_sql_append(sql, constant)
650:         if constant == :CURRENT_TIMESTAMP && supports_timestamp_usecs?
651:           sql << 'CURRENT_TIMESTAMP(6)'
652:         else
653:           super
654:         end
655:       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 678
678:       def delete_from(*tables)
679:         clone(:delete_from=>tables)
680:       end

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 658
658:       def distinct(*args)
659:         args.empty? ? super : group(*args)
660:       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 684
684:       def explain(opts=OPTS)
685:         # Load the PrettyTable class, needed for explain output
686:         Sequel.extension(:_pretty_table) unless defined?(Sequel::PrettyTable)
687: 
688:         ds = db.send(:metadata_dataset).with_sql(((opts[:extended] && (db.mariadb? || db.server_version < 50700)) ? 'EXPLAIN EXTENDED ' : 'EXPLAIN ') + select_sql).naked
689:         rows = ds.all
690:         Sequel::PrettyTable.string(rows, ds.columns)
691:       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 694
694:       def for_share
695:         lock_style(:share)
696:       end

Adds full text filter

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 699
699:       def full_text_search(cols, terms, opts = OPTS)
700:         where(full_text_sql(cols, terms, opts))
701:       end

MySQL specific full text search syntax.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 704
704:       def full_text_sql(cols, terms, opts = OPTS)
705:         terms = terms.join(' ') if terms.is_a?(Array)
706:         SQL::PlaceholderLiteralString.new((opts[:boolean] ? MATCH_AGAINST_BOOLEAN : MATCH_AGAINST), [Array(cols), terms])
707:       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 726
726:       def insert_ignore
727:         clone(:insert_ignore=>true)
728:       end

Transforms :straight to STRAIGHT_JOIN.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 710
710:       def join_type_sql(join_type)
711:         if join_type == :straight
712:           'STRAIGHT_JOIN'
713:         else
714:           super
715:         end
716:       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 759
759:       def on_duplicate_key_update(*args)
760:         clone(:on_duplicate_key_update => args)
761:       end

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 764
764:       def quoted_identifier_append(sql, c)
765:         sql << '`' << c.to_s.gsub('`', '``') << '`'
766:       end

MariaDB 10.2+ and MySQL 8+ support CTEs

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 769
769:       def supports_cte?(type=:select)
770:         if db.mariadb?
771:           type == :select && db.server_version >= 100200
772:         else
773:           case type
774:           when :select, :update, :delete
775:             db.server_version >= 80000
776:           end
777:         end
778:       end

MySQL does not support derived column lists

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 781
781:       def supports_derived_column_lists?
782:         false
783:       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 787
787:       def supports_distinct_on?
788:         true
789:       end

MySQL supports GROUP BY WITH ROLLUP (but not CUBE)

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 792
792:       def supports_group_rollup?
793:         true
794:       end

MySQL does not support INTERSECT or EXCEPT

[Source]

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

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 802
802:       def supports_limits_in_correlated_subqueries?
803:         false
804:       end

MySQL supports modifying joined datasets

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 807
807:       def supports_modifying_joins?
808:         true
809:       end

MySQL 8+ supports NOWAIT.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 812
812:       def supports_nowait?
813:         !db.mariadb? && db.server_version >= 80000
814:       end

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 818
818:       def supports_ordered_distinct_on?
819:         false
820:       end

MySQL supports pattern matching via regular expressions

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 823
823:       def supports_regexp?
824:         true
825:       end

MySQL 8+ supports SKIP LOCKED.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 828
828:       def supports_skip_locked?
829:         !db.mariadb? && db.server_version >= 80000
830:       end

Check the database setting for whether fractional timestamps are suppported.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 834
834:       def supports_timestamp_usecs?
835:         db.supports_timestamp_usecs?
836:       end

MariaDB 10.2+ and MySQL 8+ support window functions

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 839
839:       def supports_window_functions?
840:         db.server_version >= (db.mariadb? ? 100200 : 80000)
841:       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 849
849:       def update_ignore
850:         clone(:update_ignore=>true)
851:       end

[Validate]