52 lines
No EOL
1.1 KiB
Ruby
52 lines
No EOL
1.1 KiB
Ruby
|
|
|
|
module Timeseries
|
|
module Hoarder
|
|
class Table
|
|
attr_reader :table_name, :table_schema, :table_id
|
|
|
|
def initialize(db, table_name, table_schema = "public")
|
|
raise ArgumentError, "DB needs to be a Timeseries::Hoarder::Database!" unless db.is_a? Database
|
|
|
|
if (not table_name.is_a? String) or (not table_schema.is_a? String)
|
|
raise ArgumentError, "Table name and schema must be strings!"
|
|
end
|
|
|
|
@table_name = table_name
|
|
@table_schema = table_schema
|
|
|
|
@table_id = "\"#{@table_schema}\".\"#{@table_name}\""
|
|
|
|
@db = db
|
|
@pg = @db.pg
|
|
|
|
@created = false
|
|
|
|
ensure_table_exists
|
|
end
|
|
|
|
def ensure_table_exists
|
|
return if @created
|
|
|
|
@pg.transaction do
|
|
@pg.exec("SELECT pg_advisory_lock(0)")
|
|
|
|
r = @pg.exec_params("SELECT 1 FROM information_schema.tables WHERE table_name = $1 AND table_schema = $2", [@table_name, @table_schema])
|
|
|
|
if r.num_tuples >= 1
|
|
@created = true
|
|
return
|
|
end
|
|
|
|
table_creation
|
|
|
|
@created = true
|
|
end
|
|
end
|
|
|
|
def table_creation
|
|
raise "No table creation string method provided!"
|
|
end
|
|
end
|
|
end
|
|
end |