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

allow multiple occurencies of builder statements #50

Merged
merged 2 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ builder.query.each do |t|
end
```

The same builder's statement may occur multiple times.

```ruby
builder = conn.build('(/*select*/ from books) union (/*select*/ from movies)').select('title').query

# => (SELECT title from books) union (SELECT title from movies)
```

The builder predefined next _SQL Literals_

| Method | SQL Literal |
Expand Down
2 changes: 1 addition & 1 deletion lib/mini_sql/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def count(field = '*')
v
end

unless sql.sub!("/*#{k}*/", joined)
unless sql.gsub!("/*#{k}*/", joined)
raise "The section for the /*#{k}*/ clause was not found!"
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/mini_sql/builder_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ def test_sql_literal
assert_equal(builder.to_sql, 'SELECT * FROM products WHERE id = 10')
end

def test_sql_literal_multiple
builder = @connection.build("(select 1 AS x /*polymorphic_where*/) union all (select 2 AS x /*polymorphic_where*/)")
builder.sql_literal(polymorphic_where: 'where 1 = 1')
assert_equal(builder.to_sql, "(select 1 AS x where 1 = 1) union all (select 2 AS x where 1 = 1)")
end

def test_sql_literal_for_builder
user_builder = @connection.build("SELECT * FROM users /*where*/").where('id = ?', 10)
builder = @connection.build("SELECT * FROM (/*user_table*/) AS t")
Expand Down
Loading