find operation on tables
[sfa.git] / util / genitable.py
1 # genitable.py
2 #
3 # implements support for geni records stored in db tables
4 #
5 # TODO: Use existing PLC database methods? or keep this separate?
6
7 import report
8
9 from pg import DB
10 from gid import *
11 from record import *
12
13 GENI_TABLE_PREFIX = "geni$"
14
15 class GeniTable():
16     def __init__(self, create=False, hrn="unspecified.default.registry", cninfo=None):
17         global GENI_TABLE_PREFIX
18
19         self.hrn = hrn
20
21         # pgsql doesn't like table names with "." in them, to replace it with "$"
22         self.tablename = GENI_TABLE_PREFIX + self.hrn.replace(".", "$")
23
24         # establish a connection to the pgsql server
25         self.cnx = DB(cninfo['dbname'], cninfo['address'], port=cninfo['port'], user=cninfo['user'], passwd=cninfo['password'])
26
27         # if asked to create the table, then create it
28         if create:
29             self.create()
30
31     def exists(self):
32         tableList = self.cnx.get_tables()
33         if 'public.' + self.tablename in tableList:
34             return True
35         if 'public."' + self.tablename + '"' in tableList:
36             return True
37         return False
38
39     def create(self):
40         querystr = "CREATE TABLE " + self.tablename + " ( \
41                 key text, \
42                 name text, \
43                 gid text, \
44                 type text, \
45                 pointer integer);"
46
47         self.cnx.query('DROP TABLE IF EXISTS ' + self.tablename)
48         self.cnx.query(querystr)
49
50     def remove(self, record):
51         query_str = "DELETE FROM " + self.tablename + " WHERE key = '" + record.get_key() + "'"
52         self.cnx.query(query_str)
53
54     def insert(self, record):
55         fieldnames = ["key"] + record.get_field_names()
56         fieldvals = record.get_field_value_strings(fieldnames)
57         query_str = "INSERT INTO " + self.tablename + \
58                        "(" + ",".join(fieldnames) + ") " + \
59                        "VALUES(" + ",".join(fieldvals) + ")"
60         #print query_str
61         self.cnx.query(query_str)
62
63     def update(self, record):
64         names = record.get_field_names()
65         pairs = []
66         for name in names:
67            val = record.get_field_value_string(name)
68            pairs.append(name + " = " + val)
69         update = ", ".join(pairs)
70
71         query_str = "UPDATE " + self.tablename+ " SET " + update + " WHERE key = '" + record.get_key() + "'"
72         #print query_str
73         self.cnx.query(query_str)
74
75     def find_dict(self, type, value, searchfield):
76         query_str = "SELECT * FROM " + self.tablename + " WHERE " + searchfield + " = '" + str(value) + "'"
77         dict_list = self.cnx.query(query_str).dictresult()
78         result_dict_list = []
79         for dict in dict_list:
80            if (type=="*") or (dict['type'] == type):
81                result_dict_list.append(dict)
82         return result_dict_list
83
84     def find(self, type, value, searchfield):
85         result_dict_list = self.find_dict(type, value, searchfield)
86         result_rec_list = []
87         for dict in result_dict_list:
88             result_rec_list.append(GeniRecord(dict=dict))
89         return result_rec_list
90
91     def resolve_dict(self, type, hrn):
92         return self.find_dict(type, hrn, "name")
93
94     def resolve(self, type, hrn):
95         return self.find(type, hrn, "name")
96
97     def list_dict(self):
98         query_str = "SELECT * FROM " + self.tablename
99         result_dict_list = self.cnx.query(query_str).dictresult()
100         return result_dict_list
101
102     def list(self):
103         result_dict_list = self.list_dict()
104         result_rec_list = []
105         for dict in result_dict_list:
106             result_rec_list.append(GeniRecord(dict=dict))
107         return result_rec_list
108
109 def set_geni_table_prefix(x):
110     global GENI_TABLE_PREFIX
111
112     GENI_TABLE_PREFIX = x
113
114 def geni_records_purge(cninfo):
115     global GENI_TABLE_PREFIX
116
117     cnx = DB(cninfo['dbname'], cninfo['address'], port=cninfo['port'], user=cninfo['user'], passwd=cninfo['password'])
118     tableList = cnx.get_tables()
119     for table in tableList:
120         if table.startswith(GENI_TABLE_PREFIX) or \
121            table.startswith('public.' + GENI_TABLE_PREFIX) or \
122            table.startswith('public."' + GENI_TABLE_PREFIX):
123                report.trace("dropping table " + table)
124                cnx.query("DROP TABLE " + table)