baza 0.0.20 → 0.0.21
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +84 -0
- data/.rubocop_todo.yml +17 -135
- data/.travis.yml +21 -0
- data/Gemfile +10 -7
- data/Gemfile.lock +39 -44
- data/README.md +61 -3
- data/VERSION +1 -1
- data/baza.gemspec +146 -98
- data/config/best_project_practice_rubocop.yml +8 -0
- data/config/best_project_practice_rubocop_todo.yml +6 -0
- data/lib/baza.rb +8 -12
- data/lib/baza/base_sql_driver.rb +198 -52
- data/lib/baza/cloner.rb +1 -0
- data/lib/baza/column.rb +26 -0
- data/lib/baza/database.rb +19 -3
- data/lib/baza/db.rb +69 -271
- data/lib/baza/driver.rb +1 -6
- data/lib/baza/{drivers → driver}/active_record.rb +65 -21
- data/lib/baza/{drivers → driver}/active_record/columns.rb +0 -0
- data/lib/baza/driver/active_record/commands.rb +10 -0
- data/lib/baza/driver/active_record/databases.rb +10 -0
- data/lib/baza/{drivers → driver}/active_record/indexes.rb +0 -0
- data/lib/baza/{drivers → driver}/active_record/result.rb +3 -1
- data/lib/baza/{drivers → driver}/active_record/tables.rb +0 -0
- data/lib/baza/driver/active_record/users.rb +12 -0
- data/lib/baza/{drivers → driver}/mysql.rb +9 -26
- data/lib/baza/{drivers → driver}/mysql/column.rb +14 -35
- data/lib/baza/{drivers → driver}/mysql/columns.rb +9 -12
- data/lib/baza/driver/mysql/commands.rb +39 -0
- data/lib/baza/driver/mysql/database.rb +64 -0
- data/lib/baza/driver/mysql/databases.rb +63 -0
- data/lib/baza/{drivers → driver}/mysql/index.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql/indexes.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql/result.rb +15 -7
- data/lib/baza/{drivers → driver}/mysql/sqlspecs.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql/table.rb +27 -43
- data/lib/baza/{drivers → driver}/mysql/tables.rb +5 -34
- data/lib/baza/{drivers → driver}/mysql/unbuffered_result.rb +8 -2
- data/lib/baza/driver/mysql/user.rb +22 -0
- data/lib/baza/driver/mysql/users.rb +39 -0
- data/lib/baza/{drivers → driver}/mysql2.rb +19 -49
- data/lib/baza/{drivers → driver}/mysql2/column.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql2/columns.rb +0 -0
- data/lib/baza/driver/mysql2/commands.rb +2 -0
- data/lib/baza/{drivers → driver}/mysql2/database.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql2/databases.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql2/index.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql2/indexes.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql2/result.rb +3 -1
- data/lib/baza/{drivers → driver}/mysql2/table.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql2/tables.rb +0 -0
- data/lib/baza/driver/mysql2/user.rb +2 -0
- data/lib/baza/driver/mysql2/users.rb +2 -0
- data/lib/baza/{drivers → driver}/mysql_java.rb +60 -38
- data/lib/baza/{drivers → driver}/mysql_java/column.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql_java/columns.rb +0 -0
- data/lib/baza/driver/mysql_java/commands.rb +2 -0
- data/lib/baza/driver/mysql_java/database.rb +2 -0
- data/lib/baza/driver/mysql_java/databases.rb +2 -0
- data/lib/baza/{drivers → driver}/mysql_java/index.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql_java/indexes.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql_java/table.rb +0 -0
- data/lib/baza/{drivers → driver}/mysql_java/tables.rb +0 -0
- data/lib/baza/driver/mysql_java/user.rb +2 -0
- data/lib/baza/driver/mysql_java/users.rb +2 -0
- data/lib/baza/driver/pg.rb +80 -0
- data/lib/baza/driver/pg/column.rb +125 -0
- data/lib/baza/driver/pg/columns.rb +37 -0
- data/lib/baza/driver/pg/commands.rb +35 -0
- data/lib/baza/driver/pg/create_index_sql_creator.rb +51 -0
- data/lib/baza/driver/pg/database.rb +89 -0
- data/lib/baza/driver/pg/databases.rb +79 -0
- data/lib/baza/driver/pg/index.rb +35 -0
- data/lib/baza/driver/pg/indexes.rb +5 -0
- data/lib/baza/driver/pg/result.rb +139 -0
- data/lib/baza/driver/pg/table.rb +184 -0
- data/lib/baza/driver/pg/tables.rb +45 -0
- data/lib/baza/{drivers → driver}/sqlite3.rb +6 -24
- data/lib/baza/{drivers → driver}/sqlite3/column.rb +22 -24
- data/lib/baza/{drivers → driver}/sqlite3/columns.rb +6 -6
- data/lib/baza/driver/sqlite3/commands.rb +28 -0
- data/lib/baza/{drivers → driver}/sqlite3/database.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3/databases.rb +0 -1
- data/lib/baza/{drivers → driver}/sqlite3/index.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3/indexes.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3/result.rb +14 -6
- data/lib/baza/{drivers → driver}/sqlite3/sqlspecs.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3/table.rb +25 -16
- data/lib/baza/{drivers → driver}/sqlite3/tables.rb +5 -6
- data/lib/baza/{drivers → driver}/sqlite3/unbuffered_result.rb +8 -2
- data/lib/baza/{drivers → driver}/sqlite3_java.rb +13 -23
- data/lib/baza/{drivers → driver}/sqlite3_java/column.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3_java/columns.rb +0 -0
- data/lib/baza/driver/sqlite3_java/commands.rb +2 -0
- data/lib/baza/{drivers → driver}/sqlite3_java/database.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3_java/index.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3_java/indexes.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3_java/table.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3_java/tables.rb +0 -0
- data/lib/baza/{drivers → driver}/sqlite3_java/unbuffered_result.rb +14 -9
- data/lib/baza/{drivers → driver}/sqlite3_rhodes.rb +6 -24
- data/lib/baza/errors.rb +2 -0
- data/lib/baza/idquery.rb +15 -8
- data/lib/baza/index.rb +7 -0
- data/lib/baza/jdbc_driver.rb +4 -16
- data/lib/baza/jdbc_result.rb +20 -12
- data/lib/baza/mysql_base_driver.rb +7 -7
- data/lib/baza/query_buffer.rb +20 -19
- data/lib/baza/row.rb +16 -16
- data/lib/baza/sql_queries.rb +3 -0
- data/lib/baza/sql_queries/generic_insert.rb +81 -0
- data/lib/baza/sql_queries/generic_update.rb +31 -0
- data/lib/baza/sql_queries/mysql_upsert.rb +52 -0
- data/lib/baza/sql_queries/mysql_upsert_duplicate_key.rb +57 -0
- data/lib/baza/sql_queries/non_atomic_upsert.rb +25 -0
- data/lib/baza/sql_queries/postgres_upsert_duplicate_key.rb +118 -0
- data/lib/baza/sql_queries/select.rb +170 -0
- data/lib/baza/sql_queries/sqlite_upsert_duplicate_key.rb +99 -0
- data/lib/baza/table.rb +35 -8
- data/spec/active_record/models/user.rb +3 -0
- data/spec/{cloner_spec.rb → baza/cloner_spec.rb} +0 -0
- data/spec/drivers/active_record_mysql2_spec.rb +5 -3
- data/spec/drivers/active_record_mysql_spec.rb +2 -1
- data/spec/drivers/active_record_pg_spec.rb +20 -0
- data/spec/drivers/active_record_sqlite3_spec.rb +2 -1
- data/spec/drivers/mysql2_spec.rb +1 -1
- data/spec/drivers/mysql_spec.rb +10 -10
- data/spec/drivers/pg_spec.rb +18 -0
- data/spec/drivers/sqlite3_spec.rb +7 -8
- data/spec/info_active_record_example.rb +1 -1
- data/spec/{info_active_record_mysql2.rb → info_active_record_mysql2_example.rb} +3 -2
- data/spec/info_active_record_mysql2_travis.rb +35 -0
- data/spec/{info_active_record_mysql.rb → info_active_record_mysql_example.rb} +5 -4
- data/spec/info_active_record_mysql_travis.rb +36 -0
- data/spec/info_active_record_pg_example.rb +36 -0
- data/spec/info_active_record_pg_travis.rb +34 -0
- data/spec/info_active_record_sqlite3.rb +1 -1
- data/spec/info_mysql2_example.rb +1 -3
- data/spec/{info_mysql2_shippable.rb → info_mysql2_travis.rb} +2 -4
- data/spec/info_mysql_example.rb +1 -3
- data/spec/{info_mysql_shippable.rb → info_mysql_travis.rb} +2 -4
- data/spec/info_pg_example.rb +22 -0
- data/spec/info_pg_travis.rb +20 -0
- data/spec/info_sqlite3.rb +1 -3
- data/spec/spec_helper.rb +1 -1
- data/spec/support/driver_active_record_collection.rb +62 -0
- data/spec/support/driver_collection.rb +136 -121
- data/spec/support/driver_columns_collection.rb +19 -10
- data/spec/support/driver_databases_collection.rb +23 -1
- data/spec/support/driver_indexes_collection.rb +2 -2
- data/spec/support/driver_tables_collection.rb +24 -4
- data/spec/support/driver_users_collection.rb +53 -0
- metadata +185 -104
- data/lib/baza/drivers/mysql/database.rb +0 -28
- data/lib/baza/drivers/mysql/databases.rb +0 -35
- data/lib/baza/drivers/mysql_java/database.rb +0 -2
- data/lib/baza/drivers/mysql_java/databases.rb +0 -2
- data/lib/baza/model.rb +0 -875
- data/lib/baza/model_custom.rb +0 -155
- data/lib/baza/model_handler.rb +0 -910
- data/lib/baza/model_handler_sqlhelper.rb +0 -484
- data/lib/baza/revision.rb +0 -383
- data/shippable.yml +0 -17
- data/spec/info_active_record_mysql2_shippable.rb +0 -34
- data/spec/info_active_record_mysql_shippable.rb +0 -34
- data/spec/model_handler_spec.rb +0 -431
@@ -0,0 +1,35 @@
|
|
1
|
+
class Baza::Driver::Pg::Index < Baza::Index
|
2
|
+
attr_reader :name
|
3
|
+
|
4
|
+
def initialize(args)
|
5
|
+
@db = args.fetch(:db)
|
6
|
+
@data = args.fetch(:data)
|
7
|
+
@name = @data.fetch(:indexname)
|
8
|
+
end
|
9
|
+
|
10
|
+
def table
|
11
|
+
@db.tables[table_name]
|
12
|
+
end
|
13
|
+
|
14
|
+
def table_name
|
15
|
+
@data.fetch(:tablename)
|
16
|
+
end
|
17
|
+
|
18
|
+
def unique?
|
19
|
+
@data.fetch(:indexdef).include?(" UNIQUE ")
|
20
|
+
end
|
21
|
+
|
22
|
+
def primary?
|
23
|
+
name == "#{table_name}_pkey"
|
24
|
+
end
|
25
|
+
|
26
|
+
def rename(new_name)
|
27
|
+
@db.query("ALTER INDEX #{@db.sep_index}#{@db.escape_index(name)}#{@db.sep_index} RENAME TO #{@db.sep_index}#{@db.escape_index(new_name)}#{@db.sep_index}")
|
28
|
+
@name = new_name.to_s
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
def columns
|
33
|
+
@data.fetch(:indexdef).match(/\((.+)\)\Z/)[1].split(/\s*,\s/)
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
class Baza::Driver::Pg::Result
|
2
|
+
def initialize(driver, result, args = nil)
|
3
|
+
@result = result
|
4
|
+
@unbuffered = true if args && args[:unbuffered]
|
5
|
+
@type_translation = driver.db.opts[:type_translation]
|
6
|
+
|
7
|
+
types
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_enum
|
11
|
+
@enum ||= Enumerator.new do |yielder|
|
12
|
+
if @unbuffered
|
13
|
+
@result.stream_each_row do |values|
|
14
|
+
if @type_translation == :string
|
15
|
+
values = translate_values_to_strings(values)
|
16
|
+
elsif @type_translation
|
17
|
+
values = translate_values_with_types(values)
|
18
|
+
end
|
19
|
+
|
20
|
+
result_sym = Hash[*keys.zip(values).flatten]
|
21
|
+
yielder << result_sym
|
22
|
+
end
|
23
|
+
else
|
24
|
+
@result.each do |result|
|
25
|
+
values = result.values
|
26
|
+
|
27
|
+
if @type_translation == :string
|
28
|
+
values = translate_values_to_strings(values)
|
29
|
+
elsif @type_translation
|
30
|
+
values = translate_values_with_types(values)
|
31
|
+
end
|
32
|
+
|
33
|
+
result_sym = Hash[*keys.zip(values).flatten]
|
34
|
+
yielder << result_sym
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_a_enum
|
41
|
+
ArrayEnumerator.new(to_enum)
|
42
|
+
end
|
43
|
+
|
44
|
+
def fetch
|
45
|
+
to_enum.next
|
46
|
+
rescue StopIteration
|
47
|
+
nil
|
48
|
+
end
|
49
|
+
|
50
|
+
def each(&blk)
|
51
|
+
to_enum.each(&blk)
|
52
|
+
rescue StopIteration
|
53
|
+
nil
|
54
|
+
end
|
55
|
+
|
56
|
+
def to_a
|
57
|
+
array = []
|
58
|
+
each do |result|
|
59
|
+
array << result
|
60
|
+
end
|
61
|
+
|
62
|
+
array
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def keys
|
68
|
+
unless @keys
|
69
|
+
@keys = []
|
70
|
+
@result.fields.each do |field|
|
71
|
+
@keys << field.to_sym
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
@keys
|
76
|
+
end
|
77
|
+
|
78
|
+
def types
|
79
|
+
unless @types
|
80
|
+
@types = []
|
81
|
+
@result.fields.length.times do |count|
|
82
|
+
type_num = @result.ftype(count)
|
83
|
+
|
84
|
+
case type_num
|
85
|
+
when 20, 23
|
86
|
+
@types << :int
|
87
|
+
when 16, 19, 25, 26, 28, 1034, 1043
|
88
|
+
@types << :string
|
89
|
+
when 701
|
90
|
+
@types << :float
|
91
|
+
when 1114
|
92
|
+
@types << :time
|
93
|
+
when 1082
|
94
|
+
@types << :date
|
95
|
+
else
|
96
|
+
if @debug
|
97
|
+
data = nil
|
98
|
+
@result.each do |data_i|
|
99
|
+
data = data_i
|
100
|
+
break
|
101
|
+
end
|
102
|
+
|
103
|
+
value = data.values[count] if data
|
104
|
+
|
105
|
+
raise "Unknown type number: #{type_num} for this field: #{@result.fields[count]}: #{value}"
|
106
|
+
else
|
107
|
+
@types << :string
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
@types
|
114
|
+
end
|
115
|
+
|
116
|
+
def translate_values_with_types(values)
|
117
|
+
values.collect!.with_index do |value, count|
|
118
|
+
type_sym = types[count]
|
119
|
+
|
120
|
+
if type_sym == :int
|
121
|
+
value.to_i
|
122
|
+
elsif type_sym == :float
|
123
|
+
value.to_f
|
124
|
+
elsif type_sym == :time
|
125
|
+
Time.parse(value)
|
126
|
+
elsif type_sym == :date
|
127
|
+
Date.parse(value)
|
128
|
+
elsif type_sym == :string
|
129
|
+
value
|
130
|
+
else
|
131
|
+
raise "Unknown type symbol: #{type_sym}"
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def translate_values_to_strings(values)
|
137
|
+
values.map!(&:to_s)
|
138
|
+
end
|
139
|
+
end
|
@@ -0,0 +1,184 @@
|
|
1
|
+
class Baza::Driver::Pg::Table < Baza::Table
|
2
|
+
attr_reader :name
|
3
|
+
|
4
|
+
def initialize(args)
|
5
|
+
@db = args.fetch(:driver).db
|
6
|
+
@data = args.fetch(:data)
|
7
|
+
@name = @data.fetch(:table_name)
|
8
|
+
end
|
9
|
+
|
10
|
+
def drop
|
11
|
+
@db.with_database(database_name) do
|
12
|
+
@db.query("DROP TABLE \"#{@db.escape_table(name)}\"")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def database_name
|
17
|
+
@data.fetch(:table_catalog)
|
18
|
+
end
|
19
|
+
|
20
|
+
def native?
|
21
|
+
false
|
22
|
+
end
|
23
|
+
|
24
|
+
def columns(args = {})
|
25
|
+
where_args = {
|
26
|
+
table_catalog: @db.opts[:db],
|
27
|
+
table_name: name,
|
28
|
+
table_schema: "public"
|
29
|
+
}
|
30
|
+
|
31
|
+
where_args[:column_name] = args.fetch(:name) if args[:name]
|
32
|
+
|
33
|
+
columns_list = [] unless block_given?
|
34
|
+
@db.select([:information_schema, :columns], where_args) do |column_data|
|
35
|
+
column = Baza::Driver::Pg::Column.new(
|
36
|
+
db: @db,
|
37
|
+
data: column_data
|
38
|
+
)
|
39
|
+
|
40
|
+
if columns_list
|
41
|
+
columns_list << column
|
42
|
+
else
|
43
|
+
yield column
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
columns_list
|
48
|
+
end
|
49
|
+
|
50
|
+
def column(name)
|
51
|
+
column = columns(name: name).first
|
52
|
+
raise Baza::Errors::ColumnNotFound unless column
|
53
|
+
column
|
54
|
+
end
|
55
|
+
|
56
|
+
def truncate
|
57
|
+
@db.query("TRUNCATE #{@db.sep_table}#{@db.escape_table(name)}#{@db.sep_table} RESTART IDENTITY")
|
58
|
+
self
|
59
|
+
end
|
60
|
+
|
61
|
+
def indexes(args = {})
|
62
|
+
where_args = {
|
63
|
+
tablename: name
|
64
|
+
}
|
65
|
+
|
66
|
+
where_args[:indexname] = args.fetch(:name) if args[:name]
|
67
|
+
|
68
|
+
indexes_list = [] unless block_given?
|
69
|
+
@db.select(:pg_indexes, where_args) do |index_data|
|
70
|
+
index = Baza::Driver::Pg::Index.new(
|
71
|
+
db: @db,
|
72
|
+
data: index_data
|
73
|
+
)
|
74
|
+
|
75
|
+
next if index.primary?
|
76
|
+
|
77
|
+
if indexes_list
|
78
|
+
indexes_list << index
|
79
|
+
else
|
80
|
+
yield index
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
indexes_list
|
85
|
+
end
|
86
|
+
|
87
|
+
def index(name)
|
88
|
+
index = indexes(name: name).first
|
89
|
+
raise Baza::Errors::IndexNotFound unless index
|
90
|
+
index
|
91
|
+
end
|
92
|
+
|
93
|
+
def create_indexes(index_list, args = {})
|
94
|
+
Baza::Driver::Pg::Table.create_indexes(index_list, args.merge(table_name: name, db: @db))
|
95
|
+
end
|
96
|
+
|
97
|
+
def self.create_indexes(index_list, args = {})
|
98
|
+
db = args.fetch(:db)
|
99
|
+
sqls = Baza::Driver::Pg::CreateIndexSqlCreator.new(db: db, indexes: index_list, create_args: args).sqls
|
100
|
+
|
101
|
+
unless args[:return_sql]
|
102
|
+
db.transaction do
|
103
|
+
sqls.each do |sql|
|
104
|
+
db.query(sql)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
sqls if args[:return_sql]
|
110
|
+
end
|
111
|
+
|
112
|
+
def rename(new_name)
|
113
|
+
@db.query("ALTER TABLE #{@db.sep_table}#{@db.escape_table(name)}#{@db.sep_table} RENAME TO #{@db.sep_table}#{@db.escape_table(new_name)}#{@db.sep_table}")
|
114
|
+
@name = new_name.to_s
|
115
|
+
self
|
116
|
+
end
|
117
|
+
|
118
|
+
def reload
|
119
|
+
where_args = {
|
120
|
+
table_catalog: @db.opts.fetch(:db),
|
121
|
+
table_schema: "public",
|
122
|
+
table_name: name
|
123
|
+
}
|
124
|
+
|
125
|
+
data = @db.single([:information_schema, :tables], where_args)
|
126
|
+
raise Baza::Errors::TableNotFound unless data
|
127
|
+
@data = data
|
128
|
+
self
|
129
|
+
end
|
130
|
+
|
131
|
+
def optimize
|
132
|
+
@db.query("VACUUM #{@db.sep_table}#{@db.escape_table(name)}#{@db.sep_table}")
|
133
|
+
self
|
134
|
+
end
|
135
|
+
|
136
|
+
def clone(newname, _args = {})
|
137
|
+
raise "Invalid name." if newname.to_s.strip.empty?
|
138
|
+
|
139
|
+
columns_list = []
|
140
|
+
indexes_list = []
|
141
|
+
|
142
|
+
columns do |column|
|
143
|
+
columns_list << column.data
|
144
|
+
end
|
145
|
+
|
146
|
+
indexes do |index|
|
147
|
+
data = index.data
|
148
|
+
data.delete(:name)
|
149
|
+
indexes_list << data
|
150
|
+
end
|
151
|
+
|
152
|
+
@db.tables.create(newname, columns: columns_list, indexes: indexes_list)
|
153
|
+
|
154
|
+
clone_insert_from_original_table(newname, columns_list)
|
155
|
+
|
156
|
+
@db.tables[newname]
|
157
|
+
end
|
158
|
+
|
159
|
+
private
|
160
|
+
|
161
|
+
def clone_insert_from_original_table(newname, columns_list)
|
162
|
+
sql_clone = "INSERT INTO #{@db.sep_table}#{@db.escape_table(newname)}#{@db.sep_table} ("
|
163
|
+
|
164
|
+
first = true
|
165
|
+
columns_list.each do |column_data|
|
166
|
+
sql_clone << "," unless first
|
167
|
+
first = false if first
|
168
|
+
sql_clone << "#{@db.sep_col}#{@db.escape_column(column_data.fetch(:name))}#{@db.sep_col}"
|
169
|
+
end
|
170
|
+
|
171
|
+
sql_clone << ") SELECT "
|
172
|
+
|
173
|
+
first = true
|
174
|
+
columns_list.each do |column_data|
|
175
|
+
sql_clone << "," unless first
|
176
|
+
first = false if first
|
177
|
+
sql_clone << "#{@db.sep_col}#{@db.escape_column(column_data.fetch(:name))}#{@db.sep_col}"
|
178
|
+
end
|
179
|
+
|
180
|
+
sql_clone << " FROM #{@db.sep_table}#{@db.escape_table(name)}#{@db.sep_table}"
|
181
|
+
|
182
|
+
@db.query(sql_clone)
|
183
|
+
end
|
184
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
class Baza::Driver::Pg::Tables
|
2
|
+
attr_reader :db
|
3
|
+
|
4
|
+
def initialize(args)
|
5
|
+
@args = args
|
6
|
+
@db = @args.fetch(:db)
|
7
|
+
end
|
8
|
+
|
9
|
+
def [](table_name)
|
10
|
+
table = list(name: table_name).first
|
11
|
+
raise Baza::Errors::TableNotFound unless table
|
12
|
+
table
|
13
|
+
end
|
14
|
+
|
15
|
+
def list(args = {})
|
16
|
+
tables_list = [] unless block_given?
|
17
|
+
|
18
|
+
where_args = {
|
19
|
+
table_catalog: @db.opts[:db],
|
20
|
+
table_schema: "public"
|
21
|
+
}
|
22
|
+
where_args[:table_name] = args.fetch(:name) if args[:name]
|
23
|
+
|
24
|
+
@db.select([:information_schema, :tables], where_args, orderby: :table_name) do |table_data|
|
25
|
+
table = Baza::Driver::Pg::Table.new(
|
26
|
+
driver: @db.driver,
|
27
|
+
data: table_data
|
28
|
+
)
|
29
|
+
|
30
|
+
next if table.native?
|
31
|
+
|
32
|
+
if tables_list
|
33
|
+
tables_list << table
|
34
|
+
else
|
35
|
+
yield table
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
tables_list
|
40
|
+
end
|
41
|
+
|
42
|
+
def create(name, data, args = nil)
|
43
|
+
@db.current_database.create_table(name, data, args)
|
44
|
+
end
|
45
|
+
end
|
@@ -1,18 +1,6 @@
|
|
1
1
|
# This class handels SQLite3-specific behaviour.
|
2
2
|
class Baza::Driver::Sqlite3 < Baza::BaseSqlDriver
|
3
|
-
|
4
|
-
|
5
|
-
autoload :Database, "#{path}/database"
|
6
|
-
autoload :Databases, "#{path}/databases"
|
7
|
-
autoload :Table, "#{path}/table"
|
8
|
-
autoload :Tables, "#{path}/tables"
|
9
|
-
autoload :Column, "#{path}/column"
|
10
|
-
autoload :Columns, "#{path}/columns"
|
11
|
-
autoload :Index, "#{path}/index"
|
12
|
-
autoload :Indexes, "#{path}/indexes"
|
13
|
-
autoload :Result, "#{path}/result"
|
14
|
-
autoload :Sqlspecs, "#{path}/sqlspecs"
|
15
|
-
autoload :UnbufferedResult, "#{path}/unbuffered_result"
|
3
|
+
AutoAutoloader.autoload_sub_classes(self, __FILE__)
|
16
4
|
|
17
5
|
attr_reader :mutex_statement_reader
|
18
6
|
|
@@ -37,14 +25,14 @@ class Baza::Driver::Sqlite3 < Baza::BaseSqlDriver
|
|
37
25
|
end
|
38
26
|
|
39
27
|
# Constructor. This should not be called manually.
|
40
|
-
def initialize(
|
28
|
+
def initialize(db)
|
41
29
|
super
|
42
30
|
|
43
|
-
@path = @
|
31
|
+
@path = @db.opts[:path] if @db.opts[:path]
|
44
32
|
@mutex_statement_reader = Mutex.new
|
45
33
|
|
46
|
-
if @
|
47
|
-
@conn = @
|
34
|
+
if @db.opts[:conn]
|
35
|
+
@conn = @db.opts[:conn]
|
48
36
|
else
|
49
37
|
raise "No path was given." unless @path
|
50
38
|
require "sqlite3" unless ::Object.const_defined?(:SQLite3)
|
@@ -72,12 +60,6 @@ class Baza::Driver::Sqlite3 < Baza::BaseSqlDriver
|
|
72
60
|
string.to_s.gsub(/'/, "''")
|
73
61
|
end
|
74
62
|
|
75
|
-
# Returns the last inserted ID.
|
76
|
-
def last_id
|
77
|
-
return @conn.last_insert_row_id if @conn.respond_to?(:last_insert_row_id)
|
78
|
-
query("SELECT last_insert_rowid() AS id").fetch[:id].to_i
|
79
|
-
end
|
80
|
-
|
81
63
|
# Closes the connection to the database.
|
82
64
|
def close
|
83
65
|
@mutex_statement_reader.synchronize { @conn.close }
|
@@ -85,7 +67,7 @@ class Baza::Driver::Sqlite3 < Baza::BaseSqlDriver
|
|
85
67
|
|
86
68
|
# Starts a transaction, yields the database and commits.
|
87
69
|
def transaction
|
88
|
-
@conn.transaction { yield @
|
70
|
+
@conn.transaction { yield @db }
|
89
71
|
end
|
90
72
|
|
91
73
|
def supports_multiple_databases?
|