Skip to content

Commit

Permalink
Merge pull request #328 from imdrasil/add_crystal_0.35_support
Browse files Browse the repository at this point in the history
Add crystal 0.35 support
  • Loading branch information
imdrasil committed Jun 15, 2020
2 parents db9d285 + 0fcc75e commit 7042ffd
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 46 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies:
### Requirements
- you need to choose one of the existing drivers for your DB: [mysql](https://github.com/crystal-lang/crystal-mysql) or [postgres](https://github.com/will/crystal-pg); sqlite3 adapter automatically installs required driver for it;
- crystal `>= 0.34.0`.
- crystal `>= 0.35.0`.

## Usage

Expand Down
6 changes: 3 additions & 3 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ version: 0.9.0
authors:
- Roman Kalnytskyi <moranibaca@gmail.com>

crystal: 0.34.0
crystal: 0.35.0

license: MIT

development_dependencies:
mysql:
github: crystal-lang/crystal-mysql
version: "= 0.11"
version: "= 0.11.1"
pg:
github: will/crystal-pg
version: "= 0.21.0"
version: "= 0.21.1"
factory:
github: imdrasil/factory
version: "~> 0.1.4"
Expand Down
16 changes: 7 additions & 9 deletions spec/adapter/base_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -239,17 +239,15 @@ describe Jennifer::Adapter::Base do
fields = %w(name ballance age description created_at updated_at user_id)
values = ["Deepthi", nil, 28, nil, nil, nil, nil] of Jennifer::DBAny

context "when count of fields doesn't exceed limit" do
it "imports objects by " do
Contact.all.count.should eq(0)
adapter.bulk_insert(Contact.table_name, fields, (amount - 1).times.map { values }.to_a)
query_log[1].should match(argument_regex)
Contact.all.count.should eq(amount - 1)
end
it "imports objects by prepared statement" do
Contact.all.count.should eq(0)
adapter.bulk_insert(Contact.table_name, fields, (amount - 1).times.map { values }.to_a)
query_log[1].should match(argument_regex)
Contact.all.count.should eq(amount - 1)
end

context "when count of fields exceeds limit" do
it "imports objects by " do
context "when count of fields exceeds supported limit" do
it "imports objects escaping their values in query" do
Contact.all.count.should eq(0)
adapter.bulk_insert(Contact.table_name, fields, amount.times.map { values }.to_a)
query_log[1].should_not match(argument_regex)
Expand Down
3 changes: 1 addition & 2 deletions spec/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ def set_default_configuration
end

set_default_configuration

Log.builder.bind "*", :debug, Spec.logger_backend
Log.setup "db", :debug, Spec.logger_backend

I18n.load_path += ["spec/fixtures/locales/**"]
I18n.default_locale = "en"
Expand Down
5 changes: 3 additions & 2 deletions spec/integration/concurrency_test.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "./shared_helpers"
require "./spec_helper"

POOL_SIZE = 3
POOL_SIZE = 2
TIME_TO_SLEEP = 3

if Spec.adapter != "mysql"
Expand All @@ -11,7 +11,6 @@ end

Jennifer::Config.configure do |conf|
conf.read("./scripts/database.yml", Spec.adapter)
conf.logger.level = :info
conf.max_pool_size = POOL_SIZE
conf.initial_pool_size = POOL_SIZE
conf.max_idle_pool_size = POOL_SIZE
Expand Down Expand Up @@ -46,7 +45,9 @@ describe "Concurrent execution" do
tread_count.times do
spawn do
begin
puts Time.utc
adapter.exec("CREATE temporary table table1 select #{sleep_command} as col")
puts "finish: #{Time.utc}"
ch.send("")
rescue e : Exception
ch.send(e.class.to_s)
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def execute(command, options)
{status, io.to_s}
end

def clean(type = :bash)
def clean(type = DatabaseSeeder.default_interface)
yield
ensure
DatabaseSeeder.drop(type)
Expand Down
12 changes: 9 additions & 3 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require "./support/*"
require "../scripts/migrations/20170119011451314_create_contacts"
require "../scripts/migrations/20180909200027509_create_notes"


class Jennifer::Adapter::ICommandShell
class_property stub = false
end
Expand All @@ -52,11 +53,15 @@ end

# Callbacks =======================

Spec.before_suite do
Log.setup "db", :debug, Spec.logger_backend
end

Spec.before_each do
Jennifer::Adapter.default_adapter.begin_transaction
pair_only { PAIR_ADAPTER.begin_transaction }
set_default_configuration
Spec.logger_backend.entries.clear
Jennifer::Adapter.default_adapter.begin_transaction
pair_only { PAIR_ADAPTER.begin_transaction }
end

Spec.after_each do
Expand Down Expand Up @@ -115,7 +120,8 @@ def query_count
end

def query_log
Spec.logger_backend.entries.map(&.message)
offset = ENV["PAIR"]? == "1" ? 2 : 1
Spec.logger_backend.entries[offset..-1].map(&.message)
end

def read_to_end(rs)
Expand Down
2 changes: 1 addition & 1 deletion src/jennifer/adapter/base.cr
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ module Jennifer
{% for arg in Config::CONNECTION_URI_PARAMS %}
"{{arg.id}}=#{config.{{arg.id}}}",
{% end %}
].join("&", s)
].join(s, "&")
{% end %}
end
end
Expand Down
22 changes: 11 additions & 11 deletions src/jennifer/adapter/base_sql_generator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ module Jennifer
def self.insert(table, hash)
String.build do |s|
s << "INSERT INTO " << table << "("
hash.keys.join(", ", s)
hash.keys.join(s, ", ")
s << ") VALUES (" << escape_string(hash.size) << ")"
end
end

def self.bulk_insert(table : String, field_names : Array(String), rows : Int32)
String.build do |s|
s << "INSERT INTO " << table << "("
field_names.join(", ", s) { |e| s << e }
field_names.join(s, ", ") { |e| s << e }
s << ") VALUES "
escaped_row = "(" + escape_string(field_names.size) + ")"
rows.times.join(", ", s) { s << escaped_row }
rows.times.join(s, ", ") { s << escaped_row }
end
end

def self.bulk_insert(table : String, field_names : Array(String), rows : Array)
String.build do |s|
s << "INSERT INTO " << table << "("
field_names.join(", ", s) { |e| s << e }
field_names.join(s, ", ") { |e| s << e }
s << ") VALUES "

rows.each_with_index do |row, index|
Expand Down Expand Up @@ -103,7 +103,7 @@ module Jennifer
esc = escape_string(1)
String.build do |s|
s << "UPDATE " << obj.class.table_name << " SET "
obj.arguments_to_save[:fields].map { |f| "#{f}= #{esc}" }.join(", ", s)
obj.arguments_to_save[:fields].map { |f| "#{f}= #{esc}" }.join(s, ", ")
s << " WHERE " << obj.class.primary_field_name << " = " << esc
end
end
Expand All @@ -112,7 +112,7 @@ module Jennifer
esc = escape_string(1)
String.build do |s|
s << "UPDATE " << query.table << " SET "
options.map { |k, _| "#{k}= #{esc}" }.join(", ", s)
options.map { |k, _| "#{k}= #{esc}" }.join(s, ", ")
s << ' '
body_section(s, query)
end
Expand Down Expand Up @@ -168,9 +168,9 @@ module Jennifer
if !query._raw_select
table = query._table
if !exact_fields.empty?
exact_fields.join(", ", io) { |f| io << "#{table}.#{f}" }
exact_fields.join(io, ", ") { |f| io << "#{table}.#{f}" }
else
query._select_fields.join(", ", io) { |f| io << f.definition(self) }
query._select_fields.join(io, ", ") { |f| io << f.definition(self) }
end
else
io << query._raw_select.not_nil!
Expand Down Expand Up @@ -206,7 +206,7 @@ module Jennifer
return unless query._groups?

io << "GROUP BY "
query._groups!.each.join(", ", io) { |c| io << c.as_sql(self) }
query._groups!.each.join(io, ", ") { |c| io << c.as_sql(self) }
io << ' '
end

Expand All @@ -221,7 +221,7 @@ module Jennifer
def self.join_clause(io : String::Builder, query)
return unless query._joins?

query._joins!.join(" ", io) { |j| io << j.as_sql(self) }
query._joins!.join(io, " ") { |j| io << j.as_sql(self) }
end

# Generates `WHERE` query clause.
Expand All @@ -247,7 +247,7 @@ module Jennifer
return unless query._order?

io << "ORDER BY "
query._order!.join(", ", io) { |expression| io.print expression.as_sql(self) }
query._order!.join(io, ", ") { |expression| io.print expression.as_sql(self) }
io << ' '
end

Expand Down
10 changes: 5 additions & 5 deletions src/jennifer/adapter/mysql/sql_generator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Jennifer
s << " VALUES ()"
else
s << "("
opts[:fields].join(", ", s)
opts[:fields].join(s, ", ")
s << ") VALUES (" << escape_string(opts[:fields].size) << ") "
end
end
Expand All @@ -28,10 +28,10 @@ module Jennifer

unless _joins.nil?
where_clause(s, _joins[0].on)
_joins[1..-1].join(" ", s) { |e| s << e.as_sql(self) }
_joins[1..-1].join(s, " ") { |e| s << e.as_sql(self) }
end
s << " SET "
options.join(", ", s) { |(k, _)| s << k << " = " << esc }
options.join(s, ", ") { |(k, _)| s << k << " = " << esc }
s << " "
where_clause(s, query.tree)
end
Expand All @@ -43,10 +43,10 @@ module Jennifer
io << "INSERT "
io << "IGNORE " if is_ignore
io << "INTO " << table << " ("
fields.join(", ", io)
fields.join(io, ", ")
escaped_row = "(" + escape_string(fields.size) + ")"
io << ") VALUES "
rows.times.join(", ", io) { io << escaped_row }
rows.times.join(io, ", ") { io << escaped_row }
unless is_ignore
io << " ON DUPLICATE KEY UPDATE "
on_conflict.each_with_index do |(field, value), index|
Expand Down
10 changes: 5 additions & 5 deletions src/jennifer/adapter/postgres/sql_generator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module Jennifer
s << " DEFAULT VALUES"
else
s << "("
opts[:fields].join(", ", s)
opts[:fields].join(s, ", ")
s << ") VALUES (" << escape_string(opts[:fields].size) << ") "
end

Expand All @@ -45,25 +45,25 @@ module Jennifer
esc = escape_string(1)
String.build do |s|
s << "UPDATE " << query._table << " SET "
options.map { |k, _| "#{k}= #{esc}" }.join(", ", s)
options.map { |k, _| "#{k}= #{esc}" }.join(s, ", ")
s << ' '

from_clause(s, query._joins![0].table_name(self)) if query._joins?
where_clause(s, query.tree)
if query._joins?
where_clause(s, query._joins![0].on)
query._joins![1..-1].join(" ", s) { |e| s << e.as_sql(self) }
query._joins![1..-1].join(s, " ") { |e| s << e.as_sql(self) }
end
end
end

def self.insert_on_duplicate(table, fields, rows : Int32, unique_fields, on_conflict)
String.build do |io|
io << "INSERT INTO " << table << " ("
fields.join(", ", io)
fields.join(io, ", ")
escaped_row = "(" + escape_string(fields.size) + ")"
io << ") VALUES "
rows.times.join(", ", io) { io << escaped_row }
rows.times.join(io, ", ") { io << escaped_row }
io << " ON CONFLICT (" << unique_fields.join(", ") << ") "
if on_conflict.empty?
io << "DO NOTHING"
Expand Down
2 changes: 1 addition & 1 deletion src/jennifer/exceptions.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CallStack.skip(__FILE__)
Exception::CallStack.skip(__FILE__)

module Jennifer
# Is raised when pseudo-abstract method is invoked.
Expand Down
2 changes: 1 addition & 1 deletion src/jennifer/migration/table_builder/create_foreign_key.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module Jennifer
private def self.hexdigest(text)
alg = OpenSSL::Digest.new("SHA256")
alg.update(text)
alg.hexdigest
alg.final.hexstring
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/jennifer/model/resource.cr
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ module Jennifer
# ```
def inspect(io) : Nil
io << "#<" << {{@type.name.id.stringify}} << ":0x"
object_id.to_s(16, io)
object_id.to_s(io, 16)
inspect_attributes(io)
io << '>'
nil
Expand Down

0 comments on commit 7042ffd

Please sign in to comment.