@@ -63,17 +63,6 @@ def __init__(self, *args, **kw):
63
63
# that takes a single string arg. For example
64
64
# in a CGI set to "%s<BR>"
65
65
66
- # Get all the primary keys at once
67
- for rel , att in self .db .query ("""SELECT
68
- pg_class.relname, pg_attribute.attname
69
- FROM pg_class, pg_attribute, pg_index
70
- WHERE pg_class.oid = pg_attribute.attrelid AND
71
- pg_class.oid = pg_index.indrelid AND
72
- pg_index.indkey[0] = pg_attribute.attnum AND
73
- pg_index.indisprimary = 't' AND
74
- pg_attribute.attisdropped = 'f'""" ).getresult ():
75
- self .__pkeys__ [rel ] = att
76
-
77
66
def _do_debug (self , s ):
78
67
if not self .debug : return
79
68
if type (self .debug ) == StringType : print self .debug % s
@@ -85,10 +74,30 @@ def query(self, qstr):
85
74
self ._do_debug (qstr )
86
75
return self .db .query (qstr )
87
76
88
- # If third arg supplied set primary key to it
89
77
def pkey (self , cl , newpkey = None ):
78
+ """This method returns the primary key of a class. If newpkey
79
+ is set and is set and is not a dictionary then set that
80
+ value as the primary key of the class. If it is a dictionary
81
+ then replace the __pkeys__ dictionary with it."""
82
+ # Get all the primary keys at once
83
+ if type (newpkey ) == DictType :
84
+ self .__pkeys__ = newpkey
85
+ return
86
+
90
87
if newpkey :
91
88
self .__pkeys__ [cl ] = newpkey
89
+ return newpkey
90
+
91
+ if self .__pkeys__ == {}:
92
+ for rel , att in self .db .query ("""SELECT
93
+ pg_class.relname, pg_attribute.attname
94
+ FROM pg_class, pg_attribute, pg_index
95
+ WHERE pg_class.oid = pg_attribute.attrelid AND
96
+ pg_class.oid = pg_index.indrelid AND
97
+ pg_index.indkey[0] = pg_attribute.attnum AND
98
+ pg_index.indisprimary = 't' AND
99
+ pg_attribute.attisdropped = 'f'""" ).getresult ():
100
+ self .__pkeys__ [rel ] = att
92
101
93
102
# will raise an exception if primary key doesn't exist
94
103
return self .__pkeys__ [cl ]
@@ -108,7 +117,17 @@ def get_tables(self):
108
117
l .append (n [0 ])
109
118
return l
110
119
111
- def get_attnames (self , cl ):
120
+ def get_attnames (self , cl , newattnames = None ):
121
+ """This method gets a list of attribute names for a class. If
122
+ the optional newattnames exists it must be a dictionary and
123
+ will become the new attribute names dictionary."""
124
+
125
+ if type (newattnames ) == DictType :
126
+ self .__attnames__ = newattnames
127
+ return
128
+ elif newattnames :
129
+ raise error , "If supplied, newattnames must be a dictionary"
130
+
112
131
# May as well cache them
113
132
if self .__attnames__ .has_key (cl ):
114
133
return self .__attnames__ [cl ]
@@ -160,7 +179,7 @@ def get(self, cl, arg, keyname = None, view = 0):
160
179
xcl = cl
161
180
162
181
if keyname == None : # use the primary key by default
163
- keyname = self .__pkeys__ [ xcl ]
182
+ keyname = self .pkey ( xcl )
164
183
165
184
fnames = self .get_attnames (xcl )
166
185
@@ -225,6 +244,8 @@ def insert(self, cl, a):
225
244
# Update always works on the oid which get returns if available
226
245
# otherwise use the primary key. Fail if neither.
227
246
def update (self , cl , a ):
247
+ self .pkey (cl ) # make sure we have a self.__pkeys__ dictionary
248
+
228
249
foid = 'oid_%s' % cl
229
250
if a .has_key (foid ):
230
251
where = "oid = %s" % a [foid ]
0 commit comments