Skip to content

Commit

Permalink
Merge pull request #28 from crystal-garage/fix-MultilineCurlyBlock
Browse files Browse the repository at this point in the history
Use `do`...`end` instead of curly brackets for multi-line blocks
  • Loading branch information
mamantoha authored Dec 5, 2024
2 parents d37727a + d230a2e commit d47d1b2
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 51 deletions.
4 changes: 2 additions & 2 deletions spec/extensions/jsonb_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ module JSONBSpec

it "uses @> operator when it can !" do
Clear::SQL.select("*").from(:users)
.where {
.where do
(data.jsonb("security.role") == "admin") &
(data.jsonb("security.level") == 1)
}.to_sql
end.to_sql
.should eq "SELECT * FROM \"users\" WHERE (\"data\" @> '{\"security\":{\"role\":\"admin\"}}' AND " +
"\"data\" @> '{\"security\":{\"level\":1}}')"
end
Expand Down
4 changes: 2 additions & 2 deletions spec/extensions/time_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ module IntervalSpec
end

it "be used in expression engine" do
IntervalModel.query.where {
IntervalModel.query.where do
(created_at - Clear::Interval.new(months: 1)) > updated_at
}.to_sql.should eq %(SELECT * FROM "interval_table" WHERE (("created_at" - INTERVAL '1 months') > "updated_at"))
end.to_sql.should eq %(SELECT * FROM "interval_table" WHERE (("created_at" - INTERVAL '1 months') > "updated_at"))
end

it "be casted into string" do
Expand Down
20 changes: 9 additions & 11 deletions spec/model/collection_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,7 @@ module CollectionSpec
qry[2].first_name.should eq("user 2")
qry[10]?.should be_nil

expect_raises(Clear::SQL::RecordNotFoundError) {
qry[11]
}
expect_raises(Clear::SQL::RecordNotFoundError) { qry[11] }
end
end

Expand All @@ -528,9 +526,9 @@ module CollectionSpec
User.query.find! { first_name == "user 2" }.first_name.should eq("user 2")
User.query.find { first_name == "not_exists" }.should be_nil

expect_raises(Clear::SQL::RecordNotFoundError) {
expect_raises(Clear::SQL::RecordNotFoundError) do
User.query.find! { first_name == "not_exists" }
}
end
end
end

Expand All @@ -545,9 +543,9 @@ module CollectionSpec
User.query.find!({first_name: "user 2"}).first_name.should eq("user 2")
User.query.find({first_name: "not_exists"}).should be_nil

expect_raises(Clear::SQL::RecordNotFoundError) {
expect_raises(Clear::SQL::RecordNotFoundError) do
User.query.find!({first_name: "not_exists"})
}
end
end
end

Expand All @@ -562,9 +560,9 @@ module CollectionSpec
User.query.find!(first_name: "first 2", last_name: "last 2").first_name.should eq("first 2")
User.query.find(first_name: "not_exists").should be_nil

expect_raises(Clear::SQL::RecordNotFoundError) {
expect_raises(Clear::SQL::RecordNotFoundError) do
User.query.find!(first_name: "not_exists")
}
end
end
end
end
Expand All @@ -581,10 +579,10 @@ module CollectionSpec
if post = Post
.query
.join("users") { users.id == posts.user_id }
.find {
.find do
(users.first_name == "user") &
(posts.title == "title 2")
}
end
(post.id).should eq(post2.id)
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/model/model_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ module ModelSpec
u.save! # Create a new user

u2 = User.new({id: 1, first_name: "Louis"})
u2.save! { |qry|
qry.on_conflict("(id)").do_update { |up|
u2.save! do |qry|
qry.on_conflict("(id)").do_update do |up|
up.set("first_name = excluded.first_name")
.where { users.id == excluded.id }
}
}
end
end

User.query.count.should eq 1
User.query.first!.first_name.should eq("Louis")
Expand Down
8 changes: 4 additions & 4 deletions spec/sql/insert_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ module InsertSpec
"INSERT INTO \"users\" (\"a\", \"b\") VALUES ('c', 12) ON CONFLICT (a) DO NOTHING"
)

req = insert_request.values({a: "c", b: 12}).on_conflict("(b)").do_update { |upd|
req = insert_request.values({a: "c", b: 12}).on_conflict("(b)").do_update do |upd|
upd.set(a: 1).where { b == 2 }
}
end

req.to_sql.should eq(
%(INSERT INTO "users" ("a", "b") VALUES ('c', 12) ON CONFLICT (b) DO UPDATE SET "a" = 1 WHERE ("b" = 2))
)

req = insert_request.values({a: "c", b: 12}).on_conflict { age < 18 }.do_update { |upd|
req = insert_request.values({a: "c", b: 12}).on_conflict { age < 18 }.do_update do |upd|
upd.set(a: 1).where { b == 2 }
}
end

req.to_sql.should eq(
%(INSERT INTO "users" ("a", "b") VALUES ('c', 12) ON CONFLICT WHERE ("age" < 18) DO UPDATE SET "a" = 1 WHERE ("b" = 2))
Expand Down
12 changes: 6 additions & 6 deletions spec/sql/select/having_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -130,27 +130,27 @@ module HavingSpec
describe "HAVING Expression engine Nodes" do
it "stack with `AND` operator" do
now = Time.local
r = Clear::SQL.select.from(:users).having { users.id == nil }.having {
r = Clear::SQL.select.from(:users).having { users.id == nil }.having do
var("users", "updated_at") >= now
}
end
r.to_sql.should eq "SELECT * FROM \"users\" HAVING (\"users\".\"id\" IS NULL) " +
"AND (\"users\".\"updated_at\" >= #{Clear::Expression[now]})"
end

it "stack with `OR` operator" do
now = Time.local
r = Clear::SQL.select.from(:users).having { users.id == nil }.or_having {
r = Clear::SQL.select.from(:users).having { users.id == nil }.or_having do
var("users", "updated_at") >= now
}
end
r.to_sql.should eq "SELECT * FROM \"users\" HAVING ((\"users\".\"id\" IS NULL) " +
"OR (\"users\".\"updated_at\" >= #{Clear::Expression[now]}))"
end

it "AND and OR" do
r = Clear::SQL.select.from(:users).having {
r = Clear::SQL.select.from(:users).having do
((raw("users.id") > 100) & (raw("users.visible") == true)) |
(raw("users.role") == "superadmin")
}
end

r.to_sql.should eq "SELECT * FROM \"users\" HAVING (((users.id > 100) " +
"AND (users.visible = TRUE)) OR (users.role = 'superadmin'))"
Expand Down
12 changes: 6 additions & 6 deletions spec/sql/select/where_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -144,27 +144,27 @@ module WhereSpec
describe "where expressions" do
it "where.where" do
now = Time.local
r = Clear::SQL.select.from(:users).where { users.id == nil }.where {
r = Clear::SQL.select.from(:users).where { users.id == nil }.where do
var("users", "updated_at") >= now
}
end
r.to_sql.should eq "SELECT * FROM \"users\" WHERE (\"users\".\"id\" IS NULL) " +
"AND (\"users\".\"updated_at\" >= #{Clear::Expression[now]})"
end

it "where.or_where" do
now = Time.local
r = Clear::SQL.select.from(:users).where { users.id == nil }.or_where {
r = Clear::SQL.select.from(:users).where { users.id == nil }.or_where do
var("users", "updated_at") >= now
}
end
r.to_sql.should eq "SELECT * FROM \"users\" WHERE ((\"users\".\"id\" IS NULL) " +
"OR (\"users\".\"updated_at\" >= #{Clear::Expression[now]}))"
end

it "op(:&)/op(:|)" do
r = Clear::SQL.select.from(:users).where {
r = Clear::SQL.select.from(:users).where do
((raw("users.id") > 100) & (raw("users.visible") == true)) |
(raw("users.role") == "superadmin")
}
end

r.to_sql.should eq "SELECT * FROM \"users\" WHERE (((users.id > 100) " +
"AND (users.visible = TRUE)) OR (users.role = 'superadmin'))"
Expand Down
4 changes: 2 additions & 2 deletions src/clear/error_messages.cr
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ module Clear::ErrorMessages
if manual_pages.size > 0
{
"You may want to check the manual:",
manual_pages.join("\n") { |x|
manual_pages.join("\n") do |x|
build_url("https://github.com/anykeyh/clear/tree/master/manual/#{x}")
},
end,
}.join("\n") + "\n\n"
end
end
Expand Down
8 changes: 4 additions & 4 deletions src/clear/migration/migration.cr
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,17 @@ module Clear::Migration
change(dir)

dir.up do
@operations.each { |op|
@operations.each do |op|
op.up.each { |x| Clear::SQL.execute(x.as(String)) }
}
end

SQL.insert("__clear_metadatas", {metatype: "migration", value: uid.to_s}).execute
end

dir.down do
@operations.reverse_each { |op|
@operations.reverse_each do |op|
op.down.each { |x| Clear::SQL.execute(x.as(String)) }
}
end

SQL.delete("__clear_metadatas").where({metatype: "migration", value: uid.to_s}).execute
end
Expand Down
4 changes: 2 additions & 2 deletions src/clear/model/collection.cr
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ module Clear::Model
any_hash = {} of String => Clear::SQL::Any

# remove terms which are not real value but conditions like range or array
hash.each { |k, v|
hash.each do |k, v|
any_hash[k] = v if v.is_a?(Clear::SQL::Any)
}
end

tags(any_hash)

Expand Down
4 changes: 2 additions & 2 deletions src/clear/model/modules/has_validation.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ module Clear::Model::HasValidation
def valid?
clear_errors

with_triggers(:validate) {
with_triggers(:validate) do
validate
validate_fields_presence # < This is built by the column system using Union type !!
}
end

!error?
end
Expand Down
4 changes: 2 additions & 2 deletions src/clear/model/reflection/table.cr
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ class Clear::Reflection::Table
column_name: "a.attname",
})
.from({t: "pg_class", i: "pg_class", ix: "pg_index", a: "pg_attribute"})
.where {
.where do
(t.oid == ix.indrelid) &
(i.oid == ix.indexrelid) &
(a.attrelid == t.oid) &
(a.attnum == raw("ANY(ix.indkey)")) &
(t.relkind == "r") &
(t.relname == self.table_name)
}
end
.order_by("t.relname").order_by("i.relname")
.fetch do |h|
col = h["column_name"].to_s
Expand Down
4 changes: 2 additions & 2 deletions src/clear/sql/insert_query.cr
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ class Clear::SQL::InsertQuery

protected def print_values
v = @values.as(Array(Array(Inserable)))
v.map_with_index { |row, idx|
v.map_with_index do |row, idx|
raise QueryBuildingError.new "No value to insert (at row ##{idx})" if row.empty?

"(" + row.join(", ") { |x| Clear::Expression[x] } + ")"
}.join(",\n")
end.join(",\n")
end

def to_sql
Expand Down
4 changes: 2 additions & 2 deletions src/clear/sql/query/order_by.cr
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ module Clear::SQL::Query::OrderBy
#
# return `self`
def reverse_order_by
@order_bys = @order_bys.map { |rec|
@order_bys = @order_bys.map do |rec|
Record.new(rec.op,
rec.dir == :desc ? :asc : :desc,
rec.nulls.try { |n| n == :nulls_last ? :nulls_first : :nulls_last }
)
}
end
change!
end

Expand Down

0 comments on commit d47d1b2

Please sign in to comment.