def update( self, callback, *args, **kwargs): """ 例子:更新字段id = 123的条目的数据 xxx.filter(id = 123).update( cb, (field2, value1), (field2, value2), fieldN = valueN ) 回调格式: def callback(success, rows): pass """ assert self.model is not None where = self.build_where_clauses(*self.filters) paramsKey = [] paramsVal = [] kw = list(args) + list(kwargs.items()) for k, v in kw: if hasattr(v, "resolve_expression"): paramsKey.append( "{} = {}".format( self.meta.fields[k].db_column, v.resolve_expression(self.meta) ) ) else: paramsKey.append( "{} = %s".format( self.meta.fields[k].db_column ) ) paramsVal.append( v ) cmd = "UPDATE {} SET {}".format( self.meta.db_table, ", ".join( paramsKey ) ) cmd = MysqlUtility.makeSafeSql( cmd, paramsVal ) + where #DEBUG_MSG( "%s::update(), %s" % (self.__class__.__name__, cmd) ) KBEngine.executeRawDatabaseCommand( cmd, functools.partial( self._update_callback, cmd, callback ) )
def delete(self, callback, *args, **kwargs): """ def callback(success, rows): pass """ assert self.model is not None arg = self.filters + list(args) + list(kwargs.items()) where = self.build_where_clauses(*arg) cmd = "DELETE FROM {}".format( self.meta.db_table ) cmd = MysqlUtility.makeSafeSql( cmd ) + where #DEBUG_MSG( "%s::delete(), %s" % (self.__class__.__name__, cmd) ) KBEngine.executeRawDatabaseCommand( cmd, functools.partial( self._delete_callback, cmd, callback ) )
def insert( self, callback, *args, **kwargs ): """ 向数据库插入一条记录。 def callback(success, insertid): pass """ kw = list(args) + list(kwargs.items()) fieldNames = [] fieldValues = [] fieldValuesP = [] for k, v in kw: fieldNames.append( self.meta.fields[k].db_column ) fieldValues.append( v ) fieldValuesP.append( "%s" ) cmd = "INSERT INTO {} ( {} ) VALUES ( {} )".format( self.meta.db_table, ", ".join( fieldNames ), ", ".join( fieldValuesP ) ) cmd = MysqlUtility.makeSafeSql( cmd, fieldValues ) #DEBUG_MSG( "%s::insert(), %s" % (self.__class__.__name__, cmd) ) KBEngine.executeRawDatabaseCommand( cmd, functools.partial( self._insert_callback, cmd, callback ) );
def select(self, callback, *args, **kwargs): """ def callback(success, models): pass """ assert self.model is not None arg = self.filters + list(args) + list(kwargs.items()) where = self.build_where_clauses(*arg) attrs = list(self.meta.fields) fields = [self.meta.fields[e].db_column for e in attrs] queryField = ", ".join(fields) cmd = "select {} from {}".format(queryField, self.meta.db_table) cmd = MysqlUtility.makeSafeSql(cmd) if where: cmd += b" where " + where #DEBUG_MSG( "%s::select(), %s" % (self.__class__.__name__, cmd) ) KBEngine.executeRawDatabaseCommand( cmd, functools.partial(self._select_callback, cmd, attrs, callback))
def select(self, callback, *args, **kwargs): """ def callback(success, models): pass """ assert self.model is not None arg = self.filters + list(args) + list(kwargs.items()) where = self.build_where_clauses(*arg) attrs = list( self.meta.fields ) fields = [ self.meta.fields[e].db_column for e in attrs ] queryField = ", ".join( fields ) select = "SELECT {} FROM {}".format( queryField, self.meta.db_table ) cmd = b"".join([ MysqlUtility.makeSafeSql( select ), where, self.build_order_by_clauses(), self.limit_opt and (" LIMIT %d, %d" % self.limit_opt).encode() or b"", ]) #DEBUG_MSG( "%s::select(), %s" % (self.__class__.__name__, cmd) ) KBEngine.executeRawDatabaseCommand( cmd, functools.partial( self._select_callback, cmd, attrs, callback ) )