Class: Exwiw::Adapter::Sqlite3Adapter
- Inherits:
-
Base
- Object
- Base
- Exwiw::Adapter::Sqlite3Adapter
show all
- Defined in:
- lib/exwiw/adapter/sqlite3_adapter.rb
Instance Attribute Summary
Attributes inherited from Base
#connection_config
Instance Method Summary
collapse
Methods inherited from Base
#dumpable?, #initialize, #output_extension, #supports_bulk_delete?, table_config_class, #validate_as_dump_target!
Instance Method Details
#build_query(table, dump_target, table_by_name) ⇒ Object
6
7
8
|
# File 'lib/exwiw/adapter/sqlite3_adapter.rb', line 6
def build_query(table, dump_target, table_by_name)
Exwiw::QueryAstBuilder.run(table.name, table_by_name, dump_target, @logger)
end
|
#compile_ast(query_ast) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/exwiw/adapter/sqlite3_adapter.rb', line 78
def compile_ast(query_ast)
raise NotImplementedError unless query_ast.is_a?(Exwiw::QueryAst::Select)
sql = "SELECT "
sql += query_ast.columns.map { |col| compile_column_name(query_ast, col) }.join(', ')
sql += " FROM #{query_ast.from_table_name}"
query_ast.join_clauses.each do |join|
sql += " JOIN #{join.join_table_name} ON #{join.base_table_name}.#{join.foreign_key} = #{join.join_table_name}.#{join.primary_key}"
join.where_clauses.each do |where|
compiled_where_condition = compile_where_condition(where, join.join_table_name)
sql += " AND #{compiled_where_condition}"
end
end
if query_ast.where_clauses.any?
sql += " WHERE "
sql += query_ast.where_clauses.map { |where| compile_where_condition(where, query_ast.from_table_name) }.join(' AND ')
end
sql
end
|
#execute(query_ast) ⇒ Object
10
11
12
13
14
15
|
# File 'lib/exwiw/adapter/sqlite3_adapter.rb', line 10
def execute(query_ast)
sql = compile_ast(query_ast)
@logger.debug(" Executing SQL: \n#{sql}")
connection.execute(sql)
end
|
#to_bulk_delete(select_query_ast, table) ⇒ Object
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
|
# File 'lib/exwiw/adapter/sqlite3_adapter.rb', line 32
def to_bulk_delete(select_query_ast, table)
raise NotImplementedError unless select_query_ast.is_a?(Exwiw::QueryAst::Select)
sql = "DELETE FROM #{select_query_ast.from_table_name}"
if select_query_ast.join_clauses.empty?
compiled_where_conditions = select_query_ast.
where_clauses.
select { |where| where.is_a?(Exwiw::QueryAst::WhereClause) }.
map do |where|
compile_where_condition(where, select_query_ast.from_table_name)
end
if compiled_where_conditions.size > 0
sql += "\nWHERE "
sql += compiled_where_conditions.join(' AND ')
end
sql += ";"
return sql
end
subquery_ast = Exwiw::QueryAst::Select.new
first_join = select_query_ast.join_clauses.first.clone
subquery_ast.from(first_join.join_table_name)
primay_key_col = table.columns.find { |col| col.name == table.primary_key }
subquery_ast.select([primay_key_col])
select_query_ast.join_clauses[1..].each do |join|
subquery_ast.join(join)
end
first_join.where_clauses.each do |where|
subquery_ast.where(where) if where.is_a?(Exwiw::QueryAst::WhereClause)
end
foreign_key = first_join.foreign_key
subquery_sql = compile_ast(subquery_ast)
sql += "\nWHERE #{select_query_ast.from_table_name}.#{foreign_key} IN (#{subquery_sql});"
sql
end
|
#to_bulk_insert(results, table) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/exwiw/adapter/sqlite3_adapter.rb', line 17
def to_bulk_insert(results, table)
table_name = table.name
value_list = results.map do |row|
quoted_values = row.map do |value|
escape_value(value)
end
"(" + quoted_values.join(', ') + ")"
end
values = value_list.join(",\n")
column_names = table.columns.map(&:name).join(', ')
"INSERT INTO #{table_name} (#{column_names}) VALUES\n#{values};"
end
|