def execute_upd(model, fv, ids): sql = 'update {0} '.format(model.get_table_name()) for k, v in fv.items(): if v: sql += " set {0}='{1}',".format(k, v) sql = sql[:-1] sql += ' where 1=1 ' + Rule.rule_in('id', ids) info('curdHelper', 'update sql:' + sql) DbHelper.get_db().session.execute(sql)
def gen_condition_del_sql(table, conditions): """ 生成 delete from ... where ... :param table: :param conditions: :return: """ sql = "delete from {0}".format(table) sql += SqlHelper.gen_where(conditions, False) info('sqlhelper', sql) return sql
def init_db(app): if not app: raise (BlankParamExcept("app param is blank")) info( 'DbHelper', 'begin init database {0}'.format( SysConfHelper.get_item("mysql", "uri"))) app.config['SQLALCHEMY_DATABASE_URI'] = SysConfHelper.get_item( "mysql", "uri") app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True DbHelper.__db.init_app(app)
def set_attr_by_dist(self, dist): """ 根据给定的字典设置属性值 :param dist: 字典 :return: void """ if not dist or len(dist.keys()) < 1: info("basic", "param dist is empty or null") return for k, v in dist.items(): if v: setattr(self, k, v)
def gen_del_sql(model, value_in_list, field_name='id'): """ delete from table where xxx in xxx :param model: :param value_in_list: :param field_name: :return: """ ids_str = ["'" + str(idd) + "'" for idd in value_in_list] sql = "delete from {0} where {1} in({2})".format( model.get_table_name(), field_name, ','.join(ids_str)) info('sqlhelper', sql) return sql
def gen_select_sql(table, conditions, is_count=False): """ 生成 select ... from ... where ... :param table: :param conditions: :param is_count: :return: """ if is_count: sql = "select count(*) from {0}".format(table) else: sql = "select * from {0}".format(table) if not 'valid_status' in conditions.keys(): conditions['valid_status'] = Constants.VALID_STATUS_Y sql += SqlHelper.gen_where(conditions, is_count) info("genSqlHelper", sql) return sql