baza 0.0.17 → 0.0.18

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +9 -11
  3. data/Gemfile.lock +76 -50
  4. data/README.md +3 -2
  5. data/VERSION +1 -1
  6. data/baza.gemspec +107 -54
  7. data/lib/baza.rb +1 -1
  8. data/lib/baza/base_sql_driver.rb +64 -0
  9. data/lib/baza/cloner.rb +31 -0
  10. data/{include → lib/baza}/column.rb +0 -0
  11. data/{include → lib/baza}/db.rb +61 -49
  12. data/{include → lib/baza}/dbtime.rb +0 -0
  13. data/{include → lib/baza}/driver.rb +1 -1
  14. data/lib/baza/drivers/active_record.rb +107 -0
  15. data/lib/baza/drivers/active_record/columns.rb +10 -0
  16. data/lib/baza/drivers/active_record/indexes.rb +10 -0
  17. data/lib/baza/drivers/active_record/result.rb +23 -0
  18. data/lib/baza/drivers/active_record/tables.rb +12 -0
  19. data/lib/baza/drivers/mysql.rb +220 -0
  20. data/{include/drivers/mysql/mysql_column.rb → lib/baza/drivers/mysql/column.rb} +0 -0
  21. data/{include/drivers/mysql/mysql_columns.rb → lib/baza/drivers/mysql/columns.rb} +0 -0
  22. data/{include/drivers/mysql/mysql_index.rb → lib/baza/drivers/mysql/index.rb} +0 -0
  23. data/{include/drivers/mysql/mysql_indexes.rb → lib/baza/drivers/mysql/indexes.rb} +0 -0
  24. data/lib/baza/drivers/mysql/result.rb +81 -0
  25. data/{include/drivers/mysql/mysql_sqlspecs.rb → lib/baza/drivers/mysql/sqlspecs.rb} +0 -0
  26. data/{include/drivers/mysql/mysql_table.rb → lib/baza/drivers/mysql/table.rb} +2 -2
  27. data/{include/drivers/mysql/mysql_tables.rb → lib/baza/drivers/mysql/tables.rb} +0 -1
  28. data/{include/drivers/mysql/mysql_result_unbuffered.rb → lib/baza/drivers/mysql/unbuffered_result.rb} +19 -23
  29. data/lib/baza/drivers/mysql2.rb +259 -0
  30. data/lib/baza/drivers/mysql2/column.rb +2 -0
  31. data/lib/baza/drivers/mysql2/columns.rb +2 -0
  32. data/lib/baza/drivers/mysql2/index.rb +2 -0
  33. data/lib/baza/drivers/mysql2/indexes.rb +2 -0
  34. data/lib/baza/drivers/mysql2/result.rb +26 -0
  35. data/lib/baza/drivers/mysql2/table.rb +2 -0
  36. data/lib/baza/drivers/mysql2/tables.rb +2 -0
  37. data/lib/baza/drivers/mysql_java.rb +178 -0
  38. data/lib/baza/drivers/mysql_java/column.rb +2 -0
  39. data/lib/baza/drivers/mysql_java/columns.rb +2 -0
  40. data/lib/baza/drivers/mysql_java/index.rb +2 -0
  41. data/lib/baza/drivers/mysql_java/indexes.rb +2 -0
  42. data/lib/baza/drivers/mysql_java/table.rb +2 -0
  43. data/lib/baza/drivers/mysql_java/tables.rb +2 -0
  44. data/lib/baza/drivers/sqlite3.rb +81 -0
  45. data/{include/drivers/sqlite3/sqlite3_column.rb → lib/baza/drivers/sqlite3/column.rb} +0 -0
  46. data/{include/drivers/sqlite3/sqlite3_columns.rb → lib/baza/drivers/sqlite3/columns.rb} +0 -0
  47. data/{include/drivers/sqlite3/sqlite3_index.rb → lib/baza/drivers/sqlite3/index.rb} +0 -0
  48. data/{include/drivers/sqlite3/sqlite3_indexes.rb → lib/baza/drivers/sqlite3/indexes.rb} +0 -0
  49. data/lib/baza/drivers/sqlite3/result.rb +64 -0
  50. data/{include/drivers/sqlite3/sqlite3_sqlspecs.rb → lib/baza/drivers/sqlite3/sqlspecs.rb} +1 -1
  51. data/{include/drivers/sqlite3/sqlite3_table.rb → lib/baza/drivers/sqlite3/table.rb} +0 -0
  52. data/{include/drivers/sqlite3/sqlite3_tables.rb → lib/baza/drivers/sqlite3/tables.rb} +0 -0
  53. data/lib/baza/drivers/sqlite3/unbuffered_result.rb +33 -0
  54. data/lib/baza/drivers/sqlite3_java.rb +75 -0
  55. data/lib/baza/drivers/sqlite3_java/column.rb +2 -0
  56. data/lib/baza/drivers/sqlite3_java/columns.rb +2 -0
  57. data/lib/baza/drivers/sqlite3_java/index.rb +2 -0
  58. data/lib/baza/drivers/sqlite3_java/indexes.rb +2 -0
  59. data/lib/baza/drivers/sqlite3_java/table.rb +2 -0
  60. data/lib/baza/drivers/sqlite3_java/tables.rb +2 -0
  61. data/lib/baza/drivers/sqlite3_java/unbuffered_result.rb +33 -0
  62. data/lib/baza/drivers/sqlite3_rhodes.rb +78 -0
  63. data/{include → lib/baza}/dump.rb +0 -0
  64. data/{include → lib/baza}/errors.rb +0 -0
  65. data/{include → lib/baza}/idquery.rb +0 -0
  66. data/{include → lib/baza}/index.rb +0 -0
  67. data/lib/baza/jdbc_driver.rb +102 -0
  68. data/lib/baza/jdbc_result.rb +125 -0
  69. data/{include → lib/baza}/model.rb +2 -2
  70. data/{include → lib/baza}/model_custom.rb +0 -0
  71. data/{include → lib/baza}/model_handler.rb +1 -1
  72. data/{include → lib/baza}/model_handler_sqlhelper.rb +0 -0
  73. data/{include → lib/baza}/query_buffer.rb +2 -2
  74. data/lib/baza/result_base.rb +25 -0
  75. data/{include → lib/baza}/revision.rb +1 -1
  76. data/{include → lib/baza}/row.rb +0 -0
  77. data/{include → lib/baza}/sqlspecs.rb +0 -0
  78. data/{include → lib/baza}/table.rb +0 -0
  79. data/shippable.yml +3 -1
  80. data/spec/cloner_spec.rb +10 -0
  81. data/spec/drivers/active_record_mysql2_spec.rb +18 -0
  82. data/spec/drivers/active_record_mysql_spec.rb +16 -0
  83. data/spec/drivers/active_record_sqlite3_spec.rb +16 -0
  84. data/spec/drivers/mysql2_spec.rb +16 -0
  85. data/spec/{include/drivers → drivers}/mysql_spec.rb +9 -1
  86. data/spec/{include/drivers → drivers}/sqlite3_spec.rb +8 -0
  87. data/spec/info_active_record_mysql.rb +37 -0
  88. data/spec/info_active_record_mysql2.rb +37 -0
  89. data/spec/info_active_record_mysql2_shippable.rb +36 -0
  90. data/spec/info_active_record_mysql_shippable.rb +36 -0
  91. data/spec/info_active_record_sqlite3.rb +37 -0
  92. data/spec/info_mysql2_example.rb +23 -0
  93. data/spec/info_mysql2_shippable.rb +22 -0
  94. data/spec/info_mysql_example.rb +3 -4
  95. data/spec/info_mysql_shippable.rb +7 -17
  96. data/spec/info_sqlite3.rb +5 -4
  97. data/spec/model_handler_spec.rb +137 -105
  98. data/spec/spec_helper.rb +8 -0
  99. data/spec/support/driver_collection.rb +124 -34
  100. data/spec/support/driver_columns_collection.rb +0 -7
  101. data/spec/support/driver_indexes_collection.rb +4 -11
  102. data/spec/support/driver_tables_collection.rb +0 -7
  103. metadata +160 -52
  104. data/include/cloner.rb +0 -18
  105. data/include/drivers/active_record/active_record.rb +0 -159
  106. data/include/drivers/mysql/mysql.rb +0 -443
  107. data/include/drivers/mysql/mysql_result.rb +0 -42
  108. data/include/drivers/mysql/mysql_result_java.rb +0 -61
  109. data/include/drivers/mysql/mysql_result_mysql2.rb +0 -26
  110. data/include/drivers/sqlite3/sqlite3.rb +0 -159
  111. data/include/drivers/sqlite3/sqlite3_result.rb +0 -35
  112. data/include/drivers/sqlite3/sqlite3_result_java.rb +0 -39
  113. data/spec/include/cloner_spec.rb +0 -10
  114. data/spec/include/drivers/active_record_spec.rb +0 -8
  115. data/spec/info_active_record.rb +0 -49
  116. data/spec/info_active_record_shippable.rb +0 -47
@@ -0,0 +1,2 @@
1
+ class Baza::Driver::MysqlJava::Column < Baza::Driver::Mysql::Column
2
+ end
@@ -0,0 +1,2 @@
1
+ class Baza::Driver::MysqlJava::Columns < Baza::Driver::Mysql::Columns
2
+ end
@@ -0,0 +1,2 @@
1
+ class Baza::Driver::MysqlJava::Index < Baza::Driver::Mysql::Index
2
+ end
@@ -0,0 +1,2 @@
1
+ class Baza::Driver::MysqlJava::Indexes < Baza::Driver::Mysql::Indexes
2
+ end
@@ -0,0 +1,2 @@
1
+ class Baza::Driver::MysqlJava::Table < Baza::Driver::Mysql::Table
2
+ end
@@ -0,0 +1,2 @@
1
+ class Baza::Driver::MysqlJava::Tables < Baza::Driver::Mysql::Tables
2
+ end
@@ -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
@@ -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
@@ -2,4 +2,4 @@ class Baza::Driver::Sqlite3::Sqlspecs < Baza::Sqlspecs
2
2
  def strftime(val, col_str)
3
3
  return "STRFTIME('#{val}', SUBSTR(#{col_str}, 0, 20))"
4
4
  end
5
- end
5
+ end
@@ -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,2 @@
1
+ class Baza::Driver::Sqlite3Java::Column < Baza::Driver::Sqlite3::Column
2
+ end
@@ -0,0 +1,2 @@
1
+ class Baza::Driver::Sqlite3Java::Columns < Baza::Driver::Sqlite3::Columns
2
+ end
@@ -0,0 +1,2 @@
1
+ class Baza::Driver::Sqlite3Java::Index < Baza::Driver::Sqlite3::Index
2
+ end
@@ -0,0 +1,2 @@
1
+ class Baza::Driver::Sqlite3Java::Indexes < Baza::Driver::Sqlite3::Indexes
2
+ end
@@ -0,0 +1,2 @@
1
+ class Baza::Driver::Sqlite3Java::Table < Baza::Driver::Sqlite3::Table
2
+ end
@@ -0,0 +1,2 @@
1
+ class Baza::Driver::Sqlite3Java::Tables < Baza::Driver::Sqlite3::Tables
2
+ 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