Skip to content

Commit

Permalink
Quotes CTE names
Browse files Browse the repository at this point in the history
Fixes #130
  • Loading branch information
danmcclain committed Jul 18, 2014
1 parent 0fff859 commit 29df2f2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
27 changes: 14 additions & 13 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
predicate (#111) - Dan McClain
* Adds support for Rails 4.1 - Dan McClain
* Adds support for hstore columns when using `contains` (#120) - Dan McClain
* Quotes CTE names (#130) - Dan McClain

## 2.2.0

Expand All @@ -14,37 +15,37 @@
* Relation.with now accepts Arel::SelectMangers - Dan McClain

## 2.1.3

* Fixes Arel 4.0.1 issues - Dan McClain
* Prevents coversion of string to order statement - Dan McClain

## 2.1.2

* Fixes calls to count when ranking a relation - Dan McClain

## 2.1.1

* Fixes cte proxy so that it can create records - Dan McClain

## 2.1.0

* Support added for common table expressions - Dan McClain
* Support added for rank windowing function - Dan McClain
* Insert Code Climate badge into README - Doug Yun

# 2.0.0

* JRuby fixes - Dan McClain
* Updates docs and description - Dan McClain
* Rails 4 support - Dan McClain

# 1.0.0

1.0.0 is the last major and minor release for Rails 3.2.x. Postgres\_ext
will only receive bug fixes in the future. Also, bug fixes for 1.0.x
will come from PRs only, future development efforts are concentrated on
2.x.

* Fixing array tests in jruby - Dan McClain
* Removes encoding patches from PostgreSQLAdapter - Dan McClain
* Update documentation to reflect changes in 0.3.0 - Fabian Schwahn
Expand Down Expand Up @@ -137,7 +138,7 @@ encoded strings - Michael Graff (@skandragon)

## 0.0.8

Fixes add and change column
Fixes add and change column

## 0.0.7

Expand All @@ -151,5 +152,5 @@ Lots of array related fixes:
array column
* Array columns follow database defaults

Migration fix (rn0 and gilltots)
Migration fix (rn0 and gilltots)
Typos in README (bcardarella)
2 changes: 1 addition & 1 deletion lib/postgres_ext/active_record/relation/query_methods.rb
Expand Up @@ -162,7 +162,7 @@ def build_with(arel)
when Arel::SelectManager
select = Arel::SqlLiteral.new visitor.accept(expression)
end
as = Arel::Nodes::As.new Arel::SqlLiteral.new(name.to_s), select
as = Arel::Nodes::As.new Arel::SqlLiteral.new("\"#{name.to_s}\""), select
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions test/queries/common_table_expression_test.rb
Expand Up @@ -4,37 +4,37 @@
describe '.with(common_table_expression_hash)' do
it 'generates an expression with the CTE' do
query = Person.with(lucky_number_seven: Person.where(lucky_number: 7)).joins('JOIN lucky_number_seven ON lucky_number_seven.id = people.id')
query.to_sql.must_equal 'WITH lucky_number_seven AS (SELECT "people".* FROM "people" WHERE "people"."lucky_number" = 7) SELECT "people".* FROM "people" JOIN lucky_number_seven ON lucky_number_seven.id = people.id'
query.to_sql.must_equal 'WITH "lucky_number_seven" AS (SELECT "people".* FROM "people" WHERE "people"."lucky_number" = 7) SELECT "people".* FROM "people" JOIN lucky_number_seven ON lucky_number_seven.id = people.id'
end

it 'generates an expression with multiple CTEs' do
query = Person.with(lucky_number_seven: Person.where(lucky_number: 7), lucky_number_three: Person.where(lucky_number: 3)).joins('JOIN lucky_number_seven ON lucky_number_seven.id = people.id').joins('JOIN lucky_number_three ON lucky_number_three.id = people.id')
query.to_sql.must_equal 'WITH lucky_number_seven AS (SELECT "people".* FROM "people" WHERE "people"."lucky_number" = 7), lucky_number_three AS (SELECT "people".* FROM "people" WHERE "people"."lucky_number" = 3) SELECT "people".* FROM "people" JOIN lucky_number_seven ON lucky_number_seven.id = people.id JOIN lucky_number_three ON lucky_number_three.id = people.id'
query.to_sql.must_equal 'WITH "lucky_number_seven" AS (SELECT "people".* FROM "people" WHERE "people"."lucky_number" = 7), "lucky_number_three" AS (SELECT "people".* FROM "people" WHERE "people"."lucky_number" = 3) SELECT "people".* FROM "people" JOIN lucky_number_seven ON lucky_number_seven.id = people.id JOIN lucky_number_three ON lucky_number_three.id = people.id'
end

it 'generates an expression with multiple with calls' do
query = Person.with(lucky_number_seven: Person.where(lucky_number: 7)).with(lucky_number_three: Person.where(lucky_number: 3)).joins('JOIN lucky_number_seven ON lucky_number_seven.id = people.id').joins('JOIN lucky_number_three ON lucky_number_three.id = people.id')
query.to_sql.must_equal 'WITH lucky_number_seven AS (SELECT "people".* FROM "people" WHERE "people"."lucky_number" = 7), lucky_number_three AS (SELECT "people".* FROM "people" WHERE "people"."lucky_number" = 3) SELECT "people".* FROM "people" JOIN lucky_number_seven ON lucky_number_seven.id = people.id JOIN lucky_number_three ON lucky_number_three.id = people.id'
query.to_sql.must_equal 'WITH "lucky_number_seven" AS (SELECT "people".* FROM "people" WHERE "people"."lucky_number" = 7), "lucky_number_three" AS (SELECT "people".* FROM "people" WHERE "people"."lucky_number" = 3) SELECT "people".* FROM "people" JOIN lucky_number_seven ON lucky_number_seven.id = people.id JOIN lucky_number_three ON lucky_number_three.id = people.id'
end

it 'generates an expression with recursive' do
query = Person.with.recursive(lucky_number_seven: Person.where(lucky_number: 7)).joins('JOIN lucky_number_seven ON lucky_number_seven.id = people.id')
query.to_sql.must_equal 'WITH RECURSIVE lucky_number_seven AS (SELECT "people".* FROM "people" WHERE "people"."lucky_number" = 7) SELECT "people".* FROM "people" JOIN lucky_number_seven ON lucky_number_seven.id = people.id'
query.to_sql.must_equal 'WITH RECURSIVE "lucky_number_seven" AS (SELECT "people".* FROM "people" WHERE "people"."lucky_number" = 7) SELECT "people".* FROM "people" JOIN lucky_number_seven ON lucky_number_seven.id = people.id'
end

it 'accepts Arel::SelectMangers' do
arel_table = Arel::Table.new 'test'
arel_manager = arel_table.project arel_table[:foo]

query = Person.with(testing: arel_manager)
query.to_sql.must_equal 'WITH testing AS (SELECT "test"."foo" FROM "test") SELECT "people".* FROM "people"'
query.to_sql.must_equal 'WITH "testing" AS (SELECT "test"."foo" FROM "test") SELECT "people".* FROM "people"'
end
end

describe '.from_cte(common_table_expression_hash)' do
it 'generates an expression with the CTE as the main table' do
query = Person.from_cte('lucky_number_seven', Person.where(lucky_number: 7)).where(id: 5)
query.to_sql.must_equal 'WITH lucky_number_seven AS (SELECT "people".* FROM "people" WHERE "people"."lucky_number" = 7) SELECT "lucky_number_seven".* FROM "lucky_number_seven" WHERE "lucky_number_seven"."id" = 5'
query.to_sql.must_equal 'WITH "lucky_number_seven" AS (SELECT "people".* FROM "people" WHERE "people"."lucky_number" = 7) SELECT "lucky_number_seven".* FROM "lucky_number_seven" WHERE "lucky_number_seven"."id" = 5'
end

it 'returns instances of the model' do
Expand Down

0 comments on commit 29df2f2

Please sign in to comment.