| Module | Sequel::SQLite::DatasetMethods |
| In: |
lib/sequel/adapters/shared/sqlite.rb
|
Instance methods for datasets that connect to an SQLite database
| SELECT_CLAUSE_METHODS | = | Dataset.clause_methods(:select, %w'select distinct columns from join where group having compounds order limit') |
| 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 |
# File lib/sequel/adapters/shared/sqlite.rb, line 497
497: def cast_sql_append(sql, expr, type)
498: if type == Time or type == DateTime
499: sql << DATETIME_OPEN
500: literal_append(sql, expr)
501: sql << PAREN_CLOSE
502: elsif type == Date
503: sql << DATE_OPEN
504: literal_append(sql, expr)
505: sql << PAREN_CLOSE
506: else
507: super
508: end
509: 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.
# File lib/sequel/adapters/shared/sqlite.rb, line 513
513: def complex_expression_sql_append(sql, op, args)
514: case op
515: when "NOT LIKE""NOT LIKE", "NOT ILIKE""NOT ILIKE"
516: sql << NOT_SPACE
517: complex_expression_sql_append(sql, (op == "NOT ILIKE""NOT ILIKE" ? :ILIKE : :LIKE), args)
518: when :^
519: sql << complex_expression_arg_pairs(args) do |a, b|
520: a = literal(a)
521: b = literal(b)
522: "((~(#{a} & #{b})) & (#{a} | #{b}))"
523: end
524: when :extract
525: part = args.at(0)
526: raise(Sequel::Error, "unsupported extract argument: #{part.inspect}") unless format = EXTRACT_MAP[part]
527: sql << EXTRACT_OPEN << format << COMMA
528: literal_append(sql, args.at(1))
529: sql << EXTRACT_CLOSE << (part == :second ? NUMERIC : INTEGER) << PAREN_CLOSE
530: else
531: super
532: end
533: end
SQLite has CURRENT_TIMESTAMP and related constants in UTC instead of in localtime, so convert those constants to local time.
# File lib/sequel/adapters/shared/sqlite.rb, line 537
537: def constant_sql_append(sql, constant)
538: if c = CONSTANT_MAP[constant]
539: sql << c
540: else
541: super
542: end
543: 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.
# File lib/sequel/adapters/shared/sqlite.rb, line 555
555: def explain(opts=nil)
556: # Load the PrettyTable class, needed for explain output
557: Sequel.extension(:_pretty_table) unless defined?(Sequel::PrettyTable)
558:
559: ds = db.send(:metadata_dataset).clone(:sql=>"EXPLAIN #{select_sql}")
560: rows = ds.all
561: Sequel::PrettyTable.string(rows, ds.columns)
562: 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.
# File lib/sequel/adapters/shared/sqlite.rb, line 579
579: def select(*cols)
580: 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)})
581: super(*cols.map{|c| alias_qualified_column(c)})
582: else
583: super
584: end
585: 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.
# File lib/sequel/adapters/shared/sqlite.rb, line 605
605: def supports_timestamp_timezones?
606: db.use_timestamp_timezones?
607: end