baza 0.0.17 → 0.0.18
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +9 -11
- data/Gemfile.lock +76 -50
- data/README.md +3 -2
- data/VERSION +1 -1
- data/baza.gemspec +107 -54
- data/lib/baza.rb +1 -1
- data/lib/baza/base_sql_driver.rb +64 -0
- data/lib/baza/cloner.rb +31 -0
- data/{include → lib/baza}/column.rb +0 -0
- data/{include → lib/baza}/db.rb +61 -49
- data/{include → lib/baza}/dbtime.rb +0 -0
- data/{include → lib/baza}/driver.rb +1 -1
- data/lib/baza/drivers/active_record.rb +107 -0
- data/lib/baza/drivers/active_record/columns.rb +10 -0
- data/lib/baza/drivers/active_record/indexes.rb +10 -0
- data/lib/baza/drivers/active_record/result.rb +23 -0
- data/lib/baza/drivers/active_record/tables.rb +12 -0
- data/lib/baza/drivers/mysql.rb +220 -0
- data/{include/drivers/mysql/mysql_column.rb → lib/baza/drivers/mysql/column.rb} +0 -0
- data/{include/drivers/mysql/mysql_columns.rb → lib/baza/drivers/mysql/columns.rb} +0 -0
- data/{include/drivers/mysql/mysql_index.rb → lib/baza/drivers/mysql/index.rb} +0 -0
- data/{include/drivers/mysql/mysql_indexes.rb → lib/baza/drivers/mysql/indexes.rb} +0 -0
- data/lib/baza/drivers/mysql/result.rb +81 -0
- data/{include/drivers/mysql/mysql_sqlspecs.rb → lib/baza/drivers/mysql/sqlspecs.rb} +0 -0
- data/{include/drivers/mysql/mysql_table.rb → lib/baza/drivers/mysql/table.rb} +2 -2
- data/{include/drivers/mysql/mysql_tables.rb → lib/baza/drivers/mysql/tables.rb} +0 -1
- data/{include/drivers/mysql/mysql_result_unbuffered.rb → lib/baza/drivers/mysql/unbuffered_result.rb} +19 -23
- data/lib/baza/drivers/mysql2.rb +259 -0
- data/lib/baza/drivers/mysql2/column.rb +2 -0
- data/lib/baza/drivers/mysql2/columns.rb +2 -0
- data/lib/baza/drivers/mysql2/index.rb +2 -0
- data/lib/baza/drivers/mysql2/indexes.rb +2 -0
- data/lib/baza/drivers/mysql2/result.rb +26 -0
- data/lib/baza/drivers/mysql2/table.rb +2 -0
- data/lib/baza/drivers/mysql2/tables.rb +2 -0
- data/lib/baza/drivers/mysql_java.rb +178 -0
- data/lib/baza/drivers/mysql_java/column.rb +2 -0
- data/lib/baza/drivers/mysql_java/columns.rb +2 -0
- data/lib/baza/drivers/mysql_java/index.rb +2 -0
- data/lib/baza/drivers/mysql_java/indexes.rb +2 -0
- data/lib/baza/drivers/mysql_java/table.rb +2 -0
- data/lib/baza/drivers/mysql_java/tables.rb +2 -0
- data/lib/baza/drivers/sqlite3.rb +81 -0
- data/{include/drivers/sqlite3/sqlite3_column.rb → lib/baza/drivers/sqlite3/column.rb} +0 -0
- data/{include/drivers/sqlite3/sqlite3_columns.rb → lib/baza/drivers/sqlite3/columns.rb} +0 -0
- data/{include/drivers/sqlite3/sqlite3_index.rb → lib/baza/drivers/sqlite3/index.rb} +0 -0
- data/{include/drivers/sqlite3/sqlite3_indexes.rb → lib/baza/drivers/sqlite3/indexes.rb} +0 -0
- data/lib/baza/drivers/sqlite3/result.rb +64 -0
- data/{include/drivers/sqlite3/sqlite3_sqlspecs.rb → lib/baza/drivers/sqlite3/sqlspecs.rb} +1 -1
- data/{include/drivers/sqlite3/sqlite3_table.rb → lib/baza/drivers/sqlite3/table.rb} +0 -0
- data/{include/drivers/sqlite3/sqlite3_tables.rb → lib/baza/drivers/sqlite3/tables.rb} +0 -0
- data/lib/baza/drivers/sqlite3/unbuffered_result.rb +33 -0
- data/lib/baza/drivers/sqlite3_java.rb +75 -0
- data/lib/baza/drivers/sqlite3_java/column.rb +2 -0
- data/lib/baza/drivers/sqlite3_java/columns.rb +2 -0
- data/lib/baza/drivers/sqlite3_java/index.rb +2 -0
- data/lib/baza/drivers/sqlite3_java/indexes.rb +2 -0
- data/lib/baza/drivers/sqlite3_java/table.rb +2 -0
- data/lib/baza/drivers/sqlite3_java/tables.rb +2 -0
- data/lib/baza/drivers/sqlite3_java/unbuffered_result.rb +33 -0
- data/lib/baza/drivers/sqlite3_rhodes.rb +78 -0
- data/{include → lib/baza}/dump.rb +0 -0
- data/{include → lib/baza}/errors.rb +0 -0
- data/{include → lib/baza}/idquery.rb +0 -0
- data/{include → lib/baza}/index.rb +0 -0
- data/lib/baza/jdbc_driver.rb +102 -0
- data/lib/baza/jdbc_result.rb +125 -0
- data/{include → lib/baza}/model.rb +2 -2
- data/{include → lib/baza}/model_custom.rb +0 -0
- data/{include → lib/baza}/model_handler.rb +1 -1
- data/{include → lib/baza}/model_handler_sqlhelper.rb +0 -0
- data/{include → lib/baza}/query_buffer.rb +2 -2
- data/lib/baza/result_base.rb +25 -0
- data/{include → lib/baza}/revision.rb +1 -1
- data/{include → lib/baza}/row.rb +0 -0
- data/{include → lib/baza}/sqlspecs.rb +0 -0
- data/{include → lib/baza}/table.rb +0 -0
- data/shippable.yml +3 -1
- data/spec/cloner_spec.rb +10 -0
- data/spec/drivers/active_record_mysql2_spec.rb +18 -0
- data/spec/drivers/active_record_mysql_spec.rb +16 -0
- data/spec/drivers/active_record_sqlite3_spec.rb +16 -0
- data/spec/drivers/mysql2_spec.rb +16 -0
- data/spec/{include/drivers → drivers}/mysql_spec.rb +9 -1
- data/spec/{include/drivers → drivers}/sqlite3_spec.rb +8 -0
- data/spec/info_active_record_mysql.rb +37 -0
- data/spec/info_active_record_mysql2.rb +37 -0
- data/spec/info_active_record_mysql2_shippable.rb +36 -0
- data/spec/info_active_record_mysql_shippable.rb +36 -0
- data/spec/info_active_record_sqlite3.rb +37 -0
- data/spec/info_mysql2_example.rb +23 -0
- data/spec/info_mysql2_shippable.rb +22 -0
- data/spec/info_mysql_example.rb +3 -4
- data/spec/info_mysql_shippable.rb +7 -17
- data/spec/info_sqlite3.rb +5 -4
- data/spec/model_handler_spec.rb +137 -105
- data/spec/spec_helper.rb +8 -0
- data/spec/support/driver_collection.rb +124 -34
- data/spec/support/driver_columns_collection.rb +0 -7
- data/spec/support/driver_indexes_collection.rb +4 -11
- data/spec/support/driver_tables_collection.rb +0 -7
- metadata +160 -52
- data/include/cloner.rb +0 -18
- data/include/drivers/active_record/active_record.rb +0 -159
- data/include/drivers/mysql/mysql.rb +0 -443
- data/include/drivers/mysql/mysql_result.rb +0 -42
- data/include/drivers/mysql/mysql_result_java.rb +0 -61
- data/include/drivers/mysql/mysql_result_mysql2.rb +0 -26
- data/include/drivers/sqlite3/sqlite3.rb +0 -159
- data/include/drivers/sqlite3/sqlite3_result.rb +0 -35
- data/include/drivers/sqlite3/sqlite3_result_java.rb +0 -39
- data/spec/include/cloner_spec.rb +0 -10
- data/spec/include/drivers/active_record_spec.rb +0 -8
- data/spec/info_active_record.rb +0 -49
- data/spec/info_active_record_shippable.rb +0 -47
@@ -0,0 +1,81 @@
|
|
1
|
+
#This class handels SQLite3-specific behaviour.
|
2
|
+
class Baza::Driver::Sqlite3 < Baza::BaseSqlDriver
|
3
|
+
path = "#{File.dirname(__FILE__)}/sqlite3"
|
4
|
+
|
5
|
+
autoload :Table, "#{path}/table"
|
6
|
+
autoload :Tables, "#{path}/tables"
|
7
|
+
autoload :Column, "#{path}/column"
|
8
|
+
autoload :Columns, "#{path}/columns"
|
9
|
+
autoload :Index, "#{path}/index"
|
10
|
+
autoload :Indexes, "#{path}/indexes"
|
11
|
+
autoload :Result, "#{path}/result"
|
12
|
+
autoload :Sqlspecs, "#{path}/sqlspecs"
|
13
|
+
autoload :UnbufferedResult, "#{path}/unbuffered_result"
|
14
|
+
|
15
|
+
attr_reader :mutex_statement_reader
|
16
|
+
|
17
|
+
#Helper to enable automatic registering of database using Baza::Db.from_object
|
18
|
+
def self.from_object(args)
|
19
|
+
if args[:object].class.name == "SQLite3::Database"
|
20
|
+
return {
|
21
|
+
type: :success,
|
22
|
+
args: {
|
23
|
+
type: :sqlite3,
|
24
|
+
conn: args[:object]
|
25
|
+
}
|
26
|
+
}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
#Constructor. This should not be called manually.
|
31
|
+
def initialize(baza_db)
|
32
|
+
super
|
33
|
+
|
34
|
+
@path = @baza.opts[:path] if @baza.opts[:path]
|
35
|
+
@mutex_statement_reader = Mutex.new
|
36
|
+
|
37
|
+
if @baza.opts[:conn]
|
38
|
+
@conn = @baza.opts[:conn]
|
39
|
+
else
|
40
|
+
raise "No path was given." unless @path
|
41
|
+
require 'sqlite3' unless ::Object.const_defined?(:SQLite3)
|
42
|
+
|
43
|
+
@conn = ::SQLite3::Database.open(@path)
|
44
|
+
@conn.type_translation = false # Type translation is always done in the C ext for SQLite3
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
#Executes a query against the driver.
|
49
|
+
def query(sql)
|
50
|
+
@mutex_statement_reader.synchronize do
|
51
|
+
return Baza::Driver::Sqlite3::Result.new(self, @conn.prepare(sql))
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def query_ubuf(sql)
|
56
|
+
return Baza::Driver::Sqlite3::UnbufferedResult.new(self, @conn.prepare(sql))
|
57
|
+
end
|
58
|
+
|
59
|
+
#Escapes a string to be safe to used in a query.
|
60
|
+
def escape(string)
|
61
|
+
#This code is taken directly from the documentation so we dont have to rely on the SQLite3::Database class. This way it can also be used with JRuby and IronRuby...
|
62
|
+
#http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html
|
63
|
+
return string.to_s.gsub(/'/, "''")
|
64
|
+
end
|
65
|
+
|
66
|
+
#Returns the last inserted ID.
|
67
|
+
def last_id
|
68
|
+
return @conn.last_insert_row_id if @conn.respond_to?(:last_insert_row_id)
|
69
|
+
return query("SELECT last_insert_rowid() AS id").fetch[:id].to_i
|
70
|
+
end
|
71
|
+
|
72
|
+
#Closes the connection to the database.
|
73
|
+
def close
|
74
|
+
@mutex_statement_reader.synchronize { @conn.close }
|
75
|
+
end
|
76
|
+
|
77
|
+
#Starts a transaction, yields the database and commits.
|
78
|
+
def transaction
|
79
|
+
@conn.transaction { yield @baza }
|
80
|
+
end
|
81
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# This class handels the result when running MRI (or others).
|
2
|
+
class Baza::Driver::Sqlite3::Result < Baza::ResultBase
|
3
|
+
# Constructor. This should not be called manually.
|
4
|
+
def initialize(driver, statement)
|
5
|
+
@statement = statement
|
6
|
+
|
7
|
+
begin
|
8
|
+
@statement.execute
|
9
|
+
@type_translation = driver.baza.opts[:type_translation]
|
10
|
+
@types = statement.types if @type_translation == true
|
11
|
+
@columns = statement.columns.map { |column| column.to_sym }
|
12
|
+
read_results
|
13
|
+
@index = -1
|
14
|
+
ensure
|
15
|
+
@statement.try(:close)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Returns a single result.
|
20
|
+
def fetch
|
21
|
+
row = @results[@index += 1]
|
22
|
+
|
23
|
+
if row
|
24
|
+
if @types
|
25
|
+
row.map!.with_index { |value, index| translate_type(value, @types[index]) } if @types
|
26
|
+
elsif @type_translation == :string
|
27
|
+
row.map! { |value| value.to_s }
|
28
|
+
end
|
29
|
+
|
30
|
+
return Hash[*@columns.zip(row).flatten]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Loops over every result yielding them.
|
35
|
+
def each
|
36
|
+
while data = fetch
|
37
|
+
yield data
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def read_results
|
44
|
+
@results = []
|
45
|
+
|
46
|
+
loop do
|
47
|
+
row = @statement.step
|
48
|
+
break if @statement.done?
|
49
|
+
@results << row
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def translate_type(value, type)
|
54
|
+
if value
|
55
|
+
if type == 'datetime'
|
56
|
+
return Time.parse(value)
|
57
|
+
elsif type == 'date'
|
58
|
+
return Date.parse(value)
|
59
|
+
else
|
60
|
+
return value
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
File without changes
|
File without changes
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class Baza::Driver::Sqlite3::UnbufferedResult
|
2
|
+
def initialize(driver, statement)
|
3
|
+
@statement = statement
|
4
|
+
@statement.execute
|
5
|
+
@columns = statement.columns.map { |column| column.to_sym }
|
6
|
+
end
|
7
|
+
|
8
|
+
def fetch
|
9
|
+
return nil if @closed
|
10
|
+
|
11
|
+
array = @statement.step
|
12
|
+
|
13
|
+
if @statement.done?
|
14
|
+
close
|
15
|
+
return nil
|
16
|
+
end
|
17
|
+
|
18
|
+
return Hash[*@columns.zip(array).flatten] if array
|
19
|
+
end
|
20
|
+
|
21
|
+
def each
|
22
|
+
while data = fetch
|
23
|
+
yield data
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def close
|
30
|
+
@statement.close
|
31
|
+
@closed = true
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
#This class handels SQLite3-specific behaviour.
|
2
|
+
class Baza::Driver::Sqlite3Java < Baza::JdbcDriver
|
3
|
+
path = "#{File.dirname(__FILE__)}/sqlite3_java"
|
4
|
+
|
5
|
+
autoload :Table, "#{path}/table"
|
6
|
+
autoload :Tables, "#{path}/tables"
|
7
|
+
autoload :Column, "#{path}/column"
|
8
|
+
autoload :Columns, "#{path}/columns"
|
9
|
+
autoload :Index, "#{path}/index"
|
10
|
+
autoload :Indexes, "#{path}/indexes"
|
11
|
+
autoload :Result, "#{path}/result"
|
12
|
+
autoload :Sqlspecs, "#{path}/sqlspecs"
|
13
|
+
autoload :UnbufferedResult, "#{path}/unbuffered_result"
|
14
|
+
|
15
|
+
attr_reader :mutex_statement_reader
|
16
|
+
|
17
|
+
#Helper to enable automatic registering of database using Baza::Db.from_object
|
18
|
+
def self.from_object(args)
|
19
|
+
if args[:object].class.name == "Java::OrgSqlite::SQLiteConnection"
|
20
|
+
return {
|
21
|
+
type: :success,
|
22
|
+
args: {
|
23
|
+
type: :sqlite3_java,
|
24
|
+
conn: args[:object]
|
25
|
+
}
|
26
|
+
}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
#Constructor. This should not be called manually.
|
31
|
+
def initialize(baza_db)
|
32
|
+
super
|
33
|
+
|
34
|
+
@path = @baza.opts[:path] if @baza.opts[:path]
|
35
|
+
@preload_results = true
|
36
|
+
|
37
|
+
if @baza.opts[:conn]
|
38
|
+
@conn = @baza.opts[:conn]
|
39
|
+
else
|
40
|
+
org.sqlite.JDBC
|
41
|
+
reconnect
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def reconnect
|
46
|
+
raise "No path was given." unless @path
|
47
|
+
|
48
|
+
@stmt = nil
|
49
|
+
@conn = java.sql.DriverManager::getConnection("jdbc:sqlite:#{@path}")
|
50
|
+
end
|
51
|
+
|
52
|
+
#Escapes a string to be safe to used in a query.
|
53
|
+
def escape(string)
|
54
|
+
#This code is taken directly from the documentation so we dont have to rely on the SQLite3::Database class. This way it can also be used with JRuby and IronRuby...
|
55
|
+
#http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html
|
56
|
+
return string.to_s.gsub(/'/, "''")
|
57
|
+
end
|
58
|
+
|
59
|
+
#Returns the last inserted ID.
|
60
|
+
def last_id
|
61
|
+
return query("SELECT LAST_INSERT_ROWID() AS id").fetch[:id].to_i
|
62
|
+
end
|
63
|
+
|
64
|
+
def transaction
|
65
|
+
query_no_result_set("BEGIN TRANSACTION")
|
66
|
+
|
67
|
+
begin
|
68
|
+
yield @baza
|
69
|
+
query_no_result_set("COMMIT")
|
70
|
+
rescue
|
71
|
+
query_no_result_set("ROLLBACK")
|
72
|
+
raise
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class Baza::Driver::Sqlite3Java::UnbufferedResult < Baza::ResultBase
|
2
|
+
def initialize(driver, result_set)
|
3
|
+
@result_set = result_set
|
4
|
+
|
5
|
+
if @result_set
|
6
|
+
metadata = @result_set.meta_data
|
7
|
+
@columns_count = metadata.column_count
|
8
|
+
|
9
|
+
@columns = []
|
10
|
+
1.upto(@columns_count) do |count|
|
11
|
+
@columns << metadata.column_name(count).to_sym
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def fetch
|
17
|
+
result = @result_set.next if @result_set
|
18
|
+
return nil unless result
|
19
|
+
|
20
|
+
hash = {}
|
21
|
+
@columns_count.times do |count|
|
22
|
+
hash[@columns[count]] = @result_set.string(count + 1)
|
23
|
+
end
|
24
|
+
|
25
|
+
return hash
|
26
|
+
end
|
27
|
+
|
28
|
+
def each
|
29
|
+
while data = fetch
|
30
|
+
yield data
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
#This class handels SQLite3-specific behaviour.
|
2
|
+
class Baza::Driver::Sqlite3Rhodes < Baza::BaseSqlDriver
|
3
|
+
path = "#{File.dirname(__FILE__)}/sqlite3_rhodes"
|
4
|
+
|
5
|
+
autoload :Table, "#{path}/table"
|
6
|
+
autoload :Tables, "#{path}/tables"
|
7
|
+
autoload :Column, "#{path}/column"
|
8
|
+
autoload :Columns, "#{path}/columns"
|
9
|
+
autoload :Index, "#{path}/index"
|
10
|
+
autoload :Indexes, "#{path}/indexes"
|
11
|
+
autoload :Result, "#{path}/result"
|
12
|
+
autoload :Sqlspecs, "#{path}/sqlspecs"
|
13
|
+
autoload :UnbufferedResult, "#{path}/unbuffered_result"
|
14
|
+
|
15
|
+
attr_reader :mutex_statement_reader
|
16
|
+
|
17
|
+
# Helper to enable automatic registering of database using Baza::Db.from_object
|
18
|
+
def self.from_object(args)
|
19
|
+
if args[:object].class.name == "SQLite3::Database"
|
20
|
+
return {
|
21
|
+
type: :success,
|
22
|
+
args: {
|
23
|
+
type: :sqlite3,
|
24
|
+
conn: args[:object]
|
25
|
+
}
|
26
|
+
}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Constructor. This should not be called manually.
|
31
|
+
def initialize(baza_db)
|
32
|
+
super
|
33
|
+
|
34
|
+
@path = @baza.opts[:path] if @baza.opts[:path]
|
35
|
+
@mutex_statement_reader = Mutex.new
|
36
|
+
|
37
|
+
if @baza.opts[:conn]
|
38
|
+
@conn = @baza.opts[:conn]
|
39
|
+
else
|
40
|
+
raise "No path was given." unless @path
|
41
|
+
@conn = ::SQLite3::Database.new(@path, @path)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Executes a query against the driver.
|
46
|
+
def query(sql)
|
47
|
+
return Baza::Driver::Sqlite3::Result.new(self, @conn.execute(sql, sql))
|
48
|
+
end
|
49
|
+
|
50
|
+
def query_ubuf(sql)
|
51
|
+
return Baza::Driver::Sqlite3::UnbufferedResult.new(self, @conn.prepare(sql))
|
52
|
+
end
|
53
|
+
|
54
|
+
# Escapes a string to be safe to used in a query.
|
55
|
+
def escape(string)
|
56
|
+
#This code is taken directly from the documentation so we dont have to rely on the SQLite3::Database class. This way it can also be used with JRuby and IronRuby...
|
57
|
+
#http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html
|
58
|
+
return string.to_s.gsub(/'/, "''")
|
59
|
+
end
|
60
|
+
|
61
|
+
# Returns the last inserted ID.
|
62
|
+
def last_id
|
63
|
+
return @conn.last_insert_row_id if @conn.respond_to?(:last_insert_row_id)
|
64
|
+
return query("SELECT last_insert_rowid() AS id").fetch[:id].to_i
|
65
|
+
end
|
66
|
+
|
67
|
+
# Closes the connection to the database.
|
68
|
+
def close
|
69
|
+
@mutex_statement_reader.synchronize do
|
70
|
+
@conn.close
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# Starts a transaction, yields the database and commits.
|
75
|
+
def transaction
|
76
|
+
@conn.transaction { yield @baza }
|
77
|
+
end
|
78
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|