class Natural::Table
Constants
- TYPE_IDENTIFIER_MAPPING
Attributes
database[RW]
identifier[R]
Public Class Methods
new(identifier)
click to toggle source
# File natural-backend/lib/database_manager/lib/table.rb, line 14 def initialize(identifier) @identifier = identifier end
Public Instance Methods
add_column(name)
click to toggle source
# File natural-backend/lib/database_manager/lib/table.rb, line 47 def add_column(name) connection.exec( """ ALTER TABLE \"#{@identifier}\" ADD COLUMN \"#{name}\" varchar(255); """ ) end
create()
click to toggle source
# File natural-backend/lib/database_manager/lib/table.rb, line 18 def create connection.exec( """ CREATE TABLE \"#{@identifier}\" ( id SERIAL ); """ ) self end
delete_row(id)
click to toggle source
# File natural-backend/lib/database_manager/lib/table.rb, line 120 def delete_row(id) connection.exec( """ DELETE FROM \"#{@identifier}\" WHERE id=#{id}; """ ) end
delete_value(column_name, id)
click to toggle source
# File natural-backend/lib/database_manager/lib/table.rb, line 100 def delete_value(column_name, id) connection.exec( """ UPDATE \"#{@identifier}\" SET \"#{column_name}\" = NULL WHERE id=#{id}; """ ) end
destroy()
click to toggle source
# File natural-backend/lib/database_manager/lib/table.rb, line 29 def destroy connection.exec( """ DROP TABLE \"#{@identifier}\"; """ ) end
destroy_column(name)
click to toggle source
# File natural-backend/lib/database_manager/lib/table.rb, line 67 def destroy_column(name) connection.exec( """ ALTER TABLE \"#{@identifier}\" DROP COLUMN \"#{name}\" RESTRICT; """ ) end
exists?()
click to toggle source
# File natural-backend/lib/database_manager/lib/table.rb, line 37 def exists? '1' == connection.exec( """ SELECT 1 FROM pg_tables WHERE tablename = '#{@identifier}' """ ).values[0].try(:[], 0) end
has_row?(id)
click to toggle source
# File natural-backend/lib/database_manager/lib/table.rb, line 110 def has_row?(id) connection.exec( """ SELECT 1 FROM \"#{@identifier}\" WHERE id=#{id}; """ ).values[0][0] == '1' end
insert_value(column_name, value)
click to toggle source
# File natural-backend/lib/database_manager/lib/table.rb, line 76 def insert_value(column_name, value) connection.exec( """ INSERT INTO \"#{@identifier}\" (\"#{column_name}\") VALUES ('#{single_quote_escape(value)}') RETURNING ID; """ ).values[0][0] end
single_quote_escape(string)
click to toggle source
# File natural-backend/lib/database_manager/lib/table.rb, line 96 def single_quote_escape(string) string.gsub("'", %q('')) end
update_column_type(name, type_identifier)
click to toggle source
# File natural-backend/lib/database_manager/lib/table.rb, line 56 def update_column_type(name, type_identifier) type = TYPE_IDENTIFIER_MAPPING[type_identifier] || type_identifier connection.exec( """ ALTER TABLE \"#{@identifier}\" ALTER COLUMN \"#{name}\" TYPE #{type} USING (trim(\"#{name}\" )::#{type}); """ ) end
update_value(column_name, id, value)
click to toggle source
# File natural-backend/lib/database_manager/lib/table.rb, line 86 def update_value(column_name, id, value) connection.exec( """ UPDATE \"#{@identifier}\" SET \"#{column_name}\" = \'#{single_quote_escape(value)}\' WHERE id=#{id}; """ ) end