Skip to content

Commit

Permalink
first stab at fixing postgresql adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
laripk committed Jun 9, 2011
1 parent 2bfd34f commit ba4017a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
5 changes: 3 additions & 2 deletions Gemfile
Expand Up @@ -7,7 +7,8 @@ group :development, :test do
gem 'jeweler', '1.6.2'
gem 'flexmock', '0.9.0', :require => 'flexmock/test_unit'
gem 'test-unit', '2.3.0', :require => 'test/unit'
# gem 'sqlite3-ruby', '1.3.3', :require => 'sqlite3'
gem 'mysql2', '0.2.7'
gem 'pg', '0.11.0'
end

# gem 'sqlite3-ruby', '1.3.3', :require => 'sqlite3'
gem 'mysql2', '0.2.7'
4 changes: 3 additions & 1 deletion Gemfile.lock
Expand Up @@ -46,6 +46,7 @@ GEM
treetop (~> 1.4.8)
mime-types (1.16)
mysql2 (0.2.7)
pg (0.11.0)
polyglot (0.3.1)
rack (1.2.3)
rack-mount (0.6.14)
Expand Down Expand Up @@ -79,6 +80,7 @@ DEPENDENCIES
flexmock (= 0.9.0)
jeweler (= 1.6.2)
mysql2 (= 0.2.7)
rails (= 3.0.7)
pg (= 0.11.0)
rails (~> 3.0.7)
rake (= 0.8.7)
test-unit (= 2.3.0)
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.8.0.1.laripk
0.8.0.2.laripk
30 changes: 30 additions & 0 deletions lib/rails_sql_views/connection_adapters/postgresql_adapter.rb
Expand Up @@ -3,12 +3,42 @@ module ConnectionAdapters
module PostgreSQLAdapter
def self.included(base)
base.alias_method_chain :tables, :views_included unless method_defined?(:tables_with_views_included)
base.alias_method_chain :table_exists?, :views_included unless method_defined?(:table_exists_with_views_included?)
end
# Returns true as this adapter supports views.
def supports_views?
true
end

def table_exists_with_views_included?(name)
puts "\nmy postgres table_exists?", "name #{name}"
name = name.to_s
schema, table = name.split('.', 2)
puts "schema #{schema}", "table #{table}"
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(<<-SQL).first[0].to_i > 0
SELECT COUNT(*)
FROM (
select schemaname, tablename as itemname
from pg_tables
union
select schemaname, viewname
from pg_views
) combo
WHERE itemname = '#{table.gsub(/(^"|"$)/,'')}'
#{schema ? "AND schemaname = '#{schema}'" : ''}
SQL
end

def tables_with_views_included(name = nil)
q = <<-SQL
SELECT table_name, table_type
Expand Down

0 comments on commit ba4017a

Please sign in to comment.