Skip to content

Commit

Permalink
Implement function criteria (#912)
Browse files Browse the repository at this point in the history
* Add updated? and created? methods on SaveOperations

Implements #888.
Adds two new boolean methods to SaveOperation.
They work similarly to new_record? but always return false if the
record was not persisted (e.g: failed save or not-yet-performed
operation)

* Implement date function

* Move upper and lower to new criterias

* generalize the application of functions to columns

* Put in standby the extra-param function creation

* Final clean up
  • Loading branch information
davidepaolotua authored Dec 2, 2022
1 parent 10427f3 commit 87b7578
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 24 deletions.
6 changes: 6 additions & 0 deletions spec/avram/string_criteria_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ describe String::Lucky::Criteria do
end
end

describe "trim" do
it "uses TRIM" do
name.trim.eq("elon").to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM users WHERE TRIM(users.name) = $1", "elon"]
end
end

describe "not" do
describe "with chained criteria" do
it "negates the following criteria" do
Expand Down
5 changes: 5 additions & 0 deletions spec/avram/time_criteria_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ describe Time::Lucky::Criteria do
end
end

it "as_date" do
input_date = "2012-01-31"
activated_at.as_date.eq(input_date).to_sql.should eq ["SELECT users.id, users.created_at, users.updated_at, users.activated_at FROM users WHERE DATE(users.activated_at) = $1", input_date]
end

describe "extract" do
it "fails with non supported symbol" do
expect_raises(ArgumentError) { activated_at.extract(:dayz).eq(5).to_sql }
Expand Down
24 changes: 3 additions & 21 deletions src/avram/charms/string_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class String

class Criteria(T, V) < Avram::Criteria(T, V)
include Avram::IncludesCriteria(T, V)
@upper = false
@lower = false

def like(value : String) : T
add_clause(Avram::Where::Like.new(column, value))
Expand All @@ -40,25 +38,9 @@ class String
add_clause(Avram::Where::Ilike.new(column, value))
end

def upper
@upper = true
self
end

def lower
@lower = true
self
end

def column
if @upper
"UPPER(#{@column})"
elsif @lower
"LOWER(#{@column})"
else
@column
end
end
define_function_criteria(upper, V)
define_function_criteria(lower, V)
define_function_criteria(trim, String)
end
end
end
2 changes: 2 additions & 0 deletions src/avram/charms/time_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ struct Time
class Criteria(T, V) < Avram::Criteria(T, V)
include Avram::BetweenCriteria(T, V)
include Avram::ExtractCriteria

define_function_criteria(as_date, String, "DATE")
end
end
end
7 changes: 7 additions & 0 deletions src/avram/criteria.cr
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,11 @@ class Avram::Criteria(T, V)
sql_clause
end
end

macro define_function_criteria(name, output_type = V, sql_name = nil)
{% sql_name = sql_name ? sql_name.id : name.id.upcase %}
def {{name}}
Criteria(T,{{output_type}}).new(rows, "{{sql_name}}(#{column})")
end
end
end
2 changes: 1 addition & 1 deletion src/avram/criteria_extensions/between_criteria.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Avram::BetweenCriteria(T, V)
def between(low_value : V, high_value : V)
add_clauses([
Avram::Where::GreaterThanOrEqualTo.new(@column, V.adapter.to_db!(low_value)),
Avram::Where::LessThanOrEqualTo.new(@column, V.adapter.to_db!(high_value))
Avram::Where::LessThanOrEqualTo.new(@column, V.adapter.to_db!(high_value)),
])
end
end
Expand Down
1 change: 0 additions & 1 deletion src/avram/define_attribute.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ module Avram::DefineAttribute
ATTRIBUTES = [] of Nil
\{% end %}


\{% if !@type.ancestors.first.abstract? %}
\{% for attribute in @type.ancestors.first.constant :ATTRIBUTES %}
\{% ATTRIBUTES << attribute %}
Expand Down
2 changes: 1 addition & 1 deletion src/avram/model.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ abstract class Avram::Model
end

macro inherited
COLUMNS = [] of Nil # types are not checked in macros
COLUMNS = [] of Nil # types are not checked in macros
ASSOCIATIONS = [] of Nil # types are not checked in macros
include LuckyCache::Cachable
end
Expand Down

0 comments on commit 87b7578

Please sign in to comment.