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 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.
# 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.
# 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
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 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
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 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)
# File lib/sequel/adapters/shared/sqlite.rb, line 635 635: def insert_ignore 636: insert_conflict(:ignore) 637: 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 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 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 667 667: def supports_timestamp_timezones? 668: db.use_timestamp_timezones? 669: end