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
@@ -6,7 +6,7 @@ class Baza
6
6
  #Autoloader for subclasses.
7
7
  def self.const_missing(name)
8
8
  file_name = name.to_s.gsub(/(.)([A-Z])/,'\1_\2').downcase
9
- require "#{File.dirname(__FILE__)}/../include/#{file_name}.rb"
9
+ require "#{File.dirname(__FILE__)}/baza/#{file_name}.rb"
10
10
  raise "Still not defined: '#{name}'." unless Baza.const_defined?(name)
11
11
  return Baza.const_get(name)
12
12
  end
@@ -0,0 +1,64 @@
1
+ class Baza::BaseSqlDriver
2
+ attr_reader :baza, :conn, :sep_table, :sep_col, :sep_val
3
+ attr_accessor :tables, :cols, :indexes
4
+
5
+ def self.from_object(args)
6
+ end
7
+
8
+ def initialize(baza)
9
+ @baza = baza
10
+
11
+ @sep_table = "`"
12
+ @sep_col = "`"
13
+ @sep_val = "'"
14
+ end
15
+
16
+ def escape(string)
17
+ return string.to_s.gsub(/([\0\n\r\032\'\"\\])/) do
18
+ case $1
19
+ when "\0" then "\\0"
20
+ when "\n" then "\\n"
21
+ when "\r" then "\\r"
22
+ when "\032" then "\\Z"
23
+ else "\\#{$1}"
24
+ end
25
+ end
26
+ end
27
+
28
+ alias esc escape
29
+ alias escape_alternative escape
30
+
31
+ #Escapes a string to be used as a column.
32
+ def esc_col(string)
33
+ string = string.to_s
34
+ raise "Invalid column-string: #{string}" if string.index(@sep_col) != nil
35
+ return string
36
+ end
37
+
38
+ alias esc_table esc_col
39
+
40
+ def transaction
41
+ query("BEGIN TRANSACTION")
42
+
43
+ begin
44
+ yield @baza
45
+ query("COMMIT")
46
+ rescue => e
47
+ query("ROLLBACK")
48
+ end
49
+ end
50
+
51
+ def insert_multi(tablename, arr_hashes, args = nil)
52
+ sql = [] if args && args[:return_sql]
53
+
54
+ @baza.transaction do
55
+ arr_hashes.each do |hash|
56
+ res = @baza.insert(tablename, hash, args)
57
+ sql << res if args && args[:return_sql]
58
+ end
59
+ end
60
+
61
+ return sql if args && args[:return_sql]
62
+ return nil
63
+ end
64
+ end
@@ -0,0 +1,31 @@
1
+ class Baza::Cloner
2
+ def self.from_active_record_connection(connection)
3
+ if connection.class.name.include?('Mysql2Adapter')
4
+ connection = connection.instance_variable_get(:@connection)
5
+
6
+ config = connection.instance_variable_get(:@query_options)
7
+ config ||= connection.instance_variable_get(:@config)
8
+
9
+ db_args = {
10
+ type: :mysql2,
11
+ host: config[:host],
12
+ user: config[:username],
13
+ pass: config[:password],
14
+ db: config[:database]
15
+ }
16
+
17
+ Baza::Db.new(db_args)
18
+ elsif connection.class.name.include?('MysqlAdapter')
19
+ connection = connection.instance_variable_get(:@connection)
20
+
21
+ db_args = {
22
+ type: :mysql,
23
+ conn: connection
24
+ }
25
+
26
+ Baza::Db.new(db_args)
27
+ else
28
+ raise "Unsupported adapter: #{connection.class.name}"
29
+ end
30
+ end
31
+ end
File without changes
@@ -1,12 +1,12 @@
1
1
  #A wrapper of several possible database-types.
2
2
  #
3
3
  #===Examples
4
- # db = Baza::Db.new(type: "mysql", subtype: "mysql2", db: "mysql", user: "user", pass: "password")
4
+ # db = Baza::Db.new(type: :mysql2, db: "mysql", user: "user", pass: "password")
5
5
  # mysql_table = db.tables['mysql']
6
6
  # name = mysql_table.name
7
7
  # cols = mysql_table.columns
8
8
  #
9
- # db = Baza::Db.new(type: "sqlite3", path: "some_db.sqlite3")
9
+ # db = Baza::Db.new(type: :sqlite3, path: "some_db.sqlite3")
10
10
  #
11
11
  # db.q("SELECT * FROM users") do |data|
12
12
  # print data[:name]
@@ -43,8 +43,6 @@ class Baza::Db
43
43
  raise "No :object was given." unless args[:object]
44
44
 
45
45
  Baza::Db.drivers.each do |driver|
46
- require driver[:driver_path]
47
-
48
46
  const = Baza::Driver.const_get(driver[:class_name])
49
47
  next unless const.respond_to?(:from_object)
50
48
 
@@ -68,19 +66,27 @@ class Baza::Db
68
66
  self.opts = opts if opts != nil
69
67
  @int_types = [:int, :bigint, :tinyint, :smallint, :mediumint]
70
68
 
71
- if !@opts[:threadsafe]
72
- require "monitor"
69
+ unless @opts[:threadsafe]
70
+ require 'monitor'
73
71
  @mutex = Monitor.new
74
72
  end
75
73
 
76
74
  @debug = @opts[:debug]
77
75
 
78
- self.conn_exec do |driver|
76
+ conn_exec do |driver|
79
77
  @sep_table = driver.sep_table
80
78
  @sep_col = driver.sep_col
81
79
  @sep_val = driver.sep_val
82
80
  @esc_driver = driver
83
81
  end
82
+
83
+ if block_given?
84
+ begin
85
+ yield self
86
+ ensure
87
+ close
88
+ end
89
+ end
84
90
  end
85
91
 
86
92
  def args
@@ -93,10 +99,9 @@ class Baza::Db
93
99
  @opts[key.to_sym] = val
94
100
  end
95
101
 
96
- if RUBY_PLATFORM == "java"
97
- @opts[:subtype] ||= :java
98
- elsif @opts[:type] == "sqlite3" && RUBY_PLATFORM.index("mswin32") != nil
99
- @opts[:subtype] ||= :ironruby
102
+ if RUBY_PLATFORM == 'java'
103
+ @opts[:type] = :sqlite3_java if @opts[:type] == :sqlite3
104
+ @opts[:type] = :mysql_java if @opts[:type] == :mysql || @opts[:type] == :mysql2
100
105
  end
101
106
 
102
107
  @type_cc = StringCases.snake_to_camel(@opts[:type])
@@ -130,14 +135,14 @@ class Baza::Db
130
135
  # driver_instance = db.spawn
131
136
  def spawn
132
137
  raise "No type given (#{@opts.keys.join(",")})." unless @opts[:type]
133
- rpath = "#{File.dirname(__FILE__)}/#{"drivers/#{@opts[:type]}/#{@opts[:type]}.rb"}"
134
- require rpath if (!@opts.key?(:require) || @opts[:require]) && File.exists?(rpath)
138
+ rpath = "#{File.dirname(__FILE__)}/drivers/#{@opts[:type]}.rb"
139
+ require rpath if File.exists?(rpath)
135
140
  return Baza::Driver.const_get(@type_cc).new(self)
136
141
  end
137
142
 
138
143
  #Registers a driver to the current thread.
139
144
  def get_and_register_thread
140
- raise "KnjDB-object is not in threadding mode." unless @conns
145
+ raise "Baza-object is not in threadding mode" unless @conns
141
146
 
142
147
  thread_cur = Thread.current
143
148
  tid = self.__id__
@@ -237,8 +242,7 @@ class Baza::Db
237
242
  loop do
238
243
  puts "Copying rows (#{limit_from}, #{limit_incr})." if debug
239
244
  ins_arr = []
240
- q_rows = self.select(table_name, {}, {limit_from: limit_from, limit_to: limit_incr})
241
- while d_rows = q_rows.fetch
245
+ q_rows = self.select(table_name, {}, {limit_from: limit_from, limit_to: limit_incr}) do |d_rows|
242
246
  col_args = nil
243
247
 
244
248
  if table_args && table_args[:columns]
@@ -285,10 +289,10 @@ class Baza::Db
285
289
  sql = "INSERT INTO #{@sep_table}#{self.esc_table(tablename)}#{@sep_table}"
286
290
 
287
291
  if !arr_insert || arr_insert.empty?
288
- #This is the correct syntax for inserting a blank row in MySQL.
289
- if @opts[:type].to_s == "mysql"
292
+ # This is the correct syntax for inserting a blank row in MySQL.
293
+ if @opts[:type].to_s.include?("mysql")
290
294
  sql << " VALUES ()"
291
- elsif @opts[:type].to_s == "sqlite3"
295
+ elsif @opts[:type].to_s.include?("sqlite3")
292
296
  sql << " DEFAULT VALUES"
293
297
  else
294
298
  raise "Unknown database-type: '#{@opts[:type]}'."
@@ -325,7 +329,7 @@ class Baza::Db
325
329
 
326
330
  return sql if args && args[:return_sql]
327
331
 
328
- self.conn_exec do |driver|
332
+ conn_exec do |driver|
329
333
  begin
330
334
  driver.query(sql)
331
335
  rescue => e
@@ -381,7 +385,7 @@ class Baza::Db
381
385
  end
382
386
  end
383
387
 
384
- self.conn_exec do |driver|
388
+ conn_exec do |driver|
385
389
  return driver.insert_multi(tablename, arr_hashes, args)
386
390
  end
387
391
  else
@@ -427,14 +431,14 @@ class Baza::Db
427
431
 
428
432
  return sql if args && args[:return_sql]
429
433
 
430
- self.conn_exec do |driver|
434
+ conn_exec do |driver|
431
435
  driver.query(sql)
432
436
  end
433
437
  end
434
438
 
435
439
  #Checks if a given terms exists. If it does, updates it to match data. If not inserts the row.
436
440
  def upsert(table, data, terms, args = nil)
437
- row = self.select(table, terms, "limit" => 1).fetch
441
+ row = self.single(table, terms)
438
442
 
439
443
  if args && args[:buffer]
440
444
  obj = args[:buffer]
@@ -510,18 +514,26 @@ class Baza::Db
510
514
  end
511
515
  end
512
516
 
517
+ def count(tablename, arr_terms = nil)
518
+ #Set up vars.
519
+ sql = ""
520
+ args_q = nil
521
+
522
+ sql = "SELECT COUNT(*) AS count FROM #{@sep_table}#{tablename}#{@sep_table}"
523
+
524
+ if arr_terms != nil && !arr_terms.empty?
525
+ sql << " WHERE #{self.makeWhere(arr_terms)}"
526
+ end
527
+
528
+ return q(sql).fetch.fetch(:count).to_i
529
+ end
530
+
513
531
  #Returns a single row from a database.
514
532
  #
515
533
  #===Examples
516
534
  # row = db.single(:users, lastname: "Doe")
517
- def single(tablename, arr_terms = nil, args = {})
518
- args[:limit] = 1
519
-
520
- #Experienced very weird memory leak if this was not done by block. Maybe bug in Ruby 1.9.2? - knj
521
- self.select(tablename, arr_terms, args) do |data|
522
- return data
523
- end
524
-
535
+ def single(tablename, terms = nil, args = {})
536
+ select(tablename, terms, args.merge(limit: 1)) { |data| return data } #Experienced very weird memory leak if this was not done by block. Maybe bug in Ruby 1.9.2? - knj
525
537
  return false
526
538
  end
527
539
 
@@ -540,7 +552,7 @@ class Baza::Db
540
552
 
541
553
  return sql if args && args[:return_sql]
542
554
 
543
- self.conn_exec do |driver|
555
+ conn_exec do |driver|
544
556
  driver.query(sql)
545
557
  end
546
558
 
@@ -631,7 +643,7 @@ class Baza::Db
631
643
  end
632
644
 
633
645
  begin
634
- self.conn_exec do |driver|
646
+ conn_exec do |driver|
635
647
  return driver.query(string)
636
648
  end
637
649
  rescue => e
@@ -649,7 +661,7 @@ class Baza::Db
649
661
  def query_ubuf(string, &block)
650
662
  ret = nil
651
663
 
652
- self.conn_exec do |driver|
664
+ conn_exec do |driver|
653
665
  ret = driver.query_ubuf(string, &block)
654
666
  end
655
667
 
@@ -690,24 +702,24 @@ class Baza::Db
690
702
  # db.q('SELECT * FROM users') do |data|
691
703
  # print data[:name]
692
704
  # end
693
- def q(str, args = nil, &block)
705
+ def q(sql, args = nil, &block)
694
706
  #If the query should be executed in a new connection unbuffered.
695
- if args
696
- if args[:cloned_ubuf]
697
- raise "No block given." unless block
698
-
699
- self.cloned_conn(clone_args: args[:clone_args]) do |cloned_conn|
700
- ret = cloned_conn.query_ubuf(str)
701
- ret.each(&block)
702
- end
707
+ if args && args[:cloned_ubuf]
708
+ raise "No block given." unless block
703
709
 
704
- return nil
705
- else
706
- raise "Invalid arguments given: '#{args}'."
710
+ self.cloned_conn(clone_args: args[:clone_args]) do |cloned_conn|
711
+ ret = cloned_conn.query_ubuf(sql)
712
+ ret.each(&block)
707
713
  end
714
+
715
+ return nil
708
716
  end
709
717
 
710
- ret = self.query(str)
718
+ if args && args[:type] == :unbuffered
719
+ ret = self.query_ubuf(sql)
720
+ else
721
+ ret = self.query(sql)
722
+ end
711
723
 
712
724
  if block
713
725
  ret.each(&block)
@@ -728,7 +740,7 @@ class Baza::Db
728
740
  #===Examples
729
741
  # id = db.last_id
730
742
  def last_id
731
- self.conn_exec do |driver|
743
+ conn_exec do |driver|
732
744
  return driver.last_id
733
745
  end
734
746
  end
@@ -850,7 +862,7 @@ class Baza::Db
850
862
  #===Examples
851
863
  # db.method_on_driver
852
864
  def method_missing(method_name, *args)
853
- self.conn_exec do |driver|
865
+ conn_exec do |driver|
854
866
  if driver.respond_to?(method_name.to_sym)
855
867
  return driver.send(method_name, *args)
856
868
  end
File without changes
@@ -2,7 +2,7 @@
2
2
  class Baza::Driver
3
3
  #Autoloader for drivers.
4
4
  def self.const_missing(name)
5
- require_relative "drivers/#{StringCases.camel_to_snake(name)}/#{StringCases.camel_to_snake(name)}.rb"
5
+ require_relative "drivers/#{StringCases.camel_to_snake(name)}.rb"
6
6
  raise LoadError, "Still not loaded: '#{name}'." unless Baza::Driver.const_defined?(name)
7
7
  return Baza::Driver.const_get(name)
8
8
  end
@@ -0,0 +1,107 @@
1
+ class Baza::Driver::ActiveRecord
2
+ path = "#{File.dirname(__FILE__)}/active_record"
3
+
4
+ autoload :Tables, "#{path}/tables"
5
+ autoload :Columns, "#{path}/columns"
6
+ autoload :Indexes, "#{path}/indexes"
7
+ autoload :Result, "#{path}/result"
8
+
9
+ attr_reader :baza, :conn, :sep_table, :sep_col, :sep_val, :symbolize, :conn_type
10
+ attr_accessor :tables, :cols, :indexes
11
+
12
+ def self.from_object(args)
13
+ if args[:object].class.name.include?("ActiveRecord::ConnectionAdapters")
14
+ if args[:object].class.name.include?("ConnectionPool")
15
+ object_to_use = args[:object].connection
16
+ else
17
+ object_to_use = args[:object]
18
+ end
19
+
20
+ return {
21
+ type: :success,
22
+ args: {
23
+ type: :active_record,
24
+ conn: object_to_use
25
+ }
26
+ }
27
+ end
28
+
29
+ return nil
30
+ end
31
+
32
+ def initialize(baza)
33
+ @baza = baza
34
+ @conn = @baza.opts[:conn]
35
+
36
+ raise 'No conn given' unless @conn
37
+
38
+ conn_name = @conn.class.name.to_s.downcase
39
+
40
+ if conn_name.include?("mysql2")
41
+ @sep_table = "`"
42
+ @sep_col = "`"
43
+ @sep_val = "'"
44
+ @conn_type = :mysql2
45
+ @result_constant = Baza::Driver::Mysql2::Result
46
+ elsif conn_name.include?("mysql")
47
+ @sep_table = "`"
48
+ @sep_col = "`"
49
+ @sep_val = "'"
50
+ @conn_type = :mysql
51
+ @result_constant = Baza::Driver::Mysql::Result unless RUBY_PLATFORM == 'java'
52
+ elsif conn_name.include?("sqlite")
53
+ @sep_table = "`"
54
+ @sep_col = "`"
55
+ @sep_val = "'"
56
+ @conn_type = :sqlite3
57
+ else
58
+ raise "Unknown type: '#{conn_name}'."
59
+ end
60
+
61
+ @result_constant ||= Baza::Driver::ActiveRecord::Result
62
+ end
63
+
64
+ def query(sql)
65
+ @result_constant.new(self, @conn.execute(sql))
66
+ end
67
+
68
+ alias query_ubuf query
69
+
70
+ def escape(str)
71
+ @conn.quote_string(str.to_s)
72
+ end
73
+
74
+ def esc_col(string)
75
+ string = string.to_s
76
+ raise "Invalid column-string: #{string}" if string.include?(@sep_col)
77
+ return string
78
+ end
79
+
80
+ def esc_table(string)
81
+ string = string.to_s
82
+ raise "Invalid column-string: #{string}" if string.include?(@sep_col)
83
+ return string
84
+ end
85
+
86
+ def close
87
+ @conn.close
88
+ end
89
+
90
+ def transaction
91
+ if @conn_type == :mysql || @conn_type == :mysql2
92
+ query("START TRANSACTION")
93
+ elsif @conn_type == :sqlite3
94
+ query("BEGIN TRANSACTION")
95
+ else
96
+ raise "Don't know how to start transaction"
97
+ end
98
+
99
+ begin
100
+ yield @baza
101
+ query("COMMIT")
102
+ rescue
103
+ query("ROLLBACK")
104
+ raise
105
+ end
106
+ end
107
+ end