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

Instance methods for datasets that connect to an SQLite database

Methods

Included Modules

Dataset::Replace

Constants

CONSTANT_MAP = {:CURRENT_DATE=>"date(CURRENT_TIMESTAMP, 'localtime')".freeze, :CURRENT_TIMESTAMP=>"datetime(CURRENT_TIMESTAMP, 'localtime')".freeze, :CURRENT_TIME=>"time(CURRENT_TIMESTAMP, 'localtime')".freeze}
EMULATED_FUNCTION_MAP = {:char_length=>'length'.freeze}
EXTRACT_MAP = {:year=>"'%Y'", :month=>"'%m'", :day=>"'%d'", :hour=>"'%H'", :minute=>"'%M'", :second=>"'%f'"}
NOT_SPACE = Dataset::NOT_SPACE
COMMA = Dataset::COMMA
PAREN_CLOSE = Dataset::PAREN_CLOSE
AS = Dataset::AS
APOS = Dataset::APOS
EXTRACT_OPEN = "CAST(strftime(".freeze
EXTRACT_CLOSE = ') AS '.freeze
NUMERIC = 'NUMERIC'.freeze
INTEGER = 'INTEGER'.freeze
BACKTICK = '`'.freeze
BACKTICK_RE = /`/.freeze
DOUBLE_BACKTICK = '``'.freeze
BLOB_START = "X'".freeze
HSTAR = "H*".freeze
DATE_OPEN = "date(".freeze
DATETIME_OPEN = "datetime(".freeze
ONLY_OFFSET = " LIMIT -1 OFFSET ".freeze

Public Instance methods

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 521
521:       def cast_sql_append(sql, expr, type)
522:         if type == Time or type == DateTime
523:           sql << DATETIME_OPEN
524:           literal_append(sql, expr)
525:           sql << PAREN_CLOSE
526:         elsif type == Date
527:           sql << DATE_OPEN
528:           literal_append(sql, expr)
529:           sql << PAREN_CLOSE
530:         else
531:           super
532:         end
533:       end

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

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 537
537:       def complex_expression_sql_append(sql, op, args)
538:         case op
539:         when "NOT LIKE""NOT LIKE", "NOT ILIKE""NOT ILIKE"
540:           sql << NOT_SPACE
541:           complex_expression_sql_append(sql, (op == "NOT ILIKE""NOT ILIKE" ? :ILIKE : :LIKE), args)
542:         when :^
543:           complex_expression_arg_pairs_append(sql, args){|a, b| Sequel.lit(["((~(", " & ", ")) & (", " | ", "))"], a, b, a, b)}
544:         when :extract
545:           part = args.at(0)
546:           raise(Sequel::Error, "unsupported extract argument: #{part.inspect}") unless format = EXTRACT_MAP[part]
547:           sql << EXTRACT_OPEN << format << COMMA
548:           literal_append(sql, args.at(1))
549:           sql << EXTRACT_CLOSE << (part == :second ? NUMERIC : INTEGER) << PAREN_CLOSE
550:         else
551:           super
552:         end
553:       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 557
557:       def constant_sql_append(sql, constant)
558:         if c = CONSTANT_MAP[constant]
559:           sql << c
560:         else
561:           super
562:         end
563:       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 568
568:       def delete
569:         @opts[:where] ? super : where(1=>1).delete
570:       end

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

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 575
575:       def explain(opts=nil)
576:         # Load the PrettyTable class, needed for explain output
577:         Sequel.extension(:_pretty_table) unless defined?(Sequel::PrettyTable)
578: 
579:         ds = db.send(:metadata_dataset).clone(:sql=>"EXPLAIN #{select_sql}")
580:         rows = ds.all
581:         Sequel::PrettyTable.string(rows, ds.columns)
582:       end

HAVING requires GROUP BY on SQLite

[Source]

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

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

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 591
591:       def quoted_identifier_append(sql, c)
592:         sql << BACKTICK << c.to_s.gsub(BACKTICK_RE, DOUBLE_BACKTICK) << BACKTICK
593:       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 599
599:       def select(*cols)
600:         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)})
601:           super(*cols.map{|c| alias_qualified_column(c)})
602:         else
603:           super
604:         end
605:       end

SQLite 3.8.3+ supports common table expressions.

[Source]

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

SQLite does not support table aliases with column aliases

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 613
613:       def supports_derived_column_lists?
614:         false
615:       end

SQLite does not support INTERSECT ALL or EXCEPT ALL

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 618
618:       def supports_intersect_except_all?
619:         false
620:       end

SQLite does not support IS TRUE

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 623
623:       def supports_is_true?
624:         false
625:       end

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

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 628
628:       def supports_multiple_column_in?
629:         false
630:       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 635
635:       def supports_timestamp_timezones?
636:         db.use_timestamp_timezones?
637:       end

SQLite cannot use WHERE ‘t’.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 640
640:       def supports_where_true?
641:         false
642:       end

[Validate]