res=smart_query('SELECT datname FROM pg_database')
ifresandnotres.empty?
res.eachdo|row|
defaults=['template1','template0','postgres']
nextifdefaults.include?row[0]
tmp_db={}
tmp_db['DBName']=row[0]
tmp_db['Tables']=[]
postgres_login({:database=>row[0]})
tmp_tblnames=smart_query("SELECT c.relname, n.nspname FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname NOT IN ('pg_catalog','pg_toast') AND pg_catalog.pg_table_is_visible(c.oid);")
iftmp_tblnamesandnottmp_tblnames.empty?
tmp_tblnames.eachdo|tbl_row|
tmp_tbl={}
tmp_tbl['TableName']=tbl_row[0]
tmp_tbl['Columns']=[]
tmp_column_names=smart_query("SELECT A.attname, T.typname, A.attlen FROM pg_class C, pg_namespace N, pg_attribute A, pg_type T WHERE (N.oid=C.relnamespace) AND (A.attrelid=C.oid) AND (A.atttypid=T.oid) AND (A.attnum>0) AND (NOT A.attisdropped) AND (N.nspname ILIKE 'public') AND (c.relname='#{tbl_row[0]}');")
iftmp_column_namesandnottmp_column_names.empty?
tmp_column_names.eachdo|column_row|
tmp_column={}
tmp_column['ColumnName']=column_row[0]
tmp_column['ColumnType']=column_row[1]
tmp_column['ColumnLength']=column_row[2]
tmp_tbl['Columns']<<tmp_column
end
end
tmp_db['Tables']<<tmp_tbl
end
end
pg_schema<<tmp_db
end
end
returnpg_schema
end
defsmart_query(query_string)
res=postgres_query(query_string,false)
#Error handling routine here, borrowed heavily from todb