Пример #1
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
Пример #2
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)
Пример #3
0
def get(cls, __obj_pk=None, **kwargs):
    if __obj_pk is None:
        if not kwargs:
            return
    else:
        kwargs = {cls.Meta.pk: __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
Пример #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
Пример #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)
Пример #6
0
def create_table(db, s_create_sql):
    """
    Create a table for an Autumn class.
    """
    Query.begin(db=db)
    Query.raw_sqlscript(s_create_sql, db=db)
    Query.commit(db=db)
Пример #7
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()
Пример #8
0
 def raw_sql(cls, query, *args):
     result = Query.raw_sql(query, args, cls.db)
     return result