Class: Poefy::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/poefy/pg.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.connectionObject

Details for the database connection.



19
20
21
22
23
24
25
# File 'lib/poefy/pg.rb', line 19

def self.connection
  PG.connect(
    :dbname   => 'poefy',
    :user     => 'poefy',
    :password => 'poefy'
  )
end

.desc(table_name) ⇒ Object

Get the description of a table.



55
56
57
58
59
60
61
62
# File 'lib/poefy/pg.rb', line 55

def self.desc table_name
  begin
    sql = "SELECT obj_description($1::regclass, 'pg_class');"
    single_exec!(sql, [table_name]).flatten.first.to_s
  rescue
    ''
  end
end

.listObject

List all tables in the database. Does not include tables used for testing.



43
44
45
46
47
48
49
50
51
52
# File 'lib/poefy/pg.rb', line 43

def self.list
  rs = Database::single_exec! <<-SQL
    SELECT table_name
    FROM information_schema.tables
    WHERE table_schema = 'public';
  SQL
  rs.flatten.reject do |i|
    i.start_with?('spec_')
  end.sort - ['test']
end

.list_with_descObject

List all database files and their descriptions.



65
66
67
68
69
70
71
72
73
# File 'lib/poefy/pg.rb', line 65

def self.list_with_desc
  Database::list.map do |i|
    begin
      [i, Database::desc(i)]
    rescue
      [i, '']
    end
  end.to_h
end

.single_exec!(sql, sql_args = nil) ⇒ Object

Open a class-wide connection, execute a query.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/poefy/pg.rb', line 28

def self.single_exec! sql, sql_args = nil
  output = nil
  begin
    @@con ||= Database::connection
    output = if sql_args
      @@con.exec(sql, [*sql_args]).values
    else
      @@con.exec(sql).values
    end
  end
  output
end

Instance Method Details

#countObject

The number of lines in the table.



93
94
95
96
97
# File 'lib/poefy/pg.rb', line 93

def count
  return 0 if not exists?
  sql = "SELECT COUNT(*) AS num FROM #{table};"
  execute!(sql).first['num'].to_i
end

#descObject

Get/set the description of the table.



84
85
86
# File 'lib/poefy/pg.rb', line 84

def desc
  Database::desc table
end

#desc=(description) ⇒ Object



87
88
89
90
# File 'lib/poefy/pg.rb', line 87

def desc=(description)
  safe_desc = description.to_s.gsub("'","''")
  execute! "COMMENT ON TABLE #{table} IS '#{safe_desc}';"
end

#exists?Boolean

See if the table exists or not. Attempt to access table, and return false on error.

Returns:

  • (Boolean)


101
102
103
104
105
106
107
# File 'lib/poefy/pg.rb', line 101

def exists?
  open_connection
  @db.exec("SELECT $1::regclass", [*table])
  true
rescue PG::UndefinedTable
  false
end

#rhymes(word, key = nil) ⇒ Object

Get all rhyming lines for the word.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/poefy/pg.rb', line 110

def rhymes word, key = nil
  return nil if word.nil?

  sql = <<-SQL
    SELECT rhyme, final_word, syllables, line
    FROM #{table}
    WHERE rhyme = $1
    ORDER BY rhyme, final_word, syllables, line
  SQL
  output = word.to_phrase.rhymes.keys.map do |rhyme|
    execute!(sql, [rhyme]).to_a
  end.flatten

  if !key.nil? and %w[rhyme final_word syllables line].include?(key)
    output.map!{ |i| i[key] }
  end
  output
end

#typeObject

This is the type of database that is being used. It is also used as a signifier that a database has been specified.



79
80
81
# File 'lib/poefy/pg.rb', line 79

def type
  'pg'
end