Skip to content

Commit

Permalink
Upgrade to crystal 0.20.3
Browse files Browse the repository at this point in the history
  • Loading branch information
waterlink committed Jan 1, 2017
1 parent 6ee4639 commit da31e37
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 69 deletions.
4 changes: 2 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ dependencies:
version: ~> 0.2
query:
github: waterlink/query.cr
version: ~> 0.1
version: ~> 0.2
development_dependencies:
mocks:
github: waterlink/mocks.cr
version: ~> 0.9.3
singleton:
github: waterlink/singleton.cr
version: ~> 0.1
version: ~> 1.0
2 changes: 1 addition & 1 deletion spec/fake_adapter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class FakeAdapter < ActiveRecord::Adapter
end

def self.instance
Singleton.instance_of(self)
Singleton::Of(self).instance
end

def self._reset
Expand Down
46 changes: 19 additions & 27 deletions spec/sql/query_generator_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -160,33 +160,25 @@ module ActiveRecord
end

example "IS expressions" do
[

{criteria("bool").is_true, "(bool) IS TRUE", Query::EMPTY_PARAMS},
{criteria("bool").is_not_true, "(bool) IS NOT TRUE", Query::EMPTY_PARAMS},
{(criteria("bool") < 3).is_true, "(bool < :1) IS TRUE", {"1" => 3}},
{(criteria("bool") < 3).is_not_true, "(bool < :1) IS NOT TRUE", {"1" => 3}},

{criteria("bool").is_false, "(bool) IS FALSE", Query::EMPTY_PARAMS},
{criteria("bool").is_not_false, "(bool) IS NOT FALSE", Query::EMPTY_PARAMS},
{(criteria("bool") < 3).is_false, "(bool < :1) IS FALSE", {"1" => 3}},
{(criteria("bool") < 3).is_not_false, "(bool < :1) IS NOT FALSE", {"1" => 3}},

{criteria("something").is_unknown, "(something) IS UNKNOWN", Query::EMPTY_PARAMS},
{criteria("something").is_not_unknown, "(something) IS NOT UNKNOWN", Query::EMPTY_PARAMS},
{(criteria("something") < 3).is_unknown, "(something < :1) IS UNKNOWN", {"1" => 3}},
{(criteria("something") < 3).is_not_unknown, "(something < :1) IS NOT UNKNOWN", {"1" => 3}},

{criteria("something").is_null, "(something) IS NULL", Query::EMPTY_PARAMS},
{criteria("something").is_not_null, "(something) IS NOT NULL", Query::EMPTY_PARAMS},
{(criteria("something") < 3).is_null, "(something < :1) IS NULL", {"1" => 3}},
{(criteria("something") < 3).is_not_null, "(something < :1) IS NOT NULL", {"1" => 3}},

].each do |example|
query = example[0]
expected_query = Query[example[1], example[2]]
generate(query).should eq(expected_query)
end
generate(criteria("bool").is_true).should eq(Query["(bool) IS TRUE", Query::EMPTY_PARAMS])
generate(criteria("bool").is_not_true).should eq(Query["(bool) IS NOT TRUE", Query::EMPTY_PARAMS])
generate((criteria("bool") < 3).is_true).should eq(Query["(bool < :1) IS TRUE", {"1" => 3}])
generate((criteria("bool") < 3).is_not_true).should eq(Query["(bool < :1) IS NOT TRUE", {"1" => 3}])

generate(criteria("bool").is_false).should eq(Query["(bool) IS FALSE", Query::EMPTY_PARAMS])
generate(criteria("bool").is_not_false).should eq(Query["(bool) IS NOT FALSE", Query::EMPTY_PARAMS])
generate((criteria("bool") < 3).is_false).should eq(Query["(bool < :1) IS FALSE", {"1" => 3}])
generate((criteria("bool") < 3).is_not_false).should eq(Query["(bool < :1) IS NOT FALSE", {"1" => 3}])

generate(criteria("something").is_unknown).should eq(Query["(something) IS UNKNOWN", Query::EMPTY_PARAMS])
generate(criteria("something").is_not_unknown).should eq(Query["(something) IS NOT UNKNOWN", Query::EMPTY_PARAMS])
generate((criteria("something") < 3).is_unknown).should eq(Query["(something < :1) IS UNKNOWN", {"1" => 3}])
generate((criteria("something") < 3).is_not_unknown).should eq(Query["(something < :1) IS NOT UNKNOWN", {"1" => 3}])

generate(criteria("something").is_null).should eq(Query["(something) IS NULL", Query::EMPTY_PARAMS])
generate(criteria("something").is_not_null).should eq(Query["(something) IS NOT NULL", Query::EMPTY_PARAMS])
generate((criteria("something") < 3).is_null).should eq(Query["(something < :1) IS NULL", {"1" => 3}])
generate((criteria("something") < 3).is_not_null).should eq(Query["(something < :1) IS NOT NULL", {"1" => 3}])
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/model.cr
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ module ActiveRecord
null_value
end

def self.build(hash : Hash(K, V))
def self.build(hash : Hash(K, V)) forall K, V
new(hash)
end

Expand Down
63 changes: 26 additions & 37 deletions src/sql/query_generator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module ActiveRecord
def initialize(&@param_name_transformer : Int32 -> String)
end

def handle(query : Array(T), param_count = 0)
def handle(query : Array(T), param_count = 0) forall T
params = {} of String => T
query = query.map do |value|
param_count += 1
Expand All @@ -59,20 +59,9 @@ module ActiveRecord
class Fail < ArgumentError
end

def generate(query : ::ActiveRecord::SupportedType, param_count = 0)
_handle(query, param_count)
end

def generate(query : ::Query::Query, param_count = 0)
_handle(query, param_count)
end

def generate(query, param_count = 0)
::ActiveRecord::QueryGenerator::Next.new
end

def _handle(query, param_count = 0)
::ActiveRecord::QueryGenerator::Response.lift(_generate(query, param_count))
value = _generate(query, param_count)
::ActiveRecord::QueryGenerator::Response.lift(value)
rescue Fail
::ActiveRecord::QueryGenerator::Next.new
end
Expand All @@ -81,79 +70,79 @@ module ActiveRecord
Query.new(query.name)
end

def _generate(query : ::Query::Equals(T), param_count = 0)
def _generate(query : ::Query::Equals(Q, T), param_count = 0) forall Q, T
generate_binary_op(query, " = ", param_count)
end

def _generate(query : ::Query::NotEquals, param_count = 0)
def _generate(query : ::Query::NotEquals(Q, T), param_count = 0) forall Q, T
generate_binary_op(query, " <> ", param_count)
end

def _generate(query : ::Query::MoreThan, param_count = 0)
def _generate(query : ::Query::MoreThan(Q, T), param_count = 0) forall Q, T
generate_binary_op(query, " > ", param_count)
end

def _generate(query : ::Query::MoreThanOrEqual, param_count = 0)
def _generate(query : ::Query::MoreThanOrEqual(Q, T), param_count = 0) forall Q, T
generate_binary_op(query, " >= ", param_count)
end

def _generate(query : ::Query::LessThan, param_count = 0)
def _generate(query : ::Query::LessThan(Q, T), param_count = 0) forall Q, T
generate_binary_op(query, " < ", param_count)
end

def _generate(query : ::Query::LessThanOrEqual, param_count = 0)
def _generate(query : ::Query::LessThanOrEqual(Q, T), param_count = 0) forall Q, T
generate_binary_op(query, " <= ", param_count)
end

def _generate(query : ::Query::Or, param_count = 0)
def _generate(query : ::Query::Or(Q, T), param_count = 0) forall Q, T
generate_binary_op(query, " OR ", param_count, parenthesis: true)
end

def _generate(query : ::Query::In, param_count = 0)
def _generate(query : ::Query::In(Q, T), param_count = 0) forall Q, T
generate_binary_op(query, " IN ", param_count)
end

def _generate(query : ::Query::Xor, param_count = 0)
def _generate(query : ::Query::Xor(Q, T), param_count = 0) forall Q, T
generate_binary_op(query, " XOR ", param_count, parenthesis: true)
end

def _generate(query : ::Query::And, param_count = 0)
def _generate(query : ::Query::And(Q, T), param_count = 0) forall Q, T
generate_binary_op(query, " AND ", param_count, parenthesis: true)
end

def _generate(query : ::Query::Not, param_count = 0)
def _generate(query : ::Query::Not(Q), param_count = 0) forall Q
generate_unary_op(query, param_count, parenthesis: true, prefix: "NOT ")
end

def _generate(query : ::Query::IsTrue, param_count = 0)
def _generate(query : ::Query::IsTrue(Q), param_count = 0) forall Q
generate_unary_op(query, param_count, parenthesis: true, suffix: " IS TRUE")
end

def _generate(query : ::Query::IsNotTrue, param_count = 0)
def _generate(query : ::Query::IsNotTrue(Q), param_count = 0) forall Q
generate_unary_op(query, param_count, parenthesis: true, suffix: " IS NOT TRUE")
end

def _generate(query : ::Query::IsFalse, param_count = 0)
def _generate(query : ::Query::IsFalse(Q), param_count = 0) forall Q
generate_unary_op(query, param_count, parenthesis: true, suffix: " IS FALSE")
end

def _generate(query : ::Query::IsNotFalse, param_count = 0)
def _generate(query : ::Query::IsNotFalse(Q), param_count = 0) forall Q
generate_unary_op(query, param_count, parenthesis: true, suffix: " IS NOT FALSE")
end

def _generate(query : ::Query::IsUnknown, param_count = 0)
def _generate(query : ::Query::IsUnknown(Q), param_count = 0) forall Q
generate_unary_op(query, param_count, parenthesis: true, suffix: " IS UNKNOWN")
end

def _generate(query : ::Query::IsNotUnknown, param_count = 0)
def _generate(query : ::Query::IsNotUnknown(Q), param_count = 0) forall Q
generate_unary_op(query, param_count, parenthesis: true, suffix: " IS NOT UNKNOWN")
end

def _generate(query : ::Query::IsNull, param_count = 0)
def _generate(query : ::Query::IsNull(Q), param_count = 0) forall Q
generate_unary_op(query, param_count, parenthesis: true, suffix: " IS NULL")
end

def _generate(query : ::Query::IsNotNull, param_count = 0)
def _generate(query : ::Query::IsNotNull(Q), param_count = 0) forall Q
generate_unary_op(query, param_count, parenthesis: true, suffix: " IS NOT NULL")
end

Expand All @@ -162,24 +151,24 @@ module ActiveRecord
Query.new(":#{param_count}", {"#{param_count}" => query})
end

def _generate(query : Array(T), param_count = 0)
def _generate(query : Array(T), param_count = 0) forall T
result, param_count = ArrayQueryHandler.new { |name| ":#{name}" }.handle(query)
result
end

def _generate(query, params_count)
def _generate(query : T, params_count) forall T
raise Fail.new
end

private def generate_binary_op(query, separator, param_count, parenthesis = false)
private def generate_binary_op(query : ::Query::BiOperator(Q, T), separator, param_count, parenthesis = false) forall Q, T
query_a = _generate(query.left, param_count)
param_count += query_a.params.keys.size

query_b = _generate(query.right, param_count)
query_a.concat_with(separator, query_b, parenthesis)
end

private def generate_unary_op(query, param_count, parenthesis = false, prefix = "", suffix = "")
private def generate_unary_op(query : ::Query::UOperator(Q), param_count, parenthesis = false, prefix = "", suffix = "") forall Q
query_a = _generate(query.query, param_count)

query_a.wrap_with(prefix, suffix, parenthesis)
Expand Down
2 changes: 1 addition & 1 deletion src/types.cr
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ module ActiveRecord
end

macro method_missing(call)
{{default}}.\{{call.name.id}}(\{{call.args.argify}}) \{{call.block}}
{{default}}.\{{call.name.id}}(\{{call.args.splat}}) \{{call.block}}
end
{{:end.id}}

Expand Down

0 comments on commit da31e37

Please sign in to comment.