Permalink
Browse files

Canonical Sinatra app: create/drop table, function, procedure, sequence

(Also fix the way data_table is created.)

Change-Id: Iee8e0955b2c7d40251870773414160778ef8e52e
  • Loading branch information...
1 parent 2b809af commit 11068f5bef71f7eddf4fe12f45b7b3fb8fa704f3 figo committed Jan 21, 2012
Showing with 60 additions and 1 deletion.
  1. +60 −1 sinatra/app_sinatra_service/main.rb
@@ -71,6 +71,34 @@
value
end
+put '/service/mysql/table/:table' do
+ client = load_mysql
+ client.query("create table #{params[:table]} (x int);")
+ client.close
+ params[:table]
+end
+
+delete '/service/mysql/:object/:name' do
+ client = load_mysql
+ client.query("drop #{params[:object]} #{params[:name]};")
+ client.close
+ params[:name]
+end
+
+put '/service/mysql/function/:function' do
+ client = load_mysql
+ client.query("create function #{params[:function]}() returns int return 1234;");
+ client.close
+ params[:function]
+end
+
+put '/service/mysql/procedure/:procedure' do
+ client = load_mysql
+ client.query("create procedure #{params[:procedure]}() begin end;");
+ client.close
+ params[:procedure]
+end
+
post '/service/postgresql/:key' do
client = load_postgresql
value = request.env["rack.input"].read
@@ -86,6 +114,37 @@
value
end
+put '/service/postgresql/table/:table' do
+ client = load_postgresql
+ client.query("create table #{params[:table]} (x int);")
+ client.close
+ params[:table]
+end
+
+delete '/service/postgresql/:object/:name' do
+ client = load_postgresql
+ object = params[:object]
+ name = params[:name]
+ name += "()" if object=="function" # PG 'drop function' docs: "The argument types to the function must be specified"
+ client.query("drop #{object} #{name};")
+ client.close
+ name
+end
+
+put '/service/postgresql/function/:function' do
+ client = load_postgresql
+ client.query("create function #{params[:function]}() returns integer as 'select 1234;' language sql;")
+ client.close
+ params[:function]
+end
+
+put '/service/postgresql/sequence/:sequence' do
+ client = load_postgresql
+ client.query("create sequence #{params[:sequence]};")
+ client.close
+ params[:sequence]
+end
+
post '/service/rabbit/:key' do
value = request.env["rack.input"].read
client = rabbit_service
@@ -133,7 +192,7 @@ def load_mongo
def load_postgresql
postgresql_service = load_service('postgresql')
client = PGconn.open(postgresql_service['host'], postgresql_service['port'], :dbname => postgresql_service['name'], :user => postgresql_service['username'], :password => postgresql_service['password'])
- client.query("create table data_values (id varchar(20), data_value varchar(20));") if client.query("select * from information_schema.tables where table_name = 'data_values';").first.nil?
+ client.query("create table data_values (id varchar(20), data_value varchar(20));") if client.query("select * from pg_catalog.pg_class where relname = 'data_values';").num_tuples() < 1
client
end

0 comments on commit 11068f5

Please sign in to comment.