This repository has been archived by the owner on Oct 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build a query with a table. Add wheres and limits. Then build it. In future passes I want to make things more flexible. Also need to figure out where escaping values should go.
- Loading branch information
1 parent
42137cd
commit d3e7c25
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require "../spec_helper" | ||
|
||
describe "LuckyRecord::Query" do | ||
it "selects all" do | ||
new_query.to_sql.should eq "SELECT * FROM users" | ||
end | ||
|
||
it "can be limited" do | ||
query = new_query.limit(1) | ||
query.to_sql.should eq "SELECT * FROM users LIMIT 1" | ||
end | ||
|
||
it "handles simple where comparison" do | ||
query = new_query.where_eq(:name, "'Paul'") | ||
query.to_sql.should eq "SELECT * FROM users WHERE name = 'Paul'" | ||
end | ||
|
||
it "handles >" do | ||
query = new_query.where_gt(:age, 20) | ||
query.to_sql.should eq "SELECT * FROM users WHERE age > 20" | ||
end | ||
|
||
it "handles >=" do | ||
query = new_query.where_gte(:age, 20) | ||
query.to_sql.should eq "SELECT * FROM users WHERE age >= 20" | ||
end | ||
|
||
it "handles <" do | ||
query = new_query.where_lt(:age, 20) | ||
query.to_sql.should eq "SELECT * FROM users WHERE age < 20" | ||
end | ||
|
||
it "handles <=" do | ||
query = new_query.where_lte(:age, 20) | ||
query.to_sql.should eq "SELECT * FROM users WHERE age <= 20" | ||
end | ||
end | ||
|
||
private def new_query | ||
LuckyRecord::Query.new(table: :users) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
class LuckyRecord::Query | ||
private getter :table | ||
@limit : Int32? | ||
@wheres = [] of String | ||
|
||
def initialize(@table : Symbol) | ||
end | ||
|
||
def to_sql | ||
sql_clauses.reject do |clause| | ||
clause.nil? || clause.blank? | ||
end.join(" ") | ||
end | ||
|
||
private def sql_clauses | ||
[select_sql, wheres_sql, limit_sql] | ||
end | ||
|
||
private def select_sql | ||
"SELECT * FROM #{table}" | ||
end | ||
|
||
def limit(amount) | ||
@limit = amount | ||
self | ||
end | ||
|
||
private def limit_sql | ||
if @limit | ||
"LIMIT #{@limit}" | ||
end | ||
end | ||
|
||
def where_eq(column, value) | ||
@wheres << "WHERE #{column} = #{value}" | ||
self | ||
end | ||
|
||
def where_gt(column, value) | ||
@wheres << "WHERE #{column} > #{value}" | ||
self | ||
end | ||
|
||
def where_gte(column, value) | ||
@wheres << "WHERE #{column} >= #{value}" | ||
self | ||
end | ||
|
||
def where_lt(column, value) | ||
@wheres << "WHERE #{column} < #{value}" | ||
self | ||
end | ||
|
||
def where_lte(column, value) | ||
@wheres << "WHERE #{column} <= #{value}" | ||
self | ||
end | ||
|
||
private def wheres_sql | ||
@wheres.join(" ") | ||
end | ||
end |