1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
require 'active_record/connection_adapters/sqlite_adapter'
gem 'sqlite3', '~> 1.3.5'
require 'sqlite3'
module ActiveRecord
module ConnectionHandling
# sqlite3 adapter reuses sqlite_connection.
def sqlite3_connection(config) # :nodoc:
# Require database.
unless config[:database]
raise ArgumentError, "No database file specified. Missing argument: database"
end
# Allow database path relative to Rails.root, but only if
# the database path is not the special path that tells
# Sqlite to build a database only in memory.
if defined?(Rails.root) && ':memory:' != config[:database]
config[:database] = File.expand_path(config[:database], Rails.root)
end
unless 'sqlite3' == config[:adapter]
raise ArgumentError, 'adapter name should be "sqlite3"'
end
db = SQLite3::Database.new(
config[:database],
:results_as_hash => true
)
db.busy_timeout(config[:timeout]) if config[:timeout]
ConnectionAdapters::SQLite3Adapter.new(db, logger, config)
end
end
module ConnectionAdapters #:nodoc:
class SQLite3Adapter < SQLiteAdapter # :nodoc:
def quote(value, column = nil)
if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary)
s = column.class.string_to_binary(value).unpack("H*")[0]
"x'#{s}'"
else
super
end
end
# Returns the current database encoding format as a string, eg: 'UTF-8'
def encoding
@connection.encoding.to_s
end
# Returns true.
def supports_explain?
true
end
# DATABASE STATEMENTS ======================================
def explain(arel, binds = [])
sql = "EXPLAIN QUERY PLAN #{to_sql(arel, binds)}"
ExplainPrettyPrinter.new.pp(exec_query(sql, 'EXPLAIN', binds))
end
class ExplainPrettyPrinter
# Pretty prints the result of a EXPLAIN QUERY PLAN in a way that resembles
# the output of the SQLite shell:
#
# 0|0|0|SEARCH TABLE users USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)
# 0|1|1|SCAN TABLE posts (~100000 rows)
#
def pp(result) # :nodoc:
result.rows.map do |row|
row.join('|')
end.join("\n") + "\n"
end
end
end
end
end
|