import repository from arizona
[raven.git] / apps / gacks / gackscalendar_db.py
1 from gackscalendar import *
2
3 from pg import DB, ProgrammingError
4
5 """ database notes:
6       * uses postgresql
7       * RH postgresql authentication is broken
8             edit /var/lib/pgsql/data/pg_hba.conf and change the methods
9             supposed to change them to md5, but that didn't work
10             chaned them to "trust" instead
11       * create the gacks user
12             createuser -a -d -P -E -U postgres -W gacks
13 """
14
15 def estr(val):
16     val = str(val)
17     return "'" + val + "'"
18
19 class GacksDBCalendar(GacksCalendar):
20     def __init__(self):
21         GacksCalendar.__init__(self)
22         self.tablename = "gacks"
23         self.dbname = "gacks"
24         self.address = "localhost"
25         self.port = 5432
26         self.user = "gacks"
27         self.passwordfile = "/root/mysql-gacks-pass.txt"
28         self.password = "gackspassword" #open(self.passwordfile,"r").readline().strip()
29         self.open()
30
31     def reset(self):
32         self.create()
33
34     def open(self):
35         self.cnx = DB(self.dbname, self.address, port=self.port, user=self.user, passwd=self.password)
36
37     def exists(self):
38         tableList = self.cnx.get_tables()\r
39         if 'public.' + self.tablename in tableList:\r
40             return True\r
41         if 'public."' + self.tablename + '"' in tableList:\r
42             return True\r
43         return False\r
44 \r
45     def create(self):
46         querystr = "CREATE TABLE " + self.tablename + " ( \
47                         key text, \
48                         id text, \
49                         unitStart text, \
50                         unitStop text, \
51                         timeStart text, \
52                         timeStop text, \
53                         allocatorHRNs text, \
54                         consumerHRN text);"
55
56         # IF EXISTS doesn't exist in postgres < 8.2
57         try:\r
58             self.cnx.query('DROP TABLE IF EXISTS ' + self.tablename)\r
59         except ProgrammingError:\r
60             try:\r
61                 self.cnx.query('DROP TABLE ' + self.tablename)\r
62             except ProgrammingError:\r
63                 pass\r
64 \r
65         self.cnx.query(querystr)\r
66 \r
67 #        template = "CREATE INDEX %s_%s_idx ON %s (%s);"\r
68 #        indexes = [template % ( self.tablename, field, self.tablename, field) \\r
69 #                   for field in ['name', 'type' ]]\r
70 #        for index in indexes:\r
71 #            self.cnx.query(index)
72
73     def garbage_collect(self, timeStop=None):
74         raise "not implemented yet"
75
76     def find_record(self, item):
77         querystr = "SELECT * FROM " + self.tablename + " WHERE key = '" + item.get_cell_key() + "'"
78         list = self.cnx.query(querystr).dictresult()
79
80         if not list:
81             return None
82
83         if len(list) > 1:
84             raise GacksMultipleRecordCollision(item_to_delete.as_string())
85
86         return list[0]
87
88     def remove_record(self, item):
89         querystr = "DELETE FROM " + self.tablename + " WHERE key = '" + item.get_cell_key() + "'"
90         self.cnx.query(querystr)
91
92     def insert_record(self, item):
93         conflicts = self.query_overlap(item.id, item.unitStart, item.unitStop, item.timeStart, item.timeStop)
94         if conflicts:
95             raise GacksConflictingInsert(item.as_string())
96
97         querystr = "INSERT INTO " + self.tablename + \
98                        "(key, id, unitStart, unitStop, timeStart, timeStop, allocatorHRNs, consumerHRN) VALUES(" + \
99                        estr(item.get_cell_key()) + "," + \
100                        estr(item.id) + "," + \
101                        estr(item.unitStart) + "," + \
102                        estr(item.unitStop) + "," + \
103                        estr(item.timeStart) + "," + \
104                        estr(item.timeStop) + "," + \
105                        estr(",".join(item.allocatorHRNs)) + "," + \
106                        estr(item.consumerHRN) + ")"
107
108         self.cnx.query(querystr)
109
110     def is_empty(self):
111         if self.list_all_dict() == []:
112             return True
113         else:
114             return False
115
116     def list_all_dict(self):
117         querystr = "SELECT * FROM " + self.tablename
118         dict_list = self.cnx.query(querystr).dictresult()
119         return dict_list
120
121     def list_all(self):
122         list = []
123         dict_list = self.list_all_dict()
124         for dict in dict_list:
125             dict2={}
126             dict2["allocatorHRNs"] = dict["allocatorhrns"].split(",")
127             dict2["unitStart"] = dict["unitstart"]
128             dict2["unitStop"] = dict["unitstop"]
129             dict2["timeStart"] = dict["timestart"]
130             dict2["timeStop"] = dict["timestop"]
131             dict2["consumerHRN"] = dict["consumerhrn"]
132             dict2["id"] = dict["id"]
133             item = GacksRecord(dict = dict2)
134             list.append(item)
135         return list
136
137     def query_overlap(self, id=None, unitStart=0, unitStop=INFINITY, timeStart=0, timeStop=INFINITY, hasAllocator=None, isLastAllocator=None):
138         all = self.list_all()
139         list = []
140         for item in all:
141             if self.test_id(id, item.id) and \
142                self.test_lesser(unitStart, item.unitStop) and \
143                self.test_greater(unitStop, item.unitStart) and \
144                self.test_lesser(timeStart, item.timeStop) and \
145                self.test_greater(timeStop, item.timeStart) and \
146                self.test_allocator(hasAllocator, item.allocatorHRNs) and \
147                self.test_last_allocator(isLastAllocator, item.allocatorHRNs):
148                  list.append(item)
149         return list
150
151     def query_exact(self, id=None, unitStart=None, unitStop=None, timeStart=None, timeStop=None, hasAllocator=None, isLastAllocator=None):
152         all = self.list_all()
153         list = []
154         for item in all:
155             if self.test_id(id, item.id) and \
156                self.test_equal(unitStart, item.unitStart) and \
157                self.test_equal(unitStop, item.unitStop) and \
158                self.test_equal(timeStart, item.timeStart) and \
159                self.test_equal(timeStop, item.timeStop) and \
160                self.test_allocator(hasAllocator, item.allocatorHRNs) and \
161                self.test_last_allocator(isLastAllocator, item.allocatorHRNs):
162                  list.append(item)
163         return list
164
165