1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 from buildbot.util import json
17 import sqlalchemy as sa
18 import sqlalchemy.exc
19 from buildbot.db import base
23
26
28
29
31
32 return self._getObjectId((name, class_name)
33 ).addCallback(lambda objdict : objdict['id'])
34
35 @base.cached('objectids')
37 name, class_name = name_class_name_tuple
38 def thd(conn):
39 objects_tbl = self.db.model.objects
40
41 self.check_length(objects_tbl.c.name, name)
42 self.check_length(objects_tbl.c.class_name, class_name)
43
44 def select():
45 q = sa.select([ objects_tbl.c.id ],
46 whereclause=((objects_tbl.c.name == name)
47 & (objects_tbl.c.class_name == class_name)))
48 res = conn.execute(q)
49 row = res.fetchone()
50 res.close()
51 if not row:
52 raise _IdNotFoundError
53 return row.id
54
55 def insert():
56 res = conn.execute(objects_tbl.insert(),
57 name=name,
58 class_name=class_name)
59 return res.inserted_primary_key[0]
60
61
62
63
64 try:
65 return ObjDict(id=select())
66 except _IdNotFoundError:
67 pass
68
69 self._test_timing_hook(conn)
70
71 try:
72 return ObjDict(id=insert())
73 except (sqlalchemy.exc.IntegrityError,
74 sqlalchemy.exc.ProgrammingError):
75 pass
76
77 return ObjDict(id=select())
78
79 return self.db.pool.do(thd)
80
83 def thd(conn):
84 object_state_tbl = self.db.model.object_state
85
86 q = sa.select([ object_state_tbl.c.value_json ],
87 whereclause=((object_state_tbl.c.objectid == objectid)
88 & (object_state_tbl.c.name == name)))
89 res = conn.execute(q)
90 row = res.fetchone()
91 res.close()
92
93 if not row:
94 if default is self.Thunk:
95 raise KeyError("no such state value '%s' for object %d" %
96 (name, objectid))
97 return default
98 try:
99 return json.loads(row.value_json)
100 except:
101 raise TypeError("JSON error loading state value '%s' for %d" %
102 (name, objectid))
103 return self.db.pool.do(thd)
104
105 - def setState(self, objectid, name, value):
106 def thd(conn):
107 object_state_tbl = self.db.model.object_state
108
109 try:
110 value_json = json.dumps(value)
111 except:
112 raise TypeError("Error encoding JSON for %r" % (value,))
113
114 self.check_length(object_state_tbl.c.name, name)
115
116 def update():
117 q = object_state_tbl.update(
118 whereclause=((object_state_tbl.c.objectid == objectid)
119 & (object_state_tbl.c.name == name)))
120 res = conn.execute(q, value_json=value_json)
121
122
123 return res.rowcount > 0
124
125 def insert():
126 conn.execute(object_state_tbl.insert(),
127 objectid=objectid,
128 name=name,
129 value_json=value_json)
130
131
132
133
134
135 if update():
136 return
137
138 self._test_timing_hook(conn)
139
140 try:
141 insert()
142 except (sqlalchemy.exc.IntegrityError, sqlalchemy.exc.ProgrammingError):
143 pass
144
145 return self.db.pool.do(thd)
146
151