rails_sql_views 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -2,5 +2,13 @@
2
2
  * Initial release
3
3
 
4
4
  0.5.0 - Released Dec 29, 2006
5
- * Added support for PostgreSQL
6
- * Fixed the schema dumper
5
+ * Added support for PostgreSQL (Michael Schuerig)
6
+ * Fixed the schema dumper
7
+
8
+ 0.5.1 - Released Jan 10, 2006
9
+ * Patch by Clifford T. Matthews to use String.dump to dump out the view select statement
10
+
11
+ 0.6.0 -
12
+ * Added support for SQL Server (Seth Ladd)-
13
+ * Added support for using views to map non-friendly database field names to AR-friendly names (Nathan Vack)
14
+ * Added support for Oracle (Alistair Davidson)
data/Rakefile CHANGED
@@ -53,7 +53,8 @@ spec = Gem::Specification.new do |s|
53
53
  Library which adds SQL Views to Rails.
54
54
  EOF
55
55
 
56
- s.add_dependency('activerecord', '>= 1.14.4')
56
+ s.add_dependency('activerecord', '>= 1.14.4')
57
+ s.add_dependency('rake', '>= 0.7.1')
57
58
 
58
59
  s.rdoc_options << '--exclude' << '.'
59
60
  s.has_rdoc = false
@@ -24,13 +24,17 @@
24
24
  $:.unshift(File.dirname(__FILE__)) unless
25
25
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
26
26
 
27
+ require 'rubygems'
28
+ unless Kernel.respond_to?(:gem)
29
+ Kernel.send :alias_method, :gem, :require_gem
30
+ end
31
+
27
32
  unless defined?(ActiveRecord)
28
33
  begin
29
34
  $:.unshift(File.dirname(__FILE__) + "/../../activerecord/lib")
30
35
  require 'active_record'
31
36
  rescue LoadError
32
- require 'rubygems'
33
- require_gem 'activerecord'
37
+ gem 'activerecord'
34
38
  end
35
39
  end
36
40
 
@@ -40,6 +44,7 @@ require 'rails_sql_views/connection_adapters/abstract/schema_definitions'
40
44
  require 'rails_sql_views/connection_adapters/abstract/schema_statements'
41
45
  require 'rails_sql_views/connection_adapters/mysql_adapter'
42
46
  require 'rails_sql_views/connection_adapters/postgresql_adapter'
47
+ require 'rails_sql_views/connection_adapters/sqlserver_adapter'
43
48
  require 'rails_sql_views/schema_dumper'
44
49
 
45
50
  class ActiveRecord::ConnectionAdapters::AbstractAdapter
@@ -25,5 +25,43 @@ module RailsSqlViews
25
25
  end
26
26
 
27
27
  end
28
+
29
+ class MappingDefinition
30
+
31
+ # Generates a hash of the form :old_column => :new_column
32
+ # Initially, it'll map column names to themselves.
33
+ # use map_column to modify the list.
34
+ def initialize(columns)
35
+ @columns = columns
36
+ @map = Hash.new()
37
+ columns.each do |c|
38
+ @map[c] = c
39
+ end
40
+
41
+ end
42
+
43
+ # Create a mapping from an old column name to a new one.
44
+ # If the new name is nil, specify that the old column shouldn't
45
+ # appear in this new view.
46
+ def map_column(old_name, new_name)
47
+ unless @map.include?(old_name)
48
+ raise ActiveRecord::ActiveRecordError, "column #{old_name} not found, can't be mapped"
49
+ end
50
+ if new_name.nil?
51
+ @map.delete old_name
52
+ @columns.delete old_name
53
+ else
54
+ @map[old_name] = new_name
55
+ end
56
+ end
57
+
58
+ def select_cols
59
+ @columns
60
+ end
61
+
62
+ def view_cols
63
+ @columns.map { |c| @map[c] }
64
+ end
65
+ end
28
66
  end
29
67
  end
@@ -24,6 +24,26 @@ module RailsSqlViews
24
24
  execute create_sql
25
25
  end
26
26
  end
27
+
28
+ # Also creates a view, with the specific purpose of remapping column names
29
+ # to make non-ActiveRecord tables friendly with the naming
30
+ # conventions, while maintaining legacy app compatibility.
31
+ def create_mapping_view(old_name, new_name, options = {})
32
+ return unless supports_views?
33
+
34
+ col_names = columns(old_name).collect { |col| col.name.to_sym }
35
+ mapper = MappingDefinition.new(col_names)
36
+
37
+ yield mapper
38
+
39
+ if options[:force]
40
+ drop_view(new_name) rescue nil
41
+ end
42
+
43
+ view_sql = "CREATE VIEW #{new_name} (#{mapper.view_cols.join(', ')}) "
44
+ view_sql << "AS SELECT #{mapper.select_cols.join(', ')} FROM #{old_name}"
45
+ execute view_sql
46
+ end
27
47
 
28
48
  # Drop a view.
29
49
  # The +options+ hash can include the following keys:
@@ -0,0 +1,32 @@
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ class OciAdapter
4
+ # Returns true as this adapter supports views.
5
+ def supports_views?
6
+ true
7
+ end
8
+
9
+ def tables(name = nil) #:nodoc:
10
+ tables = []
11
+ execute("SELECT TABLE_NAME FROM USER_TABLES", name).each { |row| tables << row[0] }
12
+ tables
13
+ end
14
+
15
+ def views(name = nil) #:nodoc:
16
+ views = []
17
+ execute("SELECT VIEW_NAME FROM USER_VIEWS", name).each { |row| views << row[0] }
18
+ views
19
+ end
20
+
21
+ # Get the view select statement for the specified table.
22
+ def view_select_statement(view, name=nil)
23
+ row = execute("SELECT TEXT FROM USER_VIEWS WHERE VIEW_NAME = '#{view}'", name).each do |row|
24
+ return row[0]
25
+ end
26
+ raise "No view called #{view} found"
27
+ end
28
+
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ class OracleAdapter
4
+ # Returns true as this adapter supports views.
5
+ def supports_views?
6
+ true
7
+ end
8
+
9
+ def tables(name = nil) #:nodoc:
10
+ tables = []
11
+ execute("SELECT TABLE_NAME FROM USER_TABLES", name).each { |row| tables << row[0] }
12
+ tables
13
+ end
14
+
15
+ def views(name = nil) #:nodoc:
16
+ views = []
17
+ execute("SELECT VIEW_NAME FROM USER_VIEWS", name).each { |row| views << row[0] }
18
+ views
19
+ end
20
+
21
+ # Get the view select statement for the specified table.
22
+ def view_select_statement(view, name=nil)
23
+ row = execute("SELECT TEXT FROM USER_VIEWS WHERE VIEW_NAME = '#{view}'", name).each do |row|
24
+ return row[0]
25
+ end
26
+ raise "No view called #{view} found"
27
+ end
28
+
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,36 @@
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ class SQLServerAdapter
4
+ # Returns true as this adapter supports views.
5
+ def supports_views?
6
+ true
7
+ end
8
+
9
+ # Returns all the view names from the currently connected schema.
10
+ def views(name = nil)
11
+ select_values("SELECT table_name FROM information_schema.views", name)
12
+ end
13
+
14
+ # Get the view select statement for the specified view.
15
+ def view_select_statement(view, name=nil)
16
+ q =<<-ENDSQL
17
+ SELECT view_definition FROM information_schema.views
18
+ WHERE table_name = '#{view}'
19
+ ENDSQL
20
+
21
+ view_def = select_value(q, name)
22
+
23
+ if view_def
24
+ return convert_statement(view_def)
25
+ else
26
+ raise "No view called #{view} found"
27
+ end
28
+ end
29
+
30
+ private
31
+ def convert_statement(s)
32
+ s.sub(/^CREATE.* AS (select .*)/, '\1').gsub(/\n/, '')
33
+ end
34
+ end
35
+ end
36
+ end
@@ -1,8 +1,8 @@
1
1
  module RailsSqlViews
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
- MINOR = 5
5
- TINY = 1
4
+ MINOR = 6
5
+ TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.0
2
+ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: rails_sql_views
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.5.1
7
- date: 2007-01-10 00:00:00 -05:00
6
+ version: 0.6.0
7
+ date: 2007-05-09 00:00:00 -04:00
8
8
  summary: Adds SQL Views to Rails.
9
9
  require_paths:
10
10
  - lib
@@ -33,18 +33,21 @@ files:
33
33
  - README
34
34
  - Rakefile
35
35
  - lib/core_ext
36
- - lib/rails_sql_views
37
- - lib/rails_sql_views.rb
38
36
  - lib/core_ext/module.rb
37
+ - lib/rails_sql_views
39
38
  - lib/rails_sql_views/connection_adapters
40
- - lib/rails_sql_views/schema_dumper.rb
41
- - lib/rails_sql_views/version.rb
42
39
  - lib/rails_sql_views/connection_adapters/abstract
40
+ - lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb
41
+ - lib/rails_sql_views/connection_adapters/abstract/schema_statements.rb
43
42
  - lib/rails_sql_views/connection_adapters/abstract_adapter.rb
44
43
  - lib/rails_sql_views/connection_adapters/mysql_adapter.rb
44
+ - lib/rails_sql_views/connection_adapters/oci_adapter.rb
45
+ - lib/rails_sql_views/connection_adapters/oracle_adapter.rb
45
46
  - lib/rails_sql_views/connection_adapters/postgresql_adapter.rb
46
- - lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb
47
- - lib/rails_sql_views/connection_adapters/abstract/schema_statements.rb
47
+ - lib/rails_sql_views/connection_adapters/sqlserver_adapter.rb
48
+ - lib/rails_sql_views/schema_dumper.rb
49
+ - lib/rails_sql_views/version.rb
50
+ - lib/rails_sql_views.rb
48
51
  test_files: []
49
52
 
50
53
  rdoc_options:
@@ -68,3 +71,12 @@ dependencies:
68
71
  - !ruby/object:Gem::Version
69
72
  version: 1.14.4
70
73
  version:
74
+ - !ruby/object:Gem::Dependency
75
+ name: rake
76
+ version_requirement:
77
+ version_requirements: !ruby/object:Gem::Version::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 0.7.1
82
+ version: