hyperion-postgres 0.0.1.alpha2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ module Hyperion
2
+ module Postgres
3
+ class DbStrategy
4
+
5
+ def process_result(given_record, result)
6
+ result[0]
7
+ end
8
+
9
+ def process_count_result(result)
10
+ result['count']
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,42 @@
1
+ module Hyperion
2
+ module Postgres
3
+
4
+ class QueryBuilderStrategy
5
+ def quote_tick
6
+ '"'
7
+ end
8
+
9
+ def apply_limit_and_offset(sql_query, limit, offset)
10
+ apply_limit(sql_query, limit)
11
+ apply_offset(sql_query, offset)
12
+ end
13
+
14
+ def normalize_insert(sql_query_str)
15
+ "#{sql_query_str} RETURNING *"
16
+ end
17
+
18
+ def normalize_update(sql_query_str)
19
+ "#{sql_query_str} RETURNING *"
20
+ end
21
+
22
+ def empty_insert_query(table)
23
+ "INSERT INTO #{table} DEFAULT VALUES"
24
+ end
25
+
26
+ private
27
+
28
+ def apply_limit(sql_query, limit)
29
+ if limit
30
+ sql_query.append("LIMIT ?", [limit])
31
+ end
32
+ end
33
+
34
+ def apply_offset(sql_query, offset)
35
+ if offset
36
+ sql_query.append("OFFSET ?", [offset])
37
+ end
38
+ end
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,16 @@
1
+ require 'hyperion/sql'
2
+
3
+ module Hyperion
4
+ module Postgres
5
+
6
+ class QueryExecutorStrategy
7
+
8
+ def execute_write(sql_query)
9
+ command = Sql.connection.create_command(sql_query.query_str)
10
+ command.execute_reader(*sql_query.bind_values).to_a
11
+ end
12
+
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ require 'do_postgres'
2
+ require 'hyperion/sql/datastore'
3
+ require 'hyperion/postgres/query_builder_strategy'
4
+ require 'hyperion/postgres/query_executor_strategy'
5
+ require 'hyperion/postgres/db_strategy'
6
+
7
+ module Hyperion
8
+ module Postgres
9
+
10
+ def self.create_datastore
11
+ Sql::Datastore.new(DbStrategy.new, QueryExecutorStrategy.new, QueryBuilderStrategy.new)
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,79 @@
1
+ require 'do_postgres'
2
+ require 'hyperion/postgres'
3
+ require 'hyperion/sql'
4
+ require 'hyperion/dev/ds_spec'
5
+ require 'hyperion/sql/transaction_spec'
6
+
7
+ describe Hyperion::Postgres do
8
+
9
+ def execute(query)
10
+ Hyperion::Sql.connection.create_command(query).execute_non_query
11
+ end
12
+
13
+ def create_table(table_name)
14
+ execute <<-QUERY
15
+ CREATE TABLE #{table_name} (
16
+ id SERIAL PRIMARY KEY,
17
+ name VARCHAR(35),
18
+ inti INTEGER,
19
+ data VARCHAR(32)
20
+ );
21
+ QUERY
22
+ end
23
+
24
+ def drop_table(table_name)
25
+ execute "DROP TABLE IF EXISTS #{table_name};"
26
+ end
27
+
28
+ around :each do |example|
29
+ Hyperion::Sql.with_connection('postgres://localhost/hyperion_ruby') do |connection|
30
+ Hyperion::API.datastore = Hyperion::Postgres.create_datastore
31
+ example.run
32
+ end
33
+ end
34
+
35
+ context 'Datastore' do
36
+ around :each do |example|
37
+ Hyperion::Sql.rollback do
38
+ tables = ['testing', 'other_testing']
39
+ tables.each { |table| create_table(table) }
40
+ example.run
41
+ end
42
+ end
43
+
44
+ include_examples 'Datastore'
45
+
46
+ context 'Sql Injection' do
47
+ it 'escapes strings to be inserted' do
48
+ evil_name = "my evil name' --"
49
+ record = Hyperion::API.save(:kind => 'testing', :name => evil_name)
50
+ found_record = Hyperion::API.find_by_key(record[:key])
51
+ found_record[:name].should == evil_name
52
+ end
53
+
54
+ it 'escapes table names' do
55
+ evil_name = 'my evil name" --'
56
+ error_message = ""
57
+ begin
58
+ Hyperion::API.save(:kind => 'my evil name" --', :name => evil_name)
59
+ rescue Exception => e
60
+ error_message = e.message
61
+ end
62
+ error_message.should include('relation "my_evil_name"___" does not exist')
63
+ end
64
+
65
+ it 'escapes column names' do
66
+ evil_name = 'my evil name" --'
67
+ error_message = ""
68
+ begin
69
+ Hyperion::API.save(:kind => 'testing', evil_name => 'value')
70
+ rescue Exception => e
71
+ error_message = e.message
72
+ end
73
+ error_message.should include('column "my_evil_name"___" of relation "testing" does not exist')
74
+ end
75
+ end
76
+ end
77
+
78
+ it_behaves_like 'Sql Transactions'
79
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hyperion-postgres
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.alpha2
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - 8th Light, Inc.
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.11.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.11.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: hyperion-sql
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.0.1.alpha2
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.0.1.alpha2
46
+ - !ruby/object:Gem::Dependency
47
+ name: do_postgres
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - '='
52
+ - !ruby/object:Gem::Version
53
+ version: 0.10.8
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.8
62
+ description: Postgres Datastore for Hyperion
63
+ email:
64
+ - myles@8thlight.com
65
+ - skim@8thlight.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - lib/hyperion/postgres/db_strategy.rb
71
+ - lib/hyperion/postgres/query_executor_strategy.rb
72
+ - lib/hyperion/postgres/query_builder_strategy.rb
73
+ - lib/hyperion/postgres.rb
74
+ - spec/hyperion/postgres_spec.rb
75
+ homepage: https://github.com/mylesmegyesi/hyperion-ruby
76
+ licenses:
77
+ - Eclipse Public License
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: 1.8.7
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>'
92
+ - !ruby/object:Gem::Version
93
+ version: 1.3.1
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 1.8.24
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Postgres Datastore for Hyperion
100
+ test_files:
101
+ - spec/hyperion/postgres_spec.rb