-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sqlite3 stuff - much closer to being database-neutral
- Loading branch information
Nathan Long
committed
Jan 15, 2013
1 parent
08dbee6
commit 671d64c
Showing
18 changed files
with
244 additions
and
69 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
Empty file.
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'sqlite3' | ||
|
||
module Dossier | ||
module Adapter | ||
class SQLite3 | ||
attr_reader :connection | ||
|
||
def self.connection_class | ||
::SQLite3::Database | ||
end | ||
|
||
def initialize(options = {}) | ||
@connection = self.class.connection_class.new(options.fetch(:database)) | ||
end | ||
|
||
def execute(sql) | ||
Result.new connection.execute(sql) | ||
rescue ::SQLite3::Exception => e | ||
raise Dossier::ExecuteError.new "#{e.message}\n\n#{sql}" | ||
end | ||
|
||
def escape(value) | ||
connection.escape(value) | ||
end | ||
|
||
class Result | ||
attr_accessor :rows | ||
|
||
def initialize(rows) | ||
self.rows = rows | ||
end | ||
|
||
def headers | ||
rows.fields | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
require 'spec_helper' | ||
require 'dossier/adapter/sqlite3' | ||
|
||
describe Dossier::Adapter::SQLite3 do | ||
|
||
let(:db_config) { DB_CONFIG[:sqlite3] } | ||
let(:adapter) { described_class.new(db_config) } | ||
|
||
describe "initialize" do | ||
it "instantiates its connection_class" do | ||
described_class.connection_class.should_receive(:new).with(db_config[:database]) | ||
adapter | ||
end | ||
end | ||
|
||
describe "instance methods" do | ||
|
||
let(:example_query) { 'SELECT * FROM employees' } | ||
|
||
describe "execute" do | ||
|
||
it "calls `execute` on the connection" do | ||
adapter.connection.should_receive(:execute).with(example_query) | ||
adapter.execute(example_query) | ||
end | ||
|
||
it "returns an adapter result" do | ||
expect(adapter.execute(example_query)).to be_a(Dossier::Adapter::SQLite3::Result) | ||
end | ||
|
||
describe "handling SQL syntax errors" do | ||
|
||
it "raises a Dossier::ExecuteError containing the bad query" do | ||
query = "FROM SELECT romeo WHERE FROM art SELECT romeo" | ||
expect{adapter.execute(query)}.to raise_error( | ||
Dossier::ExecuteError, /#{query}/ | ||
) | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
describe "escape" do | ||
|
||
it "gets its connection to escape the value" do | ||
adapter.connection.should_receive(:escape).with('dAnGeRoUsNeSs!#') | ||
adapter.escape('dAnGeRoUsNeSs!#') | ||
end | ||
|
||
end | ||
|
||
describe Dossier::Adapter::SQLite3::Result do | ||
|
||
describe "headers" do | ||
|
||
it "calls `fields` on its results" do | ||
mock_results = mock(:results, fields: []) | ||
results = Dossier::Adapter::SQLite3::Result.new(mock_results) | ||
mock_results.should_receive(:fields) | ||
results.headers | ||
end | ||
|
||
end | ||
|
||
describe "rows" do | ||
|
||
it "responds to `each`" do | ||
results = adapter.execute(example_query) | ||
expect(results.rows).to respond_to(:each) | ||
end | ||
|
||
end | ||
end | ||
|
||
end | ||
|
||
end |
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
spec/dummy/db/migrate/20130110221932_add_division_to_employees.rb
This file was deleted.
Oops, something went wrong.
Empty file.
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 |
---|---|---|
@@ -1,4 +1,2 @@ | ||
adapter: sqlite3 | ||
database: path/to/test.sqlite3 | ||
pool: 5 | ||
timeout: 5000 |
Binary file not shown.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Id,Name,Hired On,Suspended,Division,Salary | ||
3,Elise Elderberry,2013-01-11,0,Corporate Malfeasance,99000 | ||
2,"Jimmy Jackalope, Jr.",2013-01-11,1,Tedious Toiling,20000 | ||
1,Moustafa McMann,2010-10-02,0,Zany Inventions,30000 | ||
Id,Name,Division,Salary,Suspended,Hired On | ||
3,Elise Elderberry,Corporate Malfeasance,99000,0,2013-01-11 | ||
2,"Jimmy Jackalope, Jr.",Tedious Toiling,20000,1,2013-01-11 | ||
1,Moustafa McMann,Zany Inventions,30000,0,2010-10-02 |
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
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
Oops, something went wrong.