baza 0.0.13 → 0.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/include/dbtime.rb CHANGED
@@ -7,9 +7,9 @@ class Baza::Dbtime
7
7
  def initialize(args)
8
8
  args = {:time => args} if args.is_a?(String)
9
9
 
10
- raise "Invalid arguments given." if !args.is_a?(Hash)
10
+ raise "Invalid arguments given: #{args}" if !args.is_a?(Hash)
11
11
  raise "No time given." if !args[:time]
12
- raise "Invalid time given." if !args[:time].is_a?(String)
12
+ raise "Invalid time given: #{args[:time].class.name}" if !args[:time].is_a?(String)
13
13
 
14
14
  match = args[:time].match(/^(\d+):(\d+):(\d+)$/)
15
15
  raise "Could not understand time format." if !match
data/include/driver.rb ADDED
@@ -0,0 +1,9 @@
1
+ #Subclass that contains all the drivers as further subclasses.
2
+ class Baza::Driver
3
+ #Autoloader for drivers.
4
+ def self.const_missing(name)
5
+ require_relative "drivers/#{StringCases.camel_to_snake(name)}/#{StringCases.camel_to_snake(name)}.rb"
6
+ raise LoadError, "Still not loaded: '#{name}'." unless Baza::Driver.const_defined?(name)
7
+ return Baza::Driver.const_get(name)
8
+ end
9
+ end
@@ -1,26 +1,32 @@
1
1
  class Baza::Driver::ActiveRecord
2
2
  attr_reader :baza, :conn, :sep_table, :sep_col, :sep_val, :symbolize, :conn_type
3
3
  attr_accessor :tables, :cols, :indexes
4
-
4
+
5
5
  def self.from_object(args)
6
6
  if args[:object].class.name.include?("ActiveRecord::ConnectionAdapters")
7
+ if args[:object].class.name.include?("ConnectionPool")
8
+ object_to_use = args[:object].connection
9
+ else
10
+ object_to_use = args[:object]
11
+ end
12
+
7
13
  return {
8
- :type => :success,
9
- :args => {
10
- :type => :active_record,
11
- :conn => args[:object]
14
+ type: :success,
15
+ args: {
16
+ type: :active_record,
17
+ conn: object_to_use
12
18
  }
13
19
  }
14
20
  end
15
-
21
+
16
22
  return nil
17
23
  end
18
-
24
+
19
25
  def initialize(baza)
20
26
  @baza = baza
21
27
  @conn = @baza.opts[:conn]
22
28
  conn_name = @conn.class.name.to_s.downcase
23
-
29
+
24
30
  if conn_name.include?("mysql")
25
31
  @sep_table = "`"
26
32
  @sep_col = "`"
@@ -35,51 +41,82 @@ class Baza::Driver::ActiveRecord
35
41
  raise "Unknown type: '#{conn_name}'."
36
42
  end
37
43
  end
38
-
44
+
39
45
  def query(str)
40
- Baza::Driver::ActiveRecord::Result.new(self, @conn.execute(str))
46
+ Baza::Driver::ActiveRecord::Result.new(@conn.execute(str))
41
47
  end
42
-
48
+
43
49
  def escape(str)
44
50
  @conn.quote_string(str.to_s)
45
51
  end
46
-
52
+
47
53
  def esc_col(string)
48
54
  string = string.to_s
49
55
  raise "Invalid column-string: #{string}" if string.include?(@sep_col)
50
56
  return string
51
57
  end
52
-
58
+
53
59
  def esc_table(string)
54
60
  string = string.to_s
55
61
  raise "Invalid column-string: #{string}" if string.include?(@sep_col)
56
62
  return string
57
63
  end
64
+
65
+ def close
66
+ @conn.close
67
+ end
68
+
69
+ def transaction
70
+ if @conn_type == :mysql
71
+ query("START TRANSACTION")
72
+ elsif @conn_type == :sqlite3
73
+ query("BEGIN TRANSACTION")
74
+ end
75
+
76
+ begin
77
+ yield @baza
78
+ query("COMMIT")
79
+ rescue
80
+ query("ROLLBACK")
81
+ raise
82
+ end
83
+ end
58
84
  end
59
85
 
60
86
  class Baza::Driver::ActiveRecord::Result
61
- def initialize(db, res)
62
- @db = db
87
+ def initialize(res)
63
88
  @res = res
64
89
  end
65
-
66
- def each
67
- @res.each(:as => :hash) do |hash|
68
- yield hash.symbolize_keys!
90
+
91
+ def enum
92
+ @enum ||= Enumerator.new do |y|
93
+ @res.each(as: :hash) do |result|
94
+ y << result.symbolize_keys
95
+ end
69
96
  end
70
-
71
- nil
97
+ end
98
+
99
+ def fetch
100
+ begin
101
+ return enum.next
102
+ rescue StopIteration
103
+ return false
104
+ end
105
+ end
106
+
107
+ def each &blk
108
+ enum.each &blk
72
109
  end
73
110
  end
74
111
 
75
112
  class Baza::Driver::ActiveRecord::Tables
76
113
  def initialize(args)
77
114
  @args = args
78
-
115
+
79
116
  require_relative "../#{@args[:db].conn.conn_type}/#{@args[:db].conn.conn_type}_tables"
80
117
  @proxy_to = ::Baza::Driver.const_get(StringCases.snake_to_camel(@args[:db].conn.conn_type)).const_get(:Tables).new(@args)
81
118
  end
82
-
119
+
83
120
  def method_missing(name, *args, &blk)
84
121
  @proxy_to.__send__(name, *args, &blk)
85
122
  end
@@ -88,11 +125,11 @@ end
88
125
  class Baza::Driver::ActiveRecord::Columns
89
126
  def initialize(args)
90
127
  @args = args
91
-
128
+
92
129
  require_relative "../#{@args[:db].conn.conn_type}/#{@args[:db].conn.conn_type}_columns"
93
130
  @proxy_to = ::Baza::Driver.const_get(StringCases.snake_to_camel(@args[:db].conn.conn_type)).const_get(:Columns).new(@args)
94
131
  end
95
-
132
+
96
133
  def method_missing(name, *args, &blk)
97
134
  @proxy_to.__send__(name, *args, &blk)
98
135
  end
@@ -101,11 +138,11 @@ end
101
138
  class Baza::Driver::ActiveRecord::Indexes
102
139
  def initialize(args)
103
140
  @args = args
104
-
141
+
105
142
  require_relative "../#{@args[:db].conn.conn_type}/#{@args[:db].conn.conn_type}_indexes"
106
143
  @proxy_to = ::Baza::Driver.const_get(StringCases.snake_to_camel(@args[:db].conn.conn_type)).const_get(:Indexes).new(@args)
107
144
  end
108
-
145
+
109
146
  def method_missing(name, *args, &blk)
110
147
  @proxy_to.__send__(name, *args, &blk)
111
148
  end
@@ -4,36 +4,36 @@ class Baza::Driver::Mysql::Columns
4
4
  def initialize(args)
5
5
  @args = args
6
6
  end
7
-
7
+
8
8
  #Returns the SQL for this column.
9
9
  DATA_SQL_ALLOWED_KEYS = [:type, :maxlength, :name, :primarykey, :autoincr, :default, :comment, :after, :first, :storage, :null, :renames]
10
10
  def data_sql(data)
11
11
  data.each do |key, val|
12
12
  raise "Invalid key: '#{key}' (#{key.class.name})." if !DATA_SQL_ALLOWED_KEYS.include?(key)
13
13
  end
14
-
14
+
15
15
  raise "No type given." if !data[:type]
16
16
  type = data[:type].to_sym
17
-
17
+
18
18
  data[:maxlength] = 255 if type == :varchar and !data.key?(:maxlength)
19
-
19
+
20
20
  sql = "`#{data[:name]}` #{type}"
21
21
  sql << "(#{data[:maxlength]})" if data[:maxlength]
22
22
  sql << " PRIMARY KEY" if data[:primarykey]
23
23
  sql << " AUTO_INCREMENT" if data[:autoincr]
24
24
  sql << " NOT NULL" if !data[:null]
25
-
25
+
26
26
  if data.key?(:default_func)
27
27
  sql << " DEFAULT #{data[:default_func]}"
28
28
  elsif data.key?(:default) and data[:default] != false
29
29
  sql << " DEFAULT '#{@args[:db].escape(data[:default])}'"
30
30
  end
31
-
31
+
32
32
  sql << " COMMENT '#{@args[:db].escape(data[:comment])}'" if data.key?(:comment)
33
33
  sql << " AFTER `#{@args[:db].esc_col(data[:after])}`" if data[:after] and !data[:first]
34
34
  sql << " FIRST" if data[:first]
35
35
  sql << " STORAGE #{data[:storage].to_s.upcase}" if data[:storage]
36
-
36
+
37
37
  return sql
38
38
  end
39
39
  end
@@ -41,37 +41,37 @@ end
41
41
  #This class handels every MySQL-column, that can be returned from a table-object.
42
42
  class Baza::Driver::Mysql::Columns::Column
43
43
  attr_reader :args, :name
44
-
44
+
45
45
  #Constructor. Should not be called manually.
46
46
  def initialize(args)
47
47
  @args = args
48
48
  @name = @args[:data][:Field].to_sym
49
49
  @db = @args[:db]
50
50
  end
51
-
51
+
52
52
  #Used to validate in Knj::Wrap_map.
53
53
  def __object_unique_id__
54
54
  return @name
55
55
  end
56
-
56
+
57
57
  #Returns the table-object that this column belongs to.
58
58
  def table
59
59
  return @args[:db].tables[@args[:table_name]]
60
60
  end
61
-
61
+
62
62
  #Returns all data of the column in the knjdb-format.
63
63
  def data
64
64
  return {
65
- :type => self.type,
66
- :name => self.name,
67
- :null => self.null?,
68
- :maxlength => self.maxlength,
69
- :default => self.default,
70
- :primarykey => self.primarykey?,
71
- :autoincr => self.autoincr?
65
+ type: type,
66
+ name: name,
67
+ null: null?,
68
+ maxlength: maxlength,
69
+ default: default,
70
+ primarykey: primarykey?,
71
+ autoincr: autoincr?
72
72
  }
73
73
  end
74
-
74
+
75
75
  #Returns the type of the column (integer, varchar etc.).
76
76
  def type
77
77
  if !@type
@@ -88,26 +88,26 @@ class Baza::Driver::Mysql::Columns::Column
88
88
  @maxlength = match[2].to_i
89
89
  @type = match[1].to_sym
90
90
  end
91
-
91
+
92
92
  raise "Still not type from: '#{@args[:data][:Type]}'." if @type.to_s.strip.empty?
93
93
  end
94
-
94
+
95
95
  return @type
96
96
  end
97
-
97
+
98
98
  #Return true if the columns allows null. Otherwise false.
99
99
  def null?
100
100
  return false if @args[:data][:Null] == "NO"
101
101
  return true
102
102
  end
103
-
103
+
104
104
  #Returns the maxlength.
105
105
  def maxlength
106
106
  self.type if !@maxlength
107
107
  return @maxlength if @maxlength
108
108
  return false
109
109
  end
110
-
110
+
111
111
  #Returns the default value for the column.
112
112
  def default
113
113
  return false if (self.type == :datetime or self.type == :date) and @args[:data][:Default].to_s.strip.length <= 0
@@ -115,48 +115,48 @@ class Baza::Driver::Mysql::Columns::Column
115
115
  return false if !@args[:data][:Default]
116
116
  return @args[:data][:Default]
117
117
  end
118
-
118
+
119
119
  #Returns true if the column is the primary key. Otherwise false.
120
120
  def primarykey?
121
121
  return true if @args[:data][:Key] == "PRI"
122
122
  return false
123
123
  end
124
-
124
+
125
125
  #Returns true if the column is auto-increasing. Otherwise false.
126
126
  def autoincr?
127
127
  return true if @args[:data][:Extra].include?("auto_increment")
128
128
  return false
129
129
  end
130
-
130
+
131
131
  #Returns the comment for the column.
132
132
  def comment
133
133
  return @args[:data][:Comment]
134
134
  end
135
-
135
+
136
136
  #Drops the column from the table.
137
137
  def drop
138
138
  @args[:db].query("ALTER TABLE `#{@db.esc_table(@args[:table_name])}` DROP COLUMN `#{@db.esc_col(self.name)}`")
139
139
  table = self.table.remove_column_from_list(self)
140
140
  return nil
141
141
  end
142
-
142
+
143
143
  #Changes the column properties by the given hash.
144
144
  def change(data)
145
145
  col_escaped = "`#{@args[:db].esc_col(self.name)}`"
146
146
  table_escape = "`#{@args[:db].esc_table(self.table.name)}`"
147
147
  newdata = data.clone
148
-
148
+
149
149
  newdata[:name] = self.name if !newdata.key?(:name)
150
150
  newdata[:type] = self.type if !newdata.key?(:type)
151
151
  newdata[:maxlength] = self.maxlength if !newdata.key?(:maxlength) and self.maxlength
152
152
  newdata[:null] = self.null? if !newdata.key?(:null)
153
153
  newdata[:default] = self.default if !newdata.key?(:default) and self.default
154
154
  newdata.delete(:primarykey) if newdata.key?(:primarykey)
155
-
155
+
156
156
  drop_add = true if self.name.to_s != newdata[:name].to_s
157
-
157
+
158
158
  self.table.__send__(:remove_column_from_list, self) if drop_add
159
159
  @args[:db].query("ALTER TABLE #{table_escape} CHANGE #{col_escaped} #{@args[:db].cols.data_sql(newdata)}")
160
160
  self.table.__send__(:add_column_to_list, self) if drop_add
161
161
  end
162
- end
162
+ end
@@ -5,29 +5,29 @@ class Baza::Driver::Mysql::Indexes
5
5
  end
6
6
 
7
7
  class Baza::Driver::Mysql::Indexes::Index
8
- attr_reader :columns
9
-
8
+ attr_reader :args, :columns
9
+
10
10
  def initialize(args)
11
11
  @args = args
12
12
  @columns = []
13
13
  end
14
-
14
+
15
15
  #Used to validate in Knj::Wrap_map.
16
16
  def __object_unique_id__
17
17
  return @args[:data][:Key_name]
18
18
  end
19
-
19
+
20
20
  def name
21
21
  return @args[:data][:Key_name]
22
22
  end
23
-
23
+
24
24
  def table
25
25
  return @args[:db].tables[@args[:table_name]]
26
26
  end
27
-
27
+
28
28
  def drop
29
29
  sql = "DROP INDEX `#{self.name}` ON `#{self.table.name}`"
30
-
30
+
31
31
  begin
32
32
  @args[:db].query(sql)
33
33
  rescue => e
@@ -39,14 +39,24 @@ class Baza::Driver::Mysql::Indexes::Index
39
39
  end
40
40
  end
41
41
  end
42
-
42
+
43
+ def rename newname
44
+ newname = newname.to_sym
45
+ create_args = data
46
+ create_args[:name] = newname
47
+
48
+ drop
49
+ table.create_indexes([create_args])
50
+ @args[:data][:Key_name] = newname
51
+ end
52
+
43
53
  def data
44
54
  return {
45
- :name => name,
46
- :columns => @columns
55
+ name: name,
56
+ columns: @columns
47
57
  }
48
58
  end
49
-
59
+
50
60
  #Returns true if the index is a unique-index.
51
61
  def unique?
52
62
  if @args[:data][:Index_type] == "UNIQUE"
@@ -55,15 +65,14 @@ class Baza::Driver::Mysql::Indexes::Index
55
65
  return false
56
66
  end
57
67
  end
58
-
68
+
59
69
  #Returns true if the index is a primary-index.
60
70
  def primary?
61
- return true if @args[:data][:Index_type] == "BTREE"
62
- return false
71
+ return true if @args[:data][:Key_name] == "PRIMARY"
72
+ return false
63
73
  end
64
-
65
- #Inspect crashes if this is not present? - knj.
74
+
66
75
  def to_s
67
- return "#<#{self.class.name}>"
76
+ return "#<Baza::Driver::Mysql::Index name: \"#{name}\", columns: #{@columns}, primary: #{primary?}, unique: #{unique?}>"
68
77
  end
69
78
  end