Skip to content

Commit

Permalink
added initial support for db
Browse files Browse the repository at this point in the history
Access to database is made using DataObjects
  • Loading branch information
mlomnicki committed Oct 2, 2009
1 parent 2db04dd commit 42b1c79
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dbconfig.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
host: localhost
user: username
password: secret
driver: pg
driver: postgres
database: test

3 changes: 2 additions & 1 deletion lib/postpolicy/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class << self
def load( filename )
if filename
@dbconfig = YAML.load_file( filename ).stringify_keys!.freeze
@dbi_params = ["DBI:#{@dbconfig[:driver]}:#{@dbconfig[:database]}", @dbconfig[:user], @dbconfig[:password]]
require "do_#{@dbconfig[:driver]}"
@dbi_params = "#{@dbconfig[:driver]}://#{@dbconfig[:user]}:#{@dbconfig[:password]}@#{@dbconfig[:host]}:#{@dbconfig[:port]}/#{@dbconfig[:database]}"
end
end

Expand Down
35 changes: 35 additions & 0 deletions lib/postpolicy/plugins/datasource/sql.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module PostPolicy
module DataSource

class Sql < Base

def initialize( query )
@query = query
end

def exists?( value )
result = false
connection do |conn|
command = conn.create_command( @query )
reader = command.execute_reader
while reader.next
if reader.values[0] && item[0] == value
result = true
break
end
end
end
result
end

protected
def connection( &block )
conn = DataObjects::Connection.new( Config::Db.dbi_params )
block.call( conn )
conn.close
end

end

end
end
2 changes: 1 addition & 1 deletion postpolicy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ acl:
foo:
sender: [email protected]
bar:
sender: file:///home/michal/test/filetest
#sender: file:///home/michal/test/filetest
recipient: sql://"SELECT * FROM users WHERE login = '%l'"

action:
Expand Down
5 changes: 3 additions & 2 deletions test/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
:database => 'postpolicy',
:user => 'foo',
:password => 'secret',
:driver => 'pg' }
:port => 5432,
:driver => 'postgres' }

File.open( filename, 'w' ) do |f|
f.puts config.to_yaml
end

PostPolicy::Config::Db.load( filename )
PostPolicy::Config::Db.dbconfig.should == config
dbi_params = ["DBI:#{config[:driver]}:#{config[:database]}", config[:user], config[:password]]
dbi_params = "#{config[:driver]}://#{config[:user]}:#{config[:password]}@#{config[:host]}:#{config[:port]}/#{config[:database]}"
PostPolicy::Config::Db.dbi_params.should == dbi_params

FileUtils.rm( filename )
Expand Down
8 changes: 8 additions & 0 deletions test/datasource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,11 @@
end

end

describe PostPolicy::DataSource::Sql do

it "should check in db" do
ds = PostPolicy::DataSource::Sql.new( "SELECT id from test" )
end

end

0 comments on commit 42b1c79

Please sign in to comment.