590a0020040349ebef7d48743482ddf91b5a2a9c
[sfa.git] / util / db.py
1 import os
2 from pg import DB
3 from excep import *
4 from tree import *
5 from util import *
6
7 #planetlab authentication structure
8 pl_auth = {'Username': 'ssevinc@princeton.edu',        # User account
9 'AuthMethod': 'password',        # Type of auth this is. Can be password, session ...
10 'AuthString':  'Ss3928Ee' # the password for this account
11
12
13 def get_plDB_conn():
14     dbname = 'plDB'
15     address = 'localhost'
16     port = 5433
17     user = 'postgres'
18     password = '111'
19     cnx = DB(dbname, address, port=port, user=user, passwd=password)
20     return cnx
21     
22 #copy the pl db info to requester
23 def get_plDB_info(dst):
24     dst.db_name = 'plDB'
25     dst.address = 'localhost'
26     dst.port = 5433
27     dst.user = 'postgres'
28     dst.password = '111'
29
30
31 #determines the database info of a given hrn
32 #if the hrn does not exist in the tree hierarchy None is returned
33 def determine_dbinfo(hrn, tree):
34     info = tree.tree_lookup(hrn)
35     if info == None:
36         return None
37     else:
38         db_info = info.node_data['db_info']
39         cnx = DB(db_info.db_name, db_info.address, port = db_info.port, user = db_info.user, passwd = db_info.password)
40         tablename = db_info.table_name
41         return [cnx, tablename]
42
43 #convert the parameter list to query string suitable for supplying to database queries
44 #input: query type, table name and field-value pairs
45 def generate_querystr(type, table, dict):
46     querystr = ""
47     if type == 'INSERT':
48         keys = dict.keys()
49         str1 = keys[0]
50         for i in range(1, len(keys)):
51             str1 = str1 + ','+ keys[i]
52         str2 = ""
53         for i in range(len(keys)-1):
54             if isinstance(dict[keys[i]],str):
55                 str2 = str2 + "'" + dict[keys[i]] + "', " 
56             else:
57                 str2 = str2 + str(dict[keys[i]]) + ", "
58         if isinstance(dict[keys[len(keys)-1]],str):
59             str2 = str2 + "'" + dict[keys[len(keys)-1]] + "'" 
60         else:
61             str2 = str2 + str(dict[keys[len(keys)-1]]) 
62         querystr = "INSERT INTO "+table+ "(" + str1 + ") VALUES(" + str2 + ")"
63     elif type == 'UPDATE':
64         str1 = ""
65         keys = dict.keys()
66         for i in range(len(keys)-1):
67             if keys[i] != 'hrn':
68                 if isinstance(dict[keys[i]],str):
69                     str1 = str1 + keys[i] + " = '" + dict[keys[i]] + "', " 
70                 else:
71                     str1 = str1 + keys[i] + " = " + dict[keys[i]] + ", " 
72         if keys[len(keys)-1] != 'hrn':
73             if isinstance(dict[keys[len(keys)-1]],str):
74                 str1 = str1 + keys[len(keys)-1] + " = '" + dict[keys[len(keys)-1]] + "'" 
75             else:
76                 str1 = str1 + keys[len(keys)-1] + " = '" + dict[keys[len(keys)-1]]
77         querystr = "UPDATE "+table+ " SET " + str1 + " WHERE hrn = '"+get_leaf(dict["hrn"])+"'"
78     elif type == 'DELETE':
79         querystr = "DELETE FROM "+table+" WHERE hrn = '"+get_leaf(dict["hrn"])+"'"
80     return querystr
81