Class: Peegee::Table

Inherits:
Object
  • Object
show all
Includes:
Clustering
Defined in:
lib/peegee/table.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Clustering

#cluster

Constructor Details

#initialize(opts = {}) ⇒ Table

Creates a new instance of Peegee::Table. Receives an opts hash paramater, which expects to contain a key called :table_name. A TableDoesNotExistError is thrown if the table with the supplied name is not found in the database.



14
15
16
17
18
19
20
# File 'lib/peegee/table.rb', line 14

def initialize(opts = {})
  if Peegee::Table.exists?(opts[:table_name])
    @table_name = opts[:table_name]
  else
    raise TableDoesNotExistError, "Table #{opts[:table_name]} does not exist", caller
  end
end

Instance Attribute Details

#table_nameObject

Returns the value of attribute table_name.



7
8
9
# File 'lib/peegee/table.rb', line 7

def table_name
  @table_name
end

Class Method Details

.exists?(table_name) ⇒ Boolean

Class method that finds out if a given table name exists in the database.

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/peegee/table.rb', line 23

def self.exists?(table_name)
  sql = "select * from pg_class where relname = '#{table_name}' and relkind = 'r'"
  !(ActiveRecord::Base.connection.execute(sql).entries.flatten.size == 0)
end

Instance Method Details

#ddlObject

Returns the DDL for this table as a string.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/peegee/table.rb', line 94

def ddl
  sql = 'SELECT a.attname, ' +
      'pg_catalog.format_type(a.atttypid, a.atttypmod), ' +
      '(SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128) ' +
      ' FROM pg_catalog.pg_attrdef d ' +
      ' WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef), ' +
      'a.attnotnull, a.attnum ' +
    'FROM pg_catalog.pg_attribute a ' +
    "WHERE a.attrelid = '#{oid}' AND a.attnum > 0 AND NOT a.attisdropped " +
    'ORDER BY a.attnum ' 

  column_defs = ActiveRecord::Base.connection.execute(sql).entries

  ddl = "create table #{@table_name} ( "
  columns = []
  column_defs.each do |column_def|
    #name and type
    column = column_def[0] + ' ' + column_def[1] + ' '
    #not null?
    column += 'NOT NULL ' if column_def[3] == 't'
    #add modifiers:
    column += 'DEFAULT ' + column_def[2] unless (column_def[2].nil? || column_def[2].empty?)
    columns << column
  end

  ddl += columns.join(', ') + ' )'
  ddl.gsub('\\','')
end

#dependent_foreign_keysObject

Returns an array of all dependent foreign keys for this table, as Peegee::ForeignKey objects. The first time this method is called, dependent foreign keys are retrieved from the database and cached in an instance variable. Subsequence calls will use the cached values. To force a lookup of dependent foreign keys use the dependent_foreign_keys! method instead.



49
50
51
# File 'lib/peegee/table.rb', line 49

def dependent_foreign_keys
  @dependent_foreign_keys ||= fetch_dependent_foreign_keys
end

#dependent_foreign_keys!Object



53
54
55
# File 'lib/peegee/table.rb', line 53

def dependent_foreign_keys!
  fetch_dependent_foreign_keys
end

#foreign_keysObject

Returns an array of all foreign keys for this table, as Peegee::ForeignKey objects. The first time this method is called, foreign keys are retrieved from the database and cached in an instance variable. Subsequence calls will use the cached values. To force a lookup of foreign keys use the foreign_keys! method instead.



37
38
39
# File 'lib/peegee/table.rb', line 37

def foreign_keys
  @foreign_keys ||= fetch_foreign_keys
end

#foreign_keys!Object



41
42
43
# File 'lib/peegee/table.rb', line 41

def foreign_keys!
  fetch_foreign_keys
end

#indexesObject

Returns an array of indexes for this table, as Peegee::indexes objects. The first time this method is called, unique constraints are retrieved from the database and cached in an instance variable. Subsequence calls will use the cached values. To force a lookup of unique constraints use the indexes! method instead.



85
86
87
# File 'lib/peegee/table.rb', line 85

def indexes
  @indexes ||= fetch_indexes
end

#indexes!Object



89
90
91
# File 'lib/peegee/table.rb', line 89

def indexes!
  fetch_indexes
end

#oidObject

The postgresql OID for this table.



29
30
31
# File 'lib/peegee/table.rb', line 29

def oid
  @oid ||= fetch_oid
end

#primary_keyObject

Returns an array of primary keys for this table, as Peegee::PrimaryKey objects. The first time this method is called, primary keys are retrieved from the database and cached in an instance variable. Subsequence calls will use the cached values. To force a lookup of primary keys use the primary_key! method instead.



61
62
63
# File 'lib/peegee/table.rb', line 61

def primary_key
  @primary_key ||= fetch_primary_key
end

#primary_key!Object



65
66
67
# File 'lib/peegee/table.rb', line 65

def primary_key!
  fetch_primary_key
end

#unique_constraintsObject

Returns an array of unique constraints for this table, as Peegee::UniqueConstraint objects. The first time this method is called, unique constraints are retrieved from the database and cached in an instance variable. Subsequence calls will use the cached values. To force a lookup of unique constraints use the unique_constraints! method instead.



73
74
75
# File 'lib/peegee/table.rb', line 73

def unique_constraints
  @unique_constraints ||= fetch_unique_constraints
end

#unique_constraints!Object



77
78
79
# File 'lib/peegee/table.rb', line 77

def unique_constraints!
  fetch_unique_constraints
end