baza 0.0.19 → 0.0.20

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop_todo.yml +157 -0
  3. data/Gemfile +11 -9
  4. data/Gemfile.lock +41 -9
  5. data/README.md +1 -1
  6. data/Rakefile +19 -16
  7. data/VERSION +1 -1
  8. data/baza.gemspec +31 -7
  9. data/config/best_project_practice_rubocop.yml +2 -0
  10. data/config/best_project_practice_rubocop_todo.yml +157 -0
  11. data/lib/baza/base_sql_driver.rb +79 -20
  12. data/lib/baza/cloner.rb +2 -2
  13. data/lib/baza/column.rb +6 -0
  14. data/lib/baza/database.rb +33 -0
  15. data/lib/baza/database_model.rb +26 -0
  16. data/lib/baza/database_model_functionality.rb +10 -0
  17. data/lib/baza/database_model_name.rb +16 -0
  18. data/lib/baza/db.rb +210 -413
  19. data/lib/baza/dbtime.rb +20 -20
  20. data/lib/baza/driver.rb +3 -3
  21. data/lib/baza/drivers/active_record/columns.rb +1 -1
  22. data/lib/baza/drivers/active_record/indexes.rb +1 -1
  23. data/lib/baza/drivers/active_record/result.rb +3 -5
  24. data/lib/baza/drivers/active_record/tables.rb +2 -2
  25. data/lib/baza/drivers/active_record.rb +30 -16
  26. data/lib/baza/drivers/mysql/column.rb +51 -48
  27. data/lib/baza/drivers/mysql/columns.rb +8 -8
  28. data/lib/baza/drivers/mysql/database.rb +28 -0
  29. data/lib/baza/drivers/mysql/databases.rb +35 -0
  30. data/lib/baza/drivers/mysql/index.rb +32 -22
  31. data/lib/baza/drivers/mysql/result.rb +24 -24
  32. data/lib/baza/drivers/mysql/sqlspecs.rb +1 -1
  33. data/lib/baza/drivers/mysql/table.rb +80 -91
  34. data/lib/baza/drivers/mysql/tables.rb +30 -32
  35. data/lib/baza/drivers/mysql/unbuffered_result.rb +5 -5
  36. data/lib/baza/drivers/mysql.rb +127 -220
  37. data/lib/baza/drivers/mysql2/database.rb +2 -0
  38. data/lib/baza/drivers/mysql2/databases.rb +2 -0
  39. data/lib/baza/drivers/mysql2/result.rb +3 -5
  40. data/lib/baza/drivers/mysql2.rb +21 -106
  41. data/lib/baza/drivers/mysql_java/database.rb +2 -0
  42. data/lib/baza/drivers/mysql_java/databases.rb +2 -0
  43. data/lib/baza/drivers/mysql_java.rb +16 -12
  44. data/lib/baza/drivers/sqlite3/column.rb +33 -33
  45. data/lib/baza/drivers/sqlite3/columns.rb +6 -6
  46. data/lib/baza/drivers/sqlite3/database.rb +2 -0
  47. data/lib/baza/drivers/sqlite3/databases.rb +25 -0
  48. data/lib/baza/drivers/sqlite3/index.rb +20 -6
  49. data/lib/baza/drivers/sqlite3/result.rb +16 -17
  50. data/lib/baza/drivers/sqlite3/sqlspecs.rb +1 -1
  51. data/lib/baza/drivers/sqlite3/table.rb +99 -100
  52. data/lib/baza/drivers/sqlite3/tables.rb +30 -23
  53. data/lib/baza/drivers/sqlite3/unbuffered_result.rb +2 -2
  54. data/lib/baza/drivers/sqlite3.rb +27 -14
  55. data/lib/baza/drivers/sqlite3_java/database.rb +2 -0
  56. data/lib/baza/drivers/sqlite3_java/unbuffered_result.rb +2 -2
  57. data/lib/baza/drivers/sqlite3_java.rb +12 -10
  58. data/lib/baza/drivers/sqlite3_rhodes.rb +9 -7
  59. data/lib/baza/dump.rb +55 -55
  60. data/lib/baza/errors.rb +5 -0
  61. data/lib/baza/idquery.rb +25 -25
  62. data/lib/baza/index.rb +6 -0
  63. data/lib/baza/jdbc_driver.rb +6 -8
  64. data/lib/baza/jdbc_result.rb +1 -1
  65. data/lib/baza/model.rb +299 -309
  66. data/lib/baza/model_custom.rb +74 -72
  67. data/lib/baza/model_handler.rb +244 -255
  68. data/lib/baza/model_handler_sqlhelper.rb +122 -132
  69. data/lib/baza/mysql_base_driver.rb +112 -0
  70. data/lib/baza/query_buffer.rb +23 -23
  71. data/lib/baza/result_base.rb +2 -2
  72. data/lib/baza/revision.rb +67 -67
  73. data/lib/baza/row.rb +82 -84
  74. data/lib/baza/sqlspecs.rb +1 -1
  75. data/lib/baza/table.rb +31 -0
  76. data/lib/baza.rb +43 -10
  77. data/shippable.yml +1 -0
  78. data/spec/cloner_spec.rb +4 -4
  79. data/spec/drivers/active_record_mysql2_spec.rb +3 -3
  80. data/spec/drivers/active_record_mysql_spec.rb +2 -2
  81. data/spec/drivers/active_record_sqlite3_spec.rb +2 -2
  82. data/spec/drivers/mysql2_spec.rb +1 -0
  83. data/spec/drivers/mysql_spec.rb +1 -0
  84. data/spec/drivers/sqlite3_spec.rb +4 -5
  85. data/spec/info_active_record_example.rb +4 -6
  86. data/spec/info_active_record_mysql.rb +6 -7
  87. data/spec/info_active_record_mysql2.rb +2 -4
  88. data/spec/info_active_record_mysql2_shippable.rb +2 -4
  89. data/spec/info_active_record_mysql_shippable.rb +2 -4
  90. data/spec/info_active_record_sqlite3.rb +5 -6
  91. data/spec/info_mysql2_example.rb +1 -1
  92. data/spec/info_mysql2_shippable.rb +1 -1
  93. data/spec/info_mysql_example.rb +1 -1
  94. data/spec/info_mysql_shippable.rb +1 -1
  95. data/spec/info_sqlite3.rb +4 -3
  96. data/spec/model_handler_spec.rb +102 -115
  97. data/spec/spec_helper.rb +5 -5
  98. data/spec/support/driver_collection.rb +98 -95
  99. data/spec/support/driver_columns_collection.rb +18 -22
  100. data/spec/support/driver_databases_collection.rb +29 -0
  101. data/spec/support/driver_indexes_collection.rb +7 -6
  102. data/spec/support/driver_tables_collection.rb +38 -26
  103. metadata +49 -3
@@ -0,0 +1,157 @@
1
+
2
+
3
+ # This configuration was generated by
4
+ # `rubocop --auto-gen-config`
5
+ # on 2015-12-20 21:21:20 +0100 using RuboCop version 0.35.1.
6
+ # The point is for the user to remove these configuration records
7
+ # one by one as the offenses are removed from the code base.
8
+ # Note that changes in the inspected code, or installation of new
9
+ # versions of RuboCop, may require this file to be generated again.
10
+
11
+ # Offense count: 43
12
+ # Configuration parameters: AllowSafeAssignment.
13
+ Lint/AssignmentInCondition:
14
+ Enabled: false
15
+
16
+ # Offense count: 1
17
+ Lint/ConditionPosition:
18
+ Exclude:
19
+ - 'lib/baza/model_handler.rb'
20
+
21
+ # Offense count: 2
22
+ Lint/HandleExceptions:
23
+ Exclude:
24
+ - 'spec/model_handler_spec.rb'
25
+
26
+ # Offense count: 1
27
+ Lint/NonLocalExitFromIterator:
28
+ Exclude:
29
+ - 'lib/baza/drivers/sqlite3/column.rb'
30
+
31
+ # Offense count: 8
32
+ Lint/ShadowingOuterLocalVariable:
33
+ Exclude:
34
+ - 'lib/baza/drivers/mysql/table.rb'
35
+ - 'lib/baza/drivers/sqlite3/table.rb'
36
+ - 'lib/baza/model_handler.rb'
37
+ - 'lib/baza/model_handler_sqlhelper.rb'
38
+ - 'lib/baza/revision.rb'
39
+ - 'spec/model_handler_spec.rb'
40
+
41
+ # Offense count: 1
42
+ Lint/UselessAccessModifier:
43
+ Exclude:
44
+ - 'lib/baza/model.rb'
45
+
46
+ # Offense count: 34
47
+ Lint/UselessAssignment:
48
+ Enabled: false
49
+
50
+ # Offense count: 51
51
+ Metrics/AbcSize:
52
+ Max: 558
53
+
54
+ # Offense count: 22
55
+ Metrics/BlockNesting:
56
+ Max: 6
57
+
58
+ # Offense count: 7
59
+ # Configuration parameters: CountComments.
60
+ Metrics/ClassLength:
61
+ Max: 684
62
+
63
+ # Offense count: 67
64
+ Metrics/CyclomaticComplexity:
65
+ Max: 128
66
+
67
+ # Offense count: 31
68
+ # Configuration parameters: AllowURI, URISchemes.
69
+ Metrics/LineLength:
70
+ Max: 311
71
+
72
+ # Offense count: 8
73
+ # Configuration parameters: CountComments.
74
+ Metrics/MethodLength:
75
+ Max: 382
76
+
77
+ # Offense count: 66
78
+ Metrics/PerceivedComplexity:
79
+ Max: 160
80
+
81
+ # Offense count: 1
82
+ Style/AccessorMethodName:
83
+ Exclude:
84
+ - 'lib/baza/db.rb'
85
+
86
+ # Offense count: 1
87
+ Style/AsciiComments:
88
+ Exclude:
89
+ - 'lib/baza/db.rb'
90
+
91
+ # Offense count: 1
92
+ Style/CaseEquality:
93
+ Exclude:
94
+ - 'lib/baza/drivers/mysql/result.rb'
95
+
96
+ # Offense count: 3
97
+ Style/ClassVars:
98
+ Exclude:
99
+ - 'lib/baza/model.rb'
100
+ - 'lib/baza/model_custom.rb'
101
+
102
+ # Offense count: 1
103
+ Style/EndOfLine:
104
+ Exclude:
105
+ - 'lib/baza/drivers/mysql.rb'
106
+
107
+ # Offense count: 1
108
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
109
+ Style/FormatString:
110
+ Exclude:
111
+ - 'lib/baza/row.rb'
112
+
113
+ # Offense count: 11
114
+ # Configuration parameters: AllowedVariables.
115
+ Style/GlobalVars:
116
+ Exclude:
117
+ - 'lib/baza/db.rb'
118
+ - 'lib/baza/model_custom.rb'
119
+ - 'lib/baza/row.rb'
120
+
121
+ # Offense count: 16
122
+ # Configuration parameters: MinBodyLength.
123
+ Style/GuardClause:
124
+ Exclude:
125
+ - 'lib/baza/db.rb'
126
+ - 'lib/baza/drivers/mysql/result.rb'
127
+ - 'lib/baza/drivers/sqlite3/result.rb'
128
+ - 'lib/baza/drivers/sqlite3_java/unbuffered_result.rb'
129
+ - 'lib/baza/idquery.rb'
130
+ - 'lib/baza/model.rb'
131
+ - 'lib/baza/model_handler.rb'
132
+ - 'lib/baza/query_buffer.rb'
133
+ - 'lib/baza/row.rb'
134
+
135
+ # Offense count: 1
136
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
137
+ Style/MethodName:
138
+ Enabled: false
139
+
140
+ # Offense count: 9
141
+ # Configuration parameters: NamePrefix, NamePrefixBlacklist, NameWhitelist.
142
+ Style/PredicateName:
143
+ Exclude:
144
+ - 'lib/baza/model.rb'
145
+ - 'lib/baza/model_custom.rb'
146
+ - 'lib/baza/row.rb'
147
+
148
+ # Offense count: 2
149
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
150
+ Style/RaiseArgs:
151
+ Enabled: false
152
+
153
+ # Offense count: 1
154
+ # Cop supports --auto-correct.
155
+ # Configuration parameters: WordRegex.
156
+ Style/WordArray:
157
+ MinSize: 2
@@ -2,7 +2,7 @@ class Baza::BaseSqlDriver
2
2
  attr_reader :baza, :conn, :sep_table, :sep_col, :sep_val
3
3
  attr_accessor :tables, :cols, :indexes
4
4
 
5
- def self.from_object(args)
5
+ def self.from_object(_args)
6
6
  end
7
7
 
8
8
  def initialize(baza)
@@ -14,40 +14,95 @@ class Baza::BaseSqlDriver
14
14
  end
15
15
 
16
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}"
17
+ string.to_s.gsub(/([\0\n\r\032\'\"\\])/) do
18
+ case Regexp.last_match(1)
19
+ when "\0" then "\\0"
20
+ when "\n" then "\\n"
21
+ when "\r" then "\\r"
22
+ when "\032" then "\\Z"
23
+ else "\\#{Regexp.last_match(1)}"
24
24
  end
25
25
  end
26
26
  end
27
27
 
28
- alias esc escape
29
- alias escape_alternative escape
28
+ alias_method :esc, :escape
29
+ alias_method :escape_alternative, :escape
30
30
 
31
- #Escapes a string to be used as a column.
32
- def esc_col(string)
31
+ # Escapes a string to be used as a column.
32
+ def escape_column(string)
33
33
  string = string.to_s
34
- raise "Invalid column-string: #{string}" if string.index(@sep_col) != nil
35
- return string
34
+ raise "Invalid column-string: #{string}" unless string.index(@sep_col).nil?
35
+ string
36
36
  end
37
-
38
- alias esc_table esc_col
37
+ alias escape_table escape_column
38
+ alias escape_database escape_column
39
39
 
40
40
  def transaction
41
- query("BEGIN TRANSACTION")
41
+ @baza.q("BEGIN TRANSACTION")
42
42
 
43
43
  begin
44
44
  yield @baza
45
- query("COMMIT")
45
+ @baza.q("COMMIT")
46
46
  rescue => e
47
- query("ROLLBACK")
47
+ @baza.q("ROLLBACK")
48
48
  end
49
49
  end
50
50
 
51
+ # Simply inserts data into a table.
52
+ #
53
+ #===Examples
54
+ # db.insert(:users, name: "John", lastname: "Doe")
55
+ # id = db.insert(:users, {name: "John", lastname: "Doe"}, return_id: true)
56
+ # sql = db.insert(:users, {name: "John", lastname: "Doe"}, return_sql: true) #=> "INSERT INTO `users` (`name`, `lastname`) VALUES ('John', 'Doe')"
57
+ def insert(tablename, arr_insert, args = nil)
58
+ sql = "INSERT INTO #{@sep_table}#{escape_table(tablename)}#{@sep_table}"
59
+
60
+ if !arr_insert || arr_insert.empty?
61
+ # This is the correct syntax for inserting a blank row in MySQL.
62
+ if @baza.opts.fetch(:type).to_s.include?("mysql")
63
+ sql << " VALUES ()"
64
+ elsif @baza.opts.fetch(:type).to_s.include?("sqlite3")
65
+ sql << " DEFAULT VALUES"
66
+ else
67
+ raise "Unknown database-type: '#{@baza.opts.fetch(:type)}'."
68
+ end
69
+ else
70
+ sql << " ("
71
+
72
+ first = true
73
+ arr_insert.each_key do |key|
74
+ if first
75
+ first = false
76
+ else
77
+ sql << ", "
78
+ end
79
+
80
+ sql << "#{@baza.sep_col}#{@baza.escape_column(key)}#{@baza.sep_col}"
81
+ end
82
+
83
+ sql << ") VALUES ("
84
+
85
+ first = true
86
+ arr_insert.each_value do |value|
87
+ if first
88
+ first = false
89
+ else
90
+ sql << ", "
91
+ end
92
+
93
+ sql << @baza.sqlval(value)
94
+ end
95
+
96
+ sql << ")"
97
+ end
98
+
99
+ return sql if args && args[:return_sql]
100
+
101
+ @baza.query(sql)
102
+ return @baza.last_id if args && args[:return_id]
103
+ nil
104
+ end
105
+
51
106
  def insert_multi(tablename, arr_hashes, args = nil)
52
107
  sql = [] if args && args[:return_sql]
53
108
 
@@ -59,6 +114,10 @@ class Baza::BaseSqlDriver
59
114
  end
60
115
 
61
116
  return sql if args && args[:return_sql]
62
- return nil
117
+ nil
118
+ end
119
+
120
+ def supports_multiple_databases?
121
+ false
63
122
  end
64
123
  end
data/lib/baza/cloner.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  class Baza::Cloner
2
2
  def self.from_active_record_connection(connection)
3
- if connection.class.name.include?('Mysql2Adapter')
3
+ if connection.class.name.include?("Mysql2Adapter")
4
4
  connection = connection.instance_variable_get(:@connection)
5
5
 
6
6
  config = connection.instance_variable_get(:@query_options)
@@ -15,7 +15,7 @@ class Baza::Cloner
15
15
  }
16
16
 
17
17
  Baza::Db.new(db_args)
18
- elsif connection.class.name.include?('MysqlAdapter')
18
+ elsif connection.class.name.include?("MysqlAdapter")
19
19
  connection = connection.instance_variable_get(:@connection)
20
20
 
21
21
  db_args = {
data/lib/baza/column.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  class Baza::Column
2
+ include Baza::DatabaseModelFunctionality
3
+
2
4
  def to_s
3
5
  "#<#{self.class.name} name=\"#{name}\" type=\"#{type}\" maxlength=\"#{maxlength}\" autoincr=\"#{autoincr?}\" primarykey=\"#{primarykey?}\">"
4
6
  end
@@ -6,4 +8,8 @@ class Baza::Column
6
8
  def inspect
7
9
  to_s
8
10
  end
11
+
12
+ def to_param
13
+ name
14
+ end
9
15
  end
@@ -0,0 +1,33 @@
1
+ class Baza::Database
2
+ include Baza::DatabaseModelFunctionality
3
+
4
+ attr_reader :db, :driver, :name_was
5
+ attr_accessor :name
6
+
7
+ def initialize(args)
8
+ @db = args.fetch(:db)
9
+ @driver = args.fetch(:driver)
10
+ @name = args.fetch(:name)
11
+ @name_was = @name
12
+ end
13
+
14
+ def tables
15
+ ArrayEnumerator.new do |yielder|
16
+ @db.tables.list(database: name) do |table|
17
+ yielder << table
18
+ end
19
+ end
20
+ end
21
+
22
+ def table(name)
23
+ @db.tables[name]
24
+ end
25
+
26
+ def save!
27
+ raise Baza::Errors::NotImplemented
28
+ end
29
+
30
+ def to_param
31
+ name
32
+ end
33
+ end
@@ -0,0 +1,26 @@
1
+ # Used for link-methods in Rails apps
2
+ class Baza::DatabaseModel
3
+ def initialize(instance)
4
+ @instance = instance
5
+ end
6
+
7
+ def model_name
8
+ Baza::DatabaseModelName.new(@instance)
9
+ end
10
+
11
+ def persisted?
12
+ true
13
+ end
14
+
15
+ def to_param
16
+ if @instance.respond_to?(:to_param)
17
+ @instance.try(:to_param)
18
+ else
19
+ @instance.name
20
+ end
21
+ end
22
+
23
+ def id
24
+ @instance.name
25
+ end
26
+ end
@@ -0,0 +1,10 @@
1
+ # Used for link-methods in Rails apps
2
+ module Baza::DatabaseModelFunctionality
3
+ def to_model
4
+ Baza::DatabaseModel.new(self)
5
+ end
6
+
7
+ def model_name
8
+ to_model.model_name
9
+ end
10
+ end
@@ -0,0 +1,16 @@
1
+ # Used for link-methods in Rails apps
2
+ class Baza::DatabaseModelName
3
+ def initialize(instance)
4
+ @instance = instance
5
+ end
6
+
7
+ def singular_route_key
8
+ route_key = human.underscore
9
+ route_key = "indexes" if route_key == "indices"
10
+ route_key
11
+ end
12
+
13
+ def human
14
+ @instance.class.name.split("::").last
15
+ end
16
+ end