Module Sequel::SQLite::DatasetMethods
In: lib/sequel/adapters/shared/sqlite.rb

Methods

Included Modules

Dataset::Replace UnmodifiedIdentifiers::DatasetMethods

Constants

INSERT_CONFLICT_RESOLUTIONS = %w'ROLLBACK ABORT FAIL IGNORE REPLACE'.each(&:freeze).freeze   The allowed values for insert_conflict
CONSTANT_MAP = {:CURRENT_DATE=>"date(CURRENT_TIMESTAMP, 'localtime')".freeze, :CURRENT_TIMESTAMP=>"datetime(CURRENT_TIMESTAMP, 'localtime')".freeze, :CURRENT_TIME=>"time(CURRENT_TIMESTAMP, 'localtime')".freeze}.freeze
EXTRACT_MAP = {:year=>"'%Y'", :month=>"'%m'", :day=>"'%d'", :hour=>"'%H'", :minute=>"'%M'", :second=>"'%f'"}.freeze

Public Instance methods

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 507
507:       def cast_sql_append(sql, expr, type)
508:         if type == Time or type == DateTime
509:           sql << "datetime("
510:           literal_append(sql, expr)
511:           sql << ')'
512:         elsif type == Date
513:           sql << "date("
514:           literal_append(sql, expr)
515:           sql << ')'
516:         else
517:           super
518:         end
519:       end

SQLite doesn‘t support a NOT LIKE b, you need to use NOT (a LIKE b). It doesn‘t support xor, power, or the extract function natively, so those have to be emulated.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 523
523:       def complex_expression_sql_append(sql, op, args)
524:         case op
525:         when "NOT LIKE""NOT LIKE", "NOT ILIKE""NOT ILIKE"
526:           sql << 'NOT '
527:           complex_expression_sql_append(sql, (op == "NOT ILIKE""NOT ILIKE" ? :ILIKE : :LIKE), args)
528:         when :^
529:           complex_expression_arg_pairs_append(sql, args){|a, b| Sequel.lit(["((~(", " & ", ")) & (", " | ", "))"], a, b, a, b)}
530:         when :**
531:           unless (exp = args[1]).is_a?(Integer)
532:             raise(Sequel::Error, "can only emulate exponentiation on SQLite if exponent is an integer, given #{exp.inspect}")
533:           end
534:           case exp
535:           when 0
536:             sql << '1'
537:           else
538:             sql << '('
539:             arg = args[0]
540:             if exp < 0
541:               invert = true
542:               exp = exp.abs
543:               sql << '(1.0 / ('
544:             end
545:             (exp - 1).times do 
546:               literal_append(sql, arg)
547:               sql << " * "
548:             end
549:             literal_append(sql, arg)
550:             sql << ')'
551:             if invert
552:               sql << "))"
553:             end
554:           end
555:         when :extract
556:           part = args[0]
557:           raise(Sequel::Error, "unsupported extract argument: #{part.inspect}") unless format = EXTRACT_MAP[part]
558:           sql << "CAST(strftime(" << format << ', '
559:           literal_append(sql, args[1])
560:           sql << ') AS ' << (part == :second ? 'NUMERIC' : 'INTEGER') << ')'
561:         else
562:           super
563:         end
564:       end

SQLite has CURRENT_TIMESTAMP and related constants in UTC instead of in localtime, so convert those constants to local time.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 568
568:       def constant_sql_append(sql, constant)
569:         if c = CONSTANT_MAP[constant]
570:           sql << c
571:         else
572:           super
573:         end
574:       end

SQLite performs a TRUNCATE style DELETE if no filter is specified. Since we want to always return the count of records, add a condition that is always true and then delete.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 579
579:       def delete
580:         @opts[:where] ? super : where(1=>1).delete
581:       end

Return an array of strings specifying a query explanation for a SELECT of the current dataset. Currently, the options are ignored, but it accepts options to be compatible with other adapters.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 586
586:       def explain(opts=nil)
587:         # Load the PrettyTable class, needed for explain output
588:         Sequel.extension(:_pretty_table) unless defined?(Sequel::PrettyTable)
589: 
590:         ds = db.send(:metadata_dataset).clone(:sql=>"EXPLAIN #{select_sql}")
591:         rows = ds.all
592:         Sequel::PrettyTable.string(rows, ds.columns)
593:       end

HAVING requires GROUP BY on SQLite

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 596
596:       def having(*cond)
597:         raise(InvalidOperation, "Can only specify a HAVING clause on a grouped dataset") unless @opts[:group]
598:         super
599:       end

Handle uniqueness violations when inserting, by using a specified resolution algorithm. With no options, uses INSERT OR REPLACE. SQLite supports the following conflict resolution algoriths: ROLLBACK, ABORT, FAIL, IGNORE and REPLACE.

Examples:

  DB[:table].insert_conflict.insert(a: 1, b: 2)
  # INSERT OR IGNORE INTO TABLE (a, b) VALUES (1, 2)

  DB[:table].insert_conflict(:replace).insert(a: 1, b: 2)
  # INSERT OR REPLACE INTO TABLE (a, b) VALUES (1, 2)

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 630
630:       def insert_conflict(resolution = :ignore)
631:         unless INSERT_CONFLICT_RESOLUTIONS.include?(resolution.to_s.upcase)
632:           raise Error, "Invalid value passed to Dataset#insert_conflict: #{resolution.inspect}.  The allowed values are: :rollback, :abort, :fail, :ignore, or :replace"
633:         end
634:         clone(:insert_conflict => resolution)
635:       end

Ignore uniqueness/exclusion violations when inserting, using INSERT OR IGNORE. Exists mostly for compatibility to MySQL‘s insert_ignore. Example:

  DB[:table].insert_ignore.insert(a: 1, b: 2)
  # INSERT OR IGNORE INTO TABLE (a, b) VALUES (1, 2)

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 642
642:       def insert_ignore
643:         insert_conflict(:ignore)
644:       end

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

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 602
602:       def quoted_identifier_append(sql, c)
603:         sql << '`' << c.to_s.gsub('`', '``') << '`'
604:       end

When a qualified column is selected on SQLite and the qualifier is a subselect, the column name used is the full qualified name (including the qualifier) instead of just the column name. To get correct column names, you must use an alias.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 610
610:       def select(*cols)
611:         if ((f = @opts[:from]) && f.any?{|t| t.is_a?(Dataset) || (t.is_a?(SQL::AliasedExpression) && t.expression.is_a?(Dataset))}) || ((j = @opts[:join]) && j.any?{|t| t.table.is_a?(Dataset)})
612:           super(*cols.map{|c| alias_qualified_column(c)})
613:         else
614:           super
615:         end
616:       end

SQLite 3.8.3+ supports common table expressions.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 647
647:       def supports_cte?(type=:select)
648:         db.sqlite_version >= 30803
649:       end

SQLite supports CTEs in subqueries if it supports CTEs.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 652
652:       def supports_cte_in_subqueries?
653:         supports_cte?
654:       end

SQLite does not support table aliases with column aliases

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 657
657:       def supports_derived_column_lists?
658:         false
659:       end

SQLite does not support INTERSECT ALL or EXCEPT ALL

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 662
662:       def supports_intersect_except_all?
663:         false
664:       end

SQLite does not support IS TRUE

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 667
667:       def supports_is_true?
668:         false
669:       end

SQLite does not support multiple columns for the IN/NOT IN operators

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 672
672:       def supports_multiple_column_in?
673:         false
674:       end

SQLite supports timezones in literal timestamps, since it stores them as text. But using timezones in timestamps breaks SQLite datetime functions, so we allow the user to override the default per database.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 679
679:       def supports_timestamp_timezones?
680:         db.use_timestamp_timezones?
681:       end

SQLite cannot use WHERE ‘t’.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 684
684:       def supports_where_true?
685:         false
686:       end

[Validate]