コード例 #1
0
ファイル: store.py プロジェクト: madelinebarch/flock
 def get_data(self, rtype, hid):
     self.dbc.execute("SELECT data FROM _records WHERE rtype = ? AND hid = ?",
         (rtype, buffer(hid)))
     row = self.dbc.fetchone()
     if row is None:
         return None, None
     return record.get_record_content(rtype, str(row[0]))
コード例 #2
0
ファイル: schema.py プロジェクト: madelinebarch/flock
    def insert_record(self, cur, hid, summary, data, score):
        try:
            (ctype, body) = record.get_record_content(record.RT_WORKTOKEN, data)
            if ctype != 'application/json':
                raise ValueError('Not a json')
            # Decode it
            obj = json.loads(body)
            # TODO: Why is this pylint disable needed?
            # pylint: disable=no-member

            # Grab the table special variable and make sure it's in schema
            table_name = obj['_table']
            table = self.schema[table_name]
            del obj['_table']

            # Overwrite 'special' fields
            obj['id'] = str(hid).encode('base64')[:-1]
            obj['score'] = score
            obj['timestamp'] = struct.unpack("!LL", summary)[0]

            # Check values, remove extra fields
            for field, value in list(obj.iteritems()):
                if field not in table:
                    del obj[field]
                    continue
                if not re.match('^[A-Za-z][_a-zA-Z0-9]*$', field):
                    raise ValueError('Invalid field name: ' + field)
                if type(value) is dict or type(value) is list:
                    raise ValueError('Complex types not allowed for fields')

            # Make the statement
            stmt = ('INSERT INTO ' + table_name +
                ' (' + ','.join(obj.keys()) + ') VALUES (' +
                ','.join(['?'] * len(obj)) + ')')

            # Run it
            cur.execute(stmt, obj.values())
        except: # pylint: disable=bare-except
            logger.debug("%s", traceback.format_exc())