hyperion-sql 0.0.1.alpha5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/hyperion/sql.rb CHANGED
@@ -1,23 +1,22 @@
1
1
  require 'data_objects'
2
- require 'hyperion/api'
2
+ require 'hyperion'
3
3
  require 'hyperion/sql/transaction'
4
+ require 'hyperion/util'
4
5
 
5
6
  module Hyperion
6
7
  module Sql
7
8
 
8
9
  def self.with_connection(url)
9
- connection = DataObjects::Connection.new(url)
10
- Thread.current[:connection] = connection
11
- result = yield
12
- connection.close
13
- Thread.current[:connection] = nil
14
- result
15
- end
16
-
17
- def self.with_connection_and_ds(url, name, opts={})
18
- API.with_datastore(name, opts) do
19
- with_connection(url) do
20
- yield
10
+ if Thread.current[:connection]
11
+ yield
12
+ else
13
+ connection = DataObjects::Connection.new(url)
14
+ begin
15
+ Util.bind(:connection, connection) do
16
+ yield
17
+ end
18
+ ensure
19
+ connection.close
21
20
  end
22
21
  end
23
22
  end
@@ -28,8 +27,8 @@ module Hyperion
28
27
 
29
28
  def self.rollback
30
29
  with_txn do |txn|
30
+ savepoint_id = txn.begin_savepoint
31
31
  begin
32
- savepoint_id = txn.begin_savepoint
33
32
  yield
34
33
  ensure
35
34
  txn.rollback_to_savepoint(savepoint_id)
@@ -39,8 +38,8 @@ module Hyperion
39
38
 
40
39
  def self.transaction
41
40
  with_txn do |txn|
41
+ savepoint_id = txn.begin_savepoint
42
42
  begin
43
- savepoint_id = txn.begin_savepoint
44
43
  result = yield
45
44
  txn.release_savepoint(savepoint_id)
46
45
  result
@@ -53,25 +52,21 @@ module Hyperion
53
52
 
54
53
  private
55
54
 
56
- def self.in_transaction?
57
- !Thread.current[:transaction].nil?
58
- end
59
-
60
55
  def self.with_txn
61
56
  if Thread.current[:transaction]
62
57
  yield(Thread.current[:transaction])
63
58
  else
64
- txn = (Thread.current[:transaction] = Transaction.new(connection))
65
- txn.begin
66
- result = yield(txn)
67
- txn.commit
68
- result
59
+ txn = Transaction.new(connection)
60
+ Util.bind(:transaction, txn) do
61
+ txn.begin
62
+ result = yield(txn)
63
+ txn.commit
64
+ result
65
+ end
69
66
  end
70
67
  rescue Exception => e
71
68
  txn.rollback
72
69
  raise e
73
- ensure
74
- Thread.current[:transaction] = nil
75
70
  end
76
71
  end
77
72
  end
@@ -1,4 +1,4 @@
1
- require 'hyperion/api'
1
+ require 'hyperion'
2
2
  require 'hyperion/key'
3
3
  require 'hyperion/sql/query_builder'
4
4
  require 'hyperion/sql/query_executor'
@@ -8,54 +8,73 @@ module Hyperion
8
8
 
9
9
  class Datastore
10
10
 
11
- def initialize(db_strategy, query_executor_strategy, query_builder_strategy)
11
+ def initialize(connection_url, db_strategy, query_executor_strategy, query_builder_strategy)
12
+ @connection_url = connection_url
12
13
  @db_strategy = db_strategy
13
14
  @query_executor = QueryExecutor.new(query_executor_strategy)
14
15
  @query_builder = QueryBuilder.new(query_builder_strategy)
15
16
  end
16
17
 
17
18
  def save(records)
18
- records.map do |record|
19
- if API.new?(record)
20
- execute_save_query(query_builder.build_insert(record), record)
21
- elsif non_empty_record?(record)
22
- execute_save_query(query_builder.build_update(record), record)
23
- else
24
- record
19
+ with_connection do
20
+ records.map do |record|
21
+ if Hyperion.new?(record)
22
+ execute_save_query(query_builder.build_insert(record), record)
23
+ elsif non_empty_record?(record)
24
+ execute_save_query(query_builder.build_update(record), record)
25
+ else
26
+ record
27
+ end
25
28
  end
26
29
  end
27
30
  end
28
31
 
29
32
  def find_by_key(key)
30
- find(query_from_key(key)).first
33
+ with_connection do
34
+ find(query_from_key(key)).first
35
+ end
31
36
  end
32
37
 
33
38
  def find(query)
34
- sql_query = query_builder.build_select(query)
35
- results = query_executor.execute_query(sql_query)
36
- results.map { |record| record_from_db(record, query.kind) }
39
+ with_connection do
40
+ sql_query = query_builder.build_select(query)
41
+ results = query_executor.execute_query(sql_query)
42
+ results.map { |record| record_from_db(record, query.kind) }
43
+ end
37
44
  end
38
45
 
39
46
  def delete_by_key(key)
40
- delete(query_from_key(key))
47
+ with_connection do
48
+ delete(query_from_key(key))
49
+ end
41
50
  end
42
51
 
43
52
  def delete(query)
44
- sql_query = query_builder.build_delete(query)
45
- query_executor.execute_mutation(sql_query)
46
- nil
53
+ with_connection do
54
+ sql_query = query_builder.build_delete(query)
55
+ query_executor.execute_mutation(sql_query)
56
+ nil
57
+ end
47
58
  end
48
59
 
49
60
  def count(query)
50
- sql_query = query_builder.build_count(query)
51
- results = query_executor.execute_query(sql_query)
52
- db_strategy.process_count_result(results[0])
61
+ with_connection do
62
+ sql_query = query_builder.build_count(query)
63
+ results = query_executor.execute_query(sql_query)
64
+ db_strategy.process_count_result(results[0])
65
+ end
53
66
  end
54
67
 
55
68
  private
56
69
 
57
70
  attr_reader :query_builder, :query_executor, :db_strategy
58
71
 
72
+ def with_connection
73
+ Sql.with_connection(@connection_url) do
74
+ yield
75
+ end
76
+ end
77
+
59
78
  def non_empty_record?(record)
60
79
  record = record.dup
61
80
  record.delete(:kind)
@@ -70,7 +89,7 @@ module Hyperion
70
89
  end
71
90
 
72
91
  def record_from_db(record, table)
73
- record[:key] = Key.compose_key(table, record.delete('id')) if API.new?(record)
92
+ record[:key] = Key.compose_key(table, record.delete('id')) if Hyperion.new?(record)
74
93
  record[:kind] = table
75
94
  record
76
95
  end
@@ -30,17 +30,19 @@ module Hyperion
30
30
  @app = app
31
31
  @connection_url = opts[:connection_url]
32
32
  @ds = opts[:ds]
33
- @ds_opts = opts[:ds_opts]
33
+ @ds_opts = opts[:ds_opts] || {}
34
34
  end
35
35
 
36
36
  def call(env)
37
- Sql.with_connection_and_ds(@connection_url, @ds, @ds_opts) do
38
- Sql.transaction do
39
- @app.call(env)
37
+ Hyperion.with_datastore(@ds, @ds_opts.merge(:connection_url => @connection_url)) do
38
+ Sql.with_connection(@connection_url) do
39
+ Sql.transaction do
40
+ @app.call(env)
41
+ end
40
42
  end
41
43
  end
42
- end
43
44
 
45
+ end
44
46
  end
45
47
  end
46
48
  end
@@ -1,4 +1,4 @@
1
- require 'hyperion/api'
1
+ require 'hyperion'
2
2
  require 'hyperion/sql'
3
3
 
4
4
  shared_examples_for 'Sql Transactions' do
@@ -25,7 +25,7 @@ shared_examples_for 'Sql Transactions' do
25
25
  end
26
26
 
27
27
  def test_count
28
- Hyperion::API.count_by_kind('test')
28
+ Hyperion.count_by_kind('test')
29
29
  end
30
30
 
31
31
  context 'rollback' do
@@ -45,7 +45,7 @@ shared_examples_for 'Sql Transactions' do
45
45
  write("INSERT INTO test (name, age) VALUES ('Myles', 23)")
46
46
  test_count.should == 2
47
47
  end
48
- test_count.should == 1
48
+ test_count.should == 1
49
49
  end
50
50
  test_count.should == 0
51
51
  end
@@ -1,5 +1,5 @@
1
- require 'hyperion/sql/middleware'
2
1
  require 'do_sqlite3'
2
+ require 'hyperion/sql/middleware'
3
3
 
4
4
  describe Hyperion::Sql::Middleware do
5
5
 
@@ -29,10 +29,10 @@ describe Hyperion::Sql::Middleware do
29
29
 
30
30
  it 'assigns the datastore' do
31
31
  midd = middleware lambda { |env|
32
- Hyperion::API.datastore.class.should == Hyperion::Memory
32
+ Hyperion.datastore.class.should == Hyperion::Memory
33
33
  }
34
34
  midd.call(nil)
35
- expect {Hyperion::API.datastore}.to raise_error
35
+ expect {Hyperion.datastore}.to raise_error
36
36
  end
37
37
 
38
38
  it 'starts a transaction' do
@@ -1,4 +1,38 @@
1
+ require 'do_sqlite3'
1
2
  require 'hyperion/sql'
2
3
 
3
4
  describe Hyperion::Sql do
5
+
6
+ def sql
7
+ Hyperion::Sql
8
+ end
9
+
10
+ context 'with_connection' do
11
+ it 'assigns the connection' do
12
+ sql.with_connection('sqlite3::memory:') do
13
+ Thread.current[:connection].should be_a(DataObjects::Connection)
14
+ end
15
+ end
16
+
17
+ it 'returns the block result' do
18
+ sql.with_connection('sqlite3::memory:') do
19
+ :return
20
+ end.should == :return
21
+ end
22
+
23
+ it 'closes the connection' do
24
+ sql.with_connection('sqlite3::memory:') do
25
+ Thread.current[:connection].should_receive(:close)
26
+ end
27
+ end
28
+
29
+ it 'closes the connection when an exception is raised' do
30
+ expect {
31
+ sql.with_connection('sqlite3::memory:') do
32
+ Thread.current[:connection].should_receive(:close)
33
+ raise
34
+ end
35
+ }.to raise_error
36
+ end
37
+ end
4
38
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hyperion-sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.alpha5
5
- prerelease: 6
4
+ version: 0.1.0
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - 8th Light, Inc.
8
+ - Myles Megyesi
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-18 00:00:00.000000000 Z
12
+ date: 2012-10-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -34,7 +34,7 @@ dependencies:
34
34
  requirements:
35
35
  - - '='
36
36
  - !ruby/object:Gem::Version
37
- version: 0.0.1.alpha5
37
+ version: 0.1.0
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,7 +42,7 @@ dependencies:
42
42
  requirements:
43
43
  - - '='
44
44
  - !ruby/object:Gem::Version
45
- version: 0.0.1.alpha5
45
+ version: 0.1.0
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: uuidtools
48
48
  requirement: !ruby/object:Gem::Requirement
@@ -78,7 +78,6 @@ dependencies:
78
78
  description: Shared behavior for Sql databases
79
79
  email:
80
80
  - myles@8thlight.com
81
- - skim@8thlight.com
82
81
  executables: []
83
82
  extensions: []
84
83
  extra_rdoc_files: []
@@ -109,9 +108,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
109
108
  required_rubygems_version: !ruby/object:Gem::Requirement
110
109
  none: false
111
110
  requirements:
112
- - - ! '>'
111
+ - - ! '>='
113
112
  - !ruby/object:Gem::Version
114
- version: 1.3.1
113
+ version: '0'
115
114
  requirements: []
116
115
  rubyforge_project:
117
116
  rubygems_version: 1.8.24