jqr-postgres_sequence_support 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,6 @@
1
+ v0.0.3. Initial gem release.
2
+
3
+ v0.0.2. Added create_sequence an drop_sequence by Jonathan Tron [JonathanTron].
4
+ Proper id_sequence inference by Jonathan Tron [JonathanTron].
5
+
6
+ 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.
data/Manifest ADDED
@@ -0,0 +1,6 @@
1
+ CHANGELOG
2
+ LICENSE
3
+ postgres_sequences.rb
4
+ Rakefile
5
+ README.rdoc
6
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1,45 @@
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 'jqr-postgres_sequence_support', :lib => 'postgres_sequence_support', :source => 'http://gems.github.com'
39
+
40
+ $ rake gems:install
41
+
42
+
43
+
44
+ Homepage:: http://github.com/jqr/postgres_sequence_support
45
+ License:: Copyright (c) 2008 Elijah Miller <mailto:elijah.miller@gmail.com>, released under the MIT license.
data/Rakefile ADDED
@@ -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
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{postgres_sequence_support}
5
+ s.version = "0.0.3"
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-01-15}
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", "LICENSE", "README.rdoc"]
13
+ s.files = ["CHANGELOG", "LICENSE", "postgres_sequences.rb", "Rakefile", "README.rdoc", "Manifest", "postgres_sequence_support.gemspec"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Postgres_sequence_support", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{postgres_sequence_support}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Easy manipulation of PostgreSQL sequences from ActiveRecord.}
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 2
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ s.add_development_dependency(%q<echoe>, [">= 0"])
28
+ else
29
+ s.add_dependency(%q<echoe>, [">= 0"])
30
+ end
31
+ else
32
+ s.add_dependency(%q<echoe>, [">= 0"])
33
+ end
34
+ end
@@ -0,0 +1,55 @@
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(#{quote_table_name(sequence)})").to_i
26
+ end
27
+
28
+ def current_in_sequence(sequence)
29
+ select_value("SELECT currval(#{quote_table_name(sequence)})").to_i
30
+ end
31
+
32
+ def current_in_sequence(sequence)
33
+ select_value("SELECT lastval(#{quote_table_name(sequence)})").to_i
34
+ end
35
+
36
+ def set_sequence(sequence, value, value_used = true)
37
+ select_value("SELECT setval(#{quote_table_name(sequence)}, #{quote(value)}, #{quote(value_used)})")
38
+ end
39
+
40
+ def create_sequence(sequence, start = nil)
41
+ options = ''
42
+ options << " START #{quote(start)}" if start # Set start value (ie: value returned by next_in_sequence on next call)
43
+ execute "CREATE SEQUENCE #{quote_table_name(sequence)} #{options}" unless sequence_exists?(sequence)
44
+ end
45
+
46
+ def drop_sequence(sequence)
47
+ execute "DROP SEQUENCE IF EXISTS #{quote_table_name(sequence)}"
48
+ end
49
+
50
+ def sequence_exists?(sequence)
51
+ query("SELECT relname FROM pg_class WHERE relkind='S'").flatten.include?(sequence)
52
+ end
53
+ end
54
+ end
55
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jqr-postgres_sequence_support
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Elijah Miller
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-15 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: echoe
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description: Easy manipulation of PostgreSQL sequences from ActiveRecord.
25
+ email: elijah.miller@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - CHANGELOG
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - CHANGELOG
36
+ - LICENSE
37
+ - postgres_sequences.rb
38
+ - Rakefile
39
+ - README.rdoc
40
+ - Manifest
41
+ - postgres_sequence_support.gemspec
42
+ has_rdoc: true
43
+ homepage: ""
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --line-numbers
47
+ - --inline-source
48
+ - --title
49
+ - Postgres_sequence_support
50
+ - --main
51
+ - README.rdoc
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "1.2"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project: postgres_sequence_support
69
+ rubygems_version: 1.2.0
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: Easy manipulation of PostgreSQL sequences from ActiveRecord.
73
+ test_files: []
74
+