| Module | Sequel::SQLite::DatasetMethods |
| In: |
lib/sequel/adapters/shared/sqlite.rb
|
| 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 |
# 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.
# 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.
# 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
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.
# 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
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)
# 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)
# File lib/sequel/adapters/shared/sqlite.rb, line 642
642: def insert_ignore
643: insert_conflict(:ignore)
644: 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 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 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 679
679: def supports_timestamp_timezones?
680: db.use_timestamp_timezones?
681: end