baza 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ class Baza::Sqlspecs
2
+ def initialize(args)
3
+ @args = args
4
+ end
5
+ end
data/lib/baza.rb ADDED
@@ -0,0 +1,8 @@
1
+ class Baza
2
+ #Autoloader for subclasses.
3
+ def self.const_missing(name)
4
+ require "#{File.dirname(__FILE__)}/../include/#{name.to_s.downcase}.rb"
5
+ raise "Still not defined: '#{name}'." if !Baza.const_defined?(name)
6
+ return Baza.const_get(name)
7
+ end
8
+ end
data/spec/baza_spec.rb ADDED
@@ -0,0 +1,286 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Baza" do
4
+ it "should be able to handle various encodings" do
5
+ #I never got this test to actually fail... :-(
6
+
7
+ require "baza"
8
+ require "knjrbfw"
9
+ require "knj/os"
10
+ require "rubygems"
11
+ require "sqlite3" if !Kernel.const_defined?("SQLite3") and RUBY_ENGINE != "jruby"
12
+
13
+ db_path = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3.sqlite3"
14
+ File.unlink(db_path) if File.exists?(db_path)
15
+
16
+ db = Baza::Db.new(
17
+ :type => "sqlite3",
18
+ :path => db_path,
19
+ :return_keys => "symbols",
20
+ :index_append_table_name => true
21
+ )
22
+
23
+ db.tables.create("test", {
24
+ "columns" => [
25
+ {"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
26
+ {"name" => "text", "type" => "varchar"}
27
+ ]
28
+ })
29
+
30
+
31
+
32
+ #Get a list of tables and check the list for errors.
33
+ list = db.tables.list
34
+ raise "Table not found: 'test'." if !list.key?("test")
35
+ raise "Table-name expected to be 'test' but wasnt: '#{list["test"].name}'." if list["test"].name != "test"
36
+
37
+
38
+ #Test revision to create tables, indexes and insert rows.
39
+ schema = {
40
+ "tables" => {
41
+ "test_table" => {
42
+ "columns" => [
43
+ {"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
44
+ {"name" => "name", "type" => "varchar"},
45
+ {"name" => "age", "type" => "int"},
46
+ {"name" => "nickname", "type" => "varchar"}
47
+ ],
48
+ "indexes" => [
49
+ "name"
50
+ ],
51
+ "rows" => [
52
+ {
53
+ "find_by" => {"id" => 1},
54
+ "data" => {"id" => 1, "name" => "trala"}
55
+ }
56
+ ]
57
+ }
58
+ }
59
+ }
60
+
61
+ rev = Baza::Revision.new
62
+ rev.init_db("schema" => schema, "db" => db)
63
+
64
+
65
+ #Test wrong encoding.
66
+ cont = File.read("#{File.dirname(__FILE__)}/db_spec_encoding_test_file.txt")
67
+ cont.force_encoding("ASCII-8BIT")
68
+
69
+ db.insert("test", {
70
+ "text" => cont
71
+ })
72
+
73
+
74
+ #Throw out invalid encoding because it will make dumping fail.
75
+ db.tables[:test].truncate
76
+
77
+
78
+
79
+ #Test IDQueries.
80
+ rows_count = 1250
81
+ db.transaction do
82
+ 0.upto(rows_count) do |count|
83
+ db.insert(:test_table, {:name => "User #{count}"})
84
+ end
85
+ end
86
+
87
+ block_ran = 0
88
+ idq = Baza::Idquery.new(:db => db, :debug => false, :table => :test_table, :query => "SELECT id FROM test_table") do |data|
89
+ block_ran += 1
90
+ end
91
+
92
+ raise "Block with should have ran too little: #{block_ran}." if block_ran < rows_count
93
+
94
+ block_ran = 0
95
+ db.select(:test_table, {}, {:idquery => true}) do |data|
96
+ block_ran += 1
97
+ end
98
+
99
+ raise "Block with should have ran too little: #{block_ran}." if block_ran < rows_count
100
+
101
+
102
+ #Test upserting.
103
+ data = {:name => "upsert - Kasper Johansen"}
104
+ data2 = {:name => "upsert - Kasper Nielsen Johansen"}
105
+ sel = {:nickname => "upsert - kaspernj"}
106
+
107
+ table = db.tables[:test_table]
108
+ table.reload
109
+ rows_count = table.rows_count
110
+
111
+ db.upsert(:test_table, sel, data)
112
+ row = db.select(:test_table, sel).fetch
113
+ row[:name].should eql("upsert - Kasper Johansen")
114
+
115
+ table.reload
116
+ table.rows_count.should eql(rows_count + 1)
117
+
118
+ db.upsert(:test_table, sel, data2)
119
+ row = db.select(:test_table, sel).fetch
120
+ row[:name].should eql("upsert - Kasper Nielsen Johansen")
121
+
122
+ table.reload
123
+ table.rows_count.should eql(rows_count + 1)
124
+
125
+
126
+ #Test dumping.
127
+ dump = Baza::Dump.new(:db => db, :debug => false)
128
+ str_io = StringIO.new
129
+ dump.dump(str_io)
130
+ str_io.rewind
131
+
132
+
133
+ #Remember some numbers for validation.
134
+ tables_count = db.tables.list.length
135
+
136
+
137
+ #Remove everything in the db.
138
+ db.tables.list do |table|
139
+ table.drop unless table.native?
140
+ end
141
+
142
+
143
+ #Run the exported SQL.
144
+ db.transaction do
145
+ str_io.each_line do |sql|
146
+ db.q(sql)
147
+ end
148
+ end
149
+
150
+
151
+ #Vaildate import.
152
+ raise "Not same amount of tables: #{tables_count}, #{db.tables.list.length}" if tables_count != db.tables.list.length
153
+
154
+
155
+
156
+ #Test revision table renaming.
157
+ Baza::Revision.new.init_db("db" => db, "schema" => {
158
+ "tables" => {
159
+ "new_test_table" => {
160
+ "renames" => ["test_table"]
161
+ }
162
+ }
163
+ })
164
+ tables = db.tables.list
165
+ raise "Didnt expect table 'test_table' to exist but it did." if tables.key?("test_table")
166
+ raise "Expected 'new_test_table' to exist but it didnt." if !tables.key?("new_test_table")
167
+
168
+
169
+ #Test revision for column renaming.
170
+ Baza::Revision.new.init_db("db" => db, "schema" => {
171
+ "tables" => {
172
+ "new_test_table" => {
173
+ "columns" => [
174
+ {"name" => "new_name", "type" => "varchar", "renames" => ["name"]}
175
+ ]
176
+ }
177
+ }
178
+ })
179
+ columns = db.tables["new_test_table"].columns
180
+ raise "Didnt expect 'name' to exist but it did." if columns.key?("name")
181
+ raise "Expected 'new_name'-column to exist but it didnt." if !columns.key?("new_name")
182
+
183
+
184
+ #Delete test-database if everything went well.
185
+ File.unlink(db_path) if File.exists?(db_path)
186
+ end
187
+
188
+ it "should generate proper sql" do
189
+ require "knj/db"
190
+ require "knj/os"
191
+ require "rubygems"
192
+ require "sqlite3" if !Kernel.const_defined?("SQLite3") and RUBY_ENGINE != "jruby"
193
+
194
+ db_path = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3.sqlite3"
195
+ File.unlink(db_path) if File.exists?(db_path)
196
+
197
+ db = Baza::Db.new(
198
+ :type => "sqlite3",
199
+ :path => db_path,
200
+ :return_keys => "symbols",
201
+ :index_append_table_name => true
202
+ )
203
+
204
+ time = Time.new(1985, 6, 17, 10, 30)
205
+ db.insert(:test, {:date => time}, :return_sql => true).should eql("INSERT INTO `test` (`date`) VALUES ('1985-06-17 10:30:00')")
206
+
207
+ date = Date.new(1985, 6, 17)
208
+ db.insert(:test, {:date => date}, :return_sql => true).should eql("INSERT INTO `test` (`date`) VALUES ('1985-06-17')")
209
+ end
210
+
211
+ it "should copy database structure and data" do
212
+ require "knj/db"
213
+ require "knj/os"
214
+ require "rubygems"
215
+ require "sqlite3" if !Kernel.const_defined?("SQLite3") and RUBY_ENGINE != "jruby"
216
+
217
+ db_path1 = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3_db1.sqlite3"
218
+ File.unlink(db_path1) if File.exists?(db_path1)
219
+
220
+ db_path2 = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3_db2.sqlite3"
221
+ File.unlink(db_path2) if File.exists?(db_path2)
222
+
223
+ db1 = Baza::Db.new(
224
+ :type => "sqlite3",
225
+ :path => db_path1,
226
+ :return_keys => "symbols",
227
+ :index_append_table_name => true
228
+ )
229
+
230
+ db1.tables.create(:test_table, {
231
+ "columns" => [
232
+ {"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
233
+ {"name" => "testname", "type" => "varchar"}
234
+ ],
235
+ "indexes" => [
236
+ "testname"
237
+ ]
238
+ })
239
+
240
+ table1 = db1.tables["test_table"]
241
+ cols1 = table1.columns
242
+
243
+ 100.times do |count|
244
+ table1.insert(:testname => "TestRow#{count}")
245
+ end
246
+
247
+ db2 = Baza::Db.new(
248
+ :type => "sqlite3",
249
+ :path => db_path2,
250
+ :return_keys => "symbols",
251
+ :index_append_table_name => true
252
+ )
253
+
254
+ begin
255
+ table2 = db2.tables["test_table"]
256
+ raise "Expected not-found exception."
257
+ rescue Errno::ENOENT
258
+ #expected
259
+ end
260
+
261
+ db1.copy_to(db2)
262
+
263
+ table2 = db2.tables["test_table"]
264
+
265
+ cols2 = table2.columns
266
+ cols2.length.should eql(cols1.length)
267
+
268
+ table2.rows_count.should eql(table1.rows_count)
269
+
270
+ db1.select(:test_table) do |row1|
271
+ found = 0
272
+ db2.select(:test_table, row1) do |row2|
273
+ found += 1
274
+
275
+ row1.each do |key, val|
276
+ row2[key].should eql(val)
277
+ end
278
+ end
279
+
280
+ found.should eql(1)
281
+ end
282
+
283
+ table1.indexes.length.should eql(1)
284
+ table2.indexes.length.should eql(table1.indexes.length)
285
+ end
286
+ end
@@ -0,0 +1 @@
1
+ ��pl��p���
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'baza'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,215 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: baza
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kasper Johansen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: datet
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: wref
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: knjrbfw
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 2.8.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 2.8.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: rdoc
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '3.12'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '3.12'
94
+ - !ruby/object:Gem::Dependency
95
+ name: bundler
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: 1.0.0
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: 1.0.0
110
+ - !ruby/object:Gem::Dependency
111
+ name: jeweler
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 1.8.4
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: 1.8.4
126
+ - !ruby/object:Gem::Dependency
127
+ name: sqlite3
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ description: A database abstraction layer, model framework and database framework.
143
+ email: kj@gfish.com
144
+ executables: []
145
+ extensions: []
146
+ extra_rdoc_files:
147
+ - LICENSE.txt
148
+ - README.rdoc
149
+ files:
150
+ - .document
151
+ - .rspec
152
+ - Gemfile
153
+ - Gemfile.lock
154
+ - LICENSE.txt
155
+ - README.rdoc
156
+ - Rakefile
157
+ - VERSION
158
+ - include/db.rb
159
+ - include/dbtime.rb
160
+ - include/drivers/.DS_Store
161
+ - include/drivers/mysql/mysql.rb
162
+ - include/drivers/mysql/mysql_columns.rb
163
+ - include/drivers/mysql/mysql_indexes.rb
164
+ - include/drivers/mysql/mysql_sqlspecs.rb
165
+ - include/drivers/mysql/mysql_tables.rb
166
+ - include/drivers/sqlite3/libknjdb_java_sqlite3.rb
167
+ - include/drivers/sqlite3/libknjdb_sqlite3_ironruby.rb
168
+ - include/drivers/sqlite3/sqlite3.rb
169
+ - include/drivers/sqlite3/sqlite3_columns.rb
170
+ - include/drivers/sqlite3/sqlite3_indexes.rb
171
+ - include/drivers/sqlite3/sqlite3_sqlspecs.rb
172
+ - include/drivers/sqlite3/sqlite3_tables.rb
173
+ - include/dump.rb
174
+ - include/idquery.rb
175
+ - include/model.rb
176
+ - include/model_custom.rb
177
+ - include/model_handler.rb
178
+ - include/model_handler_sqlhelper.rb
179
+ - include/query_buffer.rb
180
+ - include/revision.rb
181
+ - include/row.rb
182
+ - include/sqlspecs.rb
183
+ - lib/baza.rb
184
+ - spec/baza_spec.rb
185
+ - spec/db_spec_encoding_test_file.txt
186
+ - spec/spec_helper.rb
187
+ homepage: http://github.com/kaspernj/baza
188
+ licenses:
189
+ - MIT
190
+ post_install_message:
191
+ rdoc_options: []
192
+ require_paths:
193
+ - lib
194
+ required_ruby_version: !ruby/object:Gem::Requirement
195
+ none: false
196
+ requirements:
197
+ - - ! '>='
198
+ - !ruby/object:Gem::Version
199
+ version: '0'
200
+ segments:
201
+ - 0
202
+ hash: 3792611403292399055
203
+ required_rubygems_version: !ruby/object:Gem::Requirement
204
+ none: false
205
+ requirements:
206
+ - - ! '>='
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ requirements: []
210
+ rubyforge_project:
211
+ rubygems_version: 1.8.25
212
+ signing_key:
213
+ specification_version: 3
214
+ summary: A database abstraction layer, model framework and database framework.
215
+ test_files: []