Exemple #1
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)
Exemple #2
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
Exemple #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)
Exemple #4
0
    def __new__(cls, name, bases, attrs):
        # print "init",name
        if name == "Model" or name == "McModel":
            return super(ModelBase, cls).__new__(cls, name, bases, attrs)

        new_class = type.__new__(cls, name, bases, attrs)

        if not getattr(new_class, "Meta", None):

            class Empty:
                pass

            new_class.Meta = Empty

        if not getattr(new_class.Meta, "table", None):
            new_class.Meta.table = lower_name(name)
        new_class.Meta.table_safe = escape(new_class.Meta.table)

        # Assume id is the default
        if not getattr(new_class.Meta, "pk", None):
            new_class.Meta.pk = "id"
        if not getattr(new_class.Meta, "mc_key", None):
            mc_ver = getattr(new_class.Meta, "mc_ver", "")
            if mc_ver:
                new_class.Meta.mc_key = "%s@%s:%%s" % (name, mc_ver)
            else:
                new_class.Meta.mc_key = "%s:%%s" % name

        db = new_class.db = get_db_by_table(new_class.Meta.table)

        q = db.cursor()
        q.execute("SELECT * FROM %s LIMIT 0" % new_class.Meta.table_safe)
        q.connection.commit()

        new_class._fields = [f[0] for f in q.description]

        cache.add(new_class)
        return new_class