postgres_sequence_support 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,12 @@
1
+ v0.1.0. Minor cleanups and released to gemcutter.
2
+
3
+ v0.0.5. Remove duplicate and correct quoting by Jonathan Tron [JonathanTron].
4
+
5
+ v0.0.4. Cleanup of gem release and proper Rails plugin support.
6
+
7
+ v0.0.3. Initial gem release.
8
+
9
+ v0.0.2. Added create_sequence an drop_sequence by Jonathan Tron [JonathanTron].
10
+ Proper id_sequence inference by Jonathan Tron [JonathanTron].
11
+
12
+ v0.0.1. Initial release.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Elijah Miller
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,8 @@
1
+ CHANGELOG
2
+ init.rb
3
+ lib/postgres_sequence_support.rb
4
+ LICENSE
5
+ Manifest
6
+ postgres_sequence_support.gemspec
7
+ Rakefile
8
+ README.rdoc
@@ -0,0 +1,48 @@
1
+ = Postgres Sequence Support
2
+
3
+ Easy manipulation of PostgreSQL sequences from ActiveRecord.
4
+
5
+ == Examples
6
+
7
+ User.next_id # => 1
8
+ User.current_id # => 1
9
+
10
+ c = ActiveRecord::Base.connection
11
+ c.set_sequence('users_id_seq', 1000)
12
+ c.next_in_sequence('users_id_seq') # => 1001
13
+
14
+ # Note : create sequence don't do anything if sequence already exists
15
+ c.create_sequence('my_own_seq')
16
+ c.next_in_sequence('my_own_seq') # => 1
17
+
18
+ # You can specify sequence start
19
+ c.create_sequence('my_own_seq', 1001)
20
+ c.next_in_sequence('my_own_seq') # => 1001
21
+
22
+ # Check if sequence exists
23
+ c.sequence_exists?('my_own_seq') # => true
24
+ c.sequence_exists?('my_other_seq') # => false
25
+
26
+ # Drop sequence if it exists
27
+ c.drop_sequence('my_own_seq')
28
+
29
+
30
+ == Install
31
+
32
+ As a Rails plugin.
33
+
34
+ ./script/plugin install git://github.com/jqr/postgres_sequence_support.git
35
+
36
+ Prefer gems? Add this to your environment.rb and run the following command.
37
+
38
+ config.gem 'postgres_sequence_support'
39
+
40
+ $ rake gems:install
41
+
42
+ == Docs
43
+
44
+ http://rdoc.info/projects/jqr/postgres_sequence_support
45
+
46
+
47
+ Homepage:: http://github.com/jqr/postgres_sequence_support
48
+ License:: Copyright (c) 2008 Elijah Miller <mailto:elijah.miller@gmail.com>, released under the MIT license.
@@ -0,0 +1,21 @@
1
+ require 'spec/rake/spectask'
2
+
3
+ require 'echoe'
4
+ Echoe.new 'postgres_sequence_support' do |p|
5
+ p.description = "Easy manipulation of PostgreSQL sequences from ActiveRecord."
6
+ # p.url = "http://postgres_sequence_support.rubyforge.org"
7
+ p.author = "Elijah Miller"
8
+ p.email = "elijah.miller@gmail.com"
9
+ p.retain_gemspec = true
10
+ p.need_tar_gz = false
11
+ p.extra_deps = [
12
+ ]
13
+ end
14
+
15
+ desc 'Default: run specs'
16
+ task :default => :spec
17
+ Spec::Rake::SpecTask.new do |t|
18
+ t.spec_files = FileList["spec/**/*_spec.rb"]
19
+ end
20
+
21
+ task :test => :spec
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'postgres_sequence_support'
@@ -0,0 +1,51 @@
1
+ module ActiveRecord
2
+ class Base
3
+ def self.id_sequence
4
+ connection.default_sequence_name
5
+ end
6
+
7
+ # Returns the next id on the table's id sequence.
8
+ def self.next_id
9
+ connection.next_in_sequence(id_sequence)
10
+ end
11
+
12
+ def self.current_id
13
+ connection.current_in_sequence(id_sequence)
14
+ end
15
+
16
+ def self.last_id
17
+ connection.current_in_sequence(id_sequence)
18
+ end
19
+ end
20
+
21
+ module ConnectionAdapters
22
+ class PostgreSQLAdapter
23
+ # Returns the next value in sequence.
24
+ def next_in_sequence(sequence)
25
+ select_value("SELECT nextval('#{PGconn.quote_ident(sequence)}')").to_i
26
+ end
27
+
28
+ def current_in_sequence(sequence)
29
+ select_value("SELECT currval('#{PGconn.quote_ident(sequence)}')").to_i
30
+ end
31
+
32
+ def set_sequence(sequence, value, value_used = true)
33
+ select_value("SELECT setval('#{PGconn.quote_ident(sequence)}', #{quote(value)}, #{quote(value_used)})")
34
+ end
35
+
36
+ def create_sequence(sequence, start = nil)
37
+ options = ''
38
+ options << " START #{quote(start)}" if start # Set start value (ie: value returned by next_in_sequence on next call)
39
+ execute "CREATE SEQUENCE #{quote_table_name(sequence)} #{options}" unless sequence_exists?(sequence)
40
+ end
41
+
42
+ def drop_sequence(sequence)
43
+ execute "DROP SEQUENCE IF EXISTS #{quote_table_name(sequence)}"
44
+ end
45
+
46
+ def sequence_exists?(sequence)
47
+ query("SELECT relname FROM pg_class WHERE relkind='S'").flatten.include?(sequence)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{postgres_sequence_support}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Elijah Miller"]
9
+ s.date = %q{2009-12-03}
10
+ s.description = %q{Easy manipulation of PostgreSQL sequences from ActiveRecord.}
11
+ s.email = %q{elijah.miller@gmail.com}
12
+ s.extra_rdoc_files = ["CHANGELOG", "lib/postgres_sequence_support.rb", "LICENSE", "README.rdoc"]
13
+ s.files = ["CHANGELOG", "init.rb", "lib/postgres_sequence_support.rb", "LICENSE", "Manifest", "postgres_sequence_support.gemspec", "Rakefile", "README.rdoc"]
14
+ s.homepage = %q{}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Postgres_sequence_support", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{postgres_sequence_support}
18
+ s.rubygems_version = %q{1.3.5}
19
+ s.summary = %q{Easy manipulation of PostgreSQL sequences from ActiveRecord.}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: postgres_sequence_support
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Elijah Miller
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-03 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Easy manipulation of PostgreSQL sequences from ActiveRecord.
17
+ email: elijah.miller@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - CHANGELOG
24
+ - lib/postgres_sequence_support.rb
25
+ - LICENSE
26
+ - README.rdoc
27
+ files:
28
+ - CHANGELOG
29
+ - init.rb
30
+ - lib/postgres_sequence_support.rb
31
+ - LICENSE
32
+ - Manifest
33
+ - postgres_sequence_support.gemspec
34
+ - Rakefile
35
+ - README.rdoc
36
+ has_rdoc: true
37
+ homepage: ""
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --line-numbers
43
+ - --inline-source
44
+ - --title
45
+ - Postgres_sequence_support
46
+ - --main
47
+ - README.rdoc
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "1.2"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project: postgres_sequence_support
65
+ rubygems_version: 1.3.5
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Easy manipulation of PostgreSQL sequences from ActiveRecord.
69
+ test_files: []
70
+