baza 0.0.15 → 0.0.16
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +14 -7
- data/Gemfile.lock +19 -14
- data/README.md +1 -1
- data/VERSION +1 -1
- data/baza.gemspec +24 -21
- data/include/cloner.rb +18 -0
- data/include/column.rb +9 -0
- data/include/db.rb +2 -2
- data/include/drivers/active_record/active_record.rb +14 -4
- data/include/drivers/mysql/mysql.rb +56 -36
- data/include/drivers/mysql/mysql_column.rb +1 -1
- data/include/drivers/mysql/mysql_index.rb +2 -10
- data/include/drivers/mysql/mysql_table.rb +11 -11
- data/include/drivers/mysql/mysql_tables.rb +5 -7
- data/include/drivers/sqlite3/sqlite3.rb +43 -23
- data/include/drivers/sqlite3/sqlite3_column.rb +1 -1
- data/include/drivers/sqlite3/sqlite3_index.rb +5 -8
- data/include/drivers/sqlite3/sqlite3_result_java.rb +1 -1
- data/include/drivers/sqlite3/sqlite3_table.rb +17 -18
- data/include/drivers/sqlite3/sqlite3_tables.rb +4 -7
- data/include/index.rb +9 -0
- data/include/model.rb +1 -1
- data/include/model_handler.rb +162 -162
- data/include/table.rb +9 -0
- data/lib/baza.rb +19 -1
- data/shippable.yml +4 -1
- data/spec/include/cloner_spec.rb +10 -0
- data/spec/info_active_record.rb +20 -8
- data/spec/info_active_record_shippable.rb +18 -7
- data/spec/info_mysql_shippable.rb +16 -7
- data/spec/info_sqlite3.rb +1 -1
- data/spec/support/driver_indexes_collection.rb +16 -2
- metadata +28 -25
- data/include/drivers/sqlite3/libknjdb_java_sqlite3.rb +0 -78
- data/include/drivers/sqlite3/libknjdb_sqlite3_ironruby.rb +0 -69
@@ -1,4 +1,4 @@
|
|
1
|
-
class Baza::Driver::Mysql::Index
|
1
|
+
class Baza::Driver::Mysql::Index < Baza::Index
|
2
2
|
attr_reader :args, :columns
|
3
3
|
|
4
4
|
def initialize(args)
|
@@ -53,7 +53,7 @@ class Baza::Driver::Mysql::Index
|
|
53
53
|
|
54
54
|
#Returns true if the index is a unique-index.
|
55
55
|
def unique?
|
56
|
-
if @args[:data][:Index_type] == "UNIQUE"
|
56
|
+
if @args[:data][:Index_type] == "UNIQUE" || @args[:data][:Non_unique].to_i == 0
|
57
57
|
return true
|
58
58
|
else
|
59
59
|
return false
|
@@ -65,12 +65,4 @@ class Baza::Driver::Mysql::Index
|
|
65
65
|
return true if @args[:data][:Key_name] == "PRIMARY"
|
66
66
|
return false
|
67
67
|
end
|
68
|
-
|
69
|
-
def to_s
|
70
|
-
return "#<Baza::Driver::Mysql::Index name: \"#{name}\", columns: #{@columns}, primary: #{primary?}, unique: #{unique?}>"
|
71
|
-
end
|
72
|
-
|
73
|
-
def inspect
|
74
|
-
to_s
|
75
|
-
end
|
76
68
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
class Baza::Driver::Mysql::Table
|
1
|
+
class Baza::Driver::Mysql::Table < Baza::Table
|
2
2
|
attr_reader :list, :name
|
3
3
|
|
4
4
|
def initialize(args)
|
@@ -6,8 +6,8 @@ class Baza::Driver::Mysql::Table
|
|
6
6
|
@db = args[:db]
|
7
7
|
@data = args[:data]
|
8
8
|
@subtype = @db.opts[:subtype]
|
9
|
-
@list =
|
10
|
-
@indexes_list =
|
9
|
+
@list = Wref::Map.new
|
10
|
+
@indexes_list = Wref::Map.new
|
11
11
|
@name = @data[:Name].to_sym
|
12
12
|
@tables = args[:tables]
|
13
13
|
|
@@ -48,7 +48,7 @@ class Baza::Driver::Mysql::Table
|
|
48
48
|
def column(name)
|
49
49
|
name = name.to_sym
|
50
50
|
|
51
|
-
if col = @list.get
|
51
|
+
if col = @list.get(name)
|
52
52
|
return @list[name]
|
53
53
|
end
|
54
54
|
|
@@ -63,11 +63,11 @@ class Baza::Driver::Mysql::Table
|
|
63
63
|
@db.cols
|
64
64
|
ret = {}
|
65
65
|
sql = "SHOW FULL COLUMNS FROM `#{@db.esc_table(name)}`"
|
66
|
-
sql << " WHERE `Field` = '#{@db.esc(args[:name])}'" if args
|
66
|
+
sql << " WHERE `Field` = '#{@db.esc(args[:name])}'" if args && args.key?(:name)
|
67
67
|
|
68
68
|
@db.q(sql) do |d_cols|
|
69
69
|
column_name = d_cols[:Field].to_sym
|
70
|
-
obj = @list.get
|
70
|
+
obj = @list.get(name)
|
71
71
|
|
72
72
|
unless obj
|
73
73
|
obj = Baza::Driver::Mysql::Column.new(
|
@@ -92,7 +92,7 @@ class Baza::Driver::Mysql::Table
|
|
92
92
|
end
|
93
93
|
end
|
94
94
|
|
95
|
-
def indexes
|
95
|
+
def indexes(args = nil)
|
96
96
|
@db.indexes
|
97
97
|
ret = {}
|
98
98
|
|
@@ -101,7 +101,7 @@ class Baza::Driver::Mysql::Table
|
|
101
101
|
|
102
102
|
@db.q(sql) do |d_indexes|
|
103
103
|
next if d_indexes[:Key_name] == "PRIMARY"
|
104
|
-
obj = @indexes_list.get
|
104
|
+
obj = @indexes_list.get(d_indexes[:Key_name].to_s)
|
105
105
|
|
106
106
|
unless obj
|
107
107
|
obj = Baza::Driver::Mysql::Index.new(
|
@@ -130,7 +130,7 @@ class Baza::Driver::Mysql::Table
|
|
130
130
|
def index(name)
|
131
131
|
name = name.to_s
|
132
132
|
|
133
|
-
if index = @indexes_list.get
|
133
|
+
if index = @indexes_list.get(name)
|
134
134
|
return index
|
135
135
|
end
|
136
136
|
|
@@ -167,7 +167,7 @@ class Baza::Driver::Mysql::Table
|
|
167
167
|
sql = ""
|
168
168
|
end
|
169
169
|
|
170
|
-
if args[:create]
|
170
|
+
if args[:create] || !args.key?(:create)
|
171
171
|
sql << "CREATE"
|
172
172
|
end
|
173
173
|
|
@@ -217,7 +217,7 @@ class Baza::Driver::Mysql::Table
|
|
217
217
|
end
|
218
218
|
end
|
219
219
|
|
220
|
-
def rename
|
220
|
+
def rename(newname)
|
221
221
|
newname = newname.to_sym
|
222
222
|
oldname = name
|
223
223
|
|
@@ -10,7 +10,7 @@ class Baza::Driver::Mysql::Tables
|
|
10
10
|
@db = @args[:db]
|
11
11
|
@subtype = @db.opts[:subtype]
|
12
12
|
@list_mutex = Monitor.new
|
13
|
-
@list =
|
13
|
+
@list = Wref::Map.new
|
14
14
|
@list_should_be_reloaded = true
|
15
15
|
end
|
16
16
|
|
@@ -23,10 +23,8 @@ class Baza::Driver::Mysql::Tables
|
|
23
23
|
def [](table_name)
|
24
24
|
table_name = table_name.to_sym
|
25
25
|
|
26
|
-
|
27
|
-
return
|
28
|
-
rescue Wref::Recycled
|
29
|
-
#ignore.
|
26
|
+
if table = @list[table_name]
|
27
|
+
return table
|
30
28
|
end
|
31
29
|
|
32
30
|
tables = []
|
@@ -52,9 +50,9 @@ class Baza::Driver::Mysql::Tables
|
|
52
50
|
@db.q(sql) do |d_tables|
|
53
51
|
raise "No name was given from: #{d_tables}" unless d_tables.is_a?(Hash) && d_tables[:Name]
|
54
52
|
name = d_tables[:Name].to_sym
|
55
|
-
obj = @list.get
|
53
|
+
obj = @list.get(name)
|
56
54
|
|
57
|
-
|
55
|
+
unless obj
|
58
56
|
obj = Baza::Driver::Mysql::Table.new(
|
59
57
|
db: @db,
|
60
58
|
data: d_tables,
|
@@ -23,43 +23,52 @@ class Baza::Driver::Sqlite3
|
|
23
23
|
conn: args[:object]
|
24
24
|
}
|
25
25
|
}
|
26
|
+
elsif args[:object].class.name == "Java::OrgSqlite::SQLiteConnection"
|
27
|
+
return {
|
28
|
+
type: :success,
|
29
|
+
args: {
|
30
|
+
type: :sqlite3,
|
31
|
+
conn: args[:object]
|
32
|
+
}
|
33
|
+
}
|
26
34
|
end
|
27
35
|
|
28
36
|
return nil
|
29
37
|
end
|
30
38
|
|
31
39
|
#Constructor. This should not be called manually.
|
32
|
-
def initialize(
|
40
|
+
def initialize(baza_db)
|
33
41
|
@sep_table = "`"
|
34
42
|
@sep_col = "`"
|
35
43
|
@sep_val = "'"
|
36
44
|
|
37
|
-
@
|
38
|
-
@path = @
|
39
|
-
@
|
45
|
+
@baza = baza_db
|
46
|
+
@path = @baza.opts[:path] if @baza.opts[:path]
|
47
|
+
@baza.opts[:subtype] ||= :java if RUBY_ENGINE == "jruby"
|
48
|
+
@subtype = @baza.opts[:subtype]
|
40
49
|
|
41
|
-
@
|
42
|
-
|
43
|
-
|
44
|
-
@conn = @baza_db.opts[:conn]
|
50
|
+
if @baza.opts[:conn]
|
51
|
+
@conn = @baza.opts[:conn]
|
52
|
+
@stat = @conn.createStatement if @subtype == :java
|
45
53
|
else
|
46
|
-
raise "No path was given."
|
54
|
+
raise "No path was given." unless @path
|
47
55
|
|
48
|
-
if @
|
49
|
-
if @
|
50
|
-
require @
|
56
|
+
if @subtype == :java
|
57
|
+
if @baza.opts[:sqlite_driver]
|
58
|
+
require @baza.opts[:sqlite_driver]
|
51
59
|
else
|
52
|
-
require "
|
60
|
+
require "jdbc/sqlite3"
|
61
|
+
::Jdbc::SQLite3.load_driver
|
53
62
|
end
|
54
63
|
|
55
64
|
require "java"
|
56
65
|
import "org.sqlite.JDBC"
|
57
|
-
@conn = java.sql.DriverManager::getConnection("jdbc:sqlite:#{@
|
66
|
+
@conn = java.sql.DriverManager::getConnection("jdbc:sqlite:#{@baza.opts[:path]}")
|
58
67
|
@stat = @conn.createStatement
|
59
|
-
elsif @
|
60
|
-
@conn = SQLite3::Database.new(@path, @path)
|
68
|
+
elsif @subtype == :rhodes
|
69
|
+
@conn = ::SQLite3::Database.new(@path, @path)
|
61
70
|
else
|
62
|
-
@conn = SQLite3::Database.open(@path)
|
71
|
+
@conn = ::SQLite3::Database.open(@path)
|
63
72
|
@conn.results_as_hash = true
|
64
73
|
@conn.type_translation = false
|
65
74
|
end
|
@@ -68,9 +77,9 @@ class Baza::Driver::Sqlite3
|
|
68
77
|
|
69
78
|
#Executes a query against the driver.
|
70
79
|
def query(string)
|
71
|
-
if @
|
80
|
+
if @subtype == :rhodes
|
72
81
|
return Baza::Driver::Sqlite3::Result.new(self, @conn.execute(string, string))
|
73
|
-
elsif @
|
82
|
+
elsif @subtype == :java
|
74
83
|
begin
|
75
84
|
return Baza::Driver::Sqlite3::ResultJava.new(self, @stat.executeQuery(string))
|
76
85
|
rescue java.sql.SQLException => e
|
@@ -118,17 +127,28 @@ class Baza::Driver::Sqlite3
|
|
118
127
|
|
119
128
|
#Starts a transaction, yields the database and commits.
|
120
129
|
def transaction
|
121
|
-
@
|
122
|
-
|
130
|
+
if @subtype == :java
|
131
|
+
query("BEGIN TRANSACTION")
|
132
|
+
|
133
|
+
begin
|
134
|
+
yield(@baza)
|
135
|
+
query("COMMIT")
|
136
|
+
rescue => e
|
137
|
+
query("ROLLBACK")
|
138
|
+
end
|
139
|
+
else
|
140
|
+
@conn.transaction do
|
141
|
+
yield(@baza)
|
142
|
+
end
|
123
143
|
end
|
124
144
|
end
|
125
145
|
|
126
146
|
def insert_multi(tablename, arr_hashes, args = nil)
|
127
147
|
sql = [] if args && args[:return_sql]
|
128
148
|
|
129
|
-
@
|
149
|
+
@baza.transaction do
|
130
150
|
arr_hashes.each do |hash|
|
131
|
-
res = @
|
151
|
+
res = @baza.insert(tablename, hash, args)
|
132
152
|
sql << res if args && args[:return_sql]
|
133
153
|
end
|
134
154
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
class Baza::Driver::Sqlite3::Index
|
1
|
+
class Baza::Driver::Sqlite3::Index < Baza::Index
|
2
2
|
attr_reader :args, :columns
|
3
3
|
|
4
4
|
def initialize(args)
|
@@ -23,7 +23,7 @@ class Baza::Driver::Sqlite3::Index
|
|
23
23
|
@db.query("DROP INDEX `#{name}`")
|
24
24
|
end
|
25
25
|
|
26
|
-
def rename
|
26
|
+
def rename(newname)
|
27
27
|
newname = newname.to_sym
|
28
28
|
|
29
29
|
create_args = data
|
@@ -37,6 +37,7 @@ class Baza::Driver::Sqlite3::Index
|
|
37
37
|
def data
|
38
38
|
return {
|
39
39
|
name: name,
|
40
|
+
unique: unique?,
|
40
41
|
columns: @columns
|
41
42
|
}
|
42
43
|
end
|
@@ -45,11 +46,7 @@ class Baza::Driver::Sqlite3::Index
|
|
45
46
|
@columns
|
46
47
|
end
|
47
48
|
|
48
|
-
def
|
49
|
-
|
50
|
-
end
|
51
|
-
|
52
|
-
def inspect
|
53
|
-
to_s
|
49
|
+
def unique?
|
50
|
+
@args[:data][:unique].to_i == 1
|
54
51
|
end
|
55
52
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
class Baza::Driver::Sqlite3::Table
|
1
|
+
class Baza::Driver::Sqlite3::Table < Baza::Table
|
2
2
|
attr_reader :name, :type
|
3
3
|
|
4
4
|
def initialize(args)
|
@@ -8,8 +8,8 @@ class Baza::Driver::Sqlite3::Table
|
|
8
8
|
@type = @data[:type].to_sym
|
9
9
|
@tables = args[:tables]
|
10
10
|
|
11
|
-
@list =
|
12
|
-
@indexes_list =
|
11
|
+
@list = Wref::Map.new
|
12
|
+
@indexes_list = Wref::Map.new
|
13
13
|
end
|
14
14
|
|
15
15
|
def maxlength
|
@@ -84,7 +84,7 @@ class Baza::Driver::Sqlite3::Table
|
|
84
84
|
|
85
85
|
@db.q("PRAGMA table_info(`#{@db.esc_table(name)}`)") do |d_cols|
|
86
86
|
column_name = d_cols[:name].to_sym
|
87
|
-
obj = @list.get
|
87
|
+
obj = @list.get(column_name)
|
88
88
|
|
89
89
|
unless obj
|
90
90
|
obj = Baza::Driver::Sqlite3::Column.new(
|
@@ -223,6 +223,7 @@ class Baza::Driver::Sqlite3::Table
|
|
223
223
|
end
|
224
224
|
end
|
225
225
|
end
|
226
|
+
|
226
227
|
sql << ");"
|
227
228
|
@db.query(sql)
|
228
229
|
|
@@ -250,20 +251,18 @@ class Baza::Driver::Sqlite3::Table
|
|
250
251
|
@db.query("DROP TABLE `#{temp_name}`")
|
251
252
|
end
|
252
253
|
|
253
|
-
def index
|
254
|
+
def index(index_name)
|
254
255
|
index_name = index_name.to_sym
|
255
256
|
|
256
|
-
|
257
|
-
return
|
258
|
-
|
259
|
-
if @db.opts[:index_append_table_name]
|
260
|
-
tryname = "#{name}__#{index_name}"
|
257
|
+
if index = @indexes_list[index_name]
|
258
|
+
return index
|
259
|
+
end
|
261
260
|
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
261
|
+
if @db.opts[:index_append_table_name]
|
262
|
+
tryname = "#{name}__#{index_name}"
|
263
|
+
|
264
|
+
if index = @indexes_list[tryname]
|
265
|
+
return index
|
267
266
|
end
|
268
267
|
end
|
269
268
|
|
@@ -284,7 +283,7 @@ class Baza::Driver::Sqlite3::Table
|
|
284
283
|
|
285
284
|
@db.q("PRAGMA index_list(`#{@db.esc_table(name)}`)") do |d_indexes|
|
286
285
|
next if d_indexes[:Key_name] == "PRIMARY"
|
287
|
-
obj = @indexes_list.get
|
286
|
+
obj = @indexes_list.get(d_indexes[:name])
|
288
287
|
|
289
288
|
unless obj
|
290
289
|
obj = Baza::Driver::Sqlite3::Index.new(
|
@@ -326,8 +325,8 @@ class Baza::Driver::Sqlite3::Table
|
|
326
325
|
index_data = {name: index_data, columns: [index_data]}
|
327
326
|
end
|
328
327
|
|
329
|
-
raise "No name was given in data: '#{index_data}'." if !index_data.key?(:name)
|
330
|
-
raise "No columns was given on index #{index_data[:name]}." if index_data[:columns].empty?
|
328
|
+
raise "No name was given in data: '#{index_data}'." if !index_data.key?(:name) || index_data[:name].to_s.strip.empty?
|
329
|
+
raise "No columns was given on index #{index_data[:name]}." if !index_data[:columns] || index_data[:columns].empty?
|
331
330
|
|
332
331
|
name = index_data[:name]
|
333
332
|
name = "#{self.name}__#{name}" if @db.opts[:index_append_table_name]
|
@@ -6,17 +6,14 @@ class Baza::Driver::Sqlite3::Tables
|
|
6
6
|
@db = @args[:db]
|
7
7
|
|
8
8
|
@list_mutex = Mutex.new
|
9
|
-
@list =
|
9
|
+
@list = Wref::Map.new
|
10
10
|
end
|
11
11
|
|
12
12
|
def [](table_name)
|
13
13
|
table_name = table_name.to_sym
|
14
14
|
|
15
|
-
|
16
|
-
ret = @list[table_name]
|
15
|
+
if ret = @list.get(table_name)
|
17
16
|
return ret
|
18
|
-
rescue Wref::Recycled
|
19
|
-
#ignore.
|
20
17
|
end
|
21
18
|
|
22
19
|
self.list do |table_obj|
|
@@ -34,9 +31,9 @@ class Baza::Driver::Sqlite3::Tables
|
|
34
31
|
next if d_tables[:name] == "sqlite_sequence"
|
35
32
|
|
36
33
|
tname = d_tables[:name].to_sym
|
37
|
-
obj = @list.get
|
34
|
+
obj = @list.get(tname)
|
38
35
|
|
39
|
-
|
36
|
+
unless obj
|
40
37
|
obj = Baza::Driver::Sqlite3::Table.new(
|
41
38
|
db: @db,
|
42
39
|
data: d_tables,
|
data/include/index.rb
ADDED