forked from anathematic/rails_sql_views
-
Notifications
You must be signed in to change notification settings - Fork 3
/
postgresql_adapter.rb
50 lines (42 loc) · 1.32 KB
/
postgresql_adapter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module ActiveRecord
module ConnectionAdapters
class PostgreSQLAdapter
# Returns true as this adapter supports views.
def supports_views?
true
end
def nonview_tables(name = nil)
q = <<-SQL
SELECT table_name, table_type
FROM information_schema.tables
WHERE table_schema IN (#{schemas})
AND table_type = 'BASE_TABLE'
SQL
query(q, name).map { |row| row[0] }
end
def views(name = nil) #:nodoc:
q = <<-SQL
SELECT table_name, table_type
FROM information_schema.tables
WHERE table_schema IN (#{schemas})
AND table_type = 'VIEW'
SQL
query(q, name).map { |row| row[0] }
end
def view_select_statement(view, name = nil)
q = <<-SQL
SELECT view_definition
FROM information_schema.views
WHERE table_catalog = (SELECT catalog_name FROM information_schema.information_schema_catalog_name)
AND table_schema IN (#{schemas})
AND table_name = '#{view}'
SQL
select_value(q, name) or raise "No view called #{view} found"
end
private
def schemas
schema_search_path.split(/,/).map { |p| quote(p) }.join(',')
end
end
end
end