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 614
614:       def calc_found_rows
615:         clone(:calc_found_rows => true)
616:       end

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 554
554:       def complex_expression_sql_append(sql, op, args)
555:         case op
556:         when :IN, "NOT IN""NOT IN"
557:           ds = args[1]
558:           if ds.is_a?(Sequel::Dataset) && ds.opts[:limit]
559:             super(sql, op, [args[0], ds.from_self])
560:           else
561:             super
562:           end
563:         when :~, '!~''!~', '~*''~*', '!~*''!~*', :LIKE, 'NOT LIKE''NOT LIKE', :ILIKE, 'NOT ILIKE''NOT ILIKE'
564:           sql << '('
565:           literal_append(sql, args[0])
566:           sql << ' '
567:           sql << 'NOT ' if ['NOT LIKE''NOT LIKE', 'NOT ILIKE''NOT ILIKE', '!~''!~', '!~*''!~*'].include?(op)
568:           sql << ([:~, '!~''!~', '~*''~*', '!~*''!~*'].include?(op) ? 'REGEXP' : 'LIKE')
569:           sql << ' '
570:           sql << 'BINARY ' if [:~, '!~''!~', :LIKE, 'NOT LIKE''NOT LIKE'].include?(op)
571:           literal_append(sql, args[1])
572:           if [:LIKE, 'NOT LIKE''NOT LIKE', :ILIKE, 'NOT ILIKE''NOT ILIKE'].include?(op)
573:             sql << " ESCAPE "
574:             literal_append(sql, "\\")
575:           end
576:           sql << ')'
577:         when '||''||'
578:           if args.length > 1
579:             sql << "CONCAT"
580:             array_sql_append(sql, args)
581:           else
582:             literal_append(sql, args[0])
583:           end
584:         when 'B~''B~'
585:           sql << "CAST(~"
586:           literal_append(sql, args[0])
587:           sql << " AS SIGNED INTEGER)"
588:         else
589:           super
590:         end
591:       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 597
597:       def constant_sql_append(sql, constant)
598:         if constant == :CURRENT_TIMESTAMP && supports_timestamp_usecs?
599:           sql << 'CURRENT_TIMESTAMP(6)'
600:         else
601:           super
602:         end
603:       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 626
626:       def delete_from(*tables)
627:         clone(:delete_from=>tables)
628:       end

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 606
606:       def distinct(*args)
607:         args.empty? ? super : group(*args)
608:       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 632
632:       def explain(opts=OPTS)
633:         # Load the PrettyTable class, needed for explain output
634:         Sequel.extension(:_pretty_table) unless defined?(Sequel::PrettyTable)
635: 
636:         ds = db.send(:metadata_dataset).with_sql((opts[:extended] ? 'EXPLAIN EXTENDED ' : 'EXPLAIN ') + select_sql).naked
637:         rows = ds.all
638:         Sequel::PrettyTable.string(rows, ds.columns)
639:       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 642
642:       def for_share
643:         lock_style(:share)
644:       end

Adds full text filter

[Source]

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

MySQL specific full text search syntax.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 652
652:       def full_text_sql(cols, terms, opts = OPTS)
653:         terms = terms.join(' ') if terms.is_a?(Array)
654:         SQL::PlaceholderLiteralString.new((opts[:boolean] ? MATCH_AGAINST_BOOLEAN : MATCH_AGAINST), [Array(cols), terms])
655:       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 674
674:       def insert_ignore
675:         clone(:insert_ignore=>true)
676:       end

Transforms :straight to STRAIGHT_JOIN.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 658
658:       def join_type_sql(join_type)
659:         if join_type == :straight
660:           'STRAIGHT_JOIN'
661:         else
662:           super
663:         end
664:       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 707
707:       def on_duplicate_key_update(*args)
708:         clone(:on_duplicate_key_update => args)
709:       end

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

[Source]

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

MySQL does not support derived column lists

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 717
717:       def supports_derived_column_lists?
718:         false
719:       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 723
723:       def supports_distinct_on?
724:         true
725:       end

MySQL supports GROUP BY WITH ROLLUP (but not CUBE)

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 728
728:       def supports_group_rollup?
729:         true
730:       end

MySQL does not support INTERSECT or EXCEPT

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 733
733:       def supports_intersect_except?
734:         false
735:       end

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 738
738:       def supports_limits_in_correlated_subqueries?
739:         false
740:       end

MySQL supports modifying joined datasets

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 743
743:       def supports_modifying_joins?
744:         true
745:       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 749
749:       def supports_ordered_distinct_on?
750:         false
751:       end

MySQL supports pattern matching via regular expressions

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 754
754:       def supports_regexp?
755:         true
756:       end

Check the database setting for whether fractional timestamps are suppported.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 760
760:       def supports_timestamp_usecs?
761:         db.supports_timestamp_usecs?
762:       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 770
770:       def update_ignore
771:         clone(:update_ignore=>true)
772:       end

[Validate]