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
|
-
|
10
|
+
if supports_views?
|
11
|
+
view_definition = ViewDefinition.new(self, select_query)
|
11
12
|
|
12
|
-
|
13
|
+
yield view_definition
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
15
|
+
if options[:force]
|
16
|
+
drop_view(name) rescue nil
|
17
|
+
end
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
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
|
-
|
33
|
-
|
34
|
-
|
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 { |
|
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 { |
|
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
|
-
|
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
|
|