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 500
500:       def cast_sql_append(sql, expr, type)
501:         if type == Time or type == DateTime
502:           sql << "datetime("
503:           literal_append(sql, expr)
504:           sql << ')'
505:         elsif type == Date
506:           sql << "date("
507:           literal_append(sql, expr)
508:           sql << ')'
509:         else
510:           super
511:         end
512:       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 516
516:       def complex_expression_sql_append(sql, op, args)
517:         case op
518:         when "NOT LIKE""NOT LIKE", "NOT ILIKE""NOT ILIKE"
519:           sql << 'NOT '
520:           complex_expression_sql_append(sql, (op == "NOT ILIKE""NOT ILIKE" ? :ILIKE : :LIKE), args)
521:         when :^
522:           complex_expression_arg_pairs_append(sql, args){|a, b| Sequel.lit(["((~(", " & ", ")) & (", " | ", "))"], a, b, a, b)}
523:         when :**
524:           unless (exp = args[1]).is_a?(Integer)
525:             raise(Sequel::Error, "can only emulate exponentiation on SQLite if exponent is an integer, given #{exp.inspect}")
526:           end
527:           case exp
528:           when 0
529:             sql << '1'
530:           else
531:             sql << '('
532:             arg = args[0]
533:             if exp < 0
534:               invert = true
535:               exp = exp.abs
536:               sql << '(1.0 / ('
537:             end
538:             (exp - 1).times do 
539:               literal_append(sql, arg)
540:               sql << " * "
541:             end
542:             literal_append(sql, arg)
543:             sql << ')'
544:             if invert
545:               sql << "))"
546:             end
547:           end
548:         when :extract
549:           part = args[0]
550:           raise(Sequel::Error, "unsupported extract argument: #{part.inspect}") unless format = EXTRACT_MAP[part]
551:           sql << "CAST(strftime(" << format << ', '
552:           literal_append(sql, args[1])
553:           sql << ') AS ' << (part == :second ? 'NUMERIC' : 'INTEGER') << ')'
554:         else
555:           super
556:         end
557:       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 561
561:       def constant_sql_append(sql, constant)
562:         if c = CONSTANT_MAP[constant]
563:           sql << c
564:         else
565:           super
566:         end
567:       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 572
572:       def delete
573:         @opts[:where] ? super : where(1=>1).delete
574:       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 579
579:       def explain(opts=nil)
580:         # Load the PrettyTable class, needed for explain output
581:         Sequel.extension(:_pretty_table) unless defined?(Sequel::PrettyTable)
582: 
583:         ds = db.send(:metadata_dataset).clone(:sql=>"EXPLAIN #{select_sql}")
584:         rows = ds.all
585:         Sequel::PrettyTable.string(rows, ds.columns)
586:       end

HAVING requires GROUP BY on SQLite

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 589
589:       def having(*cond)
590:         raise(InvalidOperation, "Can only specify a HAVING clause on a grouped dataset") unless @opts[:group]
591:         super
592:       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 623
623:       def insert_conflict(resolution = :ignore)
624:         unless INSERT_CONFLICT_RESOLUTIONS.include?(resolution.to_s.upcase)
625:           raise Error, "Invalid value passed to Dataset#insert_conflict: #{resolution.inspect}.  The allowed values are: :rollback, :abort, :fail, :ignore, or :replace"
626:         end
627:         clone(:insert_conflict => resolution)
628:       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 635
635:       def insert_ignore
636:         insert_conflict(:ignore)
637:       end

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

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 595
595:       def quoted_identifier_append(sql, c)
596:         sql << '`' << c.to_s.gsub('`', '``') << '`'
597:       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 603
603:       def select(*cols)
604:         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)})
605:           super(*cols.map{|c| alias_qualified_column(c)})
606:         else
607:           super
608:         end
609:       end

SQLite 3.8.3+ supports common table expressions.

[Source]

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

SQLite does not support table aliases with column aliases

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 645
645:       def supports_derived_column_lists?
646:         false
647:       end

SQLite does not support INTERSECT ALL or EXCEPT ALL

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 650
650:       def supports_intersect_except_all?
651:         false
652:       end

SQLite does not support IS TRUE

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 655
655:       def supports_is_true?
656:         false
657:       end

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

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 660
660:       def supports_multiple_column_in?
661:         false
662:       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 667
667:       def supports_timestamp_timezones?
668:         db.use_timestamp_timezones?
669:       end

SQLite cannot use WHERE ‘t’.

[Source]

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

[Validate]