-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from timhwang21/tim/support-exec-query
Tim/support exec query
- Loading branch information
Showing
3 changed files
with
45 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,7 @@ SqlQuery.new('player/get_by_email', email: '[email protected]') | |
### Methods | ||
|
||
- **execute** - executes query and returns result data. It accepts boolean argument. When argument is false it will run raw sql query instead of prepared_for_logs. | ||
- **exec_query** - similar to `#execute` but with data returned via `ActiveRecord::Result`. | ||
- **explain** - runs explain for SQL from template | ||
- **sql** - returns SQL string | ||
- **pretty_sql** - returns SQL string prettier to read in console | ||
|
@@ -141,3 +142,10 @@ If you have some examples to share please make pull request. | |
3. Commit your changes (`git commit -am 'Add some feature'`) | ||
4. Push to the branch (`git push origin my-new-feature`) | ||
5. Create a new Pull Request | ||
|
||
To run specs, setup Postgres with the following: | ||
|
||
```sql | ||
CREATE DATABASE sqlquery; | ||
CREATE ROLE sqlquery WITH LOGIN; | ||
``` |
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 |
---|---|---|
|
@@ -145,6 +145,38 @@ class Model < ActiveRecord::Base | |
end | ||
end | ||
|
||
describe '#exec_query' do | ||
before do | ||
ActiveRecord::Base.connection.execute( | ||
"INSERT INTO players (email) VALUES ('[email protected]')" | ||
) | ||
end | ||
|
||
after do | ||
ActiveRecord::Base.connection.execute( | ||
'DELETE FROM players' | ||
) | ||
end | ||
|
||
it 'returns data from database' do | ||
expect(query.exec_query).to eq [{ 'email' => '[email protected]' }] | ||
end | ||
|
||
context 'when prepare argument is true' do | ||
it 'executes prepared query for logs' do | ||
expect(query).to receive(:prepared_for_logs) { '' } | ||
query.exec_query | ||
end | ||
end | ||
|
||
context 'when prepare argument is false' do | ||
it 'executes unchanged sql query' do | ||
expect(query).not_to receive(:prepared_for_logs) | ||
query.exec_query(false) | ||
end | ||
end | ||
end | ||
|
||
describe '#partial' do | ||
let(:file_name) { :get_player_by_email_with_template } | ||
it 'resolves partials as parts of sql queries' do | ||
|