mirror of
https://github.com/lloeki/rebel.git
synced 2025-12-06 10:04:39 +01:00
Compare commits
14 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0e82d96755 | |||
| 95ecac4946 | |||
| 071932e4be | |||
| 86ce2b65ff | |||
| a0f1153407 | |||
| 3bb41b81f0 | |||
| 31045461ab | |||
| 6ffa4e88f0 | |||
| 354f4a6860 | |||
| a1084539af | |||
| 93592329de | |||
| 02043ec233 | |||
| cfe0851f04 | |||
| f315679713 |
5 changed files with 428 additions and 104 deletions
314
README.md
314
README.md
|
|
@ -7,7 +7,7 @@ You've been fighting yet another abstraction...
|
||||||
Aren't you fed up with object-relation magic?
|
Aren't you fed up with object-relation magic?
|
||||||
But wait, here comes a humongous migration.
|
But wait, here comes a humongous migration.
|
||||||
Is ActiveRecord making you sick?
|
Is ActiveRecord making you sick?
|
||||||
To hell with that monstrous ARel expression!
|
To hell with that monstrous Arel expression!
|
||||||
Tell the truth, you were just wishing
|
Tell the truth, you were just wishing
|
||||||
That it was as simple as a here-string.
|
That it was as simple as a here-string.
|
||||||
But could it keep some Ruby notation
|
But could it keep some Ruby notation
|
||||||
|
|
@ -21,13 +21,11 @@ No bullshit
|
||||||
No layers
|
No layers
|
||||||
No wrappers
|
No wrappers
|
||||||
No smarty-pants
|
No smarty-pants
|
||||||
No weird stance
|
|
||||||
No sexy
|
No sexy
|
||||||
|
No nonsense
|
||||||
No AST
|
No AST
|
||||||
No lazy loading
|
No lazy loading
|
||||||
No crazy mapping
|
No crazy mapping
|
||||||
No pretense
|
|
||||||
No nonsense
|
|
||||||
|
|
||||||
What you write is what you get: readable and obvious
|
What you write is what you get: readable and obvious
|
||||||
What you write is what you meant: tasty and delicious
|
What you write is what you meant: tasty and delicious
|
||||||
|
|
@ -36,6 +34,314 @@ Wait, it doesn't execute!?
|
||||||
Just use your fave client gem, isn't that cute?
|
Just use your fave client gem, isn't that cute?
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Motivation
|
||||||
|
|
||||||
|
There are many a time where you end up knowing exactly what SQL query you want,
|
||||||
|
yet have to wrap your head around for the ORM to produce it, which is when the
|
||||||
|
point of such a layer is entirely defeated. Concatenating and interpolating
|
||||||
|
only goes so far.
|
||||||
|
|
||||||
|
As ActiveRecord grows, a significant decision has been taken in the Rails team
|
||||||
|
to turn Arel into a library purely internal to ActiveRecord: the whole of it is
|
||||||
|
basically considered internal and private, and only ActiveRecord's public
|
||||||
|
interface should be used. Unfortunately, some highly dynamic, complex queries
|
||||||
|
simply cannot be built using ActiveRecord, and concatenating strings to build
|
||||||
|
SQL fragments and clauses simply does not cut it.
|
||||||
|
|
||||||
|
## Philosophy
|
||||||
|
|
||||||
|
- It must be readable as being SQL
|
||||||
|
- Yet it must be as much Ruby syntax and types as possible
|
||||||
|
- It must be able to produce fragments for others to use
|
||||||
|
- And somehow be composable enough
|
||||||
|
- It must not rely on metaprogramming magic
|
||||||
|
- Nor need monkeypatching core types
|
||||||
|
|
||||||
|
## Design
|
||||||
|
|
||||||
|
There are two goals to this library:
|
||||||
|
|
||||||
|
- query building
|
||||||
|
- query execution
|
||||||
|
|
||||||
|
Query building is about assembling a string containing a partial or complete
|
||||||
|
query that you will later pass on to be executed by an executor.
|
||||||
|
|
||||||
|
Query execution is about writing a query that will be executed on the spot.
|
||||||
|
|
||||||
|
There are also non-goals to this library:
|
||||||
|
|
||||||
|
- be any sort of ORM
|
||||||
|
- or any sort of abstraction layer
|
||||||
|
- or any sort of query optimiser
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
`Rebel::SQL` is a module that contains building and execution features, and
|
||||||
|
output ANSI-style SQL.
|
||||||
|
|
||||||
|
`Rebel::SQL()` is a function that produces a customised module enabling support
|
||||||
|
for alternative dialects, and when passed a block, allows you to write things
|
||||||
|
more literally.
|
||||||
|
|
||||||
|
Ruby types are best-effort mapped to SQL entities in a simple, regular way:
|
||||||
|
|
||||||
|
- Symbols map to quoted SQL names such as tables, columns, aliases.
|
||||||
|
- Strings map to strings. Always. (Quote style can be configured).
|
||||||
|
- Integers and floats map to, well, integers and floats.
|
||||||
|
- Date, Time and DateTime map to their ISO 8601 string representation
|
||||||
|
- Booleans map to their respective ANSI literals (unless overriden by
|
||||||
|
configuration).
|
||||||
|
- `nil` maps to `NULL` and is expected to have the same "unknown" semantic
|
||||||
|
|
||||||
|
Variable arguments are generally used. Hashes, depending on context, map to:
|
||||||
|
|
||||||
|
- `=` equality or `IN` operators joined by `AND`
|
||||||
|
- `=` assignment operator joined by commas
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
### Query building
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
require 'rebel'
|
||||||
|
|
||||||
|
# Here's a typical query
|
||||||
|
Rebel::SQL.select :id, from: :customers, where: { :first_name => 'John', :last_name => 'Doe' }
|
||||||
|
=> SELECT "id" FROM "customers" WHERE "first_name" = 'John' AND "last_name" = 'Doe'
|
||||||
|
|
||||||
|
# More args give more columns
|
||||||
|
Rebel::SQL.select :first_name, :last_name, from: :customers, where: { :id => [1, 2, 3] }
|
||||||
|
=> SELECT "first_name", "last_name" FROM "customers" WHERE "id" IN (1, 2, 3)
|
||||||
|
|
||||||
|
# * is special-cased for names
|
||||||
|
Rebel::SQL.select :*, from: :customers, where: { :id => [1, 2, 3] }
|
||||||
|
=> SELECT * FROM "customers" WHERE "id" IN (1, 2, 3)
|
||||||
|
|
||||||
|
# You can emit fragments to produce clauses
|
||||||
|
puts Rebel::SQL.and_clause :id => [1, 2, 3], :country => 'GB'
|
||||||
|
=> "id" IN (1, 2, 3) AND "country" = 'GB'
|
||||||
|
Rebel::SQL.where? :id => [1, 2, 3], :country => 'GB'
|
||||||
|
=> WHERE "id" IN (1, 2, 3) AND "country" = 'GB'
|
||||||
|
|
||||||
|
# Here the question mark means where? swallows nil arguments: maybe it's a Maybe monad
|
||||||
|
Rebel::SQL.where?(nil)
|
||||||
|
=> nil
|
||||||
|
|
||||||
|
# Let's emit join clauses
|
||||||
|
Rebel::SQL.join(:contracts, on: :customer_id => :id)
|
||||||
|
#=> JOIN "contracts" ON "customer_id" = "id"
|
||||||
|
Rebel::SQL.join(:contracts).on(:customer_id => :id)
|
||||||
|
#=> JOIN "contracts" ON "customer_id" = "id"
|
||||||
|
|
||||||
|
# :contracts might have an :id too, so we can disambiguate those columns
|
||||||
|
Rebel::SQL.join(:contracts).on(:'contracts.customer_id' => :'customers.id')
|
||||||
|
#=> JOIN "contracts" ON "contracts"."customer_id" = "customers"."id"
|
||||||
|
|
||||||
|
# Other types of join are obviously available
|
||||||
|
Rebel::SQL.inner_join(:contracts).on(:'contracts.customer_id' => :'customers.id')
|
||||||
|
#=> INNER JOIN "contracts" ON "contracts"."customer_id" = "customers"."id"
|
||||||
|
Rebel::SQL.outer_join(:contracts).on(:'contracts.customer_id' => :'customers.id')
|
||||||
|
#=> OUTER JOIN "contracts" ON "contracts"."customer_id" = "customers"."id"
|
||||||
|
Rebel::SQL.left_outer_join(:contracts).on(:'contracts.customer_id' => :'customers.id')
|
||||||
|
#=> LEFT OUTER JOIN "contracts" ON "contracts"."customer_id" = "customers"."id"
|
||||||
|
Rebel::SQL.right_outer_join(:contracts).on(:'contracts.customer_id' => :'customers.id')
|
||||||
|
#=> RIGHT OUTER JOIN "contracts" ON "contracts"."customer_id" = "customers"."id"
|
||||||
|
|
||||||
|
# The type of join can be split off. Again, note the question mark.
|
||||||
|
Rebel::SQL.inner? Rebel::SQL.join(:contracts).on(:'contracts.customer_id' => :'customers.id')
|
||||||
|
#=> INNER JOIN "contracts" ON "contracts"."customer_id" = "customers"."id"
|
||||||
|
Rebel::SQL.left? Rebel::SQL.outer_join(:contracts).on(:'contracts.customer_id' => :'customers.id')
|
||||||
|
#=> LEFT OUTER JOIN "contracts" ON "contracts"."customer_id" = "customers"."id"
|
||||||
|
|
||||||
|
# And in a full query
|
||||||
|
Rebel::SQL.select :'customers.id', :'contracts.id',
|
||||||
|
from: :customers,
|
||||||
|
where: { :first_name => 'John', :last_name => 'Doe' },
|
||||||
|
inner: Rebel::SQL.join(:contracts).on(:'contracts.customer_id' => :'customers.id'),
|
||||||
|
order: Rebel::SQL.by(:'customer.age').asc
|
||||||
|
#=> SELECT "customers"."id", "contracts"."id"
|
||||||
|
# FROM "customers"
|
||||||
|
# INNER JOIN "contracts" ON "contracts"."customer_id" = "customers"."id"
|
||||||
|
# WHERE "first_name" = 'John' AND "last_name" = 'Doe'
|
||||||
|
# ORDER BY "customer"."age" ASC
|
||||||
|
|
||||||
|
# All those Rebel::SQL can get unwieldy, so let's reduce the noise
|
||||||
|
Rebel::SQL() do
|
||||||
|
select :'customers.id', :'contracts.id',
|
||||||
|
from: :customers,
|
||||||
|
where: { :first_name => 'John', :last_name => 'Doe' },
|
||||||
|
inner: join(:contracts).on(:'contracts.customer_id' => :'customers.id'),
|
||||||
|
order: by(:'customer.age').asc
|
||||||
|
end
|
||||||
|
#=> SELECT "customers"."id", "contracts"."id"
|
||||||
|
# FROM "customers"
|
||||||
|
# INNER JOIN "contracts" ON "contracts"."customer_id" = "customers"."id"
|
||||||
|
# WHERE "first_name" = 'John' AND "last_name" = 'Doe'
|
||||||
|
# ORDER BY "customer"."age" ASC
|
||||||
|
|
||||||
|
# Now, that function can be used to make things different
|
||||||
|
Rebel::SQL.name(:foo)
|
||||||
|
#=> "foo"
|
||||||
|
Rebel::SQL(identifier_quote: '`').name(:foo)
|
||||||
|
#=> `foo`
|
||||||
|
Rebel::SQL.value(true)
|
||||||
|
#=> TRUE
|
||||||
|
Rebel::SQL(true_literal: '1').value(true)
|
||||||
|
#=> 1
|
||||||
|
Rebel::SQL(true_literal: '1') { select value(true) }
|
||||||
|
#=> SELECT 1
|
||||||
|
|
||||||
|
# While we're at it, let's call arbitrary functions
|
||||||
|
Rebel::SQL() { select function('NOW') }
|
||||||
|
#=> SELECT NOW()
|
||||||
|
Rebel::SQL() { select function('LENGTH', "a string") }
|
||||||
|
#=> SELECT LENGTH('a string')
|
||||||
|
Rebel::SQL() { select function('COUNT', :id), from: :customers, where: { :age => 42 } }
|
||||||
|
#=> SELECT COUNT("id") FROM "customers" WHERE "age" = 42
|
||||||
|
Rebel::SQL() { select count(:id), from: :customers, where: { :age => 42 } }
|
||||||
|
#=> SELECT COUNT("id") FROM "customers" WHERE "age" = 42
|
||||||
|
|
||||||
|
# And throw in some aliases
|
||||||
|
Rebel::SQL() { select function('LENGTH', "a string").as(:length) }
|
||||||
|
#=> SELECT LENGTH('a string') AS "length"
|
||||||
|
Rebel::SQL() { select name(:id).as(:customer_id), from: :customers }
|
||||||
|
#=> SELECT "id" AS "customer_id" FROM "customers"
|
||||||
|
Rebel::SQL() { select count(:id).as(:count), from: :customers, where: { :age => 42 } }
|
||||||
|
#=> SELECT COUNT("id") FROM "customers" WHERE "age" = 42
|
||||||
|
|
||||||
|
# While we're counting things, let's group results
|
||||||
|
Rebel::SQL() { select count(:id).as(:count), :country, from: :customers, group: by(:country).having(count(:customer_id) => 5) }
|
||||||
|
#=> SELECT COUNT("id") AS "count", "country" FROM "customers" GROUP BY "country" HAVING COUNT("customer_id") = 5
|
||||||
|
|
||||||
|
# Passing a hash does a best effort to map Ruby to SQL
|
||||||
|
Rebel::SQL() { select :id, from: :customers, where: { :age => 42 } }
|
||||||
|
#=> SELECT "id" FROM "customers" WHERE "age" = 42
|
||||||
|
Rebel::SQL() { select :id, from: :customers, where: { :age => [20, 21, 22] } }
|
||||||
|
#=> SELECT "id" FROM "customers" WHERE "age" IN (20, 21, 22)
|
||||||
|
Rebel::SQL() { select :id, from: :customers, where: { :age => nil } }
|
||||||
|
#=> SELECT "id" FROM "customers" WHERE "age" IS NULL
|
||||||
|
|
||||||
|
# Using operators ensures the expected SQL operator is used
|
||||||
|
Rebel::SQL() { select :id, from: :customers, where: name(:age).eq(42) }
|
||||||
|
#=> SELECT "id" FROM "customers" WHERE "age" = 42
|
||||||
|
Rebel::SQL() { select :id, from: :customers, where: name(:age).eq(nil) }
|
||||||
|
#=> SELECT "id" FROM "customers" WHERE "age" = NULL
|
||||||
|
Rebel::SQL() { select :id, from: :customers, where: name(:age).ne(42) }
|
||||||
|
#=> SELECT "id" FROM "customers" WHERE "age" != NULL
|
||||||
|
Rebel::SQL() { select :id, from: :customers, where: name(:age).is(42) }
|
||||||
|
#=> SELECT "id" FROM "customers" WHERE "age" IS 42
|
||||||
|
Rebel::SQL() { select :id, from: :customers, where: name(:age).is(nil) }
|
||||||
|
#=> SELECT "id" FROM "customers" WHERE "age" IS NULL
|
||||||
|
Rebel::SQL() { select :id, from: :customers, where: name(:age).is_not(nil) }
|
||||||
|
#=> SELECT "id" FROM "customers" WHERE "age" IS NOT NULL
|
||||||
|
|
||||||
|
# Other operators are available
|
||||||
|
Rebel::SQL() { select :id, from: :customers, where: name(:age).ge(42) }
|
||||||
|
#=> SELECT "id" FROM "customers" WHERE "age" >= 42
|
||||||
|
Rebel::SQL() { select :id, from: :customers, where: name(:age).in(21, 22, 23) }
|
||||||
|
#=> SELECT "id" FROM "customers" WHERE "age" IN (21, 22, 23)
|
||||||
|
Rebel::SQL() { select :id, from: :customers, where: name(:first_name).like("J%") }
|
||||||
|
#=> SELECT "id" FROM "customers" WHERE "first_name" LIKE "J%"
|
||||||
|
|
||||||
|
# Aliases to overload operators are available
|
||||||
|
Rebel::SQL() { select :id, from: :customers, where: name(:age) == 42 }
|
||||||
|
#=> SELECT "id" FROM "customers" WHERE "age" = 42
|
||||||
|
Rebel::SQL() { select :id, from: :customers, where: name(:age) < 42 }
|
||||||
|
#=> SELECT "id" FROM "customers" WHERE "age" < 42
|
||||||
|
Rebel::SQL() { select :id, from: :customers, where: name(:age) >= 42 }
|
||||||
|
#=> SELECT "id" FROM "customers" WHERE "age" >= 42
|
||||||
|
Rebel::SQL() { select :id, from: :customers, where: name(:age) != 42 }
|
||||||
|
#=> SELECT "id" FROM "customers" WHERE "age" != 42
|
||||||
|
|
||||||
|
# Conditions can be combined
|
||||||
|
Rebel::SQL() { select :id, from: :customers, where: name(:age).gt(42).or(name(:age).lt(21)) }
|
||||||
|
#=> SELECT "id" FROM "customers" WHERE ("age" > 42 OR "age" < 21)
|
||||||
|
Rebel::SQL() { select :id, from: :customers, where: (name(:age) < 42).and(name(:age) > 21) }
|
||||||
|
#=> SELECT "id" FROM "customers" WHERE ("age" < 42 AND "age" > 21)
|
||||||
|
|
||||||
|
# Binary-wise operators can be used to tie conditions
|
||||||
|
# WARNING: Usefulness if this hack is still debated. It might be removed in the future.
|
||||||
|
Rebel::SQL() { select :id, from: :customers, where: ((name(:age) > 42) | (name(:age) < 21)) }
|
||||||
|
#=> SELECT "id" FROM "customers" WHERE ("age" > 42 OR "age" < 21)
|
||||||
|
Rebel::SQL() { select :id, from: :customers, where: name(:age).lt(42) & (name(:age).gt(21)) }
|
||||||
|
#=> SELECT "id" FROM "customers" WHERE ("age" < 42 AND "age" > 21)
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### Query execution
|
||||||
|
|
||||||
|
If you provide Rebel::SQL an environment within which a query executor is
|
||||||
|
available, queries can be executed directly.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class CreateTableCustomers
|
||||||
|
include Rebel::SQL
|
||||||
|
|
||||||
|
# provide a connection that responds to exec(query)
|
||||||
|
def conn
|
||||||
|
@conn ||= PG.connect( dbname: 'sales' )
|
||||||
|
end
|
||||||
|
|
||||||
|
# remember that SQL() returns a module!
|
||||||
|
include Rebel::SQL(true_literal: '1', false_literal: '0')
|
||||||
|
|
||||||
|
# alternatively, redefine the provided exec (which calls conn.exec)
|
||||||
|
def exec(query)
|
||||||
|
@db ||= SQLite3::Database.new "test.db"
|
||||||
|
@db.execute(query)
|
||||||
|
end
|
||||||
|
|
||||||
|
def up
|
||||||
|
create_table :customers, {
|
||||||
|
id: 'SERIAL',
|
||||||
|
name: 'VARCHAR(255)',
|
||||||
|
address: 'VARCHAR(255)',
|
||||||
|
city: 'VARCHAR(255)',
|
||||||
|
zip: 'VARCHAR(255)',
|
||||||
|
country: 'VARCHAR(255)',
|
||||||
|
}
|
||||||
|
|
||||||
|
insert_into :customers,
|
||||||
|
{ name: 'Lewis Caroll', address: '1, Alice St.', city: 'Oxford', zip: '1865', country: 'Wonderland' },
|
||||||
|
{ name: 'Neal Stephenson', address: '2, Hiro Blvd.', city: 'Los Angeles', zip: '1992', country: 'Metaverse' }
|
||||||
|
|
||||||
|
results = select :name, :country, from: :customers
|
||||||
|
|
||||||
|
update :customers, set: { city: 'FooTown' }, where: { zip: 1234 }
|
||||||
|
|
||||||
|
delete_from :customers, where: { zip: 1234 }
|
||||||
|
|
||||||
|
truncate :customers
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
drop_table :customers
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
## FAQ
|
||||||
|
|
||||||
|
### X is missing/database specific, how do I write it?
|
||||||
|
|
||||||
|
You can use `Rebel::SQL.raw("whatever")` and drop it in.
|
||||||
|
|
||||||
|
### Why the weird syntax like `inner: join` instead of `inner_join`?
|
||||||
|
|
||||||
|
This allows for a more uniform interface as well as not monkeypatching core types.
|
||||||
|
|
||||||
|
### Can I write nonsensical SQL with this?
|
||||||
|
|
||||||
|
Yes. Just as you can write nonsensical SQL in SQL.
|
||||||
|
|
||||||
|
### Your query builder is not using an AST.
|
||||||
|
|
||||||
|
That's not a question. You're welcome to implement one that does though, and if
|
||||||
|
it leverages the visitor pattern, allocates a trajillion objects along the way
|
||||||
|
and manages to produce invalid SQL in some corner cases, well congratulations
|
||||||
|
for reimplementing Arel.
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
MIT
|
MIT
|
||||||
|
|
|
||||||
122
lib/rebel/sql.rb
122
lib/rebel/sql.rb
|
|
@ -1,3 +1,5 @@
|
||||||
|
require 'date'
|
||||||
|
|
||||||
module Rebel::SQLQ
|
module Rebel::SQLQ
|
||||||
attr_reader :conn
|
attr_reader :conn
|
||||||
|
|
||||||
|
|
@ -115,26 +117,22 @@ module Rebel
|
||||||
alias | or
|
alias | or
|
||||||
|
|
||||||
def eq(n)
|
def eq(n)
|
||||||
case n
|
sql.raw("#{self} = #{sql.name_or_value(n)}")
|
||||||
when nil
|
|
||||||
sql.raw("#{self} IS NULL")
|
|
||||||
else
|
|
||||||
sql.raw("#{self} = #{sql.name_or_value(n)}")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
alias == eq
|
alias == eq
|
||||||
alias is eq
|
|
||||||
|
def is(n)
|
||||||
|
sql.raw("#{self} IS #{sql.name_or_value(n)}")
|
||||||
|
end
|
||||||
|
|
||||||
def ne(n)
|
def ne(n)
|
||||||
case n
|
sql.raw("#{self} != #{sql.name_or_value(n)}")
|
||||||
when nil
|
|
||||||
sql.raw("#{self} IS NOT NULL")
|
|
||||||
else
|
|
||||||
sql.raw("#{self} != #{sql.name_or_value(n)}")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
alias != ne
|
alias != ne
|
||||||
alias is_not ne
|
|
||||||
|
def is_not(n)
|
||||||
|
sql.raw("#{self} IS NOT #{sql.name_or_value(n)}")
|
||||||
|
end
|
||||||
|
|
||||||
def lt(n)
|
def lt(n)
|
||||||
sql.raw("#{self} < #{sql.name_or_value(n)}")
|
sql.raw("#{self} < #{sql.name_or_value(n)}")
|
||||||
|
|
@ -185,67 +183,59 @@ module Rebel
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_table(table_name, desc)
|
def create_table(table_name, desc)
|
||||||
raw <<-SQL
|
raw %[CREATE TABLE #{name(table_name)} (#{list(desc.map { |k, v| "#{name(k)} #{v}" })})]
|
||||||
CREATE TABLE #{name(table_name)} (
|
|
||||||
#{list(desc.map { |k, v| "#{name(k)} #{v}" })}
|
|
||||||
)
|
|
||||||
SQL
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def drop_table(table_name)
|
def drop_table(table_name)
|
||||||
raw <<-SQL
|
raw "DROP TABLE #{name(table_name)}"
|
||||||
DROP TABLE #{name(table_name)}
|
|
||||||
SQL
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def select(*fields, distinct: nil, from: nil, where: nil, inner: nil, left: nil, right: nil, group: nil, order: nil, limit: nil, offset: nil)
|
def select(*fields, distinct: nil, from: nil, where: nil, inner: nil, left: nil, right: nil, group: nil, order: nil, limit: nil, offset: nil)
|
||||||
raw <<-SQL
|
raw [
|
||||||
SELECT #{distinct ? "DISTINCT #{names(*distinct)}" : names(*fields)}
|
"SELECT #{distinct ? "DISTINCT #{names(*distinct)}" : names(*fields)}",
|
||||||
#{from?(from)}
|
from?(from),
|
||||||
#{inner?(inner)}
|
inner?(inner),
|
||||||
#{left?(left)}
|
left?(left),
|
||||||
#{right?(right)}
|
right?(right),
|
||||||
#{where?(where)}
|
where?(where),
|
||||||
#{group?(group)}
|
group?(group),
|
||||||
#{order?(order)}
|
order?(order),
|
||||||
#{limit?(limit, offset)}
|
limit?(limit, offset),
|
||||||
SQL
|
].compact.join(' ')
|
||||||
end
|
end
|
||||||
|
|
||||||
def insert_into(table_name, *rows)
|
def insert_into(table_name, *rows)
|
||||||
raw <<-SQL
|
raw [
|
||||||
INSERT INTO #{name(table_name)} (#{names(*rows.first.keys)})
|
"INSERT INTO #{name(table_name)} (#{names(*rows.first.keys)})",
|
||||||
VALUES #{list(rows.map { |r| "(#{values(*r.values)})" })}
|
"VALUES #{list(rows.map { |r| "(#{values(*r.values)})" })}",
|
||||||
SQL
|
].join(' ')
|
||||||
end
|
end
|
||||||
|
|
||||||
def update(table_name, set: nil, where: nil, inner: nil, left: nil, right: nil)
|
def update(table_name, set: nil, where: nil, inner: nil, left: nil, right: nil)
|
||||||
raise ArgumentError if set.nil?
|
raise ArgumentError if set.nil?
|
||||||
|
|
||||||
raw <<-SQL
|
raw [
|
||||||
UPDATE #{name(table_name)}
|
"UPDATE #{name(table_name)}",
|
||||||
SET #{assign_clause(set)}
|
"SET #{assign_clause(set)}",
|
||||||
#{inner?(inner)}
|
inner?(inner),
|
||||||
#{left?(left)}
|
left?(left),
|
||||||
#{right?(right)}
|
right?(right),
|
||||||
#{where?(where)}
|
where?(where),
|
||||||
SQL
|
].compact.join(' ')
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_from(table_name, where: nil, inner: nil, left: nil, right: nil)
|
def delete_from(table_name, where: nil, inner: nil, left: nil, right: nil)
|
||||||
raw <<-SQL
|
raw [
|
||||||
DELETE FROM #{name(table_name)}
|
"DELETE FROM #{name(table_name)}",
|
||||||
#{inner?(inner)}
|
inner?(inner),
|
||||||
#{left?(left)}
|
left?(left),
|
||||||
#{right?(right)}
|
right?(right),
|
||||||
#{where?(where)}
|
where?(where),
|
||||||
SQL
|
].join(' ')
|
||||||
end
|
end
|
||||||
|
|
||||||
def truncate(table_name)
|
def truncate(table_name)
|
||||||
raw <<-SQL
|
raw "TRUNCATE #{name(table_name)}"
|
||||||
TRUNCATE #{name(table_name)}
|
|
||||||
SQL
|
|
||||||
end
|
end
|
||||||
|
|
||||||
## Functions
|
## Functions
|
||||||
|
|
@ -285,9 +275,10 @@ module Rebel
|
||||||
|
|
||||||
## Support
|
## Support
|
||||||
|
|
||||||
def name(name)
|
def name(name = nil)
|
||||||
|
super() if name.nil? # workaround for pry and introspection
|
||||||
return name if name.is_a?(Raw)
|
return name if name.is_a?(Raw)
|
||||||
return raw('*') if name == '*'
|
return raw('*') if name == :*
|
||||||
|
|
||||||
raw(name.to_s.split('.').map { |e| "#{@identifier_quote}#{e}#{@identifier_quote}" }.join('.'))
|
raw(name.to_s.split('.').map { |e| "#{@identifier_quote}#{e}#{@identifier_quote}" }.join('.'))
|
||||||
end
|
end
|
||||||
|
|
@ -301,7 +292,10 @@ module Rebel
|
||||||
end
|
end
|
||||||
|
|
||||||
def escape_str(str)
|
def escape_str(str)
|
||||||
str.gsub(@string_quote, @escaped_string_quote)
|
str.dup.tap do |s|
|
||||||
|
s.gsub!('\\') { @escaped_string_backslash } if @escaped_string_backslash
|
||||||
|
s.gsub!(@string_quote) { @escaped_string_quote }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def value(v)
|
def value(v)
|
||||||
|
|
@ -340,6 +334,8 @@ module Rebel
|
||||||
case right
|
case right
|
||||||
when Array
|
when Array
|
||||||
name(left).in(*right)
|
name(left).in(*right)
|
||||||
|
when nil
|
||||||
|
name(left).is(name_or_value(right))
|
||||||
else
|
else
|
||||||
name(left).eq(name_or_value(right))
|
name(left).eq(name_or_value(right))
|
||||||
end
|
end
|
||||||
|
|
@ -397,11 +393,21 @@ module Rebel
|
||||||
@identifier_quote = options[:identifier_quote] || '"'
|
@identifier_quote = options[:identifier_quote] || '"'
|
||||||
@string_quote = options[:string_quote] || "'"
|
@string_quote = options[:string_quote] || "'"
|
||||||
@escaped_string_quote = options[:escaped_string_quote] || "''"
|
@escaped_string_quote = options[:escaped_string_quote] || "''"
|
||||||
|
@escaped_string_backslash = options[:escaped_string_backslash]
|
||||||
@true_literal = options[:true_literal] || 'TRUE'
|
@true_literal = options[:true_literal] || 'TRUE'
|
||||||
@false_literal = options[:false_literal] || 'FALSE'
|
@false_literal = options[:false_literal] || 'FALSE'
|
||||||
|
|
||||||
extend Rebel::SQLB
|
extend Rebel::SQLB
|
||||||
include Rebel::SQLQ
|
include Rebel::SQLQ
|
||||||
|
|
||||||
|
def self.name(name = nil)
|
||||||
|
return "Rebel::SQL" if name.nil?
|
||||||
|
super
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.inspect
|
||||||
|
"#<Rebel::SQL(#{instance_variables.map { |k| "#{k.to_s.sub(/^@/, '')}: #{instance_variable_get(k).inspect}" }.join(', ')})>"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return sql.instance_eval(&block) unless block.nil?
|
return sql.instance_eval(&block) unless block.nil?
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
Gem::Specification.new do |s|
|
Gem::Specification.new do |s|
|
||||||
s.name = 'rebel'
|
s.name = 'rebel'
|
||||||
s.version = '0.6.0'
|
s.version = '0.7.2'
|
||||||
s.licenses = ['MIT']
|
s.licenses = ['MIT']
|
||||||
s.summary = 'Fight against the Object tyranny'
|
s.summary = 'Fight against the Object tyranny'
|
||||||
s.description = 'SQL-flavoured Ruby, or is it the other way around?'
|
s.description = 'SQL-flavoured Ruby, or is it the other way around?'
|
||||||
s.authors = ['Loic Nageleisen']
|
s.authors = ['Loic Nageleisen']
|
||||||
s.email = 'loic.nageleisen@gmail.com'
|
s.email = 'loic.nageleisen@gmail.com'
|
||||||
s.files = Dir['lib/**/*.rb']
|
s.files = Dir['lib/**/*.rb']
|
||||||
s.homepage = 'https://github.com/lloeki/rebel.git'
|
s.homepage = 'https://gitlab.com/lloeki/rebel.git'
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,6 @@ class TestExec < Minitest::Test
|
||||||
def test_select
|
def test_select
|
||||||
create_table :foo, id: 'INT', col: 'VARCHAR(255)'
|
create_table :foo, id: 'INT', col: 'VARCHAR(255)'
|
||||||
insert_into :foo, id: 1, col: 'whatevs'
|
insert_into :foo, id: 1, col: 'whatevs'
|
||||||
assert_equal(select('*', from: :foo), [[1, 'whatevs']])
|
assert_equal(select(:*, from: :foo), [[1, 'whatevs']])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ class TestRaw < Minitest::Test
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_mysql(expected, &actual)
|
def assert_mysql(expected, &actual)
|
||||||
assert_equal(expected.to_s, Rebel::SQL(identifier_quote: '`', string_quote: '"', escaped_string_quote: '""', &actual).to_s)
|
assert_equal(expected.to_s, Rebel::SQL(identifier_quote: '`', escaped_string_quote: "\\'", escaped_string_backslash: '\\', &actual).to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_sqlite(expected, &actual)
|
def assert_sqlite(expected, &actual)
|
||||||
|
|
@ -48,19 +48,19 @@ class TestRaw < Minitest::Test
|
||||||
|
|
||||||
def test_is
|
def test_is
|
||||||
assert_sql('"foo" IS NULL') { name(:foo).is(nil) }
|
assert_sql('"foo" IS NULL') { name(:foo).is(nil) }
|
||||||
assert_sql('"foo" = 42') { name(:foo).is(42) }
|
assert_sql('"foo" IS 42') { name(:foo).is(42) }
|
||||||
assert_sql('"foo" = "bar"') { name(:foo).is(name(:bar)) }
|
assert_sql('"foo" IS "bar"') { name(:foo).is(name(:bar)) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_is_not
|
def test_is_not
|
||||||
assert_sql('"foo" IS NOT NULL') { name(:foo).is_not(nil) }
|
assert_sql('"foo" IS NOT NULL') { name(:foo).is_not(nil) }
|
||||||
assert_sql('"foo" != 42') { name(:foo).is_not(42) }
|
assert_sql('"foo" IS NOT 42') { name(:foo).is_not(42) }
|
||||||
assert_sql('"foo" != "bar"') { name(:foo).is_not(name(:bar)) }
|
assert_sql('"foo" IS NOT "bar"') { name(:foo).is_not(name(:bar)) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_eq
|
def test_eq
|
||||||
assert_sql('"foo" IS NULL') { name(:foo).eq(nil) }
|
assert_sql('"foo" = NULL') { name(:foo).eq(nil) }
|
||||||
assert_sql('"foo" IS NULL') { name(:foo) == nil }
|
assert_sql('"foo" = NULL') { name(:foo) == nil }
|
||||||
assert_sql('"foo" = "bar"') { name(:foo).eq(name(:bar)) }
|
assert_sql('"foo" = "bar"') { name(:foo).eq(name(:bar)) }
|
||||||
assert_sql('"foo" = "bar"') { name(:foo) == name(:bar) }
|
assert_sql('"foo" = "bar"') { name(:foo) == name(:bar) }
|
||||||
end
|
end
|
||||||
|
|
@ -68,8 +68,8 @@ class TestRaw < Minitest::Test
|
||||||
def test_ne
|
def test_ne
|
||||||
assert_sql('"foo" != "bar"') { name(:foo).ne(name(:bar)) }
|
assert_sql('"foo" != "bar"') { name(:foo).ne(name(:bar)) }
|
||||||
assert_sql('"foo" != "bar"') { name(:foo) != name(:bar) }
|
assert_sql('"foo" != "bar"') { name(:foo) != name(:bar) }
|
||||||
assert_sql('"foo" IS NOT NULL') { name(:foo).ne(nil) }
|
assert_sql('"foo" != NULL') { name(:foo).ne(nil) }
|
||||||
assert_sql('"foo" IS NOT NULL') { name(:foo) != nil }
|
assert_sql('"foo" != NULL') { name(:foo) != nil }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_lt
|
def test_lt
|
||||||
|
|
@ -112,6 +112,8 @@ class TestRaw < Minitest::Test
|
||||||
assert_sql('WHERE "foo" = 1 AND "bar" = 2 AND "baz" = 3') { where?(foo: 1, bar: 2, baz: 3) }
|
assert_sql('WHERE "foo" = 1 AND "bar" = 2 AND "baz" = 3') { where?(foo: 1, bar: 2, baz: 3) }
|
||||||
assert_sql('WHERE ("foo" = 1 OR "bar" = 2) AND "baz" = 3') { where?(name(:foo).eq(1).or(name(:bar).eq(2)), name(:baz).eq(3)) }
|
assert_sql('WHERE ("foo" = 1 OR "bar" = 2) AND "baz" = 3') { where?(name(:foo).eq(1).or(name(:bar).eq(2)), name(:baz).eq(3)) }
|
||||||
assert_sql('WHERE ("foo" = 1 OR "bar" = 2)') { where?(name(:foo).eq(1).or(name(:bar).eq(2))) }
|
assert_sql('WHERE ("foo" = 1 OR "bar" = 2)') { where?(name(:foo).eq(1).or(name(:bar).eq(2))) }
|
||||||
|
assert_sql('WHERE "foo" IS NULL') { where?(foo: nil) }
|
||||||
|
assert_sql('WHERE "foo" IN (1, 2, 3)') { where?(foo: [1, 2, 3]) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_join
|
def test_join
|
||||||
|
|
@ -135,33 +137,43 @@ class TestRaw < Minitest::Test
|
||||||
|
|
||||||
def test_string
|
def test_string
|
||||||
assert_sql("'FOO'") { value('FOO') }
|
assert_sql("'FOO'") { value('FOO') }
|
||||||
assert_mysql('"FOO"') { value('FOO') }
|
assert_mysql("'FOO'") { value('FOO') }
|
||||||
assert_postgresql("'FOO'") { value('FOO') }
|
assert_postgresql("'FOO'") { value('FOO') }
|
||||||
assert_sqlite("'FOO'") { value('FOO') }
|
assert_sqlite("'FOO'") { value('FOO') }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_escaped_string
|
def test_escaped_string
|
||||||
assert_sql("'FOO''BAR'") { value("FOO'BAR") }
|
assert_sql (%q('FOO''BAR')) { value(%q(FOO'BAR)) }
|
||||||
assert_mysql('"FOO\'BAR"') { value("FOO'BAR") }
|
assert_postgresql (%q('FOO''BAR')) { value(%q(FOO'BAR)) }
|
||||||
assert_postgresql("'FOO''BAR'") { value("FOO'BAR") }
|
assert_sqlite (%q('FOO''BAR')) { value(%q(FOO'BAR)) }
|
||||||
assert_sqlite("'FOO''BAR'") { value("FOO'BAR") }
|
assert_mysql (%q('FOO\'BAR')) { value(%q(FOO'BAR)) }
|
||||||
|
|
||||||
assert_sql("'FOO\"BAR'") { value('FOO"BAR') }
|
assert_sql (%q('FOO"BAR')) { value(%q(FOO"BAR)) }
|
||||||
assert_mysql('"FOO""BAR"') { value('FOO"BAR') }
|
assert_postgresql (%q('FOO"BAR')) { value(%q(FOO"BAR)) }
|
||||||
assert_postgresql("'FOO\"BAR'") { value('FOO"BAR') }
|
assert_sqlite (%q('FOO"BAR')) { value(%q(FOO"BAR)) }
|
||||||
assert_sqlite("'FOO\"BAR'") { value('FOO"BAR') }
|
assert_mysql (%q('FOO"BAR')) { value(%q(FOO"BAR)) }
|
||||||
|
|
||||||
|
assert_sql (%q('FOO\BAR')) { value(%q(FOO\BAR)) }
|
||||||
|
assert_postgresql (%q('FOO\BAR')) { value(%q(FOO\BAR)) }
|
||||||
|
assert_sqlite (%q('FOO\BAR')) { value(%q(FOO\BAR)) }
|
||||||
|
assert_mysql (%q('FOO\\BAR')) { value(%q(FOO\BAR)) }
|
||||||
|
|
||||||
|
assert_sql (%q('FOO\\''BAR')) { value(%q(FOO\'BAR)) }
|
||||||
|
assert_postgresql (%q('FOO\\''BAR')) { value(%q(FOO\'BAR)) }
|
||||||
|
assert_sqlite (%q('FOO\\''BAR')) { value(%q(FOO\'BAR)) }
|
||||||
|
assert_mysql (%q('FOO\\\'BAR')) { value(%q(FOO\'BAR)) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_boolean_literal
|
def test_boolean_literal
|
||||||
assert_sql("TRUE") { value(true) }
|
assert_sql('TRUE') { value(true) }
|
||||||
assert_mysql("TRUE") { value(true) }
|
assert_mysql('TRUE') { value(true) }
|
||||||
assert_postgresql("TRUE") { value(true) }
|
assert_postgresql('TRUE') { value(true) }
|
||||||
assert_sqlite("1") { value(true) }
|
assert_sqlite('1') { value(true) }
|
||||||
|
|
||||||
assert_sql("FALSE") { value(false) }
|
assert_sql('FALSE') { value(false) }
|
||||||
assert_mysql("FALSE") { value(false) }
|
assert_mysql('FALSE') { value(false) }
|
||||||
assert_postgresql("FALSE") { value(false) }
|
assert_postgresql('FALSE') { value(false) }
|
||||||
assert_sqlite("0") { value(false) }
|
assert_sqlite('0') { value(false) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_value
|
def test_value
|
||||||
|
|
@ -177,7 +189,7 @@ class TestRaw < Minitest::Test
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_select
|
def test_select
|
||||||
assert_sql('SELECT * FROM "foo"') { select(raw('*'), from: name(:foo)).gsub(/\s+/, ' ').strip }
|
assert_sql('SELECT * FROM "foo"') { select(raw('*'), from: name(:foo)) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_select_without_from
|
def test_select_without_from
|
||||||
|
|
@ -185,50 +197,50 @@ class TestRaw < Minitest::Test
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_select_distinct
|
def test_select_distinct
|
||||||
assert_sql('SELECT DISTINCT "bar" FROM "foo"') { select(distinct: :bar, from: :foo).gsub(/\s+/, ' ').strip }
|
assert_sql('SELECT DISTINCT "bar" FROM "foo"') { select(distinct: :bar, from: :foo) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_select_distinct_multiple
|
def test_select_distinct_multiple
|
||||||
assert_sql('SELECT DISTINCT "bar", "baz" FROM "foo"') { select(distinct: [:bar, :baz], from: :foo).gsub(/\s+/, ' ').strip }
|
assert_sql('SELECT DISTINCT "bar", "baz" FROM "foo"') { select(distinct: [:bar, :baz], from: :foo) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_select_group_by
|
def test_select_group_by
|
||||||
assert_sql('SELECT "bar" FROM "foo" GROUP BY "baz"') { select(:bar, from: :foo, group: by(:baz)).gsub(/\s+/, ' ').strip }
|
assert_sql('SELECT "bar" FROM "foo" GROUP BY "baz"') { select(:bar, from: :foo, group: by(:baz)) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_select_group_by_having
|
def test_select_group_by_having
|
||||||
assert_sql('SELECT "bar" FROM "foo" GROUP BY "baz" HAVING COUNT("qux") > 5') { select(:bar, from: :foo, group: by(:baz).having(count(:qux).gt(5))).gsub(/\s+/, ' ').strip }
|
assert_sql('SELECT "bar" FROM "foo" GROUP BY "baz" HAVING COUNT("qux") > 5') { select(:bar, from: :foo, group: by(:baz).having(count(:qux).gt(5))) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_select_order_by
|
def test_select_order_by
|
||||||
assert_sql('SELECT "bar" FROM "foo" ORDER BY "baz"') { select(:bar, from: :foo, order: by(:baz)).gsub(/\s+/, ' ').strip }
|
assert_sql('SELECT "bar" FROM "foo" ORDER BY "baz"') { select(:bar, from: :foo, order: by(:baz)) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_select_order_by_asc
|
def test_select_order_by_asc
|
||||||
assert_sql('SELECT "bar" FROM "foo" ORDER BY "baz" ASC') { select(:bar, from: :foo, order: by(:baz).asc).gsub(/\s+/, ' ').strip }
|
assert_sql('SELECT "bar" FROM "foo" ORDER BY "baz" ASC') { select(:bar, from: :foo, order: by(:baz).asc) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_select_order_by_desc
|
def test_select_order_by_desc
|
||||||
assert_sql('SELECT "bar" FROM "foo" ORDER BY "baz" DESC') { select(:bar, from: :foo, order: by(:baz).desc).gsub(/\s+/, ' ').strip }
|
assert_sql('SELECT "bar" FROM "foo" ORDER BY "baz" DESC') { select(:bar, from: :foo, order: by(:baz).desc) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_select_multiple_order_by
|
def test_select_multiple_order_by
|
||||||
assert_sql('SELECT "bar" FROM "foo" ORDER BY "baz", "qux"') { select(:bar, from: :foo, order: by(:baz, :qux)).gsub(/\s+/, ' ').strip }
|
assert_sql('SELECT "bar" FROM "foo" ORDER BY "baz", "qux"') { select(:bar, from: :foo, order: by(:baz, :qux)) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_select_multiple_order_by_opposing
|
def test_select_multiple_order_by_opposing
|
||||||
assert_sql('SELECT "bar" FROM "foo" ORDER BY "baz" ASC, "qux" DESC') { select(:bar, from: :foo, order: by(name(:baz).asc, name(:qux).desc)).gsub(/\s+/, ' ').strip }
|
assert_sql('SELECT "bar" FROM "foo" ORDER BY "baz" ASC, "qux" DESC') { select(:bar, from: :foo, order: by(name(:baz).asc, name(:qux).desc)) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_select_limit
|
def test_select_limit
|
||||||
assert_sql('SELECT "bar" FROM "foo" LIMIT 10') { select(:bar, from: :foo, limit: 10).gsub(/\s+/, ' ').strip }
|
assert_sql('SELECT "bar" FROM "foo" LIMIT 10') { select(:bar, from: :foo, limit: 10) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_select_offset
|
def test_select_offset
|
||||||
assert_sql('SELECT "bar" FROM "foo" LIMIT 10 OFFSET 20') { select(:bar, from: :foo, limit: 10, offset: 20).gsub(/\s+/, ' ').strip }
|
assert_sql('SELECT "bar" FROM "foo" LIMIT 10 OFFSET 20') { select(:bar, from: :foo, limit: 10, offset: 20) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_nested_select
|
def test_nested_select
|
||||||
assert_sql('SELECT * FROM "foo" WHERE "bar" IN ( SELECT "bar" FROM "foo" )') { select(raw('*'), from: name(:foo), where: name(:bar).in(select(name(:bar), from: name(:foo)))).gsub(/\s+/, ' ').strip }
|
assert_sql('SELECT * FROM "foo" WHERE "bar" IN (SELECT "bar" FROM "foo")') { select(raw('*'), from: name(:foo), where: name(:bar).in(select(name(:bar), from: name(:foo)))) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue