baza 0.0.35 → 0.0.36

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +5 -5
  2. data/Gemfile +1 -1
  3. data/Gemfile.lock +14 -17
  4. data/VERSION +1 -1
  5. data/baza.gemspec +8 -7
  6. data/lib/baza/base_sql_driver.rb +38 -8
  7. data/lib/baza/cloner.rb +10 -0
  8. data/lib/baza/commands/select.rb +15 -8
  9. data/lib/baza/db.rb +8 -4
  10. data/lib/baza/driver/active_record.rb +7 -0
  11. data/lib/baza/driver/mysql/column.rb +4 -6
  12. data/lib/baza/driver/mysql/columns.rb +4 -4
  13. data/lib/baza/driver/mysql/database.rb +4 -4
  14. data/lib/baza/driver/mysql/databases.rb +3 -3
  15. data/lib/baza/driver/mysql/foreign_key.rb +2 -2
  16. data/lib/baza/driver/mysql/index.rb +1 -1
  17. data/lib/baza/driver/mysql/sql/column.rb +4 -4
  18. data/lib/baza/driver/mysql/sql/create_indexes.rb +3 -3
  19. data/lib/baza/driver/mysql/sql/create_table.rb +1 -1
  20. data/lib/baza/driver/mysql/table.rb +16 -16
  21. data/lib/baza/driver/mysql_java.rb +3 -3
  22. data/lib/baza/driver/pg/column.rb +5 -13
  23. data/lib/baza/driver/pg/columns.rb +4 -4
  24. data/lib/baza/driver/pg/create_index_sql_creator.rb +3 -3
  25. data/lib/baza/driver/pg/database.rb +4 -4
  26. data/lib/baza/driver/pg/databases.rb +1 -1
  27. data/lib/baza/driver/pg/foreign_key.rb +2 -2
  28. data/lib/baza/driver/pg/index.rb +1 -1
  29. data/lib/baza/driver/pg/table.rb +9 -9
  30. data/lib/baza/driver/pg/tables.rb +1 -1
  31. data/lib/baza/driver/sqlite3/column.rb +1 -1
  32. data/lib/baza/driver/sqlite3/columns.rb +1 -1
  33. data/lib/baza/driver/sqlite3/index.rb +1 -1
  34. data/lib/baza/driver/sqlite3/table.rb +4 -4
  35. data/lib/baza/driver/tiny.rb +83 -8
  36. data/lib/baza/driver/tiny/result.rb +18 -2
  37. data/lib/baza/idquery.rb +3 -3
  38. data/lib/baza/mysql_base_driver.rb +3 -3
  39. data/lib/baza/sql_queries/generic_insert.rb +3 -3
  40. data/lib/baza/sql_queries/generic_update.rb +2 -2
  41. data/lib/baza/sql_queries/mysql_upsert.rb +5 -5
  42. data/lib/baza/sql_queries/mysql_upsert_duplicate_key.rb +3 -3
  43. data/lib/baza/sql_queries/postgres_upsert_duplicate_key.rb +7 -7
  44. data/lib/baza/sql_queries/select.rb +8 -8
  45. data/lib/baza/sql_queries/sqlite_upsert_duplicate_key.rb +6 -6
  46. data/lib/baza/table.rb +3 -3
  47. data/spec/baza/cloner_spec.rb +2 -2
  48. data/spec/drivers/tiny_spec.rb +27 -0
  49. data/spec/info_active_record_pg_peak_flow.rb +2 -2
  50. data/spec/info_active_record_sqlite3.rb +6 -13
  51. data/spec/info_sqlite3.rb +3 -12
  52. metadata +8 -7
@@ -46,7 +46,7 @@ class Baza::Driver::Pg::Tables < Baza::Tables
46
46
 
47
47
  create_table_sql = "CREATE"
48
48
  create_table_sql << " TEMPORARY" if data[:temp]
49
- create_table_sql << " TABLE #{db.sep_table}#{db.escape_table(table_name)}#{db.sep_table} ("
49
+ create_table_sql << " TABLE #{db.quote_table(table_name)} ("
50
50
 
51
51
  first = true
52
52
  data.fetch(:columns).each do |col_data|
@@ -108,7 +108,7 @@ class Baza::Driver::Sqlite3::Column < Baza::Column
108
108
  end
109
109
 
110
110
  def reload
111
- @db.q("PRAGMA table_info(`#{@db.escape_table(table_name)}`)") do |data|
111
+ @db.q("PRAGMA table_info(#{@db.quote_table(table_name)})") do |data|
112
112
  next unless data.fetch(:name) == name
113
113
  @data = data
114
114
  @type = nil
@@ -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 #{@db.sqlval(data.fetch(:default))}"
42
+ sql << " DEFAULT #{@db.quote_value(data.fetch(:default))}"
43
43
  end
44
44
 
45
45
  sql
@@ -53,7 +53,7 @@ class Baza::Driver::Sqlite3::Index < Baza::Index
53
53
 
54
54
  def reload
55
55
  data = nil
56
- @db.query("PRAGMA index_list(`#{@db.escape_table(name)}`)") do |d_indexes|
56
+ @db.query("PRAGMA index_list(#{@db.quote_table(name)})") do |d_indexes|
57
57
  next unless d_indexes.fetch(:name) == name
58
58
  data = d_indexes
59
59
  break
@@ -90,7 +90,7 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
90
90
  @db.columns
91
91
  ret = []
92
92
 
93
- @db.query("PRAGMA table_info(`#{@db.escape_table(name)}`)") do |d_cols|
93
+ @db.query("PRAGMA table_info(#{@db.quote_table(name)})") do |d_cols|
94
94
  column_name = d_cols.fetch(:name)
95
95
  obj = @list.get(column_name)
96
96
 
@@ -283,7 +283,7 @@ class Baza::Driver::Sqlite3::Table < Baza::Table
283
283
  @db.indexes
284
284
  ret = [] unless block_given?
285
285
 
286
- @db.query("PRAGMA index_list(`#{@db.escape_table(name)}`)") do |d_indexes|
286
+ @db.query("PRAGMA index_list(#{@db.quote_table(name)})") do |d_indexes|
287
287
  next if d_indexes[:Key_name] == "PRIMARY"
288
288
  obj = @indexes_list.get(d_indexes[:name])
289
289
 
@@ -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.sep_index}#{@db.escape_column(index_name)}#{@db.sep_index} ON #{@db.sep_table}#{@db.escape_table(name)}#{@db.sep_table} ("
336
+ sql << " INDEX #{@db.quote_index(index_name)} ON #{@db.quote_table(name)} ("
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.sep_col}#{@db.escape_column(col_name)}#{@db.sep_col}"
342
+ sql << @db.quote_column(col_name)
343
343
  end
344
344
 
345
345
  sql << ")"
@@ -1,22 +1,19 @@
1
1
  class Baza::Driver::Tiny < Baza::BaseSqlDriver
2
- SEPARATOR_DATABASE = "`".freeze
3
- SEPARATOR_TABLE = "".freeze
4
- SEPARATOR_COLUMN = "".freeze
2
+ SEPARATOR_DATABASE = "[".freeze
3
+ SEPARATOR_TABLE = "[".freeze
4
+ SEPARATOR_COLUMN = "[".freeze
5
+ SEPARATOR_INDEX = "[".freeze
5
6
  SEPARATOR_VALUE = "'".freeze
6
- SEPARATOR_INDEX = "`".freeze
7
7
 
8
8
  def initialize(db)
9
- super
10
-
11
9
  @sep_database = SEPARATOR_DATABASE
12
10
  @sep_table = SEPARATOR_TABLE
13
11
  @sep_col = SEPARATOR_COLUMN
14
12
  @sep_val = SEPARATOR_VALUE
15
13
  @sep_index = SEPARATOR_INDEX
16
- end
17
14
 
18
- def initialize(db)
19
15
  super
16
+
20
17
  @client = TinyTds::Client.new(username: db.opts.fetch(:user), password: db.opts.fetch(:pass), host: db.opts.fetch(:host))
21
18
  end
22
19
 
@@ -24,6 +21,48 @@ class Baza::Driver::Tiny < Baza::BaseSqlDriver
24
21
  @client.close
25
22
  end
26
23
 
24
+ def escape(value)
25
+ @client.escape(value)
26
+ end
27
+
28
+ def self.escape_identifier(string)
29
+ string = string.to_s
30
+ raise "Invalid column-string: #{string}" if string.include?("[") || string.include?("]")
31
+ string
32
+ end
33
+
34
+ def self.escape_database(name)
35
+ escape_identifier(name)
36
+ end
37
+
38
+ def self.escape_column(name)
39
+ escape_identifier(name)
40
+ end
41
+
42
+ def self.escape_index(name)
43
+ escape_identifier(name)
44
+ end
45
+
46
+ def self.escape_table(name)
47
+ escape_identifier(name)
48
+ end
49
+
50
+ def escape_database(name)
51
+ self.class.escape_identifier(name)
52
+ end
53
+
54
+ def escape_column(name)
55
+ self.class.escape_identifier(name)
56
+ end
57
+
58
+ def escape_index(name)
59
+ self.class.escape_identifier(name)
60
+ end
61
+
62
+ def escape_table(name)
63
+ self.class.escape_identifier(name)
64
+ end
65
+
27
66
  def insert(table_name, data, args = {})
28
67
  sql = Baza::SqlQueries::GenericInsert.new({
29
68
  db: @db,
@@ -39,4 +78,40 @@ class Baza::Driver::Tiny < Baza::BaseSqlDriver
39
78
  result = @client.execute(sql)
40
79
  Baza::Driver::Tiny::Result.new(result)
41
80
  end
81
+
82
+ def self.quote_identifier(name)
83
+ "[#{escape_database(name)}]"
84
+ end
85
+
86
+ def self.quote_database(database_name)
87
+ quote_identifier(database_name)
88
+ end
89
+
90
+ def self.quote_column(column_name)
91
+ quote_identifier(column_name)
92
+ end
93
+
94
+ def self.quote_index(index_name)
95
+ quote_identifier(index_name)
96
+ end
97
+
98
+ def self.quote_table(table_name)
99
+ quote_identifier(table_name)
100
+ end
101
+
102
+ def quote_database(database_name)
103
+ self.class.quote_identifier(database_name)
104
+ end
105
+
106
+ def quote_column(column_name)
107
+ self.class.quote_identifier(column_name)
108
+ end
109
+
110
+ def quote_index(index_name)
111
+ self.class.quote_identifier(index_name)
112
+ end
113
+
114
+ def quote_table(table_name)
115
+ self.class.quote_identifier(table_name)
116
+ end
42
117
  end
@@ -3,7 +3,23 @@ class Baza::Driver::Tiny::Result < Baza::ResultBase
3
3
  @result = result.to_a
4
4
  end
5
5
 
6
- def each(*args, &blk)
7
- @result.each(*args, &blk)
6
+ def each(&blk)
7
+ enum.each(&blk)
8
+ end
9
+
10
+ def fetch
11
+ enum.next
12
+ rescue StopIteration
13
+ nil
14
+ end
15
+
16
+ private
17
+
18
+ def enum
19
+ @enum ||= Enumerator.new do |yielder|
20
+ @result.each do |result|
21
+ yielder << result
22
+ end
23
+ end
8
24
  end
9
25
  end
@@ -85,8 +85,8 @@ private
85
85
 
86
86
  # Spawns a new database-result to read from.
87
87
  def new_res
88
- table_esc = "#{@db.sep_table}#{@db.escape_table(@args[:table])}#{@db.sep_table}"
89
- col_esc = "#{@db.sep_col}#{@db.escape_column(@args[:col])}#{@db.sep_col}"
88
+ table_esc = @db.quote_table(@args.fetch(:table))
89
+ col_esc = @db.quote_column(@args.fetch(:col))
90
90
  ids = @ids.shift(@args[:size])
91
91
 
92
92
  if ids.empty?
@@ -94,7 +94,7 @@ private
94
94
  return nil
95
95
  end
96
96
 
97
- ids_sql = ids.map { |id| "#{@db.sep_val}#{@db.esc(id)}#{@db.sep_val}" }.join(",")
97
+ ids_sql = ids.map { |id| @db.quote_value(id) }.join(",")
98
98
  query_str = "SELECT * FROM #{table_esc} WHERE #{table_esc}.#{col_esc} IN (#{ids_sql})"
99
99
  puts "Query: #{query_str}" if @debug
100
100
 
@@ -37,7 +37,7 @@ class Baza::MysqlBaseDriver < Baza::BaseSqlDriver
37
37
  keys.each do |col_name|
38
38
  sql << "," unless first
39
39
  first = false if first
40
- sql << "`#{escape_column(col_name)}`"
40
+ sql << quote_column(col_name)
41
41
  end
42
42
 
43
43
  sql << ") VALUES ("
@@ -59,7 +59,7 @@ class Baza::MysqlBaseDriver < Baza::BaseSqlDriver
59
59
  sql << ","
60
60
  end
61
61
 
62
- sql << @db.sqlval(val)
62
+ sql << @db.quote_value(val)
63
63
  end
64
64
  else
65
65
  hash.each do |_key, val|
@@ -69,7 +69,7 @@ class Baza::MysqlBaseDriver < Baza::BaseSqlDriver
69
69
  sql << ","
70
70
  end
71
71
 
72
- sql << @db.sqlval(val)
72
+ sql << @db.quote_value(val)
73
73
  end
74
74
  end
75
75
  end
@@ -21,7 +21,7 @@ class Baza::SqlQueries::GenericInsert
21
21
  end
22
22
 
23
23
  def to_sql
24
- sql = "INSERT INTO #{@db.sep_table}#{@db.escape_table(@table_name)}#{@db.sep_table}"
24
+ sql = "INSERT INTO #{@db.quote_table(@table_name)}"
25
25
 
26
26
  if !@data || @data.empty?
27
27
  sql << " #{sql_default_values}"
@@ -55,7 +55,7 @@ private
55
55
  sql << ", "
56
56
  end
57
57
 
58
- sql << "#{@db.sep_col}#{@db.escape_column(key)}#{@db.sep_col}"
58
+ sql << @db.quote_column(key)
59
59
  end
60
60
 
61
61
  sql << ")"
@@ -73,7 +73,7 @@ private
73
73
  sql << ", "
74
74
  end
75
75
 
76
- quoted = @db.sqlval(value)
76
+ quoted = @db.quote_value(value)
77
77
  quoted = convert_line_breaks(quoted) if @replace_line_breaks
78
78
 
79
79
  sql << quoted
@@ -16,13 +16,13 @@ class Baza::SqlQueries::GenericUpdate
16
16
  end
17
17
 
18
18
  def to_sql
19
- sql = "UPDATE #{@db.sep_col}#{@db.escape_table(@table_name)}#{@db.sep_col} SET "
19
+ sql = "UPDATE #{@db.quote_table(@table_name)} SET "
20
20
 
21
21
  first = true
22
22
  @data.each do |key, value|
23
23
  sql << ", " unless first
24
24
  first = false if first
25
- sql << "#{@db.sep_col}#{@db.escape_column(key)}#{@db.sep_col} = #{@db.sqlval(value)}"
25
+ sql << "#{@db.quote_column(key)} = #{@db.quote_value(value)}"
26
26
  end
27
27
 
28
28
  sql << " WHERE #{@db.sql_make_where(@terms)}" if @terms && !@terms.empty?
@@ -9,7 +9,7 @@ class Baza::SqlQueries::MysqlUpsert
9
9
  def execute
10
10
  procedure_name = "baza_upsert_#{SecureRandom.hex(5)}"
11
11
 
12
- sql = "CREATE PROCEDURE `#{@db.escape_table(procedure_name)}` () BEGIN\n"
12
+ sql = "CREATE PROCEDURE #{@db.quote_table(procedure_name)} () BEGIN\n"
13
13
  sql << "\tIF EXISTS(#{select_query}) THEN\n"
14
14
  sql << "\t\t#{update_sql};\n"
15
15
  sql << "\tELSE\n"
@@ -20,9 +20,9 @@ class Baza::SqlQueries::MysqlUpsert
20
20
  @db.query(sql)
21
21
 
22
22
  begin
23
- @db.query("CALL `#{@db.escape_table(procedure_name)}`")
23
+ @db.query("CALL #{@db.quote_table(procedure_name)}")
24
24
  ensure
25
- @db.query("DROP PROCEDURE `#{@db.escape_table(procedure_name)}`")
25
+ @db.query("DROP PROCEDURE #{@db.quote_table(procedure_name)}")
26
26
  end
27
27
  end
28
28
 
@@ -30,13 +30,13 @@ private
30
30
 
31
31
  def select_query
32
32
  sql = ""
33
- sql << "SELECT * FROM #{@db.sep_table}#{@db.escape_table(@table_name)}#{@db.sep_table} WHERE"
33
+ sql << "SELECT * FROM #{@db.quote_table(@table_name)} WHERE"
34
34
 
35
35
  first = true
36
36
  @terms.each do |column, value|
37
37
  sql << " AND" unless first
38
38
  first = false if first
39
- sql << " #{@db.sep_col}#{@db.escape_column(column)}#{@db.sep_col} = #{@db.sqlval(value)}"
39
+ sql << " #{@db.quote_column(column)} = #{@db.quote_value(value)}"
40
40
  end
41
41
 
42
42
  sql
@@ -15,14 +15,14 @@ class Baza::SqlQueries::MysqlUpsertDuplicateKey
15
15
  first = true
16
16
 
17
17
  if @return_id
18
- sql << " #{@db.sep_col}#{@db.escape_column(primary_key_column_name)}#{@db.sep_col} = LAST_INSERT_ID(#{@db.sep_col}#{@db.escape_column(primary_key_column_name)}#{@db.sep_col})"
18
+ sql << " #{@db.quote_column(primary_key_column_name)} = LAST_INSERT_ID(#{@db.quote_column(primary_key_column_name)})"
19
19
  first = false
20
20
  end
21
21
 
22
22
  @updates.each do |key, value|
23
23
  sql << "," unless first
24
24
  first = false if first
25
- sql << " #{@db.sep_col}#{@db.escape_column(key)}#{@db.sep_col} = #{@db.sqlval(value)}"
25
+ sql << " #{@db.quote_column(key)} = #{@db.quote_value(value)}"
26
26
  end
27
27
 
28
28
  sql
@@ -52,6 +52,6 @@ private
52
52
  end
53
53
 
54
54
  def last_insert_sql
55
- "SELECT LAST_INSERT_ID() AS `id` FROM #{@db.sep_table}#{@db.escape_table(@table_name)}#{@db.sep_table}"
55
+ "SELECT LAST_INSERT_ID() AS #{@db.quote_column("id")} FROM #{@db.quote_table(@table_name)}"
56
56
  end
57
57
  end
@@ -65,14 +65,14 @@ private
65
65
  sql << ", " unless first
66
66
  first = false if first
67
67
 
68
- sql << "#{@db.sep_col}#{@db.escape_column(column_name)}#{@db.sep_col}"
68
+ sql << @db.quote_column(column_name)
69
69
  end
70
70
 
71
71
  sql
72
72
  end
73
73
 
74
74
  def insert_sql
75
- sql = "INSERT INTO #{@db.sep_table}#{@db.escape_table(@table_name)}#{@db.sep_table} ("
75
+ sql = "INSERT INTO #{@db.quote_table(@table_name)} ("
76
76
 
77
77
  combined_data = @updates.merge(@terms)
78
78
 
@@ -80,7 +80,7 @@ private
80
80
  combined_data.each_key do |column_name|
81
81
  sql << ", " unless first
82
82
  first = false if first
83
- sql << "#{@db.sep_col}#{@db.escape_column(column_name)}#{@db.sep_col}"
83
+ sql << @db.quote_column(column_name)
84
84
  end
85
85
 
86
86
  sql << ") VALUES ("
@@ -89,7 +89,7 @@ private
89
89
  combined_data.each_value do |value|
90
90
  sql << ", " unless first
91
91
  first = false if first
92
- sql << @db.sqlval(value).to_s
92
+ sql << @db.quote_value(value).to_s
93
93
  end
94
94
 
95
95
  sql << ")"
@@ -97,7 +97,7 @@ private
97
97
  end
98
98
 
99
99
  def update_sql
100
- "UPDATE #{@db.sep_table}#{@db.escape_table(@table_name)}#{@db.sep_table} #{update_set_sql} #{update_where_sql}"
100
+ "UPDATE #{@db.quote_table(@table_name)} #{update_set_sql} #{update_where_sql}"
101
101
  end
102
102
 
103
103
  def update_set_sql
@@ -107,7 +107,7 @@ private
107
107
  @updates.each do |key, value|
108
108
  sql << ", " unless first
109
109
  first = false if first
110
- sql << "#{@db.sep_col}#{@db.escape_column(key)}#{@db.sep_col} = #{@db.sqlval(value)}"
110
+ sql << "#{@db.quote_column(key)} = #{@db.quote_value(value)}"
111
111
  end
112
112
 
113
113
  sql
@@ -120,7 +120,7 @@ private
120
120
  @terms.each do |key, value|
121
121
  sql << " AND " unless first
122
122
  first = false if first
123
- sql << "#{@db.sep_col}#{@db.escape_column(key)}#{@db.sep_col} = #{@db.sqlval(value)}"
123
+ sql << "#{@db.quote_column(key)} = #{@db.quote_value(value)}"
124
124
  end
125
125
 
126
126
  sql
@@ -126,9 +126,9 @@ private
126
126
  first = false if first
127
127
 
128
128
  if select.is_a?(Symbol)
129
- select << " #{@db.sep_col}#{@db.escape_column(select)}#{@db.sep_col}"
129
+ select << " #{@db.quote_column(select)}"
130
130
  else
131
- select << @db.sqlval(select)
131
+ select << @db.quote_value(select)
132
132
  end
133
133
  end
134
134
  end
@@ -143,7 +143,7 @@ private
143
143
  @froms.each do |from|
144
144
  sql << "," unless first
145
145
  first = false if first
146
- sql << " #{@db.sep_table}#{@db.escape_table(from)}#{@db.sep_table}"
146
+ sql << " #{@db.quote_table(from)}"
147
147
  end
148
148
 
149
149
  sql
@@ -167,12 +167,12 @@ private
167
167
 
168
168
  if where.is_a?(Hash)
169
169
  where.each do |key, value|
170
- sql << "#{@db.sep_col}#{@db.escape_column(key)}#{@db.sep_col} = #{@db.sqlval(value)}"
170
+ sql << "#{@db.quote_column(key)} = #{@db.quote_value(value)}"
171
171
  end
172
172
  elsif where.is_a?(String)
173
173
  sql_arg = where.clone
174
174
  args.each do |arg|
175
- sql_arg.sub!("?", @db.sqlval(arg))
175
+ sql_arg.sub!("?", @db.quote_value(arg))
176
176
  end
177
177
 
178
178
  sql << sql_arg
@@ -192,10 +192,10 @@ private
192
192
  unless @count
193
193
  if @page
194
194
  @per_page ||= 30
195
- sql = "LIMIT #{@db.sqlval(@per_page)} OFFSET #{@per_page * (current_page - 1)}"
195
+ sql = "LIMIT #{@db.quote_value(@per_page)} OFFSET #{@per_page * (current_page - 1)}"
196
196
  elsif @limit
197
- sql = "LIMIT #{@db.sqlval(@limit)}"
198
- sql << ", #{@db.sqlval(@offset)}" if @offset
197
+ sql = "LIMIT #{@db.quote_value(@limit)}"
198
+ sql << ", #{@db.quote_value(@offset)}" if @offset
199
199
  end
200
200
  end
201
201