@@ -63,17 +63,6 @@ def __init__(self, *args, **kw):
6363 # that takes a single string arg. For example
6464 # in a CGI set to "%s<BR>"
6565
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-
7766 def _do_debug (self , s ):
7867 if not self .debug : return
7968 if type (self .debug ) == StringType : print self .debug % s
@@ -85,10 +74,30 @@ def query(self, qstr):
8574 self ._do_debug (qstr )
8675 return self .db .query (qstr )
8776
88- # If third arg supplied set primary key to it
8977 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+
9087 if newpkey :
9188 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
92101
93102 # will raise an exception if primary key doesn't exist
94103 return self .__pkeys__ [cl ]
@@ -108,7 +117,17 @@ def get_tables(self):
108117 l .append (n [0 ])
109118 return l
110119
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+
112131 # May as well cache them
113132 if self .__attnames__ .has_key (cl ):
114133 return self .__attnames__ [cl ]
@@ -160,7 +179,7 @@ def get(self, cl, arg, keyname = None, view = 0):
160179 xcl = cl
161180
162181 if keyname == None : # use the primary key by default
163- keyname = self .__pkeys__ [ xcl ]
182+ keyname = self .pkey ( xcl )
164183
165184 fnames = self .get_attnames (xcl )
166185
@@ -225,6 +244,8 @@ def insert(self, cl, a):
225244 # Update always works on the oid which get returns if available
226245 # otherwise use the primary key. Fail if neither.
227246 def update (self , cl , a ):
247+ self .pkey (cl ) # make sure we have a self.__pkeys__ dictionary
248+
228249 foid = 'oid_%s' % cl
229250 if a .has_key (foid ):
230251 where = "oid = %s" % a [foid ]
0 commit comments