Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add SQL builder module and AST node classes - add .toBeLikeQuery matcher
- Loading branch information
1 parent
1faa86d
commit 89f7aa4
Showing
15 changed files
with
300 additions
and
1 deletion.
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
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,5 @@ | ||
module.exports = ({ Monarch, _ }) -> | ||
|
||
_.extend Monarch.Relations.Relation.prototype, | ||
toSql: -> | ||
(new Monarch.Sql.Builder).visit(this).toSql() |
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,11 @@ | ||
module.exports = ({ Monarch, _ }) -> | ||
|
||
class Monarch.Sql.BinaryOperator | ||
constructor: (@left, @right, @operator) -> | ||
|
||
toSql: -> | ||
[ | ||
@left.toSql(), | ||
@operator, | ||
@right.toSql() | ||
].join(' ') |
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,75 @@ | ||
module.exports = ({ Monarch, _ }) -> | ||
|
||
class Monarch.Sql.Builder | ||
visit: Monarch.Util.Visitor.visit | ||
|
||
visit_Relations_Table: (r) -> | ||
new Monarch.Sql.Query( | ||
new Monarch.Sql.TableRef(r.resourceName())) | ||
|
||
visit_Relations_Selection: (r) -> | ||
_.tap(@visit(r.operand), (query) => | ||
query.condition = @visit(r.predicate)) | ||
|
||
visit_Relations_OrderBy: (r) -> | ||
_.tap(@visit(r.operand), (query) => | ||
query.orderByExpressions = r.orderByExpressions.map (e) => @visit(e)) | ||
|
||
visit_Relations_Limit: (r) -> | ||
_.tap(@visit(r.operand), (query) => | ||
query.limitCount = r.count) | ||
|
||
visit_Relations_Offset: (r) -> | ||
_.tap(@visit(r.operand), (query) => | ||
query.offsetCount = r.count) | ||
|
||
visit_Relations_Union: (r) -> | ||
new Monarch.Sql.Union(@visit(r.left), @visit(r.right)) | ||
|
||
visit_Relations_Difference: (r) -> | ||
new Monarch.Sql.Difference(@visit(r.left), @visit(r.right)) | ||
|
||
visit_Relations_InnerJoin: (r) -> | ||
leftQuery = @visit(r.left) | ||
rightQuery = @visit(r.right) | ||
condition = @visit(r.predicate) | ||
|
||
new Monarch.Sql.Query( | ||
new Monarch.Sql.JoinTableRef( | ||
leftQuery.tableRef, | ||
rightQuery.tableRef, | ||
condition | ||
)) | ||
|
||
visit_Expressions_And: (e) -> | ||
visitBinaryOperator.call(this, e, "AND") | ||
|
||
visit_Expressions_Equal: (e) -> | ||
visitBinaryOperator.call(this, e, "=") | ||
|
||
visit_Expressions_Column: (e) -> | ||
new Monarch.Sql.Column(e.table.resourceName(), e.resourceName()) | ||
|
||
visit_Expressions_OrderBy: (e) -> | ||
new Monarch.Sql.OrderByExpression( | ||
e.column.table.resourceName(), | ||
e.column.resourceName(), | ||
directionString(e.directionCoefficient)) | ||
|
||
visit_String: (e) -> | ||
new Monarch.Sql.StringLiteral(e) | ||
|
||
visit_Boolean: (e) -> | ||
new Monarch.Sql.Literal(e) | ||
|
||
visit_Number: (e) -> | ||
new Monarch.Sql.Literal(e) | ||
|
||
visitBinaryOperator = (e, operator) -> | ||
new Monarch.Sql.BinaryOperator( | ||
@visit(e.left), | ||
@visit(e.right), | ||
operator) | ||
|
||
directionString = (coefficient) -> | ||
if (coefficient == -1) then 'DESC' else 'ASC' |
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,7 @@ | ||
module.exports = ({ Monarch, _ }) -> | ||
|
||
class Monarch.Sql.Column | ||
constructor: (@tableName, @name) -> | ||
|
||
toSql: -> | ||
""" "#{@tableName}"."#{@name}" """ |
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,7 @@ | ||
module.exports = ({ Monarch, _ }) -> | ||
|
||
class Monarch.Sql.Difference | ||
constructor: (@left, @right) -> | ||
|
||
toSql: -> | ||
[@left.toSql(), "EXCEPT", @right.toSql()].join(' ') |
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,13 @@ | ||
module.exports = ({ Monarch, _ }) -> | ||
|
||
class Monarch.Sql.JoinTableRef | ||
constructor: (@left, @right, @condition) -> | ||
|
||
toSql: -> | ||
[ | ||
@left.toSql(), | ||
"INNER JOIN", | ||
@right.toSql(), | ||
"ON", | ||
@condition.toSql() | ||
].join(' ') |
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,7 @@ | ||
module.exports = ({ Monarch, _ }) -> | ||
|
||
class Monarch.Sql.Literal | ||
constructor: (@value) -> | ||
|
||
toSql: -> | ||
@value.toString() |
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,9 @@ | ||
module.exports = ({ Monarch, _ }) -> | ||
|
||
class Monarch.Sql.OrderByExpression | ||
constructor: (@tableName, @columnName, @directionString) -> | ||
|
||
toSql: -> | ||
""" | ||
"#{@tableName}"."#{@columnName}" #{@directionString} | ||
""" |
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,37 @@ | ||
module.exports = ({ Monarch, _ }) -> | ||
|
||
class Monarch.Sql.Query | ||
constructor: (@tableRef) -> | ||
@condition = null | ||
@orderByExpressions = [] | ||
|
||
toSql: -> | ||
_.compact([ | ||
@selectClauseSql(), | ||
@fromClauseSql(), | ||
@whereClauseSql(), | ||
@orderByClauseSql(), | ||
@limitClauseSql(), | ||
@offsetClauseSql() | ||
]).join(' ') | ||
|
||
selectClauseSql: -> | ||
"SELECT *" | ||
|
||
fromClauseSql: -> | ||
"FROM " + @tableRef.toSql() | ||
|
||
whereClauseSql: -> | ||
"WHERE " + @condition.toSql() if @condition | ||
|
||
orderByClauseSql: -> | ||
if not _.isEmpty(@orderByExpressions) | ||
"ORDER BY " + @orderByExpressions.map((e) -> e.toSql()).join(', ') | ||
|
||
limitClauseSql: -> | ||
if @limitCount | ||
"LIMIT " + @limitCount | ||
|
||
offsetClauseSql: -> | ||
if @offsetCount | ||
"OFFSET " + @offsetCount |
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,5 @@ | ||
module.exports = ({ Monarch, _ }) -> | ||
|
||
class Monarch.Sql.StringLiteral extends Monarch.Sql.Literal | ||
toSql: -> | ||
"'" + super + "'" |
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,8 @@ | ||
module.exports = ({ Monarch, _ }) -> | ||
|
||
class Monarch.Sql.TableRef | ||
constructor: (@tableName) -> | ||
|
||
toSql: -> | ||
'"' + @tableName + '"' | ||
|
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,7 @@ | ||
module.exports = ({ Monarch, _ }) -> | ||
|
||
class Monarch.Sql.Union | ||
constructor: (@left, @right) -> | ||
|
||
toSql: -> | ||
[@left.toSql(), "UNION", @right.toSql()].join(' ') |
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 |
---|---|---|
@@ -1,3 +1,13 @@ | ||
beforeEach -> | ||
@addMatchers | ||
toBeLikeQuery: (sql) -> | ||
normalizeSql(@actual) == normalizeSql(sql) | ||
|
||
normalizeSql = (string) -> | ||
string | ||
.replace(/\s+/g, ' ') | ||
.replace(/[(\s*$)]/g, '') | ||
|
||
module.exports = | ||
Monarch: require "#{__dirname}/../../lib/server/index" | ||
_: require "underscore" |
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,95 @@ | ||
{ Monarch } = require "../spec_helper" | ||
|
||
describe "Sql.Builder", -> | ||
blogs = blogPosts = null | ||
|
||
class Blog extends Monarch.Record | ||
@extended(this) | ||
@columns | ||
public: 'boolean' | ||
title: 'string' | ||
authorId: 'integer' | ||
|
||
class BlogPost extends Monarch.Record | ||
@extended(this) | ||
@columns | ||
public: 'boolean' | ||
blogId: 'integer' | ||
title: 'string' | ||
|
||
beforeEach -> | ||
blogs = Blog.table | ||
blogPosts = BlogPost.table | ||
|
||
describe "tables", -> | ||
it "constructs a table query", -> | ||
expect(blogPosts.toSql()).toBeLikeQuery(""" | ||
SELECT * FROM "blog_posts" | ||
""") | ||
|
||
describe "selections", -> | ||
it "constructs a query with the right WHERE clause", -> | ||
relation = blogPosts.where({ public: true, blogId: 1 }) | ||
expect(relation.toSql()).toBeLikeQuery(""" | ||
SELECT * FROM "blog_posts" | ||
WHERE "blog_posts"."public" = true AND "blog_posts"."blog_id" = 1 | ||
""") | ||
|
||
it "quotes string literals correctly", -> | ||
relation = blogPosts.where({ title: "Node Fibers and You" }) | ||
expect(relation.toSql()).toBeLikeQuery(""" | ||
SELECT * FROM "blog_posts" | ||
WHERE "blog_posts"."title" = 'Node Fibers and You' | ||
""") | ||
|
||
describe "orderings", -> | ||
it "constructs a query with a valid order by clause", -> | ||
relation = blogPosts.orderBy("title desc") | ||
expect(relation.toSql()).toBeLikeQuery(""" | ||
SELECT * FROM "blog_posts" | ||
ORDER BY "blog_posts"."title" DESC, "blog_posts"."id" ASC | ||
""") | ||
|
||
describe "limits", -> | ||
it "constructs a limit query", -> | ||
relation = blogPosts.limit(5) | ||
expect(relation.toSql()).toBeLikeQuery(""" | ||
SELECT * FROM "blog_posts" LIMIT 5 | ||
""") | ||
|
||
it "constructs a limit query with an offset", -> | ||
relation = blogPosts.limit(5, 2) | ||
expect(relation.toSql()).toBeLikeQuery(""" | ||
SELECT * FROM "blog_posts" LIMIT 5 OFFSET 2 | ||
""") | ||
|
||
describe "unions", -> | ||
it "constructs a set union query", -> | ||
left = blogPosts.where(blogId: 5) | ||
right = blogPosts.where(public: true) | ||
relation = left.union(right) | ||
expect(relation.toSql()).toBeLikeQuery(""" | ||
SELECT * FROM "blog_posts" WHERE "blog_posts"."blog_id" = 5 | ||
UNION | ||
SELECT * FROM "blog_posts" WHERE "blog_posts"."public" = true | ||
""") | ||
|
||
describe "differences", -> | ||
it "constructs a set difference query", -> | ||
left = blogPosts.where(blogId: 5) | ||
right = blogPosts.where(public: true) | ||
relation = left.difference(right) | ||
expect(relation.toSql()).toBeLikeQuery(""" | ||
SELECT * FROM "blog_posts" WHERE "blog_posts"."blog_id" = 5 | ||
EXCEPT | ||
SELECT * FROM "blog_posts" WHERE "blog_posts"."public" = true | ||
""") | ||
|
||
describe "joins", -> | ||
it "constructs a join query", -> | ||
relation = blogs.join(blogPosts) | ||
expect(relation.toSql()).toBeLikeQuery(""" | ||
SELECT * FROM "blogs" | ||
INNER JOIN "blog_posts" | ||
ON "blogs"."id" = "blog_posts"."blog_id" | ||
""") |