| | 92 | # Return the list of all tables in the schema search path. |
|---|
| | 93 | def tables(name = nil) |
|---|
| | 94 | schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',') |
|---|
| | 95 | query(<<-SQL, name).map { |row| row[0] } |
|---|
| | 96 | SELECT tablename |
|---|
| | 97 | FROM pg_tables |
|---|
| | 98 | WHERE schemaname IN (#{schemas}) |
|---|
| | 99 | SQL |
|---|
| | 100 | end |
|---|
| | 101 | |
|---|
| | 102 | def indexes(table_name, name = nil) |
|---|
| | 103 | result = query(<<-SQL, name) |
|---|
| | 104 | SELECT i.relname, d.indisunique, a.attname |
|---|
| | 105 | FROM pg_class t, pg_class i, pg_index d, pg_attribute a |
|---|
| | 106 | WHERE i.relkind = 'i' |
|---|
| | 107 | AND d.indexrelid = i.oid |
|---|
| | 108 | AND d.indisprimary = 'f' |
|---|
| | 109 | AND t.oid = d.indrelid |
|---|
| | 110 | AND t.relname = '#{table_name}' |
|---|
| | 111 | AND a.attrelid = t.oid |
|---|
| | 112 | AND ( d.indkey[0]=a.attnum OR d.indkey[1]=a.attnum |
|---|
| | 113 | OR d.indkey[2]=a.attnum OR d.indkey[3]=a.attnum |
|---|
| | 114 | OR d.indkey[4]=a.attnum OR d.indkey[5]=a.attnum |
|---|
| | 115 | OR d.indkey[6]=a.attnum OR d.indkey[7]=a.attnum |
|---|
| | 116 | OR d.indkey[8]=a.attnum OR d.indkey[9]=a.attnum ) |
|---|
| | 117 | ORDER BY i.relname |
|---|
| | 118 | SQL |
|---|
| | 119 | |
|---|
| | 120 | current_index = nil |
|---|
| | 121 | indexes = [] |
|---|
| | 122 | |
|---|
| | 123 | result.each do |row| |
|---|
| | 124 | if current_index != row[0] |
|---|
| | 125 | indexes << IndexDefinition.new(table_name, row[0], row[1] == "t", []) |
|---|
| | 126 | current_index = row[0] |
|---|
| | 127 | end |
|---|
| | 128 | |
|---|
| | 129 | indexes.last.columns << row[2] |
|---|
| | 130 | end |
|---|
| | 131 | |
|---|
| | 132 | indexes |
|---|
| | 133 | end |
|---|
| | 134 | |
|---|
| 93 | | column_definitions(table_name).collect do |name, type, default| |
|---|
| 94 | | Column.new(name, default_value(default), translate_field_type(type)) |
|---|
| | 136 | column_definitions(table_name).collect do |name, type, default, notnull| |
|---|
| | 137 | Column.new(name, default_value(default), translate_field_type(type), |
|---|
| | 138 | notnull == "f") |
|---|