oldmoe-activerecord-neverblock-postgresql-adapter 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README ADDED
@@ -0,0 +1,8 @@
1
+ == ActiveRecord NeverBlock PostgreSQL Adapter
2
+
3
+ An ActiveRecord Adapter for the NeverBlock::PostgreSQL database driver. It is able to run async SQL queries.
4
+
5
+ The adapter supports transactions in a Fiber context.
6
+
7
+ === License
8
+ Ruby License, http://www.ruby-lang.org/en/LICENSE.txt.
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "activerecord-neverblock-postgresql-adapter"
3
+ s.version = "0.1.0"
4
+ s.date = "2008-08-13"
5
+ s.summary = "Fibered ActiveRecord PostgreSQL adapter"
6
+ s.email = "oldmoe@gmail.com"
7
+ s.homepage = "http://github.com/oldmoe/neverblock-pg"
8
+ s.description = "Fibered ActiveRecord PostgreSQL adapter."
9
+ s.has_rdoc = true
10
+ s.authors = ["Muhammad A. Ali", "Ahmed Sobhi"]
11
+ s.files = [
12
+ "activerecord-neverblock-postgresql-adapter.gemspec",
13
+ "README",
14
+ "lib/active_record/connection_adapters/neverblock_postgresql_adapter.rb"]
15
+ s.rdoc_options = ["--main", "README"]
16
+ s.extra_rdoc_files = ["README"]
17
+ #s.add_dependency("neverblock")
18
+ #s.add_dependency("neverblock-pg")
19
+ end
20
+
@@ -0,0 +1,90 @@
1
+ require 'active_record/connection_adapters/postgresql_adapter'
2
+ require 'neverblock-pg'
3
+ require 'never_block/frameworks/activerecord'
4
+
5
+
6
+ class ActiveRecord::ConnectionAdapters::NeverBlockPostgreSQLAdapter < ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
7
+ # Returns 'FiberedPostgreSQL' as adapter name for identification purposes.
8
+ def adapter_name
9
+ 'NeverBlockPostgreSQL'
10
+ end
11
+
12
+ def begin_db_transaction
13
+ @connection.begin_db_transaction
14
+ end
15
+
16
+ def commit_db_transaction
17
+ @connection.commit_db_transaction
18
+ end
19
+
20
+ def rollback_db_transaction
21
+ @connection.rollback_db_transaction
22
+ end
23
+ # Executes an INSERT query and returns the new record's ID, this wont
24
+ # work on earlier versions of PostgreSQL but they don't suppor the async
25
+ # interface anyway
26
+ def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
27
+ @connection.exec(sql << " returning id ")
28
+ end
29
+
30
+ def connect
31
+ size = @connection_parameters.shift
32
+ @connection = ::NB::DB::PooledFiberedPostgresConnection.new(@connection_parameters, size)
33
+
34
+ PGconn.translate_results = false if PGconn.respond_to?(:translate_results=)
35
+
36
+ # Ignore async_exec and async_query when using postgres-pr.
37
+ @async = @config[:allow_concurrency] && @connection.respond_to?(:async_exec)
38
+
39
+ # Use escape string syntax if available. We cannot do this lazily when encountering
40
+ # the first string, because that could then break any transactions in progress.
41
+ # See: http://www.postgresql.org/docs/current/static/runtime-config-compatible.html
42
+ # If PostgreSQL doesn't know the standard_conforming_strings parameter then it doesn't
43
+ # support escape string syntax. Don't override the inherited quoted_string_prefix.
44
+ @connection.begin_db_transaction
45
+ if supports_standard_conforming_strings?
46
+ self.class.instance_eval do
47
+ define_method(:quoted_string_prefix) { 'E' }
48
+ end
49
+ end
50
+
51
+ # Money type has a fixed precision of 10 in PostgreSQL 8.2 and below, and as of
52
+ # PostgreSQL 8.3 it has a fixed precision of 19. PostgreSQLColumn.extract_precision
53
+ # should know about this but can't detect it there, so deal with it here.
54
+ money_precision = (postgresql_version >= 80300) ? 19 : 10
55
+ ::ActiveRecord::ConnectionAdapters::PostgreSQLColumn.module_eval(<<-end_eval)
56
+ def extract_precision(sql_type)
57
+ if sql_type =~ /^money$/
58
+ #{money_precision}
59
+ else
60
+ super
61
+ end
62
+ end
63
+ end_eval
64
+
65
+ configure_connection
66
+ @connection.commit_db_transaction
67
+ end
68
+ end
69
+
70
+ class ActiveRecord::Base
71
+ # Establishes a connection to the database that's used by all Active Record objects
72
+ def self.neverblock_postgresql_connection(config) # :nodoc:
73
+ config = config.symbolize_keys
74
+ host = config[:host]
75
+ port = config[:port] || 5432
76
+ username = config[:username].to_s
77
+ password = config[:password].to_s
78
+ size = config[:connections] || 4
79
+
80
+ if config.has_key?(:database)
81
+ database = config[:database]
82
+ else
83
+ raise ArgumentError, "No database specified. Missing argument: database."
84
+ end
85
+
86
+ # The postgres drivers don't allow the creation of an unconnected PGconn object,
87
+ # so just pass a nil connection object for the time being.
88
+ ::ActiveRecord::ConnectionAdapters::NeverBlockPostgreSQLAdapter.new(nil, logger, [size, host, port, nil, nil, database, username, password], config)
89
+ end
90
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oldmoe-activerecord-neverblock-postgresql-adapter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Muhammad A. Ali
8
+ - Ahmed Sobhi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2008-08-13 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Fibered ActiveRecord PostgreSQL adapter.
18
+ email: oldmoe@gmail.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - README
25
+ files:
26
+ - activerecord-neverblock-postgresql-adapter.gemspec
27
+ - README
28
+ - lib/active_record/connection_adapters/neverblock_postgresql_adapter.rb
29
+ has_rdoc: true
30
+ homepage: http://github.com/oldmoe/neverblock-pg
31
+ post_install_message:
32
+ rdoc_options:
33
+ - --main
34
+ - README
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.2.0
53
+ signing_key:
54
+ specification_version: 2
55
+ summary: Fibered ActiveRecord PostgreSQL adapter
56
+ test_files: []
57
+