rails_sql_views 0.3.0 → 0.4.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.
@@ -7,20 +7,22 @@ module RailsSqlViews
7
7
  # Specify restrictions for inserts or updates in updatable views. ANSI SQL 92 defines two check option
8
8
  # values: CASCADED and LOCAL. See your database documentation for allowed values.
9
9
  def create_view(name, select_query, options={})
10
- view_definition = ViewDefinition.new(self, select_query)
10
+ if supports_views?
11
+ view_definition = ViewDefinition.new(self, select_query)
11
12
 
12
- yield view_definition
13
+ yield view_definition
13
14
 
14
- if options[:force]
15
- drop_view(name) rescue nil
16
- end
15
+ if options[:force]
16
+ drop_view(name) rescue nil
17
+ end
17
18
 
18
- create_sql = "CREATE VIEW "
19
- create_sql << "#{name} ("
20
- create_sql << view_definition.to_sql
21
- create_sql << ") AS #{view_definition.select_query}"
22
- create_sql << " WITH #{options[:check_option]} CHECK OPTION" if options[:check_option]
23
- execute create_sql
19
+ create_sql = "CREATE VIEW "
20
+ create_sql << "#{name} ("
21
+ create_sql << view_definition.to_sql
22
+ create_sql << ") AS #{view_definition.select_query}"
23
+ create_sql << " WITH #{options[:check_option]} CHECK OPTION" if options[:check_option]
24
+ execute create_sql
25
+ end
24
26
  end
25
27
 
26
28
  # Drop a view.
@@ -29,9 +31,11 @@ module RailsSqlViews
29
31
  # Specify the drop behavior. ANSI SQL 92 defines two drop behaviors, CASCADE and RESTRICT. See your
30
32
  # database documentation to determine what drop behaviors are available.
31
33
  def drop_view(name, options={})
32
- drop_sql = "DROP VIEW #{name}"
33
- drop_sql << " #{options[:drop_behavior]}" if options[:drop_behavior]
34
- execute drop_sql
34
+ if supports_views?
35
+ drop_sql = "DROP VIEW #{name}"
36
+ drop_sql << " #{options[:drop_behavior]}" if options[:drop_behavior]
37
+ execute drop_sql
38
+ end
35
39
  end
36
40
  end
37
41
  end
@@ -8,15 +8,28 @@ module ActiveRecord
8
8
 
9
9
  def tables(name = nil) #:nodoc:
10
10
  tables = []
11
- execute("SHOW TABLE STATUS", name).each { |field| tables << field[0] if field[17] != 'VIEW' }
11
+ execute("SHOW TABLE STATUS", name).each { |row| tables << row[0] if row[17] != 'VIEW' }
12
12
  tables
13
13
  end
14
14
 
15
15
  def views(name = nil) #:nodoc:
16
16
  views = []
17
- execute("SHOW TABLE STATUS", name).each { |field| views << field[0] if field[17] == 'VIEW' }
17
+ execute("SHOW TABLE STATUS", name).each { |row| views << row[0] if row[17] == 'VIEW' }
18
18
  views
19
19
  end
20
+
21
+ # Get the view select statement for the specified table.
22
+ def view_select_statement(view, name=nil)
23
+ row = execute("SHOW CREATE VIEW #{view}", name).each do |row|
24
+ return convert_statement(row[1]) if row[0] == view
25
+ end
26
+ raise "No view called #{view} found"
27
+ end
28
+
29
+ private
30
+ def convert_statement(s)
31
+ s.gsub!(/.* AS (select .*)/, '\1')
32
+ end
20
33
  end
21
34
  end
22
35
  end
@@ -15,7 +15,9 @@ module ActiveRecord
15
15
  # Add views to the end of the dump stream
16
16
  def dump_with_views(stream)
17
17
  dump_without_views(stream)
18
- views(stream)
18
+ if @connection.supports_views?
19
+ views(stream)
20
+ end
19
21
  trailer_without_views(stream)
20
22
  stream
21
23
  end
@@ -43,6 +45,7 @@ module ActiveRecord
43
45
  v = StringIO.new
44
46
 
45
47
  v.print " create_view #{view.inspect}"
48
+ v.print ", \"#{@connection.view_select_statement(view)}\""
46
49
  v.print ", :force => true"
47
50
  v.puts " do |v|"
48
51
 
@@ -1,7 +1,7 @@
1
1
  module RailsSqlViews
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
- MINOR = 3
4
+ MINOR = 4
5
5
  TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: rails_sql_views
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.3.0
6
+ version: 0.4.0
7
7
  date: 2006-12-28 00:00:00 -05:00
8
8
  summary: Adds SQL Views to Rails.
9
9
  require_paths: