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
@@ -1,42 +0,0 @@
1
- #This class controls the results for the normal MySQL-driver.
2
- class Baza::Driver::Mysql::Result
3
- #Constructor. This should not be called manually.
4
- def initialize(driver, result)
5
- @driver = driver
6
- @result = result
7
- @mutex = Mutex.new
8
-
9
- if @result
10
- @keys = []
11
- @result.fetch_fields.each do |key|
12
- @keys << key.name.to_sym
13
- end
14
- end
15
- end
16
-
17
- #Returns a single result as a hash with symbols as keys.
18
- def fetch
19
- fetched = nil
20
- @mutex.synchronize do
21
- fetched = @result.fetch_row
22
- end
23
-
24
- return false unless fetched
25
-
26
- ret = {}
27
- count = 0
28
- @keys.each do |key|
29
- ret[key] = fetched[count]
30
- count += 1
31
- end
32
-
33
- return ret
34
- end
35
-
36
- #Loops over every result yielding it.
37
- def each
38
- while data = self.fetch
39
- yield(data)
40
- end
41
- end
42
- end
@@ -1,61 +0,0 @@
1
- #This class controls the result for the Java-MySQL-driver.
2
- class Baza::Driver::Mysql::ResultJava
3
- #Constructor. This should not be called manually.
4
- def initialize(knjdb, opts, result)
5
- @baza_db = knjdb
6
- @result = result
7
-
8
- if !opts.key?(:result) || opts[:result] == "hash"
9
- @as_hash = true
10
- elsif opts[:result] == "array"
11
- @as_hash = false
12
- else
13
- raise "Unknown type of result: '#{opts[:result]}'."
14
- end
15
- end
16
-
17
- #Reads meta-data about the query like keys and count.
18
- def read_meta
19
- @result.before_first
20
- meta = @result.meta_data
21
- @count = meta.column_count
22
-
23
- @keys = []
24
- 1.upto(@count) do |count|
25
- @keys << meta.column_label(count).to_sym
26
- end
27
- end
28
-
29
- def fetch
30
- return false unless @result
31
- self.read_meta unless @keys
32
- status = @result.next
33
-
34
- unless status
35
- @result = nil
36
- @keys = nil
37
- @count = nil
38
- return false
39
- end
40
-
41
- if @as_hash
42
- ret = {}
43
- 1.upto(@count) do |count|
44
- ret[@keys[count - 1]] = @result.object(count)
45
- end
46
- else
47
- ret = []
48
- 1.upto(@count) do |count|
49
- ret << @result.object(count)
50
- end
51
- end
52
-
53
- return ret
54
- end
55
-
56
- def each
57
- while data = self.fetch
58
- yield(data)
59
- end
60
- end
61
- end
@@ -1,26 +0,0 @@
1
- #This class controls the result for the MySQL2 driver.
2
- class Baza::Driver::Mysql::ResultMysql2
3
- #Constructor. This should not be called manually.
4
- def initialize(result)
5
- @result = result
6
- end
7
-
8
- #Returns a single result.
9
- def fetch
10
- @enum = @result.to_enum if !@enum
11
-
12
- begin
13
- return @enum.next
14
- rescue StopIteration
15
- return false
16
- end
17
- end
18
-
19
- #Loops over every single result yielding it.
20
- def each
21
- @result.each do |res|
22
- next unless res #This sometimes happens when streaming results...
23
- yield(res)
24
- end
25
- end
26
- end
@@ -1,159 +0,0 @@
1
- #This class handels SQLite3-specific behaviour.
2
- class Baza::Driver::Sqlite3
3
- autoload :Table, "#{File.dirname(__FILE__)}/sqlite3_table"
4
- autoload :Tables, "#{File.dirname(__FILE__)}/sqlite3_tables"
5
- autoload :Column, "#{File.dirname(__FILE__)}/sqlite3_column"
6
- autoload :Columns, "#{File.dirname(__FILE__)}/sqlite3_columns"
7
- autoload :Index, "#{File.dirname(__FILE__)}/sqlite3_index"
8
- autoload :Indexes, "#{File.dirname(__FILE__)}/sqlite3_indexes"
9
- autoload :Result, "#{File.dirname(__FILE__)}/sqlite3_result"
10
- autoload :ResultJava, "#{File.dirname(__FILE__)}/sqlite3_result_java"
11
- autoload :Sqlspecs, "#{File.dirname(__FILE__)}/sqlite3_sqlspecs"
12
-
13
- attr_reader :baza, :conn, :sep_table, :sep_col, :sep_val, :symbolize
14
- attr_accessor :tables, :cols, :indexes
15
-
16
- #Helper to enable automatic registering of database using Baza::Db.from_object
17
- def self.from_object(args)
18
- if args[:object].class.name == "SQLite3::Database"
19
- return {
20
- type: :success,
21
- args: {
22
- type: :sqlite3,
23
- conn: args[:object]
24
- }
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
- }
34
- end
35
-
36
- return nil
37
- end
38
-
39
- #Constructor. This should not be called manually.
40
- def initialize(baza_db)
41
- @sep_table = "`"
42
- @sep_col = "`"
43
- @sep_val = "'"
44
-
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]
49
-
50
- if @baza.opts[:conn]
51
- @conn = @baza.opts[:conn]
52
- @stat = @conn.createStatement if @subtype == :java
53
- else
54
- raise "No path was given." unless @path
55
-
56
- if @subtype == :java
57
- if @baza.opts[:sqlite_driver]
58
- require @baza.opts[:sqlite_driver]
59
- else
60
- require "jdbc/sqlite3"
61
- ::Jdbc::SQLite3.load_driver
62
- end
63
-
64
- require "java"
65
- import "org.sqlite.JDBC"
66
- @conn = java.sql.DriverManager::getConnection("jdbc:sqlite:#{@baza.opts[:path]}")
67
- @stat = @conn.createStatement
68
- elsif @subtype == :rhodes
69
- @conn = ::SQLite3::Database.new(@path, @path)
70
- else
71
- @conn = ::SQLite3::Database.open(@path)
72
- @conn.results_as_hash = true
73
- @conn.type_translation = false
74
- end
75
- end
76
- end
77
-
78
- #Executes a query against the driver.
79
- def query(string)
80
- if @subtype == :rhodes
81
- return Baza::Driver::Sqlite3::Result.new(self, @conn.execute(string, string))
82
- elsif @subtype == :java
83
- begin
84
- return Baza::Driver::Sqlite3::ResultJava.new(self, @stat.executeQuery(string))
85
- rescue java.sql.SQLException => e
86
- if e.message.to_s.index("query does not return ResultSet") != nil
87
- return Baza::Driver::Sqlite3::ResultJava.new(self, nil)
88
- else
89
- raise e
90
- end
91
- end
92
- else
93
- return Baza::Driver::Sqlite3::Result.new(self, @conn.execute(string))
94
- end
95
- end
96
-
97
- #SQLite3 driver doesnt support unbuffered queries??
98
- alias query_ubuf query
99
-
100
- #Escapes a string to be safe to used in a query.
101
- def escape(string)
102
- #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...
103
- #http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html
104
- return string.to_s.gsub(/'/, "''")
105
- end
106
-
107
- #Escapes a string to be used as a column.
108
- def esc_col(string)
109
- string = string.to_s
110
- raise "Invalid column-string: #{string}" if string.index(@sep_col) != nil
111
- return string
112
- end
113
-
114
- alias :esc_table :esc_col
115
- alias :esc :escape
116
-
117
- #Returns the last inserted ID.
118
- def last_id
119
- return @conn.last_insert_row_id if @conn.respond_to?(:last_insert_row_id)
120
- return query("SELECT last_insert_rowid() AS id").fetch[:id].to_i
121
- end
122
-
123
- #Closes the connection to the database.
124
- def close
125
- @conn.close
126
- end
127
-
128
- #Starts a transaction, yields the database and commits.
129
- def transaction
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
143
- end
144
- end
145
-
146
- def insert_multi(tablename, arr_hashes, args = nil)
147
- sql = [] if args && args[:return_sql]
148
-
149
- @baza.transaction do
150
- arr_hashes.each do |hash|
151
- res = @baza.insert(tablename, hash, args)
152
- sql << res if args && args[:return_sql]
153
- end
154
- end
155
-
156
- return sql if args && args[:return_sql]
157
- return nil
158
- end
159
- end
@@ -1,35 +0,0 @@
1
- #This class handels the result when running MRI (or others).
2
- class Baza::Driver::Sqlite3::Result
3
- #Constructor. This should not be called manually.
4
- def initialize(driver, result_array)
5
- @result_array = result_array
6
- @index = 0
7
- end
8
-
9
- #Returns a single result.
10
- def fetch
11
- result_hash = @result_array[@index]
12
- return false unless result_hash
13
- @index += 1
14
-
15
- ret = {}
16
- result_hash.each do |key, val|
17
- if (Float(key) rescue false)
18
- #do nothing.
19
- elsif !key.is_a?(Symbol)
20
- ret[key.to_sym] = val
21
- else
22
- ret[key] = val
23
- end
24
- end
25
-
26
- return ret
27
- end
28
-
29
- #Loops over every result yielding them.
30
- def each
31
- while data = self.fetch
32
- yield(data)
33
- end
34
- end
35
- end
@@ -1,39 +0,0 @@
1
- #This class handels results when running in JRuby.
2
- class Baza::Driver::Sqlite3::ResultJava
3
- def initialize(driver, rs)
4
- @index = 0
5
- retkeys = driver.baza.opts[:return_keys]
6
-
7
- if rs
8
- metadata = rs.getMetaData
9
- columns_count = metadata.getColumnCount
10
-
11
- @rows = []
12
- while rs.next
13
- row_data = {}
14
- for i in (1..columns_count)
15
- col_name = metadata.getColumnName(i).to_sym
16
- row_data[col_name] = rs.getString(i)
17
- end
18
-
19
- @rows << row_data
20
- end
21
- end
22
- end
23
-
24
- #Returns a single result.
25
- def fetch
26
- return false unless @rows
27
- ret = @rows[@index]
28
- return false unless ret
29
- @index += 1
30
- return ret
31
- end
32
-
33
- #Loops over every result and yields them.
34
- def each
35
- while data = fetch
36
- yield data
37
- end
38
- end
39
- end
@@ -1,10 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Baza::Cloner do
4
- it 'can clone drivers' do
5
- require 'info_active_record'
6
- conn = Baza::InfoActiveRecord.connection
7
- baza_db = Baza::Cloner.from_active_record_connection(conn[:conn])
8
- expect(baza_db.query('SELECT 1 AS test').fetch[:test]).to eq 1
9
- end
10
- end
@@ -1,8 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Baza::Driver::ActiveRecord do
4
- it_behaves_like "a baza driver"
5
- it_should_behave_like "a baza tables driver"
6
- it_should_behave_like "a baza columns driver"
7
- it_should_behave_like "a baza indexes driver"
8
- end
@@ -1,49 +0,0 @@
1
- class Baza::InfoActiveRecord
2
- attr_reader :db
3
-
4
- def self.connection
5
- require "active_record"
6
-
7
- if RUBY_ENGINE == "jruby"
8
- require "/usr/share/java/mysql-connector-java.jar" if File.exists?("/usr/share/java/mysql-connector-java.jar")
9
- @conn_pool ||= ::ActiveRecord::Base.establish_connection(
10
- adapter: "mysql",
11
- host: "localhost",
12
- database: "baza-test",
13
- username: "baza-test",
14
- password: "BBH7djRUKzL5nmG3"
15
- )
16
- @conn ||= @conn_pool.connection
17
- else
18
- @conn_pool ||= ::ActiveRecord::Base.establish_connection(
19
- adapter: "mysql2",
20
- host: "localhost",
21
- database: "baza-test",
22
- username: "baza-test",
23
- password: "BBH7djRUKzL5nmG3"
24
- )
25
- @conn ||= @conn_pool.connection
26
- end
27
-
28
- return {pool: @conn_pool, conn: @conn}
29
- end
30
-
31
- def initialize
32
- data = Baza::InfoActiveRecord.connection
33
-
34
- @db = Baza::Db.new(
35
- type: :active_record,
36
- conn: data[:conn]
37
- )
38
- end
39
-
40
- def before
41
- @db.tables.list.each do |name, table|
42
- table.drop
43
- end
44
- end
45
-
46
- def after
47
- @db.close
48
- end
49
- end
@@ -1,47 +0,0 @@
1
- class Baza::InfoActiveRecord
2
- attr_reader :db
3
-
4
- def self.connection
5
- require "active_record"
6
-
7
- if RUBY_ENGINE == "jruby"
8
- require "/usr/share/java/mysql-connector-java.jar" if File.exists?("/usr/share/java/mysql-connector-java.jar")
9
- @conn_pool ||= ::ActiveRecord::Base.establish_connection(
10
- adapter: "mysql",
11
- host: "localhost",
12
- database: "baza",
13
- username: "shippa"
14
- )
15
- @conn ||= @conn_pool.connection
16
- else
17
- @conn_pool ||= ::ActiveRecord::Base.establish_connection(
18
- adapter: "mysql2",
19
- host: "localhost",
20
- database: "baza",
21
- username: "shippa"
22
- )
23
- @conn ||= @conn_pool.connection
24
- end
25
-
26
- return {pool: @conn_pool, conn: @conn}
27
- end
28
-
29
- def initialize
30
- data = Baza::InfoActiveRecord.connection
31
-
32
- @db = Baza::Db.new(
33
- type: :active_record,
34
- conn: data[:conn]
35
- )
36
- end
37
-
38
- def before
39
- @db.tables.list.each do |name, table|
40
- table.drop
41
- end
42
- end
43
-
44
- def after
45
- @db.close
46
- end
47
- end