Exemplo n.º 1
0
 def update(self, **kwds):
     set_what = ','.join(
         '%s=%%s'%(
             escape(k)
         )
         for k in kwds.keys()
     )
     query = 'UPDATE %s SET %s WHERE `%s` = %%s' % (
         self.Meta.table_safe,
         set_what, self.Meta.pk
     )
     values = kwds.values()+[getattr(self, self.Meta.pk)]
     Query.raw_sql(query, values, self.db)
Exemplo n.º 2
0
 def get(cls, __obj_pk=None, **kwargs):
     if __obj_pk is None:
         if not kwargs:
             return
     else:
         kwargs = {
             'id': __obj_pk
         }
     q = Query(model=cls, conditions=kwargs)
     q.limit = (0, 1)
     q = q.execute_query()
     q = q.fetchone()
     if q:
         obj = cls(*q)
         obj.__dict__['_new_record'] = False
         return obj
Exemplo n.º 3
0
    def _new_save(self):
        'Uses SQL INSERT to create new record'
        # if pk field is set, we want to insert it too
        # if pk field is None, we want to auto-create it from lastrowid
        pk = self._get_pk()
        auto_pk = 1 and (pk is None) or 0
        fields = [
            f for f in self._fields
            if f != self.Meta.pk or not auto_pk
        ]

        used_fields = []
        values = []
        for i in fields:
            v = getattr(self, i, None)
            #print i,v
            if v is not None:
                used_fields.append(escape(i))
                values.append(v)
        query = 'INSERT INTO %s (%s) VALUES (%s)' % (self.Meta.table_safe,
                ', '.join(used_fields),
                ', '.join(['%s'] * len(used_fields))
        )
        cursor = Query.raw_sql(query, values, self.db)



        if pk is None:
            self._set_pk(cursor.lastrowid)
        return True
Exemplo n.º 4
0
    def _update(self):
        if not self._changed:return
        'Uses SQL UPDATE to update record'
        query = 'UPDATE %s SET ' % self.Meta.table_safe
        query += ','.join(['%s=%%s' % escape(f) for f in self._changed])
        query += ' WHERE %s=%%s ' % (escape(self.Meta.pk))

        values = [getattr(self, f) for f in self._changed]
        values.append(self._get_pk())

        cursor = Query.raw_sql(query, values, self.db)
Exemplo n.º 5
0
 def delete(self):
     'Deletes record from database'
     query = 'DELETE FROM %s WHERE `%s` = %%s' % (self.Meta.table_safe, self.Meta.pk)
     values = [getattr(self, self.Meta.pk)]
     Query.raw_sql(query, values, self.db)
Exemplo n.º 6
0
 def raw_sql(cls, query, *args):
     result = Query.raw_sql(query, args, cls.db)
     return result