Class: PgDiff::Table
- Inherits:
-
Object
- Object
- PgDiff::Table
- Defined in:
- lib/table.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#constraints ⇒ Object
Returns the value of attribute constraints.
-
#indexes ⇒ Object
Returns the value of attribute indexes.
-
#schema ⇒ Object
Returns the value of attribute schema.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
Instance Method Summary collapse
- #attribute_index(name) ⇒ Object
- #has_attribute?(name) ⇒ Boolean
- #has_constraint?(name) ⇒ Boolean
- #has_index?(name) ⇒ Boolean
- #index_creation ⇒ Object
-
#initialize(conn, schema, table_name) ⇒ Table
constructor
A new instance of Table.
- #name ⇒ Object
- #table_creation ⇒ Object
Constructor Details
#initialize(conn, schema, table_name) ⇒ Table
Returns a new instance of Table.
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 |
# File 'lib/table.rb', line 5 def initialize(conn, schema, table_name) @schema = schema @table_name = table_name @attributes = {} @constraints = {} @indexes = {} @atlist = [] att_query = " select attname, format_type(atttypid, atttypmod) as a_type, attnotnull, pg_get_expr(adbin, attrelid) as a_default\n from pg_attribute left join pg_attrdef on (adrelid = attrelid and adnum = attnum)\n where attrelid = '\#{schema}.\#{table_name}'::regclass and not attisdropped and attnum > 0\n order by attnum\n EOT\n conn.query(att_query).each do |tuple|\n attname = tuple['attname']\n typedef = tuple['a_type']\n notnull = tuple['attnotnull']\n default = tuple['a_default']\n @attributes[attname] = Attribute.new(attname, typedef, notnull, default)\n @atlist << attname\n end\n\n ind_query = <<-EOT\n select indexrelid::regclass as indname, pg_get_indexdef(indexrelid) as def\n from pg_index where indrelid = '\#{schema}.\#{table_name}'::regclass and not indisprimary\n EOT\n conn.query(ind_query).each do |tuple|\n name = tuple['indname']\n value = tuple['def']\n @indexes[name] = value\n end\n\n cons_query = <<-EOT\n select conname, pg_get_constraintdef(oid) from pg_constraint where conrelid = '\#{schema}.\#{table_name}'::regclass\n EOT\n conn.query(cons_query).each do |tuple|\n name = tuple['conname']\n value = tuple['pg_get_constraintdef']\n @constraints[name] = value\n end\n @constraints.keys.each do |cname|\n @indexes.delete(\"\#{schema}.\#{cname}\") if has_index?(cname)\n end\nend\n" |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
3 4 5 |
# File 'lib/table.rb', line 3 def attributes @attributes end |
#constraints ⇒ Object
Returns the value of attribute constraints.
3 4 5 |
# File 'lib/table.rb', line 3 def constraints @constraints end |
#indexes ⇒ Object
Returns the value of attribute indexes.
3 4 5 |
# File 'lib/table.rb', line 3 def indexes @indexes end |
#schema ⇒ Object
Returns the value of attribute schema.
3 4 5 |
# File 'lib/table.rb', line 3 def schema @schema end |
#table_name ⇒ Object
Returns the value of attribute table_name.
3 4 5 |
# File 'lib/table.rb', line 3 def table_name @table_name end |
Instance Method Details
#attribute_index(name) ⇒ Object
55 56 57 |
# File 'lib/table.rb', line 55 def attribute_index(name) @atlist.index(name) end |
#has_attribute?(name) ⇒ Boolean
51 52 53 |
# File 'lib/table.rb', line 51 def has_attribute?(name) @attributes.has_key?(name) end |
#has_constraint?(name) ⇒ Boolean
63 64 65 |
# File 'lib/table.rb', line 63 def has_constraint?(name) @constraints.has_key?(name) end |
#has_index?(name) ⇒ Boolean
59 60 61 |
# File 'lib/table.rb', line 59 def has_index?(name) @indexes.has_key?(name) || @indexes.has_key?("#{schema}.#{name}") end |
#index_creation ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/table.rb', line 82 def index_creation out = [] @indexes.values.each do |c| out << (c+";") end out.join("\n") end |
#name ⇒ Object
78 79 80 |
# File 'lib/table.rb', line 78 def name "#{schema}.#{table_name}" end |
#table_creation ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/table.rb', line 67 def table_creation out = ["CREATE TABLE #{name} ("] stmt = [] @atlist.each do |attname| stmt << @attributes[attname].definition end out << stmt.join(",\n") out << ");" out.join("\n") end |