~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to lib/common/db.py

  • Committer: dcoles
  • Date: 2008-08-06 01:32:58 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:981
Groups: Added in support for creating groups in the database through 
userservice (with required capibilities and a new return_insert() database 
function). Implemented create_project_set which requires CAP_MANAGEPROJECTS.

Show diffs side-by-side

added added

removed removed

Lines of Context:
187
187
        if dry: return query
188
188
        self.db.query(query)
189
189
 
 
190
    def return_insert(self, dict, tablename, tablefields, returning,
 
191
        disallowed=frozenset([]), dry=False):
 
192
        """Inserts a new row in a table, using data from a supplied
 
193
        dictionary (which will be checked by check_dict) and returns certain 
 
194
        fields as a dict.
 
195
        dict: Dictionary mapping column names to values. The values may be
 
196
            any of the following types:
 
197
            str, int, long, float, NoneType.
 
198
        tablename: String, name of the table to insert into. Will NOT be
 
199
            escaped - must be a valid identifier.
 
200
        returning: List of fields to return, not escaped
 
201
        tablefields, disallowed: see check_dict.
 
202
        dry: Returns the SQL query as a string, and does not execute it.
 
203
        Raises a DBException if the dictionary contains invalid fields.
 
204
        """
 
205
        if not DB.check_dict(dict, tablefields, disallowed):
 
206
            extras = set(dict.keys()) - tablefields
 
207
            raise DBException("Supplied dictionary contains invalid fields. (%s)" % (repr(extras)))
 
208
        # Build two lists concurrently: field names and values, as SQL strings
 
209
        fieldnames = []
 
210
        values = []
 
211
        for k,v in dict.items():
 
212
            fieldnames.append(k)
 
213
            values.append(_escape(v))
 
214
        if len(fieldnames) == 0: return
 
215
        fieldnames = ', '.join(fieldnames)
 
216
        values = ', '.join(values)
 
217
        returns = ', '.join(returning)
 
218
        query = ("INSERT INTO %s (%s) VALUES (%s) RETURNING (%s);"
 
219
            % (tablename, fieldnames, values, returns))
 
220
        if dry: return query
 
221
        return self.db.query(query)
 
222
 
 
223
 
190
224
    def update(self, primarydict, updatedict, tablename, tablefields,
191
225
        primary_keys, disallowed_update=frozenset([]), dry=False):
192
226
        """Updates a row in a table, matching against primarydict to find the