Skip to content

Commit

Permalink
allow multiple occurencies of builder statements (#50)
Browse files Browse the repository at this point in the history
* allow multiple occurencies of builder statements
* fix readme

---------

Co-authored-by: Sergei Malykh <sergey.malykh@groupprice.ru>
  • Loading branch information
xronos-i-am and Sergei Malykh committed Aug 1, 2023
1 parent 426bb43 commit 6a94f53
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
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
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
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

0 comments on commit 6a94f53

Please sign in to comment.