Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita4bc5ee

Browse files
committed
Change the pkey method so that the caller can optionally set the dictionary
used for the primary key lookup. This will prevent a database lookupfor each connection object that gets created. This could be a significantoptimization on a busy system.Similarly, the get_attnames method allows for the attributes dictionaryto be installed directly.
1 parentf393ee0 commita4bc5ee

File tree

1 file changed

+35
-14
lines changed
  • src/interfaces/python

1 file changed

+35
-14
lines changed

‎src/interfaces/python/pg.py

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
forrel,attinself.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
ifnotself.debug:return
7968
iftype(self.debug)==StringType:printself.debug%s
@@ -85,10 +74,30 @@ def query(self, qstr):
8574
self._do_debug(qstr)
8675
returnself.db.query(qstr)
8776

88-
# If third arg supplied set primary key to it
8977
defpkey(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+
iftype(newpkey)==DictType:
84+
self.__pkeys__=newpkey
85+
return
86+
9087
ifnewpkey:
9188
self.__pkeys__[cl]=newpkey
89+
returnnewpkey
90+
91+
ifself.__pkeys__== {}:
92+
forrel,attinself.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
returnself.__pkeys__[cl]
@@ -108,7 +117,17 @@ def get_tables(self):
108117
l.append(n[0])
109118
returnl
110119

111-
defget_attnames(self,cl):
120+
defget_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+
iftype(newattnames)==DictType:
126+
self.__attnames__=newattnames
127+
return
128+
elifnewattnames:
129+
raiseerror,"If supplied, newattnames must be a dictionary"
130+
112131
# May as well cache them
113132
ifself.__attnames__.has_key(cl):
114133
returnself.__attnames__[cl]
@@ -160,7 +179,7 @@ def get(self, cl, arg, keyname = None, view = 0):
160179
xcl=cl
161180

162181
ifkeyname==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
defupdate(self,cl,a):
247+
self.pkey(cl)# make sure we have a self.__pkeys__ dictionary
248+
228249
foid='oid_%s'%cl
229250
ifa.has_key(foid):
230251
where="oid = %s"%a[foid]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp