Class: PgKingdom::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/pgkingdom/table.rb

Overview

Class, which represents table in database. Contains information about table schema, name, columns (with types and options), constraints, primary and foreign keys. Also provides methods to generate #create(#create!), #drop(#drop!) SQL statements.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &attr_block) ⇒ Postruby::Table

Defines new Table with a given block.

Raises:

  • (SyntaxError)

    when block was not given of block is empty.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pgkingdom/table.rb', line 20

def initialize(name, &attr_block)
  if attr_block.nil?
    raise SyntaxError.new "Table columns should be given within a block."
  end

  if name.is_a? Array
    @schema = name.first
    @name = name.last
  else
    @schema = :public
    @name = name
  end

  @columns = []
  @columns.extend ColumnsHelper

  instance_eval &attr_block

  unless @columns.any?
    raise SyntaxError.new "Given block should not be empty."
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(type, *opts) ⇒ Object

Acts as DSL to define table columns.

Raises:

  • (RuntimeError)

    when column type is not supported or column was redeclared for current table.



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pgkingdom/table.rb', line 45

def method_missing(type, *opts)
  unless @columns.available? type
    raise "Unsupported column datatype #{type} for table #{@name}."
  end

  col_name = opts.shift
  if @columns.defined? col_name
    raise "Column #{col_name} with type #{type} has been redeclared for table #{@name}."
  end

  @columns.add col_name, type, opts
end

Instance Attribute Details

#nameString|Symbol (readonly)

Returns table name.



15
16
17
# File 'lib/pgkingdom/table.rb', line 15

def name
  @name
end

#schemaString|Symbol (readonly)

Returns schema name.



11
12
13
# File 'lib/pgkingdom/table.rb', line 11

def schema
  @schema
end

Instance Method Details

#column_defined?(name) ⇒ Boolean

Helper, checks if column was defined in Table.



95
# File 'lib/pgkingdom/table.rb', line 95

def column_defined?(name) @columns.defined? name end

#column_exists?(name, type = nil) ⇒ String

Generates SQL query to check if column exists in table.



99
100
101
# File 'lib/pgkingdom/table.rb', line 99

def column_exists?(name, type=nil)
  # TODO: write pgSQL query
end

#create(safe = true) ⇒ String

Generates SQL to create table in the database. When safe is true and table already exists it allows to skip this action.



66
67
68
69
70
71
72
73
# File 'lib/pgkingdom/table.rb', line 66

def create(safe=true)
  allow_skip = safe ? " IF NOT EXISTS" : ""
  "  CREATE TABLE\#{allow_skip} \#{table_path} (\n    \#{@columns.join}\n  );\n  SQL\nend\n"

#create!String

Helper, which calls create with safe=false.



87
# File 'lib/pgkingdom/table.rb', line 87

def create!; create(false) end

#drop(safe = true) ⇒ String

Generates SQL to drop table in the database. When safe is true and table doesn't exist it allows to skip this action.



79
80
81
82
83
# File 'lib/pgkingdom/table.rb', line 79

def drop(safe=true)
  "  DROP TABLE\#{safe ? \" IF EXISTS\" : \"\"} \#{table_path};\n  SQL\nend\n"

#drop!String

Helper, which calls drop with safe=false.



91
# File 'lib/pgkingdom/table.rb', line 91

def drop!; drop(false) end

#table_exists?String

Generates SQL to check if table exists in the database.



105
106
107
# File 'lib/pgkingdom/table.rb', line 105

def table_exists?
  "SELECT to_regclass('#{table_path}') IS NOT NULL AS table_exists;"
end

#table_pathString

Helper, which returns table path ..



60
# File 'lib/pgkingdom/table.rb', line 60

def table_path; "#{@schema}.#{@name}" end