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,10 @@
1
+ class Baza::Driver::ActiveRecord::Columns
2
+ def initialize(args)
3
+ @args = args
4
+ @proxy_to = ::Baza::Driver.const_get(StringCases.snake_to_camel(@args[:db].conn.conn_type)).const_get(:Columns).new(@args)
5
+ end
6
+
7
+ def method_missing(name, *args, &blk)
8
+ @proxy_to.__send__(name, *args, &blk)
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class Baza::Driver::ActiveRecord::Indexes
2
+ def initialize(args)
3
+ @args = args
4
+ @proxy_to = ::Baza::Driver.const_get(StringCases.snake_to_camel(@args[:db].conn.conn_type)).const_get(:Indexes).new(@args)
5
+ end
6
+
7
+ def method_missing(name, *args, &blk)
8
+ @proxy_to.__send__(name, *args, &blk)
9
+ end
10
+ end
@@ -0,0 +1,23 @@
1
+ class Baza::Driver::ActiveRecord::Result < Baza::ResultBase
2
+ def initialize(driver, result)
3
+ @result = result
4
+ @type_translation = driver.baza.opts[:type_translation]
5
+ end
6
+
7
+ def fetch
8
+ begin
9
+ return to_enum.next
10
+ rescue StopIteration
11
+ return false
12
+ end
13
+ end
14
+
15
+ def each
16
+ return unless @result
17
+
18
+ @result.each do |result|
19
+ result = Hash[result.map { |k, v| [k, v.to_s] }] if @type_translation == :string
20
+ yield result.symbolize_keys
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,12 @@
1
+ class Baza::Driver::ActiveRecord::Tables
2
+ def initialize(args)
3
+ @args = args
4
+
5
+ require "#{File.dirname(__FILE__)}/../#{@args[:db].conn.conn_type}"
6
+ @proxy_to = ::Baza::Driver.const_get(StringCases.snake_to_camel(@args[:db].conn.conn_type)).const_get(:Tables).new(@args)
7
+ end
8
+
9
+ def method_missing(name, *args, &blk)
10
+ @proxy_to.__send__(name, *args, &blk)
11
+ end
12
+ end
@@ -0,0 +1,220 @@
1
+ class Baza::Driver::Mysql < Baza::BaseSqlDriver
2
+ path = "#{File.dirname(__FILE__)}/mysql"
3
+
4
+ autoload :Table, "#{path}/table"
5
+ autoload :Tables, "#{path}/tables"
6
+ autoload :Column, "#{path}/column"
7
+ autoload :Columns, "#{path}/columns"
8
+ autoload :Index, "#{path}/index"
9
+ autoload :Indexes, "#{path}/indexes"
10
+ autoload :Result, "#{path}/result"
11
+ autoload :UnbufferedResult, "#{path}/unbuffered_result"
12
+ autoload :Sqlspecs, "#{path}/sqlspecs"
13
+
14
+ attr_reader :conn
15
+
16
+ def self.from_object(args)
17
+ raise 'Mysql does not support auth extraction' if args[:object].class.name == 'Mysql'
18
+ end
19
+
20
+ def initialize(baza)
21
+ super
22
+
23
+ @opts = @baza.opts
24
+
25
+ require 'monitor'
26
+ @mutex = Monitor.new
27
+
28
+ if baza.opts[:conn]
29
+ @conn = baza.opts[:conn]
30
+ else
31
+ if @opts[:encoding]
32
+ @encoding = @opts[:encoding]
33
+ else
34
+ @encoding = 'utf8'
35
+ end
36
+
37
+ if @baza.opts.key?(:port)
38
+ @port = @baza.opts[:port].to_i
39
+ else
40
+ @port = 3306
41
+ end
42
+
43
+ reconnect
44
+ end
45
+ end
46
+
47
+ #This method handels the closing of statements and results for the Java MySQL-mode.
48
+ def java_mysql_resultset_killer(id)
49
+ data = @java_rs_data[id]
50
+ return nil unless data
51
+
52
+ data[:res].close
53
+ data[:stmt].close
54
+ @java_rs_data.delete(id)
55
+ end
56
+
57
+ #Cleans the wref-map holding the tables.
58
+ def clean
59
+ tables.clean if tables
60
+ end
61
+
62
+ #Respawns the connection to the MySQL-database.
63
+ def reconnect
64
+ @mutex.synchronize do
65
+ require 'mysql' unless ::Object.const_defined?(:Mysql)
66
+ @conn = ::Mysql.real_connect(@baza.opts[:host], @baza.opts[:user], @baza.opts[:pass], @baza.opts[:db], @port)
67
+ query("SET NAMES '#{self.esc(@encoding)}'") if @encoding
68
+ end
69
+ end
70
+
71
+ #Executes a query and returns the result.
72
+ def query(str)
73
+ str = str.to_s
74
+ str = str.force_encoding("UTF-8") if @encoding == "utf8" and str.respond_to?(:force_encoding)
75
+ tries = 0
76
+
77
+ begin
78
+ tries += 1
79
+ @mutex.synchronize do
80
+ return Baza::Driver::Mysql::Result.new(self, @conn.query(str))
81
+ end
82
+ rescue => e
83
+ if tries <= 3
84
+ if e.message == "MySQL server has gone away" || e.message == "closed MySQL connection" or e.message == "Can't connect to local MySQL server through socket"
85
+ sleep 0.5
86
+ reconnect
87
+ retry
88
+ elsif e.message.include?("No operations allowed after connection closed") or e.message == "This connection is still waiting for a result, try again once you have the result" or e.message == "Lock wait timeout exceeded; try restarting transaction"
89
+ reconnect
90
+ retry
91
+ end
92
+ end
93
+
94
+ raise e
95
+ end
96
+ end
97
+
98
+ #Executes an unbuffered query and returns the result that can be used to access the data.
99
+ def query_ubuf(str)
100
+ @mutex.synchronize do
101
+ @conn.query_with_result = false
102
+ return Baza::Driver::Mysql::UnbufferedResult.new(@conn, @opts, @conn.query(str))
103
+ end
104
+ end
105
+
106
+ #Escapes a string to be safe to use in a query.
107
+ def escape_alternative(string)
108
+ return @conn.escape_string(string.to_s)
109
+ end
110
+
111
+ #Returns the last inserted ID for the connection.
112
+ def last_id
113
+ @mutex.synchronize { return @conn.insert_id.to_i }
114
+ end
115
+
116
+ #Closes the connection threadsafe.
117
+ def close
118
+ @mutex.synchronize { @conn.close }
119
+ end
120
+
121
+ #Destroyes the connection.
122
+ def destroy
123
+ @conn = nil
124
+ @baza = nil
125
+ @mutex = nil
126
+ @subtype = nil
127
+ @encoding = nil
128
+ @query_args = nil
129
+ @port = nil
130
+ end
131
+
132
+ #Inserts multiple rows in a table. Can return the inserted IDs if asked to in arguments.
133
+ def insert_multi(tablename, arr_hashes, args = nil)
134
+ sql = "INSERT INTO `#{tablename}` ("
135
+
136
+ first = true
137
+ if args && args[:keys]
138
+ keys = args[:keys]
139
+ elsif arr_hashes.first.is_a?(Hash)
140
+ keys = arr_hashes.first.keys
141
+ else
142
+ raise "Could not figure out keys."
143
+ end
144
+
145
+ keys.each do |col_name|
146
+ sql << "," unless first
147
+ first = false if first
148
+ sql << "`#{self.esc_col(col_name)}`"
149
+ end
150
+
151
+ sql << ") VALUES ("
152
+
153
+ first = true
154
+ arr_hashes.each do |hash|
155
+ if first
156
+ first = false
157
+ else
158
+ sql << "),("
159
+ end
160
+
161
+ first_key = true
162
+ if hash.is_a?(Array)
163
+ hash.each do |val|
164
+ if first_key
165
+ first_key = false
166
+ else
167
+ sql << ","
168
+ end
169
+
170
+ sql << @baza.sqlval(val)
171
+ end
172
+ else
173
+ hash.each do |key, val|
174
+ if first_key
175
+ first_key = false
176
+ else
177
+ sql << ","
178
+ end
179
+
180
+ sql << @baza.sqlval(val)
181
+ end
182
+ end
183
+ end
184
+
185
+ sql << ")"
186
+
187
+ return sql if args && args[:return_sql]
188
+
189
+ self.query(sql)
190
+
191
+ if args && args[:return_id]
192
+ first_id = self.last_id
193
+ raise "Invalid ID: #{first_id}" if first_id.to_i <= 0
194
+ ids = [first_id]
195
+ 1.upto(arr_hashes.length - 1) do |count|
196
+ ids << first_id + count
197
+ end
198
+
199
+ ids_length = ids.length
200
+ arr_hashes_length = arr_hashes.length
201
+ raise "Invalid length (#{ids_length}, #{arr_hashes_length})." unless ids_length == arr_hashes_length
202
+
203
+ return ids
204
+ else
205
+ return nil
206
+ end
207
+ end
208
+
209
+ def transaction
210
+ @baza.q("START TRANSACTION")
211
+
212
+ begin
213
+ yield @baza
214
+ @baza.q("COMMIT")
215
+ rescue
216
+ @baza.q("ROLLBACK")
217
+ raise
218
+ end
219
+ end
220
+ end
@@ -0,0 +1,81 @@
1
+ #This class controls the results for the normal MySQL-driver.
2
+ class Baza::Driver::Mysql::Result < Baza::ResultBase
3
+ INT_TYPES = {
4
+ ::Mysql::Field::TYPE_DECIMAL => true,
5
+ ::Mysql::Field::TYPE_TINY => true,
6
+ ::Mysql::Field::TYPE_LONG => true,
7
+ ::Mysql::Field::TYPE_YEAR => true
8
+ }
9
+ FLOAT_TYPES = {
10
+ ::Mysql::Field::TYPE_DECIMAL => true,
11
+ ::Mysql::Field::TYPE_FLOAT => true,
12
+ ::Mysql::Field::TYPE_DOUBLE => true
13
+ }
14
+ TIME_TYPES = {
15
+ ::Mysql::Field::TYPE_DATETIME => true
16
+ }
17
+ DATE_TYPES = {
18
+ ::Mysql::Field::TYPE_DATE => true
19
+ }
20
+
21
+ #Constructor. This should not be called manually.
22
+ def initialize(driver, result)
23
+ @driver = driver
24
+ @result = result
25
+ @mutex = Mutex.new
26
+ @type_translation = driver.baza.opts[:type_translation]
27
+
28
+ if @result
29
+ @keys = []
30
+ @types = [] if @type_translation
31
+
32
+ @result.fetch_fields.each do |key|
33
+ @keys << key.name.to_sym
34
+ @types << key.type if @type_translation
35
+ end
36
+ end
37
+ end
38
+
39
+ #Returns a single result as a hash with symbols as keys.
40
+ def fetch
41
+ fetched = nil
42
+ @mutex.synchronize do
43
+ fetched = @result.fetch_row
44
+ end
45
+
46
+ return false unless fetched
47
+
48
+ if @type_translation == true
49
+ fetched.collect!.with_index do |value, count|
50
+ translate_value_to_type(value, @types[count])
51
+ end
52
+ end
53
+
54
+ return Hash[*@keys.zip(fetched).flatten]
55
+ end
56
+
57
+ #Loops over every result yielding it.
58
+ def each
59
+ while data = fetch
60
+ yield data
61
+ end
62
+ end
63
+
64
+ private
65
+
66
+ def translate_value_to_type(value, type_no)
67
+ unless value === nil
68
+ if INT_TYPES[type_no]
69
+ return value.to_i
70
+ elsif FLOAT_TYPES[type_no]
71
+ return value.to_f
72
+ elsif TIME_TYPES[type_no]
73
+ return Time.parse(value)
74
+ elsif DATE_TYPES[type_no]
75
+ return Date.parse(value)
76
+ else
77
+ return value.to_s
78
+ end
79
+ end
80
+ end
81
+ end
@@ -5,7 +5,6 @@ class Baza::Driver::Mysql::Table < Baza::Table
5
5
  @args = args
6
6
  @db = args[:db]
7
7
  @data = args[:data]
8
- @subtype = @db.opts[:subtype]
9
8
  @list = Wref::Map.new
10
9
  @indexes_list = Wref::Map.new
11
10
  @name = @data[:Name].to_sym
@@ -32,7 +31,8 @@ class Baza::Driver::Mysql::Table < Baza::Table
32
31
 
33
32
  #Returns true if the table is safe to drop.
34
33
  def native?
35
- return true if @db.q("SELECT DATABASE() AS db").fetch[:db] == "mysql"
34
+ data = @db.q("SELECT DATABASE() AS db").fetch
35
+ return true if data[:db] == "mysql"
36
36
  return false
37
37
  end
38
38
 
@@ -8,7 +8,6 @@ class Baza::Driver::Mysql::Tables
8
8
  def initialize(args)
9
9
  @args = args
10
10
  @db = @args[:db]
11
- @subtype = @db.opts[:subtype]
12
11
  @list_mutex = Monitor.new
13
12
  @list = Wref::Map.new
14
13
  @list_should_be_reloaded = true
@@ -1,5 +1,5 @@
1
1
  #This class controls the unbuffered result for the normal MySQL-driver.
2
- class Baza::Driver::Mysql::ResultUnbuffered
2
+ class Baza::Driver::Mysql::UnbufferedResult < Baza::ResultBase
3
3
  #Constructor. This should not be called manually.
4
4
  def initialize(conn, opts, result)
5
5
  @conn = conn
@@ -14,15 +14,6 @@ class Baza::Driver::Mysql::ResultUnbuffered
14
14
  end
15
15
  end
16
16
 
17
- #Lods the keys for the object.
18
- def load_keys
19
- @keys = []
20
- keys = @res.fetch_fields
21
- keys.each do |key|
22
- @keys << key.name.to_sym
23
- end
24
- end
25
-
26
17
  #Returns a single result.
27
18
  def fetch
28
19
  if @enum
@@ -40,7 +31,7 @@ class Baza::Driver::Mysql::ResultUnbuffered
40
31
  @enum = @res.to_enum
41
32
  ret = @enum.next
42
33
  rescue Mysql::Error
43
- #Reset it to run non-unbuffered again and then return false.
34
+ # Reset it to run non-unbuffered again and then return false.
44
35
  @conn.query_with_result = true
45
36
  return false
46
37
  rescue StopIteration
@@ -49,24 +40,29 @@ class Baza::Driver::Mysql::ResultUnbuffered
49
40
  end
50
41
  end
51
42
 
52
- if !@as_hash
53
- return ret
43
+ if @as_hash
44
+ load_keys unless @keys
45
+ return Hash[*@keys.zip(ret).flatten]
54
46
  else
55
- self.load_keys if !@keys
56
-
57
- ret_h = {}
58
- @keys.each_index do |key_no|
59
- ret_h[@keys[key_no]] = ret[key_no]
60
- end
61
-
62
- return ret_h
47
+ return ret
63
48
  end
64
49
  end
65
50
 
66
51
  #Loops over every single result yielding it.
67
52
  def each
68
- while data = self.fetch
69
- yield(data)
53
+ while row = fetch
54
+ yield row
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ #Lods the keys for the object.
61
+ def load_keys
62
+ @keys = []
63
+ keys = @res.fetch_fields
64
+ keys.each do |key|
65
+ @keys << key.name.to_sym
70
66
  end
71
67
  end
72
68
  end