sequel_oracle_extensions 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,123 +1,148 @@
1
- require 'sequel'
2
- Sequel.require 'adapters/shared/oracle'
3
-
4
- # The oracle_schemata extension adds some schema related methods to the Oracle database adapater.
5
- module Sequel
6
- module Oracle
7
- module DatabaseMethods
8
-
9
- SELECT_INDEXES_SQL = %q{
10
- SELECT i.index_name, i.status, i.uniqueness, ic.column_name
11
- FROM all_indexes i
12
- INNER JOIN all_ind_columns ic
13
- ON ic.index_owner = i.owner AND ic.index_name = i.index_name
14
- WHERE i.table_name = ? AND i.dropped = 'NO'
15
- ORDER BY status DESC, index_name, ic.column_position
16
- }.freeze
17
-
18
- # Returns the indexes for the given table. By default, it does not return primary keys.
19
- #
20
- # * <tt>:all</tt> - Returns all indexes, even primary keys.
21
- def indexes(table, options={})
22
- sql, m = SELECT_INDEXES_SQL, output_identifier_meth
23
- table = m[table]
24
- ixs = Hash.new{|h,k| h[k] = {:table_name=>table, :columns=>[]}}
25
-
26
- if options[:all]
27
- sql = sql.sub /ORDER BY /, %q{ AND NOT EXISTS (
28
- SELECT uc.index_name FROM all_constraints uc
29
- WHERE uc.index_name = i.index_name AND uc.owner = i.owner AND uc.constraint_type = 'P'
30
- )
31
- ORDER BY }
32
- end
33
-
34
- metadata_dataset.with_sql(SELECT_INDEXES_SQL, table.to_s.upcase).each do |r|
35
- r = Hash[ r.map{|k,v| [k, (k==:index_name || k==:column_name) ? m[v] : v]} ]
36
- ix = ixs[m.call r.delete(:index_name)]
37
- ix[:valid] = r.delete(:status)=='VALID'
38
- ix[:unique] = r.delete(:uniqueness)=='UNIQUE'
39
- ix[:columns] << r.delete(:column_name)
40
- ix.update r
41
- end
42
- ixs
43
- end
44
-
45
- # Returns the primary key for the given +table+ (or +schema.table+), as a hash.
46
- #
47
- # * <tt>:enabled</tt> - Only look for keys that are enabled (true) or disabled (false). By default (nil),
48
- # looks for any matching key.
49
- # * <tt>:all</tt> - Return an array of matching keys, instead of the first matching key.
50
- #
51
- def primary_key(qualified_table, options={})
52
- result = table_constraints(qualified_table, options) do |ds,table_name,x_cons|
53
- ds.where(:c__constraint_type=>'P', :c__table_name=>table_name).
54
- order(:status.desc, :index_name, :cc__position)
55
- end
56
- options[:all] ? result : result.first
57
- end
58
-
59
- # Returns the foreign keys defined on the given +table+ (or +schema.table+), as an array of hashes.
60
- #
61
- # * <tt>:enabled</tt> - Only look for keys that are enabled (true) or disabled (false). By default (nil),
62
- # looks for all matching keys.
63
- def foreign_keys(qualified_table, options={})
64
- table_constraints(qualified_table, options) do |ds,table_name,x_cons|
65
- ds.where(:c__constraint_type=>'R', :c__table_name=>table_name).
66
- order(:table_name, :constraint_name, :cc__position)
67
- end
68
- end
69
-
70
- # Returns foreign keys that refer to the given +table+ (or +schema.table+), as an array of hashes.
71
- #
72
- # * <tt>:enabled</tt> - Only look for keys that are enabled (true) or disabled (false). By default (nil),
73
- # looks for all matching keys.
74
- def references(qualified_table, options={})
75
- table_constraints(qualified_table, options) do |ds,table_name,x_cons|
76
- ds.join(:"#{x_cons}traints___t", [[:owner,:c__r_owner], [:constraint_name,:c__r_constraint_name]]).
77
- where(:c__constraint_type=>'R', :t__constraint_type=>'P', :t__table_name=>table_name).
78
- order(:table_name, :constraint_name, :cc__position)
79
- end
80
- end
81
-
82
- private
83
-
84
- # Internal helper method for introspection of table constraints.
85
- def table_constraints(qualified_table,options={})
86
- ds, result = metadata_dataset, []
87
- schema, table = ds.schema_and_table(qualified_table)
88
- x_cons = schema.nil? ? 'user_cons' : 'all_cons'
89
- inm = ds.identifier_input_method
90
-
91
- # Build the dataset and apply filters for introspection of constraints.
92
- # Also allows the caller to customize the dataset.
93
- ds = ds.select(:c__constraint_name, :c__table_name, :c__rely, :c__status, :c__validated,
94
- :cc__column_name, :c__index_name, :c__constraint_type).
95
- from(:"#{x_cons}traints___c").
96
- join(:"#{x_cons}_columns___cc", [[:owner,:owner], [:constraint_name,:constraint_name]])
97
- unless schema.nil?
98
- ds = ds.where :c__owner => schema.to_s.send(inm)
99
- end
100
- unless (z = options[:enabled]).nil?
101
- ds = ds.where :status => (z ? 'ENABLED' : 'DISABLED')
102
- end
103
- ds = yield ds, table.to_s.send(inm), x_cons
104
-
105
- # Return the table constraints as an array of hashes, including a column list.
106
- hash = Hash.new do |h,k|
107
- result.push :constraint_name=>ds.send(:output_identifier,k), :columns=>[]
108
- h[k] = result.last
109
- end
110
- ds.each do |row|
111
- ref = hash[row[:constraint_name]]
112
- ref[:table_name]||= ds.send(:output_identifier,row.delete(:table_name))
113
- ref[:columns] << ds.send(:output_identifier,row.delete(:column_name))
114
- ref[:rely] ||= row.delete(:rely)=='RELY'
115
- ref[:enabled] ||= row.delete(:status)=='ENABLED'
116
- ref[:validated] ||= row.delete(:validated)=='VALIDATED'
117
- ref[:index_name]||= ds.send(:output_identifier,row.delete(:index_name)) if row[:index_name]
118
- end
119
- result
120
- end
121
- end
122
- end
123
- end
1
+ require 'sequel'
2
+ Sequel.require 'adapters/shared/oracle'
3
+
4
+ # The oracle_schemata extension adds some schema related methods to the Oracle database adapater.
5
+ module Sequel
6
+ module Oracle
7
+ module DatabaseMethods
8
+
9
+ # Returns the indexes for the given +table+ (or +schema.table+), as an array of hashes.
10
+ # By default, it does not return primary keys.
11
+ #
12
+ # * <tt>:valid</tt> - Only look for indexes that are valid (true) or unusable (false). By default (nil),
13
+ # looks for any matching index.
14
+ # * <tt>:all</tt> - Returns all indexes, even ones used for primary keys.
15
+ def indexes(qualified_table, options={})
16
+ ds, result = metadata_dataset, []
17
+ outm = lambda{|k| ds.send :output_identifier, k}
18
+ schema, table = ds.schema_and_table(qualified_table).map{|k| k.to_s.send(ds.identifier_input_method) if k}
19
+
20
+ # Build the dataset and apply filters for introspection of indexes.
21
+ ds = ds.select(:i__index_name, :i__status, :i__uniqueness, :i__visibility,
22
+ :i__index_type, :i__join_index, :ic__column_name).
23
+ from(:"all_indexes___i").
24
+ join(:"all_ind_columns___ic", [ [:index_owner,:owner], [:index_name,:index_name] ]).
25
+ where(:i__table_name=>table, :i__dropped=>'NO').
26
+ order(:status.desc, :index_name, :ic__column_position)
27
+ unless schema.nil?
28
+ ds = ds.where :i__owner => schema
29
+ end
30
+ unless (z = options[:valid]).nil?
31
+ ds = ds.where :i__status => (z ? 'VALID' : 'UNUSABLE')
32
+ end
33
+ if options[:all]
34
+ pk = from(:all_constraints.as(:c)).where(:c__constraint_type=>'P').
35
+ where(:c__index_name=>:i__index_name, :c__owner=>:i__owner)
36
+ ds = ds.where ~pk.exists
37
+ end
38
+
39
+ # Return the table constraints as an array of hashes, including a column list.
40
+ hash = Hash.new do |h,k|
41
+ result.push :index_name=>outm[k], :table_name=>outm[table], :columns=>[]
42
+ h[k] = result.last
43
+ end
44
+ ds.each do |row|
45
+ ref = hash[row[:index_name]]
46
+ ref[:index_type] ||= row[:index_type]
47
+ ref[:join_index] ||= row[:join_index]=='YES'
48
+ ref[:status] ||= row[:status]=='VALID'
49
+ ref[:uniqueness] ||= row[:uniqueness]=='UNIQUE'
50
+ ref[:visibility] ||= row[:visibility]=='VISIBLE'
51
+ ref[:columns] << outm[row[:column_name]]
52
+ end
53
+ result
54
+ end
55
+
56
+ # Returns the primary key for the given +table+ (or +schema.table+), as a hash.
57
+ #
58
+ # * <tt>:enabled</tt> - Only look for keys that are enabled (true) or disabled (false). By default (nil),
59
+ # looks for any matching key.
60
+ # * <tt>:all</tt> - Return an array of matching keys, instead of the first matching key.
61
+ #
62
+ def primary_key(qualified_table, options={})
63
+ result = table_constraints qualified_table, 'P', options
64
+ options[:all] ? result : result.first
65
+ end
66
+
67
+ # Returns unique constraints defined on the given +table+ (or +schema.table+), as an array of hashes.
68
+ #
69
+ # * <tt>:enabled</tt> - Only look for keys that are enabled (true) or disabled (false). By default (nil),
70
+ # looks for all matching keys.
71
+ def unique_keys(qualified_table, options={})
72
+ table_constraints qualified_table, 'U', options
73
+ end
74
+
75
+ # Returns foreign keys defined on the given +table+ (or +schema.table+), as an array of hashes.
76
+ #
77
+ # * <tt>:enabled</tt> - Only look for keys that are enabled (true) or disabled (false). By default (nil),
78
+ # looks for all matching keys.
79
+ def foreign_keys(qualified_table, options={})
80
+ table_constraints qualified_table, 'R', options
81
+ end
82
+
83
+ # Returns foreign keys that refer to the given +table+ (or +schema.table+), as an array of hashes.
84
+ #
85
+ # * <tt>:enabled</tt> - Only look for keys that are enabled (true) or disabled (false). By default (nil),
86
+ # looks for all matching keys.
87
+ def references(qualified_table, options={})
88
+ table_constraints qualified_table, 'R', options.merge(:table_name_column=>:t__table_name)
89
+ end
90
+
91
+ private
92
+
93
+ # Internal helper method for introspection of table constraints.
94
+ def table_constraints(qualified_table, constraint_type, options={})
95
+ ds, result = metadata_dataset, []
96
+ outm = lambda{|k| ds.send :output_identifier, k}
97
+ schema, table = ds.schema_and_table(qualified_table).map{|k| k.to_s.send(ds.identifier_input_method) if k}
98
+ x_cons = schema.nil? ? 'user_cons' : 'all_cons'
99
+
100
+ # Build the dataset and apply filters for introspection of constraints.
101
+ # Also allows the caller to customize the dataset.
102
+ ds = ds.select(:c__constraint_name, :c__table_name, :c__rely, :c__status, :c__validated, :cc__column_name).
103
+ from(:"#{x_cons}traints___c").
104
+ join(:"#{x_cons}_columns___cc", [ [:owner,:owner], [:constraint_name,:constraint_name] ]).
105
+ where((options[:table_name_column]||:c__table_name)=>table, :c__constraint_type=>constraint_type).
106
+ order(:table_name, :status.desc, :constraint_name, :cc__position)
107
+ unless schema.nil?
108
+ ds = ds.where :c__owner => schema
109
+ end
110
+ unless (z = options[:enabled]).nil?
111
+ ds = ds.where :c__status => (z ? 'ENABLED' : 'DISABLED')
112
+ end
113
+
114
+ if constraint_type == 'R'
115
+ ds = ds.select_more(:c__r_constraint_name, :t__table_name.as(:r_table_name)).
116
+ join(:"#{x_cons}traints___t", [ [:owner,:c__r_owner], [:constraint_name,:c__r_constraint_name] ]).
117
+ where(:t__constraint_type=>'P')
118
+ else
119
+ ds = ds.select_more(:c__index_name)
120
+ end
121
+ ds = yield ds, table if block_given?
122
+
123
+ # Return the table constraints as an array of hashes, including a column list.
124
+ hash = Hash.new do |h,k|
125
+ result.push :constraint_name=>outm[k], :constraint_type=>constraint_type, :columns=>[]
126
+ h[k] = result.last
127
+ end
128
+ ds.each do |row|
129
+ ref = hash[row[:constraint_name]]
130
+ ref[:table_name] ||= outm[row[:table_name]]
131
+ ref[:rely] ||= row[:rely]=='RELY'
132
+ ref[:enabled] ||= row[:status]=='ENABLED'
133
+ ref[:validated] ||= row[:validated]=='VALIDATED'
134
+ ref[:columns] << outm[row[:column_name]]
135
+
136
+ if row.include? :r_constraint_name
137
+ ref[:r_constraint_name] ||= outm[row[:r_constraint_name]]
138
+ ref[:r_table_name] ||= outm[row[:r_table_name]]
139
+ end
140
+ if row[:index_name]
141
+ ref[:index_name] ||= outm[row[:index_name]]
142
+ end
143
+ end
144
+ result
145
+ end
146
+ end
147
+ end
148
+ end
@@ -1,4 +1,4 @@
1
- require 'sequel'
2
- require 'sequel/oracle_extensions/schemata'
3
- require 'sequel/oracle_extensions/merge'
4
- require 'sequel/oracle_extensions/hints'
1
+ require 'sequel'
2
+ require 'sequel/oracle_extensions/schemata'
3
+ require 'sequel/oracle_extensions/merge'
4
+ require 'sequel/oracle_extensions/hints'
@@ -1 +1 @@
1
- require 'sequel/oracle_extensions'
1
+ require 'sequel/oracle_extensions'
@@ -1,56 +1,62 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in rakefile, and run 'rake gemspec'
4
- # -*- encoding: utf-8 -*-
5
-
6
- Gem::Specification.new do |s|
7
- s.name = %q{sequel_oracle_extensions}
8
- s.version = "0.5.2"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Joe Khoobyar"]
12
- s.date = %q{2011-08-16}
13
- s.description = %q{Oracle extensions for Sequel, including MERGE statements, optimizer hints, and schema extensions.}
14
- s.email = %q{joe@ankhcraft.com}
15
- s.extra_rdoc_files = [
16
- "LICENSE",
17
- "README.rdoc"
18
- ]
19
- s.files = [
20
- ".document",
21
- ".rspec",
22
- "LICENSE",
23
- "README.rdoc",
24
- "Rakefile",
25
- "VERSION",
26
- "lib/sequel/oracle_extensions.rb",
27
- "lib/sequel/oracle_extensions/hints.rb",
28
- "lib/sequel/oracle_extensions/merge.rb",
29
- "lib/sequel/oracle_extensions/schemata.rb",
30
- "lib/sequel_oracle_extensions.rb",
31
- "sequel_oracle_extensions.gemspec",
32
- "spec/sequel/oracle_extensions/hints_spec.rb",
33
- "spec/sequel/oracle_extensions/merge_spec.rb",
34
- "spec/spec_helper.rb"
35
- ]
36
- s.homepage = %q{http://github.com/joekhoobyar/sequel_oracle_extensions}
37
- s.require_paths = ["lib"]
38
- s.rubygems_version = %q{1.5.2}
39
- s.summary = %q{Oracle MERGE, optimizer hints, an schema extensions for Sequel}
40
-
41
- if s.respond_to? :specification_version then
42
- s.specification_version = 3
43
-
44
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
- s.add_runtime_dependency(%q<sequel>, [">= 3.10.0"])
46
- s.add_development_dependency(%q<rspec>, [">= 2.0.0"])
47
- else
48
- s.add_dependency(%q<sequel>, [">= 3.10.0"])
49
- s.add_dependency(%q<rspec>, [">= 2.0.0"])
50
- end
51
- else
52
- s.add_dependency(%q<sequel>, [">= 3.10.0"])
53
- s.add_dependency(%q<rspec>, [">= 2.0.0"])
54
- end
55
- end
56
-
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{sequel_oracle_extensions}
8
+ s.version = "0.5.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Joe Khoobyar"]
12
+ s.date = %q{2011-08-17}
13
+ s.description = %q{Oracle extensions for Sequel, including MERGE statements, optimizer hints, and schema extensions.}
14
+ s.email = %q{joe@ankhcraft.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/sequel/oracle_extensions.rb",
27
+ "lib/sequel/oracle_extensions/hints.rb",
28
+ "lib/sequel/oracle_extensions/merge.rb",
29
+ "lib/sequel/oracle_extensions/schemata.rb",
30
+ "lib/sequel_oracle_extensions.rb",
31
+ "sequel_oracle_extensions.gemspec",
32
+ "spec/sequel/oracle_extensions/hints_spec.rb",
33
+ "spec/sequel/oracle_extensions/merge_spec.rb",
34
+ "spec/spec_helper.rb"
35
+ ]
36
+ s.homepage = %q{http://github.com/joekhoobyar/sequel_oracle_extensions}
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.7}
39
+ s.summary = %q{Oracle MERGE, optimizer hints, an schema extensions for Sequel}
40
+ s.test_files = [
41
+ "spec/sequel/oracle_extensions/hints_spec.rb",
42
+ "spec/sequel/oracle_extensions/merge_spec.rb",
43
+ "spec/spec_helper.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
+ s.add_runtime_dependency(%q<sequel>, [">= 3.10.0"])
52
+ s.add_development_dependency(%q<rspec>, [">= 2.0.0"])
53
+ else
54
+ s.add_dependency(%q<sequel>, [">= 3.10.0"])
55
+ s.add_dependency(%q<rspec>, [">= 2.0.0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<sequel>, [">= 3.10.0"])
59
+ s.add_dependency(%q<rspec>, [">= 2.0.0"])
60
+ end
61
+ end
62
+