rmap 0.4.0 → 0.5.0

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.
@@ -0,0 +1,25 @@
1
+
2
+ load File.expand_path("../../rmap.gemspec", __FILE__)
3
+
4
+ require 'rmap'
5
+
6
+ create do
7
+ if ::Rmap::Database::exists?('rmap_test')
8
+ ::Rmap::Database::drop('rmap_test')
9
+ end
10
+ ::Rmap::Database::create('rmap_test')
11
+ end
12
+
13
+ set_up do
14
+ posts.add :title, :string
15
+ posts.add :body, :text
16
+
17
+ (1..10).each do |i|
18
+ posts.insert({:title => "Test title #{i}", :body => "Test body #{i}"})
19
+ end
20
+
21
+ end
22
+
23
+ tear_down do
24
+ ::Rmap::Database::drop('rmap_test')
25
+ end
@@ -0,0 +1,14 @@
1
+
2
+ posts.should_be_kind_of ::Rmap::Table
3
+
4
+ posts.should_have_method :find
5
+
6
+ posts.find(1).should_be_kind_of ::Rmap::Row
7
+
8
+ posts.find(1).id.should_equal(1)
9
+
10
+ posts.find(3423434).should_equal(nil)
11
+
12
+ row = posts.find(:title => "Test title 5")
13
+
14
+ row.id.should_equal(5)
@@ -0,0 +1,10 @@
1
+
2
+ posts.should_be_kind_of ::Rmap::Table
3
+
4
+ row = posts.find_by_title("Test title 3")
5
+
6
+ row.should_be_kind_of ::Rmap::Row
7
+
8
+ row.id.should_equal(3)
9
+
10
+ posts.find_by_title("Test title 3 asdda").should_equal(nil)
@@ -0,0 +1,8 @@
1
+
2
+ table('posts').should_be_kind_of ::Rmap::Table
3
+
4
+ table(:posts).should_be_kind_of ::Rmap::Table
5
+
6
+ table('posts').name.should_equal('posts')
7
+
8
+ table(:posts).name.should_equal('posts')
@@ -16,25 +16,8 @@ module Rmap
16
16
  Ripl.start :binding => db.bindings
17
17
  end
18
18
 
19
- def self.current_migration(options)
20
- if !db.table? :rmap_vars
21
- db.create :rmap_vars
22
- db.rmap_vars.add :key, :string
23
- db.rmap_vars.add :value, :binary
24
- end
25
-
26
- if db.rmap_vars.key_eq(:current_migration).count == 0
27
- db.rmap_vars.insert(:key => :current_migration, :value => 0)
28
- current_migration = 0
29
- else
30
- current_migration = db.rmap_vars.key_eq(:current_migration).first.value.to_i
31
- end
32
-
33
- if options[:echo] != false
34
- puts current_migration
35
- end
36
-
37
- current_migration
19
+ def self.current_migration
20
+ puts db.current_migration
38
21
  end
39
22
 
40
23
  module Generate
@@ -90,80 +73,21 @@ module Rmap
90
73
 
91
74
  end
92
75
 
93
-
94
76
  def self.migrate(options = {})
95
- db = Rmap::Commands::db
96
-
97
- current_migration = Rmap::Commands::current_migration(:echo => false)
98
-
99
- migrations = Dir.new("#{Rmap::CONF_ROOT}/migrations/").to_a.find_all{|file| ::File.file? "#{Rmap::CONF_ROOT}/migrations/#{file}" }.map{ |file| Rmap::Migration.new("#{Rmap::CONF_ROOT}/migrations/#{file}") }.sort {|l,r| l.schema_version <=> r.schema_version}
100
-
101
- if !options[:to].nil?
102
- to = options[:to].to_i
103
- found = false
104
- migrations.each do |migration|
105
- if migration.schema_version == to
106
- found = true
107
- break
108
- end
109
- end
110
-
111
- if !found
112
- raise "No such migration '#{to}' exists"
113
- end
114
-
115
- if to > current_migration
116
- migrations.each do |migration|
117
- if migration.schema_version <= current_migration
118
- next
119
- end
120
-
121
- if migration.schema_version <= to
122
- puts "up: #{migration.schema_version}"
123
- db.run &migration.up_block
124
- else
125
- break
126
- end
127
- end
128
- db.rmap_vars.key_eq(:current_migration).value = to.to_s
129
- elsif to < current_migration
130
- migrations.reverse.each do |migration|
131
- if migration.schema_version > current_migration
132
- next
133
- end
134
- if migration.schema_version > to
135
- puts "down: #{migration.schema_version}"
136
- db.run &migration.down_block
137
- else
138
- break
139
- end
140
- end
141
- db.rmap_vars.key_eq(:current_migration).value = to
142
- else
143
- raise "already at migration #{to}"
144
- end
145
-
146
- elsif migrations.count > 0
147
- migrations.each do |migration|
148
- if migration.schema_version > current_migration
149
- puts "up: #{migration.schema_version}"
150
- db.run &migration.up_block
151
- end
152
- end
153
- db.rmap_vars.key_eq(:current_migration).value = migrations.last.schema_version
154
- end
155
-
77
+ db = self.db
78
+ db.migrate(options)
79
+ puts "At migration: #{db.current_migration}"
156
80
  end
157
81
 
158
- private
159
-
160
- def self.db
161
- db = Rmap::Database.new
162
- if Rmap.const_defined? :CONF_ROOT
163
- db.run("#{Rmap::CONF_ROOT}/conf.rmap.rb")
82
+ private
83
+
84
+ def self.db
85
+ db = Rmap::Database.new
86
+ if Rmap.const_defined? :CONF_ROOT
87
+ db.run("#{Rmap::CONF_ROOT}/conf.rmap.rb")
88
+ end
89
+ db
164
90
  end
165
- db
166
- end
167
91
 
168
92
  end
169
93
  end
@@ -3,6 +3,27 @@ require 'mysql2'
3
3
 
4
4
  module Rmap
5
5
  class Database
6
+
7
+ def self.create(database, connection = {})
8
+ connection = {:host => "localhost", :username => "root", :password => ""}.merge connection
9
+ Mysql2::Client.new(connection).query("create database `#{database}`");
10
+ connection[:database] = database
11
+ self.new(connection)
12
+ end
13
+
14
+ def self.drop(database, connection = {})
15
+ connection = {:host => "localhost", :username => "root", :password => ""}.merge connection
16
+ Mysql2::Client.new(connection).query("drop database `#{database}`");
17
+ end
18
+
19
+ def self.list(connection = {})
20
+ connection = {:host => "localhost", :username => "root", :password => ""}.merge connection
21
+ Mysql2::Client.new(connection).query("show databases", :as => :array).map{|a| a[0]}
22
+ end
23
+
24
+ def self.exists?(database, connection = {})
25
+ list(connection).include? database.to_s
26
+ end
6
27
 
7
28
  def initialize(connection={}, &block)
8
29
  self.connection = connection
@@ -49,8 +70,12 @@ module Rmap
49
70
  table_names.include?(name.to_s)
50
71
  end
51
72
 
73
+ def table(name)
74
+ Table.new(self, name.to_s)
75
+ end
76
+
52
77
  def method_missing name, *args
53
- Table.new(self, name)
78
+ table(name)
54
79
  end
55
80
 
56
81
  def create(name)
@@ -77,5 +102,112 @@ module Rmap
77
102
  client.query("rollback")
78
103
  end
79
104
 
105
+ def current_migration
106
+ if !table? :rmap_vars
107
+ rmap_vars.add :key, :string
108
+ rmap_vars.add :value, :binary
109
+ end
110
+
111
+ if rmap_vars.key_eq(:current_migration).count == 0
112
+ rmap_vars.insert(:key => :current_migration, :value => 0)
113
+ 0
114
+ else
115
+ rmap_vars.key_eq(:current_migration).first.value.to_i
116
+ end
117
+ end
118
+
119
+ def migrate(options = {})
120
+ migrations_dir = !options[:migrations_dir].nil? ? (options[:migrations_dir].match(/\/\Z/) ? options[:migrations_dir] : "#{options[:migrations_dir]}/") : "#{Rmap::CONF_ROOT}/migrations/"
121
+
122
+ current_migration = self.current_migration
123
+
124
+ migrations = Dir.new(migrations_dir).to_a.find_all{|file| ::File.file? "#{migrations_dir}#{file}" }.map{ |file| Rmap::Migration.new("#{migrations_dir}#{file}") }.sort {|l,r| l.schema_version <=> r.schema_version}
125
+
126
+ if migrations.count == 0
127
+ raise "There are currently no migrations."
128
+ end
129
+
130
+ if options[:to].nil?
131
+ to = migrations.last.schema_version
132
+ if to == current_migration
133
+ raise "There are no are more migrations that can be applied."
134
+ end
135
+ elsif options[:to].to_s == 'previous'
136
+ if current_migration == 0
137
+ raise "No migrations have been applied."
138
+ elsif current_migration == migrations.first.schema_version
139
+ to = 0
140
+ else
141
+ migrations.each_with_index do |migration, i|
142
+ if current_migration == migration.schema_version
143
+ to = migrations[i - 1]
144
+ break
145
+ end
146
+ end
147
+ end
148
+ elsif options[:to].to_s == 'next'
149
+ if current_migration == 0
150
+ to = migrations.first.schema_version
151
+ elsif current_migration == migrations.last.schema_version
152
+ raise "There are no are more migrations that can be applied."
153
+ end
154
+ migrations.each_with_index do |migration, i|
155
+ if current_migration == migration.schema_version
156
+ to = migrations[i + 1]
157
+ break
158
+ end
159
+ end
160
+ else
161
+ found = false
162
+ to = options[:to].to_i
163
+
164
+ if to == current_migration
165
+ raise "Already at migration #{to}"
166
+ end
167
+
168
+ if to != 0
169
+ migrations.each do |migration, i|
170
+ if migration.schema_version == to
171
+ found = true
172
+ break
173
+ end
174
+ end
175
+
176
+ if !found
177
+ raise "No such migration '#{to}' exists"
178
+ end
179
+ end
180
+ end
181
+
182
+ if to > current_migration
183
+ migrations.each do |migration|
184
+ if migration.schema_version <= current_migration
185
+ next
186
+ end
187
+
188
+ if migration.schema_version <= to
189
+ run &migration.up_block
190
+ else
191
+ break
192
+ end
193
+ end
194
+ else
195
+ migrations.reverse.each do |migration|
196
+ if migration.schema_version > current_migration
197
+ next
198
+ end
199
+ if migration.schema_version > to
200
+ run &migration.down_block
201
+ else
202
+ break
203
+ end
204
+ end
205
+ end
206
+
207
+ rmap_vars.key_eq(:current_migration).value = to
208
+
209
+ self
210
+ end
211
+
80
212
  end
81
213
  end
@@ -49,7 +49,7 @@ module Rmap
49
49
  'second_ge' => lambda {|left,right| "second(#{left}) >= #{right}"},
50
50
  }
51
51
 
52
- attr_accessor :name
52
+ attr_accessor :name, :page
53
53
 
54
54
  def initialize(database, name)
55
55
  @database = database
@@ -58,6 +58,7 @@ module Rmap
58
58
  Table::BINARY_FILTER_METHODS.each {|name, block| @binary_filter_methods_args[name] = []}
59
59
  @join_list = []
60
60
  @order_by_list = []
61
+ @page = 1
61
62
  end
62
63
 
63
64
  Table::BINARY_FILTER_METHODS.each do |name,block|
@@ -89,6 +90,8 @@ module Rmap
89
90
  all.map{|row| row.fetch(name).first}
90
91
  elsif column?(name.to_s.sub(/=\Z/, "")) && name.match(/\A(.*)=\Z/)
91
92
  all.each{|row| row.update($1 => args[0])}
93
+ elsif name.match /\Afind_by_(.*)\Z/
94
+ eq($1, args[0]).first
92
95
  elsif name.match /\A(.*?)_(#{BINARY_FILTER_METHODS.keys.join('|')})\Z/
93
96
  @binary_filter_methods_args[$2].push([$1.split(/_or_/),args[0]])
94
97
  self
@@ -242,15 +245,11 @@ module Rmap
242
245
  end
243
246
 
244
247
  def generate_limit_sql
245
- if !@limit_row_count.nil?
246
- "limit #{@limit_offset}, #{@limit_row_count}"
247
- else
248
- ""
249
- end
248
+ @page_size.nil? ? "" : "limit #{(@page - 1) * @page_size}, #{@page_size}"
250
249
  end
251
250
 
252
- def generate_select_sql(expression_list_sql, without_limit_sql = false)
253
- "select #{format_sql(expression_list_sql)} #{generate_from_sql} #{generate_where_sql} #{generate_group_by_sql} #{generate_order_by_sql} #{without_limit_sql ? "" : generate_limit_sql}"
251
+ def generate_select_sql(expression_list_sql, without_limit = false)
252
+ "select #{format_sql(expression_list_sql)} #{generate_from_sql} #{generate_where_sql} #{generate_group_by_sql} #{generate_order_by_sql} #{without_limit ? "" : generate_limit_sql}"
254
253
  end
255
254
 
256
255
  def explain
@@ -261,15 +260,6 @@ module Rmap
261
260
  @database.client.query(generate_select_sql('id')).count
262
261
  end
263
262
 
264
- def page_count
265
- count_without_limit = @database.client.query(generate_select_sql('id', true)).count
266
- if count_without_limit > 0
267
- @page_size.nil? ? 1 : (count_with_limit.to_f / @page_size).ceil
268
- else
269
- 0
270
- end
271
- end
272
-
273
263
  def all
274
264
  out = []
275
265
  @database.client.query(generate_select_sql('id'), :as => :hash).each do |row|
@@ -300,9 +290,9 @@ module Rmap
300
290
  @database.client.last_id
301
291
  end
302
292
 
303
- def sum(sql_expression)
293
+ def sum(sql_expression, limit = nil)
304
294
  out = 0
305
- @database.client.query(generate_select_sql("sum(#{sql_expression})"), :as => :array).each{|row| out += row.first}
295
+ @database.client.query(generate_select_sql("sum(#{sql_expression})", limit), :as => :array).each{|row| out += row.first}
306
296
  out
307
297
  end
308
298
 
@@ -328,7 +318,6 @@ module Rmap
328
318
  @database.client.query("alter table `#{@name}` add `#{name}` int signed not null")
329
319
  when :foreign_key
330
320
  @database.client.query("alter table `#{@name}` add `#{name}` int unsigned not null")
331
- @database.client.query("alter table `#{@name}` add index(`#{name}`)")
332
321
  when :date
333
322
  @database.client.query("alter table `#{@name}` add `#{name}` date not null")
334
323
  when :datetime
@@ -338,6 +327,11 @@ module Rmap
338
327
  when :decimal
339
328
  @database.client.query("alter table `#{@name}` add `#{name}` decimal not null")
340
329
  end
330
+
331
+ if type == :foreign_key || options[:index] == true
332
+ @database.client.query("alter table `#{@name}` add index(`#{name}`)")
333
+ end
334
+
341
335
  end
342
336
 
343
337
  def remove(name)
@@ -362,12 +356,36 @@ module Rmap
362
356
  end
363
357
  end
364
358
 
365
- def paginate(page, page_size = 10)
366
- @limit_row_count = page_size
367
- @limit_offset = (page - 1) * page_size
359
+ def paginate(page_size = 10)
360
+ @page_size = page_size
361
+ self
362
+ end
363
+
364
+ def set_page(page)
365
+ @page = page.to_i
368
366
  self
369
367
  end
370
368
 
369
+ def page_count
370
+ count_without_limit = @database.client.query(generate_select_sql('id', true)).count
371
+ if !@page_size.nil?
372
+ (count_without_limit.to_f / @page_size).ceil
373
+ else
374
+ count_without_limit > 0 ? 1 : 0
375
+ end
376
+ end
377
+
378
+ def find(arg1)
379
+ if arg1.respond_to? :to_hash
380
+ arg1.to_hash.each do |k,v|
381
+ self.eq(k, v)
382
+ end
383
+ else
384
+ self.id_eq(arg1)
385
+ end
386
+ first
387
+ end
388
+
371
389
  private
372
390
 
373
391
  def to_singular(value)
@@ -1,3 +1,3 @@
1
1
  module Rmap
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rmap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-16 00:00:00.000000000Z
12
+ date: 2012-10-03 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mysql2
16
- requirement: &83687640 !ruby/object:Gem::Requirement
16
+ requirement: &76407690 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *83687640
24
+ version_requirements: *76407690
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: json
27
- requirement: &83684760 !ruby/object:Gem::Requirement
27
+ requirement: &76505030 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '1'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *83684760
35
+ version_requirements: *76505030
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: ripl
38
- requirement: &83684030 !ruby/object:Gem::Requirement
38
+ requirement: &76562470 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *83684030
46
+ version_requirements: *76562470
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: ripl-multi_line
49
- requirement: &83683360 !ruby/object:Gem::Requirement
49
+ requirement: &76625460 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *83683360
57
+ version_requirements: *76625460
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: powerline
60
- requirement: &83682550 !ruby/object:Gem::Requirement
60
+ requirement: &76720530 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *83682550
68
+ version_requirements: *76720530
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: gengin
71
- requirement: &83681920 !ruby/object:Gem::Requirement
71
+ requirement: &76819340 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *83681920
79
+ version_requirements: *76819340
80
80
  description:
81
81
  email:
82
82
  - jody@jodysalt.com
@@ -90,6 +90,10 @@ files:
90
90
  - README.markdown
91
91
  - Rakefile
92
92
  - bin/rmap
93
+ - features/conf.testbed.rb
94
+ - features/find.test.testbed.rb
95
+ - features/find_by.test.testbed.rb
96
+ - features/table.test.testbed.rb
93
97
  - lib/rmap.rb
94
98
  - lib/rmap/commands.rb
95
99
  - lib/rmap/database.rb