Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use quotes when referencing columns #1059

Merged
merged 5 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions db/migrations/20240818230651_create_notes.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CreateNotes::V20240818230651 < Avram::Migrator::Migration::V1
def migrate
create table_for(Note) do
primary_key id : Int64
add_timestamps
add from : String
add read : Bool, default: false
add text : String
end
end

def rollback
drop table_for(Note)
end
end
6 changes: 3 additions & 3 deletions spec/avram/bool_criteria_spec.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "../spec_helper"

private class QueryMe < BaseModel
COLUMN_SQL = "users.id, users.created_at, users.updated_at, users.admin"
COLUMN_SQL = %("users"."id", "users"."created_at", "users"."updated_at", "users"."admin")

table users do
column admin : Bool
Expand All @@ -11,8 +11,8 @@ end
describe Bool::Lucky::Criteria do
describe "is" do
it "=" do
admin.eq(true).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM users WHERE users.admin = $1", "true"]
admin.eq(false).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM users WHERE users.admin = $1", "false"]
admin.eq(true).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM users WHERE \"users\".\"admin\" = $1", "true"]
jwoertink marked this conversation as resolved.
Show resolved Hide resolved
admin.eq(false).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM users WHERE \"users\".\"admin\" = $1", "false"]
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/avram/float_criteria_spec.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "../spec_helper"

private class QueryMe < BaseModel
COLUMN_SQL = "purchases.id, purchases.created_at, purchases.updated_at, purchases.amount"
COLUMN_SQL = %("purchases"."id", "purchases"."created_at", "purchases"."updated_at", "purchases"."amount")

table purchases do
column amount : Float64
Expand All @@ -11,19 +11,19 @@ end
describe Float64::Lucky::Criteria do
describe "abs" do
it "uses ABS" do
amount.abs.eq(39.99).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM purchases WHERE ABS(purchases.amount) = $1", "39.99"]
amount.abs.eq(39.99).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM purchases WHERE ABS(\"purchases\".\"amount\") = $1", "39.99"]
end
end

describe "ceil" do
it "uses CEIL" do
amount.ceil.eq(40.0).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM purchases WHERE CEIL(purchases.amount) = $1", "40.0"]
amount.ceil.eq(40.0).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM purchases WHERE CEIL(\"purchases\".\"amount\") = $1", "40.0"]
end
end

describe "floor" do
it "uses FLOOR" do
amount.floor.eq(39.0).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM purchases WHERE FLOOR(purchases.amount) = $1", "39.0"]
amount.floor.eq(39.0).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM purchases WHERE FLOOR(\"purchases\".\"amount\") = $1", "39.0"]
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/avram/integer_criteria_spec.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "../spec_helper"

private class QueryMe < BaseModel
COLUMN_SQL = "transactions.id, transactions.created_at, transactions.updated_at, transactions.small_amount, transactions.amount, transactions.big_amount"
COLUMN_SQL = %("transactions"."id", "transactions"."created_at", "transactions"."updated_at", "transactions"."small_amount", "transactions"."amount", "transactions"."big_amount")

table transactions do
column small_amount : Int16
Expand All @@ -14,9 +14,9 @@ end
describe "Int::Lucky::Criteria" do
describe "abs" do
it "uses ABS" do
small_amount.abs.eq(4).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM transactions WHERE ABS(transactions.small_amount) = $1", "4"]
amount.abs.eq(400).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM transactions WHERE ABS(transactions.amount) = $1", "400"]
big_amount.abs.eq(40000).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM transactions WHERE ABS(transactions.big_amount) = $1", "40000"]
small_amount.abs.eq(4).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM transactions WHERE ABS(\"transactions\".\"small_amount\") = $1", "4"]
amount.abs.eq(400).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM transactions WHERE ABS(\"transactions\".\"amount\") = $1", "400"]
big_amount.abs.eq(40000).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM transactions WHERE ABS(\"transactions\".\"big_amount\") = $1", "40000"]
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions spec/avram/model_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ describe Avram::Model do
user.email.to_s.should eq "[email protected]"
end

it "allows columns named the same as reserved SQL names" do
NoteFactory.create(&.from("Me").text("hi").read(true))
note = NoteQuery.new.from("Me").first
note.from.should eq("Me")
note.text.should eq("hi")
note.read?.should eq(true)
Note::SaveOperation.update!(note, read: false)
note = note.reload
note.read?.should eq(false)
Note::DeleteOperation.delete!(note)
NoteQuery.new.select_count.should eq(0)
end

describe "reload" do
it "can reload a model" do
user = UserFactory.create &.name("Original Name")
Expand Down
4 changes: 4 additions & 0 deletions spec/support/factories/note_factory.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class NoteFactory < BaseFactory
def initialize
end
end
10 changes: 10 additions & 0 deletions spec/support/models/note.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Note < BaseModel
table do
column from : String
column read : Bool = false
column text : String
end
end

class NoteQuery < Note::BaseQuery
end
2 changes: 1 addition & 1 deletion src/avram/base_query_template.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Avram::BaseQueryTemplate

macro generate_criteria_method(name, type)
def \{{ name }}
\{{ type }}.adapter.criteria(self, "#{table_name}.\{{ name }}")
\{{ type }}.adapter.criteria(self, %("#{table_name}"."\{{ name }}"))
end
end

Expand Down
4 changes: 2 additions & 2 deletions src/avram/insert.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Avram::Insert
if @column_names.empty?
"*"
else
@column_names.join(", ") { |column| "#{@table}.#{column}" }
@column_names.join(", ") { |column| %("#{@table}"."#{column}") }
end
end

Expand All @@ -21,7 +21,7 @@ class Avram::Insert
end

private def fields : String
@params.keys.join(", ")
@params.keys.join(", ") { |col| %("#{col}") }
end

private def values_placeholders : String
Expand Down
2 changes: 1 addition & 1 deletion src/avram/migrator/columns/base.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ abstract class Avram::Migrator::Columns::Base

private def build_add_statement : String
String.build do |row|
row << name.to_s
row << %("#{name}")
row << " "
row << column_type + as_array_type
row << null_fragment
Expand Down
2 changes: 1 addition & 1 deletion src/avram/migrator/columns/primary_keys/base.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ abstract class Avram::Migrator::Columns::PrimaryKeys::Base
abstract def column_type

def build : String
%( #{name} #{column_type} PRIMARY KEY)
%( "#{name}" #{column_type} PRIMARY KEY)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Avram::Migrator::Columns::PrimaryKeys
end

def build : String
%( #{name} #{column_type} PRIMARY KEY DEFAULT gen_random_uuid())
%( "#{name}" #{column_type} PRIMARY KEY DEFAULT gen_random_uuid())
end
end
end
2 changes: 1 addition & 1 deletion src/avram/query_builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class Avram::QueryBuilder
end

def select(selection : Array(ColumnName)) : self
@selections = selection.join(", ") { |column| "#{@table}.#{column}" }
@selections = selection.join(", ") { |column| %("#{@table}"."#{column}") }
self
end

Expand Down
Loading