Class: ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
- Defined in:
- lib/citier/sql_adapters.rb
Instance Method Summary collapse
- #table_exists?(name) ⇒ Boolean
- #table_existsB?(name) ⇒ Boolean
- #tables(name = nil) ⇒ Object
- #tablesL(name = nil) ⇒ Object
- #views_existsB?(name) ⇒ Boolean
- #viewsL(name = nil) ⇒ Object
Instance Method Details
#table_exists?(name) ⇒ Boolean
68 69 70 71 72 |
# File 'lib/citier/sql_adapters.rb', line 68 def table_exists?(name) a=table_existsB?(name) b=views_existsB?(name) return a||b end |
#table_existsB?(name) ⇒ Boolean
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/citier/sql_adapters.rb', line 75 def table_existsB?(name) name = name.to_s schema, table = name.split('.', 2) unless table # A table was provided without a schema table = schema schema = nil end if name =~ /^"/ # Handle quoted table names table = name schema = nil end query(" SELECT COUNT(*)\n FROM pg_tables\n WHERE tablename = '\#{table.gsub(/(^\"|\"$)/,'')}'\n \#{schema ? \"AND schemaname = '\#{schema}'\" : ''}\n SQL\n\nend\n").first[0].to_i > 0 |
#tables(name = nil) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/citier/sql_adapters.rb', line 42 def tables(name = nil) a=tablesL(name) b=viewsL(name) if(b!=[]) a=a+b end return a end |
#tablesL(name = nil) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/citier/sql_adapters.rb', line 51 def tablesL(name = nil) query(" SELECT tablename\n FROM pg_tables\n WHERE schemaname = ANY (current_schemas(false))\n SQL\nend\n", name).map { |row| row[0] } |
#views_existsB?(name) ⇒ Boolean
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/citier/sql_adapters.rb', line 97 def views_existsB?(name) name = name.to_s schema, table = name.split('.', 2) unless table # A table was provided without a schema table = schema schema = nil end if name =~ /^"/ # Handle quoted table names table = name schema = nil end query(" SELECT COUNT(*)\n FROM pg_views\n WHERE viewname = '\#{table.gsub(/(^\"|\"$)/,'')}'\n \#{schema ? \"AND schemaname = '\#{schema}'\" : ''}\n SQL\n\nend\n").first[0].to_i > 0 |
#viewsL(name = nil) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/citier/sql_adapters.rb', line 59 def viewsL(name = nil) query(" SELECT viewname\n FROM pg_views\n WHERE schemaname = ANY (current_schemas(false))\n SQL\nend\n", name).map { |row| row[0] } |