Beispiel #1
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)
     self.mc_flush()
Beispiel #2
0
def table_exists(db, table_name):
    """
    Given an Autumn model, check to see if its table exists.
    """
    try:
        s_sql = 'SELECT * FROM %s LIMIT 1;' % table_name
        Query.raw_sql(s_sql, db=db)
    except Exception:
        return False

    # if no exception, the table exists and we are done
    return True
Beispiel #3
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)
Beispiel #4
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
        auto_pk = 1 and (self._get_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)
            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 self._get_pk() is None:
            self._set_pk(cursor.lastrowid)
        return True
Beispiel #5
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)
Beispiel #6
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)
Beispiel #7
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
        auto_pk = 1 and (self._get_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)
            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 self._get_pk() is None:
            self._set_pk(cursor.lastrowid)
        return True
Beispiel #8
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)
     self.mc_flush()
Beispiel #9
0
 def raw_sql(cls, query, *args):
     result = Query.raw_sql(query, args, cls.db)
     return result
Beispiel #10
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)
Beispiel #11
0
 def raw_sql(cls, query, *args):
     result = Query.raw_sql(query, args, cls.db)
     return result