baza 0.0.20 → 0.0.21

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +84 -0
  3. data/.rubocop_todo.yml +17 -135
  4. data/.travis.yml +21 -0
  5. data/Gemfile +10 -7
  6. data/Gemfile.lock +39 -44
  7. data/README.md +61 -3
  8. data/VERSION +1 -1
  9. data/baza.gemspec +146 -98
  10. data/config/best_project_practice_rubocop.yml +8 -0
  11. data/config/best_project_practice_rubocop_todo.yml +6 -0
  12. data/lib/baza.rb +8 -12
  13. data/lib/baza/base_sql_driver.rb +198 -52
  14. data/lib/baza/cloner.rb +1 -0
  15. data/lib/baza/column.rb +26 -0
  16. data/lib/baza/database.rb +19 -3
  17. data/lib/baza/db.rb +69 -271
  18. data/lib/baza/driver.rb +1 -6
  19. data/lib/baza/{drivers → driver}/active_record.rb +65 -21
  20. data/lib/baza/{drivers → driver}/active_record/columns.rb +0 -0
  21. data/lib/baza/driver/active_record/commands.rb +10 -0
  22. data/lib/baza/driver/active_record/databases.rb +10 -0
  23. data/lib/baza/{drivers → driver}/active_record/indexes.rb +0 -0
  24. data/lib/baza/{drivers → driver}/active_record/result.rb +3 -1
  25. data/lib/baza/{drivers → driver}/active_record/tables.rb +0 -0
  26. data/lib/baza/driver/active_record/users.rb +12 -0
  27. data/lib/baza/{drivers → driver}/mysql.rb +9 -26
  28. data/lib/baza/{drivers → driver}/mysql/column.rb +14 -35
  29. data/lib/baza/{drivers → driver}/mysql/columns.rb +9 -12
  30. data/lib/baza/driver/mysql/commands.rb +39 -0
  31. data/lib/baza/driver/mysql/database.rb +64 -0
  32. data/lib/baza/driver/mysql/databases.rb +63 -0
  33. data/lib/baza/{drivers → driver}/mysql/index.rb +0 -0
  34. data/lib/baza/{drivers → driver}/mysql/indexes.rb +0 -0
  35. data/lib/baza/{drivers → driver}/mysql/result.rb +15 -7
  36. data/lib/baza/{drivers → driver}/mysql/sqlspecs.rb +0 -0
  37. data/lib/baza/{drivers → driver}/mysql/table.rb +27 -43
  38. data/lib/baza/{drivers → driver}/mysql/tables.rb +5 -34
  39. data/lib/baza/{drivers → driver}/mysql/unbuffered_result.rb +8 -2
  40. data/lib/baza/driver/mysql/user.rb +22 -0
  41. data/lib/baza/driver/mysql/users.rb +39 -0
  42. data/lib/baza/{drivers → driver}/mysql2.rb +19 -49
  43. data/lib/baza/{drivers → driver}/mysql2/column.rb +0 -0
  44. data/lib/baza/{drivers → driver}/mysql2/columns.rb +0 -0
  45. data/lib/baza/driver/mysql2/commands.rb +2 -0
  46. data/lib/baza/{drivers → driver}/mysql2/database.rb +0 -0
  47. data/lib/baza/{drivers → driver}/mysql2/databases.rb +0 -0
  48. data/lib/baza/{drivers → driver}/mysql2/index.rb +0 -0
  49. data/lib/baza/{drivers → driver}/mysql2/indexes.rb +0 -0
  50. data/lib/baza/{drivers → driver}/mysql2/result.rb +3 -1
  51. data/lib/baza/{drivers → driver}/mysql2/table.rb +0 -0
  52. data/lib/baza/{drivers → driver}/mysql2/tables.rb +0 -0
  53. data/lib/baza/driver/mysql2/user.rb +2 -0
  54. data/lib/baza/driver/mysql2/users.rb +2 -0
  55. data/lib/baza/{drivers → driver}/mysql_java.rb +60 -38
  56. data/lib/baza/{drivers → driver}/mysql_java/column.rb +0 -0
  57. data/lib/baza/{drivers → driver}/mysql_java/columns.rb +0 -0
  58. data/lib/baza/driver/mysql_java/commands.rb +2 -0
  59. data/lib/baza/driver/mysql_java/database.rb +2 -0
  60. data/lib/baza/driver/mysql_java/databases.rb +2 -0
  61. data/lib/baza/{drivers → driver}/mysql_java/index.rb +0 -0
  62. data/lib/baza/{drivers → driver}/mysql_java/indexes.rb +0 -0
  63. data/lib/baza/{drivers → driver}/mysql_java/table.rb +0 -0
  64. data/lib/baza/{drivers → driver}/mysql_java/tables.rb +0 -0
  65. data/lib/baza/driver/mysql_java/user.rb +2 -0
  66. data/lib/baza/driver/mysql_java/users.rb +2 -0
  67. data/lib/baza/driver/pg.rb +80 -0
  68. data/lib/baza/driver/pg/column.rb +125 -0
  69. data/lib/baza/driver/pg/columns.rb +37 -0
  70. data/lib/baza/driver/pg/commands.rb +35 -0
  71. data/lib/baza/driver/pg/create_index_sql_creator.rb +51 -0
  72. data/lib/baza/driver/pg/database.rb +89 -0
  73. data/lib/baza/driver/pg/databases.rb +79 -0
  74. data/lib/baza/driver/pg/index.rb +35 -0
  75. data/lib/baza/driver/pg/indexes.rb +5 -0
  76. data/lib/baza/driver/pg/result.rb +139 -0
  77. data/lib/baza/driver/pg/table.rb +184 -0
  78. data/lib/baza/driver/pg/tables.rb +45 -0
  79. data/lib/baza/{drivers → driver}/sqlite3.rb +6 -24
  80. data/lib/baza/{drivers → driver}/sqlite3/column.rb +22 -24
  81. data/lib/baza/{drivers → driver}/sqlite3/columns.rb +6 -6
  82. data/lib/baza/driver/sqlite3/commands.rb +28 -0
  83. data/lib/baza/{drivers → driver}/sqlite3/database.rb +0 -0
  84. data/lib/baza/{drivers → driver}/sqlite3/databases.rb +0 -1
  85. data/lib/baza/{drivers → driver}/sqlite3/index.rb +0 -0
  86. data/lib/baza/{drivers → driver}/sqlite3/indexes.rb +0 -0
  87. data/lib/baza/{drivers → driver}/sqlite3/result.rb +14 -6
  88. data/lib/baza/{drivers → driver}/sqlite3/sqlspecs.rb +0 -0
  89. data/lib/baza/{drivers → driver}/sqlite3/table.rb +25 -16
  90. data/lib/baza/{drivers → driver}/sqlite3/tables.rb +5 -6
  91. data/lib/baza/{drivers → driver}/sqlite3/unbuffered_result.rb +8 -2
  92. data/lib/baza/{drivers → driver}/sqlite3_java.rb +13 -23
  93. data/lib/baza/{drivers → driver}/sqlite3_java/column.rb +0 -0
  94. data/lib/baza/{drivers → driver}/sqlite3_java/columns.rb +0 -0
  95. data/lib/baza/driver/sqlite3_java/commands.rb +2 -0
  96. data/lib/baza/{drivers → driver}/sqlite3_java/database.rb +0 -0
  97. data/lib/baza/{drivers → driver}/sqlite3_java/index.rb +0 -0
  98. data/lib/baza/{drivers → driver}/sqlite3_java/indexes.rb +0 -0
  99. data/lib/baza/{drivers → driver}/sqlite3_java/table.rb +0 -0
  100. data/lib/baza/{drivers → driver}/sqlite3_java/tables.rb +0 -0
  101. data/lib/baza/{drivers → driver}/sqlite3_java/unbuffered_result.rb +14 -9
  102. data/lib/baza/{drivers → driver}/sqlite3_rhodes.rb +6 -24
  103. data/lib/baza/errors.rb +2 -0
  104. data/lib/baza/idquery.rb +15 -8
  105. data/lib/baza/index.rb +7 -0
  106. data/lib/baza/jdbc_driver.rb +4 -16
  107. data/lib/baza/jdbc_result.rb +20 -12
  108. data/lib/baza/mysql_base_driver.rb +7 -7
  109. data/lib/baza/query_buffer.rb +20 -19
  110. data/lib/baza/row.rb +16 -16
  111. data/lib/baza/sql_queries.rb +3 -0
  112. data/lib/baza/sql_queries/generic_insert.rb +81 -0
  113. data/lib/baza/sql_queries/generic_update.rb +31 -0
  114. data/lib/baza/sql_queries/mysql_upsert.rb +52 -0
  115. data/lib/baza/sql_queries/mysql_upsert_duplicate_key.rb +57 -0
  116. data/lib/baza/sql_queries/non_atomic_upsert.rb +25 -0
  117. data/lib/baza/sql_queries/postgres_upsert_duplicate_key.rb +118 -0
  118. data/lib/baza/sql_queries/select.rb +170 -0
  119. data/lib/baza/sql_queries/sqlite_upsert_duplicate_key.rb +99 -0
  120. data/lib/baza/table.rb +35 -8
  121. data/spec/active_record/models/user.rb +3 -0
  122. data/spec/{cloner_spec.rb → baza/cloner_spec.rb} +0 -0
  123. data/spec/drivers/active_record_mysql2_spec.rb +5 -3
  124. data/spec/drivers/active_record_mysql_spec.rb +2 -1
  125. data/spec/drivers/active_record_pg_spec.rb +20 -0
  126. data/spec/drivers/active_record_sqlite3_spec.rb +2 -1
  127. data/spec/drivers/mysql2_spec.rb +1 -1
  128. data/spec/drivers/mysql_spec.rb +10 -10
  129. data/spec/drivers/pg_spec.rb +18 -0
  130. data/spec/drivers/sqlite3_spec.rb +7 -8
  131. data/spec/info_active_record_example.rb +1 -1
  132. data/spec/{info_active_record_mysql2.rb → info_active_record_mysql2_example.rb} +3 -2
  133. data/spec/info_active_record_mysql2_travis.rb +35 -0
  134. data/spec/{info_active_record_mysql.rb → info_active_record_mysql_example.rb} +5 -4
  135. data/spec/info_active_record_mysql_travis.rb +36 -0
  136. data/spec/info_active_record_pg_example.rb +36 -0
  137. data/spec/info_active_record_pg_travis.rb +34 -0
  138. data/spec/info_active_record_sqlite3.rb +1 -1
  139. data/spec/info_mysql2_example.rb +1 -3
  140. data/spec/{info_mysql2_shippable.rb → info_mysql2_travis.rb} +2 -4
  141. data/spec/info_mysql_example.rb +1 -3
  142. data/spec/{info_mysql_shippable.rb → info_mysql_travis.rb} +2 -4
  143. data/spec/info_pg_example.rb +22 -0
  144. data/spec/info_pg_travis.rb +20 -0
  145. data/spec/info_sqlite3.rb +1 -3
  146. data/spec/spec_helper.rb +1 -1
  147. data/spec/support/driver_active_record_collection.rb +62 -0
  148. data/spec/support/driver_collection.rb +136 -121
  149. data/spec/support/driver_columns_collection.rb +19 -10
  150. data/spec/support/driver_databases_collection.rb +23 -1
  151. data/spec/support/driver_indexes_collection.rb +2 -2
  152. data/spec/support/driver_tables_collection.rb +24 -4
  153. data/spec/support/driver_users_collection.rb +53 -0
  154. metadata +185 -104
  155. data/lib/baza/drivers/mysql/database.rb +0 -28
  156. data/lib/baza/drivers/mysql/databases.rb +0 -35
  157. data/lib/baza/drivers/mysql_java/database.rb +0 -2
  158. data/lib/baza/drivers/mysql_java/databases.rb +0 -2
  159. data/lib/baza/model.rb +0 -875
  160. data/lib/baza/model_custom.rb +0 -155
  161. data/lib/baza/model_handler.rb +0 -910
  162. data/lib/baza/model_handler_sqlhelper.rb +0 -484
  163. data/lib/baza/revision.rb +0 -383
  164. data/shippable.yml +0 -17
  165. data/spec/info_active_record_mysql2_shippable.rb +0 -34
  166. data/spec/info_active_record_mysql_shippable.rb +0 -34
  167. data/spec/model_handler_spec.rb +0 -431
@@ -5,16 +5,17 @@ class Baza::Driver::Sqlite3::Column < Baza::Column
5
5
  # Constructor. This should not be called manually.
6
6
  def initialize(args)
7
7
  @args = args
8
- @db = @args[:db]
8
+ @data = args.fetch(:data)
9
+ @db = @args.fetch(:db)
9
10
  end
10
11
 
11
12
  # Returns the name of the column.
12
13
  def name
13
- @args[:data][:name]
14
+ @data.fetch(:name)
14
15
  end
15
16
 
16
17
  def table_name
17
- @args[:table_name]
18
+ @args.fetch(:table_name)
18
19
  end
19
20
 
20
21
  # Returns the columns table-object.
@@ -38,21 +39,20 @@ class Baza::Driver::Sqlite3::Column < Baza::Column
38
39
  # Returns the type of the column.
39
40
  def type
40
41
  unless @type
41
- if match = @args[:data][:type].match(/^([A-z]+)$/)
42
+ if (match = @data.fetch(:type).match(/^([A-z]+)$/))
42
43
  @maxlength = false
43
- type = match[0].to_sym
44
- elsif match = @args[:data][:type].match(/^decimal\((\d+),(\d+)\)$/)
44
+ type = match[0].downcase.to_sym
45
+ elsif (match = @data.fetch(:type).match(/^decimal\((\d+),(\d+)\)$/))
45
46
  @maxlength = "#{match[1]},#{match[2]}"
46
47
  type = :decimal
47
- elsif match = @args[:data][:type].match(/^enum\((.+)\)$/)
48
+ elsif (match = @data.fetch(:type).match(/^enum\((.+)\)$/))
48
49
  @maxlength = match[1]
49
50
  type = :enum
50
- elsif match = @args[:data][:type].match(/^(.+)\((\d+)\)$/)
51
+ elsif (match = @data.fetch(:type).match(/^(.+)\((\d+)\)$/))
51
52
  @maxlength = match[2]
52
53
  type = match[1].to_sym
53
- elsif @args[:data].key?(:type) && @args[:data][:type].to_s == ""
54
- # A type can actually be empty in SQLite... Wtf?
55
- return @args[:data][:type]
54
+ elsif @data.key?(:type) && @data.fetch(:type).to_s == ""
55
+ return @data[:type] # A type can actually be empty in SQLite... Wtf?
56
56
  end
57
57
 
58
58
  if type == :integer
@@ -61,7 +61,7 @@ class Baza::Driver::Sqlite3::Column < Baza::Column
61
61
  @type = type
62
62
  end
63
63
 
64
- raise "Still not type? (#{@args[:data]})" if @type.to_s.strip.empty?
64
+ raise "Still not type? (#{@data})" if @type.to_s.strip.empty?
65
65
  end
66
66
 
67
67
  @type
@@ -69,39 +69,37 @@ class Baza::Driver::Sqlite3::Column < Baza::Column
69
69
 
70
70
  # Returns true if the column allows null. Otherwise false.
71
71
  def null?
72
- return false if @args[:data][:notnull].to_i == 1
72
+ return false if @data.fetch(:notnull).to_i == 1
73
73
  true
74
74
  end
75
75
 
76
76
  # Returns the maxlength of the column.
77
77
  def maxlength
78
- type unless @maxlength
78
+ type unless @maxlength.nil?
79
79
  return @maxlength if @maxlength
80
80
  false
81
81
  end
82
82
 
83
83
  # Returns the default value of the column.
84
84
  def default
85
- def_val = @args[:data][:dflt_value]
85
+ def_val = @data.fetch(:dflt_value)
86
86
 
87
- if def_val && match = def_val.match(/\A'(.*)'\Z/)
87
+ if def_val && (match = def_val.match(/\A'(.*)'\Z/))
88
88
  return match[1]
89
89
  end
90
90
 
91
- return false if @args[:data][:dflt_value].to_s.empty?
91
+ return nil if @data.fetch(:dflt_value).to_s.empty?
92
92
  def_val
93
93
  end
94
94
 
95
95
  # Returns true if the column is the primary key.
96
96
  def primarykey?
97
- return false if @args[:data][:pk].to_i == 0
98
- true
97
+ @data.fetch(:pk).to_i == 1
99
98
  end
100
99
 
101
100
  # Returns true if the column is auto-increasing.
102
101
  def autoincr?
103
- return true if @args[:data][:pk].to_i == 1 && @args[:data][:type].to_s == "integer"
104
- false
102
+ primarykey? && @data.fetch(:type).casecmp("integer").zero?
105
103
  end
106
104
 
107
105
  # Drops the column from the table.
@@ -112,7 +110,7 @@ class Baza::Driver::Sqlite3::Column < Baza::Column
112
110
  def reload
113
111
  @db.q("PRAGMA table_info(`#{@db.escape_table(table_name)}`)") do |data|
114
112
  next unless data.fetch(:name) == name
115
- @args[:data] = data
113
+ @data = data
116
114
  @type = nil
117
115
  return nil
118
116
  end
@@ -134,13 +132,13 @@ class Baza::Driver::Sqlite3::Column < Baza::Column
134
132
  @type = nil
135
133
  @maxlength = nil
136
134
 
137
- new_table = table.copy(
135
+ table.copy(
138
136
  alter_columns: {
139
137
  name => newdata
140
138
  }
141
139
  )
142
140
 
143
- @args[:data][:name] = newdata.fetch(:name).to_s
141
+ @data[:name] = newdata.fetch(:name).to_s
144
142
  reload
145
143
  end
146
144
  end
@@ -4,10 +4,10 @@ class Baza::Driver::Sqlite3::Columns
4
4
 
5
5
  # Constructor. This should not be called manually.
6
6
  def initialize(args)
7
- @args = args
7
+ @db = args.fetch(:db)
8
8
  end
9
9
 
10
- DATA_SQL_ALLOWED_KEYS = [:name, :type, :maxlength, :autoincr, :primarykey, :null, :default, :default_func, :renames, :after, :renames]
10
+ DATA_SQL_ALLOWED_KEYS = [:name, :type, :maxlength, :autoincr, :primarykey, :null, :default, :default_func, :renames, :after, :renames].freeze
11
11
  # Returns SQL for a knjdb-compatible hash.
12
12
  def data_sql(data)
13
13
  data.each_key do |key|
@@ -24,10 +24,10 @@ class Baza::Driver::Sqlite3::Columns
24
24
 
25
25
  data[:maxlength] = 255 if type == :varchar && !data.key?(:maxlength)
26
26
  data[:maxlength] = 11 if type == :int && !data.key?(:maxlength) && !data[:autoincr] && !data[:primarykey]
27
- type = :integer if @args[:db].int_types.index(type) && (data[:autoincr] || data[:primarykey])
27
+ type = :integer if @db.int_types.index(type) && (data[:autoincr] || data[:primarykey])
28
28
 
29
- sql = "`#{data[:name]}` #{type}"
30
- sql << "(#{data[:maxlength]})" if data[:maxlength] && !data[:autoincr]
29
+ sql = "`#{data.fetch(:name)}` #{type}"
30
+ sql << "(#{data.fetch(:maxlength)})" if data[:maxlength] && !data[:autoincr]
31
31
  sql << " PRIMARY KEY" if data[:primarykey]
32
32
  sql << " AUTOINCREMENT" if data[:autoincr]
33
33
 
@@ -39,7 +39,7 @@ class Baza::Driver::Sqlite3::Columns
39
39
  if data.key?(:default_func)
40
40
  sql << " DEFAULT #{data[:default_func]}"
41
41
  elsif data.key?(:default) && data[:default] != false
42
- sql << " DEFAULT '#{@args[:db].escape(data[:default])}'"
42
+ sql << " DEFAULT #{@db.sqlval(data.fetch(:default))}"
43
43
  end
44
44
 
45
45
  sql
@@ -0,0 +1,28 @@
1
+ class Baza::Driver::Sqlite3::Commands
2
+ def initialize(args)
3
+ @db = args.fetch(:db)
4
+ end
5
+
6
+ def upsert_duplicate_key(table_name, updates, terms = {}, args = {})
7
+ Baza::SqlQueries::SqliteUpsertDuplicateKey.new({
8
+ db: @db,
9
+ table_name: table_name,
10
+ updates: updates,
11
+ terms: terms
12
+ }.merge(args)).execute
13
+ end
14
+
15
+ def upsert(table_name, updates, terms, args = {})
16
+ Baza::SqlQueries::NonAtomicUpsert.new(
17
+ db: @db,
18
+ table_name: table_name,
19
+ buffer: args[:buffer],
20
+ terms: terms,
21
+ updates: updates
22
+ ).execute
23
+ end
24
+
25
+ def last_id
26
+ @db.query("SELECT last_insert_rowid() AS id").fetch.fetch(:id).to_i
27
+ end
28
+ end
@@ -1,7 +1,6 @@
1
1
  class Baza::Driver::Sqlite3::Databases
2
2
  def initialize(args)
3
3
  @db = args.fetch(:db)
4
- raise "Db wasn't a baza object" unless @db.class.name.include?("Baza")
5
4
  end
6
5
 
7
6
  def [](name)
@@ -6,7 +6,7 @@ class Baza::Driver::Sqlite3::Result < Baza::ResultBase
6
6
 
7
7
  begin
8
8
  @statement.execute
9
- @type_translation = driver.baza.opts[:type_translation]
9
+ @type_translation = driver.db.opts[:type_translation]
10
10
  @types = statement.types if @type_translation == true
11
11
  @columns = statement.columns.map(&:to_sym)
12
12
  read_results
@@ -32,8 +32,14 @@ class Baza::Driver::Sqlite3::Result < Baza::ResultBase
32
32
 
33
33
  # Loops over every result yielding them.
34
34
  def each
35
- while data = fetch
36
- yield data
35
+ loop do
36
+ data = fetch
37
+
38
+ if data
39
+ yield data
40
+ else
41
+ break
42
+ end
37
43
  end
38
44
  end
39
45
 
@@ -50,12 +56,14 @@ private
50
56
  end
51
57
 
52
58
  def translate_type(value, type)
53
- return unless value
59
+ return if value.to_s.empty?
54
60
 
55
61
  if type == "datetime"
56
- Time.parse(value)
62
+ return nil if value == "NULL"
63
+ return Time.parse(value)
57
64
  elsif type == "date"
58
- Date.parse(value)
65
+ return nil if value == "NULL"
66
+ return Date.parse(value)
59
67
  else
60
68
  value
61
69
  end
@@ -87,7 +87,7 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
87
87
  end
88
88
 
89
89
  def columns
90
- @db.cols
90
+ @db.columns
91
91
  ret = []
92
92
 
93
93
  @db.query("PRAGMA table_info(`#{@db.escape_table(name)}`)") do |d_cols|
@@ -122,14 +122,14 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
122
122
  # if col_data.key?("after")
123
123
  # self.create_column_programmatic(col_data)
124
124
  # else
125
- @db.query("ALTER TABLE `#{name}` ADD COLUMN #{@db.cols.data_sql(col_data)};")
125
+ @db.query("ALTER TABLE `#{name}` ADD COLUMN #{@db.columns.data_sql(col_data)};")
126
126
  # end
127
127
  end
128
128
  end
129
129
 
130
130
  def create_column_programmatic(col_data)
131
131
  temp_name = "temptable_#{Time.now.to_f.to_s.hash}"
132
- cloned_tabled = clone(temp_name)
132
+ clone(temp_name)
133
133
  cols_cur = columns
134
134
  @db.query("DROP TABLE `#{name}`")
135
135
 
@@ -138,10 +138,10 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
138
138
  cols_cur.each do |name, col|
139
139
  sql << ", " unless first
140
140
  first = false if first
141
- sql << @db.cols.data_sql(col.data)
141
+ sql << @db.columns.data_sql(col.data)
142
142
 
143
143
  if col_data[:after] && col_data[:after] == name
144
- sql << ", #{@db.cols.data_sql(col_data)}"
144
+ sql << ", #{@db.columns.data_sql(col_data)}"
145
145
  end
146
146
  end
147
147
  sql << ");"
@@ -170,7 +170,7 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
170
170
  columns.each do |col|
171
171
  sql << ", " unless first
172
172
  first = false if first
173
- sql << @db.cols.data_sql(col.data)
173
+ sql << @db.columns.data_sql(col.data)
174
174
  end
175
175
  sql << ");"
176
176
 
@@ -182,7 +182,7 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
182
182
  indexes.each do |index|
183
183
  index_name = index.name.gsub(/\A#{Regexp.escape(name)}_/, "")
184
184
 
185
- if @db.opts[:index_append_table_name] && match = index_name.match(/\A(.+?)__(.+)\Z/)
185
+ if @db.opts[:index_append_table_name] && (match = index_name.match(/\A(.+?)__(.+)\Z/))
186
186
  index_name = match[2]
187
187
  else
188
188
  # Two indexes with the same name can't exist, and we are cloning, so we need to change the name
@@ -206,7 +206,7 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
206
206
 
207
207
  def copy(args = {})
208
208
  temp_name = "temptable_#{Time.now.to_f.to_s.hash}"
209
- cloned_tabled = clone(temp_name)
209
+ clone(temp_name)
210
210
  cols_cur = columns
211
211
  @db.query("DROP TABLE `#{name}`")
212
212
 
@@ -219,15 +219,15 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
219
219
  first = false if first
220
220
 
221
221
  if args.key?(:alter_columns) && args[:alter_columns][col.name]
222
- sql << @db.cols.data_sql(args[:alter_columns][col.name])
222
+ sql << @db.columns.data_sql(args[:alter_columns][col.name])
223
223
  else
224
- sql << @db.cols.data_sql(col.data)
224
+ sql << @db.columns.data_sql(col.data)
225
225
  end
226
226
 
227
227
  next unless args[:new]
228
228
  args[:new].each do |col_data|
229
229
  if col_data[:after] && col_data[:after] == col.name
230
- sql << ", #{@db.cols.data_sql(col_data)}"
230
+ sql << ", #{@db.columns.data_sql(col_data)}"
231
231
  end
232
232
  end
233
233
  end
@@ -333,13 +333,13 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
333
333
 
334
334
  sql = "CREATE"
335
335
  sql << " UNIQUE" if index_data[:unique]
336
- sql << " INDEX '#{@db.escape_column(index_name)}' ON `#{@db.escape_table(name)}` ("
336
+ sql << " INDEX #{@db.sep_index}#{@db.escape_column(index_name)}#{@db.sep_index} ON #{@db.sep_table}#{@db.escape_table(name)}#{@db.sep_table} ("
337
337
 
338
338
  first = true
339
339
  index_data.fetch(:columns).each do |col_name|
340
340
  sql << ", " unless first
341
341
  first = false if first
342
- sql << "`#{@db.escape_column(col_name)}`"
342
+ sql << "#{@db.sep_col}#{@db.escape_column(col_name)}#{@db.sep_col}"
343
343
  end
344
344
 
345
345
  sql << ")"
@@ -376,8 +376,8 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
376
376
  ret
377
377
  end
378
378
 
379
- def insert(data)
380
- @db.insert(name, data)
379
+ def insert(data, args = {})
380
+ @db.insert(name, data, args)
381
381
  end
382
382
 
383
383
  def to_s
@@ -392,6 +392,15 @@ private
392
392
 
393
393
  def parse_columns_from_sql(sql)
394
394
  columns_sql = sql.match(/\((.+?)\)\Z/)[1]
395
- columns_sql.split(",").map { |column| column.match(/`(.+)`/)[1] }
395
+
396
+ columns_sql.split(",").map do |column|
397
+ if (match = column.match(/`(.+)`/))
398
+ match[1]
399
+ elsif (match = column.match(/"(.+)"/))
400
+ match[1]
401
+ else
402
+ raise "Couldn't parse column part: #{column}"
403
+ end
404
+ end
396
405
  end
397
406
  end
@@ -14,9 +14,8 @@ class Baza::Driver::Sqlite3::Tables
14
14
  def [](table_name)
15
15
  table_name = table_name.to_s
16
16
 
17
- if ret = @list.get(table_name)
18
- return ret
19
- end
17
+ ret = @list.get(table_name)
18
+ return ret if ret
20
19
 
21
20
  list(name: table_name) do |table|
22
21
  return table if table.name == table_name
@@ -32,7 +31,7 @@ class Baza::Driver::Sqlite3::Tables
32
31
  tables_args = {type: "table"}
33
32
  tables_args[:name] = args.fetch(:name) if args[:name]
34
33
 
35
- q_tables = @db.select("sqlite_master", tables_args, orderby: "name") do |d_tables|
34
+ @db.select("sqlite_master", tables_args, orderby: "name") do |d_tables|
36
35
  table_name = d_tables.fetch(:name)
37
36
  next if table_name == "sqlite_sequence"
38
37
 
@@ -76,7 +75,7 @@ class Baza::Driver::Sqlite3::Tables
76
75
  @list[table.name] = table
77
76
  end
78
77
 
79
- CREATE_ALLOWED_KEYS = [:indexes, :columns]
78
+ CREATE_ALLOWED_KEYS = [:indexes, :columns].freeze
80
79
  def create(name, data, args = nil)
81
80
  data.each_key do |key|
82
81
  raise "Invalid key: '#{key}' (#{key.class.name})." unless CREATE_ALLOWED_KEYS.include?(key)
@@ -90,7 +89,7 @@ class Baza::Driver::Sqlite3::Tables
90
89
  data.fetch(:columns).each do |col_data|
91
90
  sql << ", " unless first
92
91
  first = false if first
93
- sql << @db.cols.data_sql(col_data)
92
+ sql << @db.columns.data_sql(col_data)
94
93
  end
95
94
 
96
95
  sql << ")"
@@ -19,8 +19,14 @@ class Baza::Driver::Sqlite3::UnbufferedResult
19
19
  end
20
20
 
21
21
  def each
22
- while data = fetch
23
- yield data
22
+ loop do
23
+ data = fetch
24
+
25
+ if data
26
+ yield data
27
+ else
28
+ break
29
+ end
24
30
  end
25
31
  end
26
32
 
@@ -1,18 +1,6 @@
1
1
  # This class handels SQLite3-specific behaviour.
2
2
  class Baza::Driver::Sqlite3Java < Baza::JdbcDriver
3
- path = "#{File.dirname(__FILE__)}/sqlite3_java"
4
-
5
- autoload :Database, "#{path}/database"
6
- autoload :Databases, "#{path}/databases"
7
- autoload :Table, "#{path}/table"
8
- autoload :Tables, "#{path}/tables"
9
- autoload :Column, "#{path}/column"
10
- autoload :Columns, "#{path}/columns"
11
- autoload :Index, "#{path}/index"
12
- autoload :Indexes, "#{path}/indexes"
13
- autoload :Result, "#{path}/result"
14
- autoload :Sqlspecs, "#{path}/sqlspecs"
15
- autoload :UnbufferedResult, "#{path}/unbuffered_result"
3
+ AutoAutoloader.autoload_sub_classes(self, __FILE__)
16
4
 
17
5
  attr_reader :mutex_statement_reader
18
6
 
@@ -29,15 +17,22 @@ class Baza::Driver::Sqlite3Java < Baza::JdbcDriver
29
17
  end
30
18
  end
31
19
 
20
+ def self.args
21
+ [{
22
+ label: "Path",
23
+ name: "path"
24
+ }]
25
+ end
26
+
32
27
  # Constructor. This should not be called manually.
33
- def initialize(baza_db)
28
+ def initialize(db)
34
29
  super
35
30
 
36
- @path = @baza.opts[:path] if @baza.opts[:path]
31
+ @path = @db.opts[:path] if @db.opts[:path]
37
32
  @preload_results = true
38
33
 
39
- if @baza.opts[:conn]
40
- @conn = @baza.opts[:conn]
34
+ if @db.opts[:conn]
35
+ @conn = @db.opts[:conn]
41
36
  else
42
37
  org.sqlite.JDBC
43
38
  reconnect
@@ -58,16 +53,11 @@ class Baza::Driver::Sqlite3Java < Baza::JdbcDriver
58
53
  string.to_s.gsub(/'/, "''")
59
54
  end
60
55
 
61
- # Returns the last inserted ID.
62
- def last_id
63
- query("SELECT LAST_INSERT_ROWID() AS id").fetch[:id].to_i
64
- end
65
-
66
56
  def transaction
67
57
  query_no_result_set("BEGIN TRANSACTION")
68
58
 
69
59
  begin
70
- yield @baza
60
+ yield @db
71
61
  query_no_result_set("COMMIT")
72
62
  rescue
73
63
  query_no_result_set("ROLLBACK")