baza 0.0.38 → 0.0.39
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +0 -14
- data/lib/baza/database.rb +1 -1
- data/lib/baza/db.rb +5 -4
- data/lib/baza/driver/active_record/columns.rb +2 -2
- data/lib/baza/driver/active_record/commands.rb +2 -2
- data/lib/baza/driver/active_record/databases.rb +2 -2
- data/lib/baza/driver/active_record/foreign_keys.rb +13 -0
- data/lib/baza/driver/active_record/indexes.rb +2 -2
- data/lib/baza/driver/active_record/tables.rb +3 -3
- data/lib/baza/driver/active_record/users.rb +2 -2
- data/lib/baza/driver/active_record.rb +3 -1
- data/lib/baza/driver/mysql/columns.rb +1 -1
- data/lib/baza/driver/mysql/database.rb +11 -38
- data/lib/baza/driver/mysql/foreign_key.rb +2 -0
- data/lib/baza/driver/mysql/foreign_keys.rb +13 -0
- data/lib/baza/driver/mysql/sql/column.rb +3 -2
- data/lib/baza/driver/mysql/sql/create_table.rb +11 -0
- data/lib/baza/driver/mysql/table.rb +43 -4
- data/lib/baza/driver/mysql/tables.rb +22 -9
- data/lib/baza/driver/mysql2/foreign_keys.rb +2 -0
- data/lib/baza/driver/mysql2.rb +6 -5
- data/lib/baza/driver/mysql_java.rb +2 -2
- data/lib/baza/driver/pg/columns.rb +1 -1
- data/lib/baza/driver/pg/create_index_sql_creator.rb +2 -5
- data/lib/baza/driver/pg/database.rb +2 -2
- data/lib/baza/driver/pg/foreign_key.rb +2 -0
- data/lib/baza/driver/pg/foreign_keys.rb +13 -0
- data/lib/baza/driver/pg/result.rb +5 -1
- data/lib/baza/driver/pg/table.rb +43 -0
- data/lib/baza/driver/pg/tables.rb +20 -13
- data/lib/baza/driver/sqlite3/columns.rb +1 -1
- data/lib/baza/driver/sqlite3/foreign_key.rb +14 -0
- data/lib/baza/driver/sqlite3/foreign_keys.rb +11 -0
- data/lib/baza/driver/sqlite3/table.rb +41 -6
- data/lib/baza/driver/sqlite3/tables.rb +15 -14
- data/lib/baza/driver/sqlite3.rb +1 -1
- data/lib/baza/driver/sqlite3_java.rb +1 -1
- data/lib/baza/driver/sqlite3_rhodes.rb +1 -1
- data/lib/baza/dump.rb +3 -2
- data/lib/baza/foreign_key.rb +1 -1
- data/lib/baza/jdbc_driver.rb +1 -1
- data/lib/baza/mysql_base_driver.rb +1 -1
- data/lib/baza/query_buffer.rb +36 -6
- data/lib/baza/result_base.rb +2 -5
- data/lib/baza/tables.rb +1 -1
- metadata +26 -269
- data/.document +0 -5
- data/.github/dependabot.yml +0 -13
- data/.rspec +0 -1
- data/.rubocop.yml +0 -84
- data/.rubocop_todo.yml +0 -39
- data/.ruby-version +0 -1
- data/Gemfile +0 -32
- data/Gemfile.lock +0 -164
- data/VERSION +0 -1
- data/baza.gemspec +0 -264
- data/config/best_project_practice_rubocop.yml +0 -10
- data/config/best_project_practice_rubocop_todo.yml +0 -163
- data/peak_flow.yml +0 -31
- data/spec/active_record/models/user.rb +0 -3
- data/spec/baza/cloner_spec.rb +0 -10
- data/spec/baza/sql_queries/generic_insert_spec.rb +0 -26
- data/spec/baza/sql_queries/select_spec.rb +0 -38
- data/spec/drivers/active_record_mysql2_spec.rb +0 -22
- data/spec/drivers/active_record_mysql_spec.rb +0 -19
- data/spec/drivers/active_record_pg_spec.rb +0 -22
- data/spec/drivers/active_record_sqlite3_spec.rb +0 -18
- data/spec/drivers/mysql2_spec.rb +0 -19
- data/spec/drivers/mysql_spec.rb +0 -58
- data/spec/drivers/pg/columns_spec.rb +0 -45
- data/spec/drivers/pg/table_spec.rb +0 -20
- data/spec/drivers/pg_spec.rb +0 -20
- data/spec/drivers/sqlite3_spec.rb +0 -67
- data/spec/drivers/tiny_spec.rb +0 -27
- data/spec/info_active_record_example.rb +0 -35
- data/spec/info_active_record_mysql2_example.rb +0 -36
- data/spec/info_active_record_mysql2_peak_flow.rb +0 -36
- data/spec/info_active_record_mysql_example.rb +0 -37
- data/spec/info_active_record_mysql_peak_flow.rb +0 -37
- data/spec/info_active_record_pg_example.rb +0 -36
- data/spec/info_active_record_pg_peak_flow.rb +0 -36
- data/spec/info_active_record_sqlite3.rb +0 -29
- data/spec/info_mysql2_example.rb +0 -21
- data/spec/info_mysql2_peak_flow.rb +0 -21
- data/spec/info_mysql_example.rb +0 -21
- data/spec/info_mysql_peak_flow.rb +0 -21
- data/spec/info_pg_example.rb +0 -22
- data/spec/info_pg_peak_flow.rb +0 -22
- data/spec/info_sqlite3.rb +0 -19
- data/spec/spec_helper.rb +0 -18
- data/spec/support/driver_active_record_collection.rb +0 -62
- data/spec/support/driver_collection.rb +0 -396
- data/spec/support/driver_columns_collection.rb +0 -64
- data/spec/support/driver_databases_collection.rb +0 -48
- data/spec/support/driver_foreign_keys_collection.rb +0 -54
- data/spec/support/driver_importer_collection.rb +0 -38
- data/spec/support/driver_indexes_collection.rb +0 -66
- data/spec/support/driver_tables_collection.rb +0 -116
- data/spec/support/driver_users_collection.rb +0 -53
data/spec/info_mysql2_example.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
class Baza::InfoMysql2
|
2
|
-
attr_reader :db
|
3
|
-
|
4
|
-
def initialize(args = {})
|
5
|
-
@db = Baza::Db.new({
|
6
|
-
type: :mysql2,
|
7
|
-
host: "localhost",
|
8
|
-
user: "baza-test",
|
9
|
-
pass: "password",
|
10
|
-
db: "baza-test"
|
11
|
-
}.merge(args))
|
12
|
-
end
|
13
|
-
|
14
|
-
def before
|
15
|
-
@db.tables.list(&:drop)
|
16
|
-
end
|
17
|
-
|
18
|
-
def after
|
19
|
-
@db.close
|
20
|
-
end
|
21
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
class Baza::InfoMysql2
|
2
|
-
attr_reader :db
|
3
|
-
|
4
|
-
def initialize(args = {})
|
5
|
-
@db = Baza::Db.new({
|
6
|
-
type: :mysql2,
|
7
|
-
host: "mysql",
|
8
|
-
user: "build",
|
9
|
-
pass: "password",
|
10
|
-
db: "baza"
|
11
|
-
}.merge(args))
|
12
|
-
end
|
13
|
-
|
14
|
-
def before
|
15
|
-
@db.tables.list(&:drop)
|
16
|
-
end
|
17
|
-
|
18
|
-
def after
|
19
|
-
@db.close
|
20
|
-
end
|
21
|
-
end
|
data/spec/info_mysql_example.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
class Baza::InfoMysql
|
2
|
-
attr_reader :db
|
3
|
-
|
4
|
-
def initialize(args = {})
|
5
|
-
@db = Baza::Db.new({
|
6
|
-
type: :mysql,
|
7
|
-
host: "localhost",
|
8
|
-
user: "baza-test",
|
9
|
-
pass: "password",
|
10
|
-
db: "baza-test"
|
11
|
-
}.merge(args))
|
12
|
-
end
|
13
|
-
|
14
|
-
def before
|
15
|
-
@db.tables.list(&:drop)
|
16
|
-
end
|
17
|
-
|
18
|
-
def after
|
19
|
-
@db.close
|
20
|
-
end
|
21
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
class Baza::InfoMysql
|
2
|
-
attr_reader :db
|
3
|
-
|
4
|
-
def initialize(args = {})
|
5
|
-
@db = Baza::Db.new({
|
6
|
-
type: :mysql,
|
7
|
-
host: "mysql",
|
8
|
-
user: "build",
|
9
|
-
pass: "password",
|
10
|
-
db: "baza"
|
11
|
-
}.merge(args))
|
12
|
-
end
|
13
|
-
|
14
|
-
def before
|
15
|
-
@db.tables.list(&:drop)
|
16
|
-
end
|
17
|
-
|
18
|
-
def after
|
19
|
-
@db.close
|
20
|
-
end
|
21
|
-
end
|
data/spec/info_pg_example.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
class Baza::InfoPg
|
2
|
-
attr_reader :db
|
3
|
-
|
4
|
-
def initialize(args = {})
|
5
|
-
@db = Baza::Db.new({
|
6
|
-
type: :pg,
|
7
|
-
host: "127.0.0.1",
|
8
|
-
user: "baza-test",
|
9
|
-
pass: "password",
|
10
|
-
db: "baza-test",
|
11
|
-
debug: false
|
12
|
-
}.merge(args))
|
13
|
-
end
|
14
|
-
|
15
|
-
def before
|
16
|
-
@db.tables.list(&:drop)
|
17
|
-
end
|
18
|
-
|
19
|
-
def after
|
20
|
-
@db.close
|
21
|
-
end
|
22
|
-
end
|
data/spec/info_pg_peak_flow.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
class Baza::InfoPg
|
2
|
-
attr_reader :db
|
3
|
-
|
4
|
-
def initialize(args = {})
|
5
|
-
@db = Baza::Db.new({
|
6
|
-
type: :pg,
|
7
|
-
host: "postgres",
|
8
|
-
user: "build",
|
9
|
-
pass: "password",
|
10
|
-
db: "baza",
|
11
|
-
debug: false
|
12
|
-
}.merge(args))
|
13
|
-
end
|
14
|
-
|
15
|
-
def before
|
16
|
-
@db.tables.list(&:drop)
|
17
|
-
end
|
18
|
-
|
19
|
-
def after
|
20
|
-
@db.close
|
21
|
-
end
|
22
|
-
end
|
data/spec/info_sqlite3.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
class Baza::InfoSqlite3
|
2
|
-
attr_reader :db
|
3
|
-
|
4
|
-
def initialize(args = {})
|
5
|
-
require "sqlite3" unless RUBY_ENGINE == "jruby"
|
6
|
-
|
7
|
-
@db = Baza::Db.new({
|
8
|
-
type: :sqlite3,
|
9
|
-
path: "#{Dir.tmpdir}/#{SecureRandom.hex(8)}.sqlite3",
|
10
|
-
index_append_table_name: true,
|
11
|
-
sql_to_error: true,
|
12
|
-
debug: false
|
13
|
-
}.merge(args))
|
14
|
-
end
|
15
|
-
|
16
|
-
def before; end
|
17
|
-
|
18
|
-
def after; end
|
19
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
require "baza"
|
2
|
-
require "pry"
|
3
|
-
|
4
|
-
if RUBY_PLATFORM == "java"
|
5
|
-
require "jdbc/mysql"
|
6
|
-
::Jdbc::MySQL.load_driver
|
7
|
-
|
8
|
-
require "jdbc/sqlite3"
|
9
|
-
::Jdbc::SQLite3.load_driver
|
10
|
-
end
|
11
|
-
|
12
|
-
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
13
|
-
|
14
|
-
RSpec.configure do |config|
|
15
|
-
config.expect_with :rspec do |c|
|
16
|
-
c.syntax = [:expect]
|
17
|
-
end
|
18
|
-
end
|
@@ -1,62 +0,0 @@
|
|
1
|
-
require "active_record"
|
2
|
-
require_relative "../active_record/models/user"
|
3
|
-
|
4
|
-
shared_examples_for "an active record driver" do
|
5
|
-
let(:driver) { constant.new(type_translation: true) }
|
6
|
-
let(:db) { driver.db }
|
7
|
-
let(:db_with_type_translation) { constant.new(type_translation: true, debug: false).db }
|
8
|
-
let(:row) do
|
9
|
-
test_table.insert(text: "Kasper", number: 30, float: 4.5)
|
10
|
-
db.select(:test, text: "Kasper").fetch
|
11
|
-
end
|
12
|
-
let(:test_table) do
|
13
|
-
db.tables.create(
|
14
|
-
"test",
|
15
|
-
columns: [
|
16
|
-
{name: "id", type: :int, autoincr: true, primarykey: true},
|
17
|
-
{name: "text", type: :varchar},
|
18
|
-
{name: "number", type: :int, default: 0},
|
19
|
-
{name: "float", type: :float, default: 0.0}
|
20
|
-
]
|
21
|
-
)
|
22
|
-
db.tables[:test]
|
23
|
-
end
|
24
|
-
|
25
|
-
before do
|
26
|
-
driver.before
|
27
|
-
|
28
|
-
db.tables.create(
|
29
|
-
"users",
|
30
|
-
columns: [
|
31
|
-
{name: "id", type: :int, autoincr: true, primarykey: true},
|
32
|
-
{name: "email", type: :varchar}
|
33
|
-
],
|
34
|
-
indexes: [
|
35
|
-
{name: "index_on_email", columns: ["email"], unique: true}
|
36
|
-
]
|
37
|
-
)
|
38
|
-
end
|
39
|
-
|
40
|
-
after do
|
41
|
-
driver.after
|
42
|
-
end
|
43
|
-
|
44
|
-
it "saves models through baza" do
|
45
|
-
user = User.new(email: "test@example.com")
|
46
|
-
expect(user.valid?).to eq true
|
47
|
-
db.driver.save_model!(user, update_on_duplicate_key: true)
|
48
|
-
expect(user.persisted?).to eq true
|
49
|
-
expect(user.id).to eq 1
|
50
|
-
expect(user.email).to eq "test@example.com"
|
51
|
-
end
|
52
|
-
|
53
|
-
it "upserts" do
|
54
|
-
user1 = User.new(email: "test@example.com")
|
55
|
-
db.driver.save_model!(user1)
|
56
|
-
|
57
|
-
user2 = User.new(email: "test@example.com")
|
58
|
-
db.driver.save_model!(user2, update_on_duplicate_key: true)
|
59
|
-
|
60
|
-
expect(user2.id).to eq user1.id
|
61
|
-
end
|
62
|
-
end
|
@@ -1,396 +0,0 @@
|
|
1
|
-
shared_examples_for "a baza driver" do
|
2
|
-
let(:driver) { constant.new(type_translation: :string) }
|
3
|
-
let(:driver2) { constant.new }
|
4
|
-
let(:db) { driver.db }
|
5
|
-
let(:db2) { driver2.db }
|
6
|
-
let(:db_with_type_translation) { constant.new(type_translation: true, debug: false).db }
|
7
|
-
let(:row) do
|
8
|
-
test_table.insert(text: "Kasper", number: 30, float: 4.5)
|
9
|
-
db.select(:test, text: "Kasper").fetch
|
10
|
-
end
|
11
|
-
let(:test_table) do
|
12
|
-
db.tables.create(
|
13
|
-
"test",
|
14
|
-
columns: [
|
15
|
-
{name: "id", type: :int, autoincr: true, primarykey: true},
|
16
|
-
{name: "text", type: :varchar},
|
17
|
-
{name: "number", type: :int, default: 0},
|
18
|
-
{name: "float", type: :float, default: 0.0},
|
19
|
-
{name: "created_at", type: :datetime}
|
20
|
-
]
|
21
|
-
)
|
22
|
-
db.tables[:test]
|
23
|
-
end
|
24
|
-
|
25
|
-
before do
|
26
|
-
driver.before
|
27
|
-
driver2.before
|
28
|
-
end
|
29
|
-
|
30
|
-
after do
|
31
|
-
driver.after
|
32
|
-
driver2.after
|
33
|
-
end
|
34
|
-
|
35
|
-
it "does id-queries" do
|
36
|
-
test_table
|
37
|
-
|
38
|
-
rows_count = 1250
|
39
|
-
db.transaction do
|
40
|
-
rows_count.times do |count|
|
41
|
-
db.insert(:test, text: "User #{count}")
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
block_ran = 0
|
46
|
-
Baza::Idquery.new(db: db, debug: false, table: :test, query: "SELECT id FROM test") do
|
47
|
-
block_ran += 1
|
48
|
-
end
|
49
|
-
|
50
|
-
expect(block_ran).to eq rows_count
|
51
|
-
|
52
|
-
block_ran = 0
|
53
|
-
db.select(:test, {}, idquery: true) do
|
54
|
-
block_ran += 1
|
55
|
-
end
|
56
|
-
|
57
|
-
expect(block_ran).to eq rows_count
|
58
|
-
end
|
59
|
-
|
60
|
-
it "does unbuffered queries" do
|
61
|
-
test_table
|
62
|
-
|
63
|
-
10.times do |count|
|
64
|
-
db.insert(:test, text: "Test #{count}")
|
65
|
-
end
|
66
|
-
|
67
|
-
count_results = 0
|
68
|
-
db.query_ubuf("SELECT * FROM test") do |row|
|
69
|
-
expect(row[:text]).to eq "Test #{count_results}"
|
70
|
-
count_results += 1
|
71
|
-
end
|
72
|
-
|
73
|
-
expect(count_results).to eq 10
|
74
|
-
end
|
75
|
-
|
76
|
-
it "#upsert" do
|
77
|
-
test_table.create_columns([{name: "nickname", type: :varchar}])
|
78
|
-
|
79
|
-
# Test upserting.
|
80
|
-
data = {text: "upsert - Kasper Johansen"}
|
81
|
-
data2 = {text: "upsert - Kasper Nielsen Johansen"}
|
82
|
-
sel = {nickname: "upsert - kaspernj"}
|
83
|
-
|
84
|
-
table = db.tables[:test]
|
85
|
-
table.reload
|
86
|
-
rows_count = table.rows_count
|
87
|
-
|
88
|
-
db.upsert(:test, data, sel)
|
89
|
-
row = db.select(:test, sel).fetch
|
90
|
-
expect(row[:text]).to eq "upsert - Kasper Johansen"
|
91
|
-
|
92
|
-
table.reload
|
93
|
-
expect(table.rows_count).to eq rows_count + 1
|
94
|
-
|
95
|
-
db.upsert(:test, data2, sel)
|
96
|
-
row = db.select(:test, sel).fetch
|
97
|
-
expect(row[:text]).to eq "upsert - Kasper Nielsen Johansen"
|
98
|
-
|
99
|
-
table.reload
|
100
|
-
expect(table.rows_count).to eq rows_count + 1
|
101
|
-
end
|
102
|
-
|
103
|
-
describe "#upsert_duplicate_key" do
|
104
|
-
before do
|
105
|
-
test_table.create_indexes([name: "unique_text", columns: ["text"], unique: true])
|
106
|
-
end
|
107
|
-
|
108
|
-
it "inserts records with terms" do
|
109
|
-
expect(test_table.rows_count).to eq 0
|
110
|
-
test_table.upsert_duplicate_key({number: 2}, text: "test1")
|
111
|
-
expect(test_table.rows_count).to eq 1
|
112
|
-
end
|
113
|
-
|
114
|
-
it "updates existing records with terms" do
|
115
|
-
test_table.insert(text: "test1", number: 1)
|
116
|
-
test_table.upsert_duplicate_key({number: 2}, text: "test1")
|
117
|
-
expect(test_table.rows_count).to eq 1
|
118
|
-
|
119
|
-
rows = test_table.rows.to_a.map(&:to_hash)
|
120
|
-
rows[0][:float] = "0.0" if rows[0][:float] == "0"
|
121
|
-
expect(rows).to eq [{id: "1", text: "test1", number: "2", float: "0.0", created_at: ""}]
|
122
|
-
end
|
123
|
-
|
124
|
-
it "inserts with empty terms" do
|
125
|
-
expect(test_table.rows_count).to eq 0
|
126
|
-
id = test_table.upsert_duplicate_key({text: "test2"}, {}, return_id: true)
|
127
|
-
expect(test_table.rows_count).to eq 1
|
128
|
-
expect(id).to eq 1
|
129
|
-
end
|
130
|
-
|
131
|
-
it "updates existing records with empty terms" do
|
132
|
-
test_table.insert(text: "test1", number: 1)
|
133
|
-
id = test_table.upsert_duplicate_key({number: 2, text: "test1"}, {}, return_id: true)
|
134
|
-
expect(test_table.rows_count).to eq 1
|
135
|
-
expect(id).to eq 1
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
it "dumps as SQL" do
|
140
|
-
dump = Baza::Dump.new(db: db, debug: false)
|
141
|
-
str_io = StringIO.new
|
142
|
-
dump.dump(str_io)
|
143
|
-
str_io.rewind
|
144
|
-
|
145
|
-
# Remember some numbers for validation.
|
146
|
-
tables_count = db.tables.list.length
|
147
|
-
|
148
|
-
# Remove everything in the db.
|
149
|
-
db.tables.list.select(&:native?).each(&:drop)
|
150
|
-
|
151
|
-
# Run the exported SQL.
|
152
|
-
db.transaction do
|
153
|
-
str_io.each_line do |sql|
|
154
|
-
db.q(sql)
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
# Vaildate import.
|
159
|
-
raise "Not same amount of tables: #{tables_count}, #{db.tables.list.length}" if tables_count != db.tables.list.length
|
160
|
-
end
|
161
|
-
|
162
|
-
it "generates proper sql" do
|
163
|
-
time = Time.new(1985, 6, 17, 10, 30)
|
164
|
-
expect(db.insert(:test, {date: time}, return_sql: true)).to eq "INSERT INTO #{db.sep_table}test#{db.sep_table} (#{db.sep_col}date#{db.sep_col}) VALUES (#{db.sep_val}1985-06-17 10:30:00#{db.sep_val})"
|
165
|
-
|
166
|
-
date = Date.new(1985, 6, 17)
|
167
|
-
expect(db.insert(:test, {date: date}, return_sql: true)).to eq "INSERT INTO #{db.sep_table}test#{db.sep_table} (#{db.sep_col}date#{db.sep_col}) VALUES (#{db.sep_val}1985-06-17#{db.sep_val})"
|
168
|
-
end
|
169
|
-
|
170
|
-
it "is able to make new connections based on given objects" do
|
171
|
-
# Mysql doesn't support it...
|
172
|
-
unless db.opts.fetch(:type) == :mysql
|
173
|
-
Baza::Db.from_object(object: db.driver.conn)
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
it "is able to do ID-queries through the select-method" do
|
178
|
-
db.tables.create(
|
179
|
-
:test_table,
|
180
|
-
columns: [
|
181
|
-
{name: :idrow, type: :int, autoincr: true, primarykey: true},
|
182
|
-
{name: :name, type: :varchar}
|
183
|
-
]
|
184
|
-
)
|
185
|
-
|
186
|
-
count = 0
|
187
|
-
100.times do
|
188
|
-
arr = []
|
189
|
-
100.times do
|
190
|
-
count += 1
|
191
|
-
arr << {name: "Kasper #{count}"}
|
192
|
-
end
|
193
|
-
|
194
|
-
db.insert_multi(:test_table, arr)
|
195
|
-
end
|
196
|
-
|
197
|
-
count_found = 0
|
198
|
-
db.select(:test_table, nil, idquery: :idrow) do |row|
|
199
|
-
count_found += 1
|
200
|
-
|
201
|
-
expect(row[:name]).to eq "Kasper #{count_found}"
|
202
|
-
end
|
203
|
-
|
204
|
-
expect(count_found).to eq 10_000
|
205
|
-
end
|
206
|
-
|
207
|
-
it "uses query buffers" do
|
208
|
-
db.tables.create(
|
209
|
-
:test_table,
|
210
|
-
columns: [
|
211
|
-
{name: :id, type: :int, autoincr: true, primarykey: true},
|
212
|
-
{name: :name, type: :varchar}
|
213
|
-
]
|
214
|
-
)
|
215
|
-
|
216
|
-
upsert = false
|
217
|
-
count_inserts = 0
|
218
|
-
db.q_buffer do |buffer|
|
219
|
-
2500.times do |count|
|
220
|
-
if upsert
|
221
|
-
buffer.upsert(:test_table, {name: "Kasper #{count}"}, name: "Kasper #{count}")
|
222
|
-
upsert = false
|
223
|
-
else
|
224
|
-
buffer.insert(:test_table, name: "Kasper #{count}")
|
225
|
-
upsert = true
|
226
|
-
end
|
227
|
-
|
228
|
-
count_inserts += 1
|
229
|
-
end
|
230
|
-
end
|
231
|
-
|
232
|
-
expect(count_inserts).to eq 2500
|
233
|
-
|
234
|
-
test_table = db.tables[:test_table]
|
235
|
-
expect(test_table.rows_count).to eq 2500
|
236
|
-
|
237
|
-
count = 0
|
238
|
-
db.q_buffer do |buffer|
|
239
|
-
upsert = false
|
240
|
-
|
241
|
-
db.select(:test_table, {}, orderby: :id) do |row|
|
242
|
-
expect(row[:name]).to eq "Kasper #{count}"
|
243
|
-
|
244
|
-
if upsert
|
245
|
-
buffer.upsert(:test_table, {name: "Kasper #{count}-#{count}"}, id: row.fetch(:id))
|
246
|
-
upsert = false
|
247
|
-
else
|
248
|
-
buffer.update(:test_table, {name: "Kasper #{count}-#{count}"}, id: row.fetch(:id))
|
249
|
-
upsert = true
|
250
|
-
end
|
251
|
-
|
252
|
-
count += 1
|
253
|
-
end
|
254
|
-
end
|
255
|
-
|
256
|
-
expect(count).to eq 2500
|
257
|
-
|
258
|
-
count = 0
|
259
|
-
db.select(:test_table, {}, orderby: :id) do |row|
|
260
|
-
expect(row[:name]).to eq "Kasper #{count}-#{count}"
|
261
|
-
count += 1
|
262
|
-
end
|
263
|
-
|
264
|
-
expect(count).to eq 2500
|
265
|
-
|
266
|
-
# Test the flush-async which flushes transactions in a thread asyncronous.
|
267
|
-
db.q_buffer(flush_async: true) do |buffer|
|
268
|
-
count = 0
|
269
|
-
db.select(:test_table) do |row|
|
270
|
-
count += 1
|
271
|
-
time_start = Time.now.to_f if count == 1000
|
272
|
-
|
273
|
-
buffer.delete(:test_table, id: row.fetch(:id))
|
274
|
-
|
275
|
-
next unless count == 1000
|
276
|
-
time_end = Time.now.to_f
|
277
|
-
|
278
|
-
time_spent = time_end - time_start
|
279
|
-
raise "Too much time spent: '#{time_spent}'." if time_spent > 0.015
|
280
|
-
end
|
281
|
-
end
|
282
|
-
|
283
|
-
expect(test_table.rows_count).to eq 0
|
284
|
-
end
|
285
|
-
|
286
|
-
describe "results" do
|
287
|
-
before do
|
288
|
-
test_table.insert(text: "test 1")
|
289
|
-
test_table.insert(text: "test 2")
|
290
|
-
end
|
291
|
-
|
292
|
-
it '#to_a' do
|
293
|
-
array = db.select(:test).to_a
|
294
|
-
expect(array.length).to eq 2
|
295
|
-
end
|
296
|
-
|
297
|
-
it '#to_a_enum' do
|
298
|
-
array_enum = db.select(:test).to_a_enum
|
299
|
-
count = 0
|
300
|
-
array_enum.each { count += 1 }
|
301
|
-
expect(count).to eq 2
|
302
|
-
expect(array_enum.length).to eq 2
|
303
|
-
end
|
304
|
-
|
305
|
-
it '#to_enum' do
|
306
|
-
enum = db.select(:test).to_enum
|
307
|
-
|
308
|
-
count = 0
|
309
|
-
enum.each { count += 1 }
|
310
|
-
expect(count).to eq 2
|
311
|
-
end
|
312
|
-
end
|
313
|
-
|
314
|
-
it "counts" do
|
315
|
-
test_table.insert(text: "test 1")
|
316
|
-
expect(db.count(:test, text: "test 1")).to eq 1
|
317
|
-
end
|
318
|
-
|
319
|
-
it "doesnt do type translation by default" do
|
320
|
-
expect(row.fetch(:text).class).to eq String
|
321
|
-
expect(row.fetch(:number).class).to eq String
|
322
|
-
expect(row.fetch(:float).class).to eq String
|
323
|
-
end
|
324
|
-
|
325
|
-
it "does type translation" do
|
326
|
-
db_with_type_translation.tables.create(
|
327
|
-
:test,
|
328
|
-
columns: [
|
329
|
-
{name: "id", type: :int, autoincr: true, primarykey: true},
|
330
|
-
{name: "text", type: :varchar},
|
331
|
-
{name: "number", type: :int},
|
332
|
-
{name: "float", type: :float},
|
333
|
-
{name: "created_at", type: :datetime},
|
334
|
-
{name: "date", type: :date}
|
335
|
-
]
|
336
|
-
)
|
337
|
-
|
338
|
-
db_with_type_translation.insert(:test, text: "Kasper", number: 30, float: 4.5, created_at: Time.now, date: Date.new(2015, 06, 17))
|
339
|
-
|
340
|
-
row = db_with_type_translation.select(:test, text: "Kasper").fetch
|
341
|
-
|
342
|
-
expect(row.fetch(:text).class).to eq String
|
343
|
-
expect(row.fetch(:number).class.name).to eq "Integer"
|
344
|
-
expect(row.fetch(:float).class).to eq Float
|
345
|
-
|
346
|
-
if db.driver.conn.class.name == "ActiveRecord::ConnectionAdapters::SQLite3Adapter"
|
347
|
-
check_time_and_date = false
|
348
|
-
elsif db.driver.class.name == "Baza::Driver::ActiveRecord" && RUBY_PLATFORM == "java"
|
349
|
-
check_time_and_date = false
|
350
|
-
else
|
351
|
-
check_time_and_date = true
|
352
|
-
end
|
353
|
-
|
354
|
-
if check_time_and_date
|
355
|
-
expect(row.fetch(:created_at).class).to eq Time
|
356
|
-
expect(row.fetch(:date).class).to eq Date
|
357
|
-
end
|
358
|
-
end
|
359
|
-
|
360
|
-
it "returns arguments used to connect" do
|
361
|
-
require_relative "../../lib/baza/driver/active_record"
|
362
|
-
|
363
|
-
unless db.driver.is_a?(Baza::Driver::ActiveRecord)
|
364
|
-
args = db.driver.class.args
|
365
|
-
expect(args).to be_a Array
|
366
|
-
end
|
367
|
-
end
|
368
|
-
|
369
|
-
it "#new_query" do
|
370
|
-
test_table
|
371
|
-
test_table.insert(text: "Kasper")
|
372
|
-
|
373
|
-
query = db.new_query.from(:test).where(text: "Kasper").to_a
|
374
|
-
query[0][:float] = query[0][:float].to_f.to_s if query[0][:float] == "0"
|
375
|
-
expect(query.to_a).to eq [{id: "1", text: "Kasper", number: "0", float: "0.0", created_at: ""}]
|
376
|
-
end
|
377
|
-
|
378
|
-
it "#last_id" do
|
379
|
-
test_table.insert(text: "Kasper")
|
380
|
-
expect(db.last_id).to eq 1
|
381
|
-
end
|
382
|
-
|
383
|
-
it "handels null values for datetimes" do
|
384
|
-
id = test_table.insert({text: "Kasper"}, return_id: true)
|
385
|
-
row = test_table.row(id)
|
386
|
-
expect(row[:created_at]).to eq ""
|
387
|
-
end
|
388
|
-
|
389
|
-
describe "#insert" do
|
390
|
-
it "returns id" do
|
391
|
-
test_table
|
392
|
-
id = db.insert("test", {text: "Kasper"}, return_id: true)
|
393
|
-
expect(id).to eq 1
|
394
|
-
end
|
395
|
-
end
|
396
|
-
end
|
@@ -1,64 +0,0 @@
|
|
1
|
-
shared_examples_for "a baza columns driver" do
|
2
|
-
let(:driver) { constant.new }
|
3
|
-
let(:db) { driver.db }
|
4
|
-
let(:test_table) do
|
5
|
-
db.tables.create(
|
6
|
-
"test",
|
7
|
-
columns: [
|
8
|
-
{name: "id", type: :int, autoincr: true, primarykey: true},
|
9
|
-
{name: "text", type: :varchar}
|
10
|
-
]
|
11
|
-
)
|
12
|
-
db.tables[:test]
|
13
|
-
end
|
14
|
-
|
15
|
-
before do
|
16
|
-
driver.before
|
17
|
-
end
|
18
|
-
|
19
|
-
after do
|
20
|
-
driver.after
|
21
|
-
end
|
22
|
-
|
23
|
-
it "renames columns for renamed tables" do
|
24
|
-
test_table.columns # Load up columns to make them set table-name.
|
25
|
-
|
26
|
-
test_table.rename("test2")
|
27
|
-
test_table.column(:text).change(name: "text2")
|
28
|
-
|
29
|
-
table = db.tables[:test2]
|
30
|
-
column = table.column(:text2)
|
31
|
-
expect(column.table.name).to eq "test2"
|
32
|
-
end
|
33
|
-
|
34
|
-
it "creates columns right" do
|
35
|
-
col_id = test_table.column(:id)
|
36
|
-
expect(col_id.type).to eq :int
|
37
|
-
expect(col_id.primarykey?).to eq true
|
38
|
-
expect(col_id.autoincr?).to eq true
|
39
|
-
expect(col_id.default).to eq nil
|
40
|
-
|
41
|
-
col_text = test_table.column(:text)
|
42
|
-
expect(col_text.type).to eq :varchar
|
43
|
-
expect(col_text.primarykey?).to eq false
|
44
|
-
expect(col_text.autoincr?).to eq false
|
45
|
-
expect(col_text.default).to eq nil
|
46
|
-
end
|
47
|
-
|
48
|
-
it "is able to change columns" do
|
49
|
-
col_text = test_table.column(:text)
|
50
|
-
col_text.change(name: "text2", type: :int, default: 5)
|
51
|
-
|
52
|
-
expect(col_text.type).to eq :int
|
53
|
-
expect(col_text.default).to eq "5"
|
54
|
-
expect(col_text.name).to eq "text2"
|
55
|
-
end
|
56
|
-
|
57
|
-
it "is able to drop a column" do
|
58
|
-
test_table.column(:text).drop
|
59
|
-
|
60
|
-
expect do
|
61
|
-
test_table.column(:text)
|
62
|
-
end.to raise_error(Baza::Errors::ColumnNotFound)
|
63
|
-
end
|
64
|
-
end
|