def build_stmt(self): """ Build field definition """ stmt = [] if self.allow_null is True or self.allow_null == 1: stmt.append('NULL') elif self.allow_null is False or self.allow_null == 0: stmt.append('NOT NULL') else: raise ValueError('Allow_null value must be True, False, 0 or 1') if self.default is not None: default = self.default if isinstance(self, Float): default = float(default) if not isinstance(default, self._py_type): raise ValueError( f'Except default value {self._py_type} now is {default}') if isinstance(default, str): default = f"'{self.default}'" stmt.append(f'DEFAULT {default}') elif not self.allow_null: Logger.warning( f'Not to give default value for NOT NULL field {self.name}') stmt.append(f"COMMENT '{self.comment}'") return stmt
async def _update(cls, data, where=None): """ do update by where condition """ if not isinstance(data, dict): raise ValueError('Invalid data type') where_format = None if where is not None: if not isinstance(where, (_Where, _Logic)): raise ValueError('Invalid where type {}'.format(type(where))) where_format = where.format_() cls._has_cols_checker(where_format.col) update_fields = list(data.keys()) update_values = [data[f] for f in update_fields] set_clause = ','.join(['='.join([f, '%s']) for f in update_fields]) if where_format: updete_sql = SQL.update_.complete.format( table_name=cls.__meta__.table, kv=set_clause, condition=where_format.where ) update_values.extend(where_format.arg) else: updete_sql = SQL.update_.no_where.format( table_name=cls.__meta__.table, kv=set_clause ) Logger.warning('Dangerous operation: {}'.format(updete_sql)) return await RequestClient().execute(updete_sql, values=update_values)
def build_stmt(self): stmt = [] if self.allow_null is True or self.allow_null == 1: stmt.append('NULL') elif self.allow_null is False or self.allow_null == 0: stmt.append('NOT NULL') else: raise ValueError('Allow_null value must be True, False, 0 or 1') if self.auto is not None: if self.auto == 'on_create': stmt.append('DEFAULT CURRENT_TIMESTAMP') elif self.auto == 'on_update': stmt.append( 'DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP') elif self.default is not None: if not isinstance(self.default, self._py_type): raise ValueError( f'Except default value {self._py_type} now is {self.default}' ) stmt.append(f'DEFAULT {self.default}') elif not self.allow_null: Logger.warning( f'Not to give default value for NOT NULL field {self.name}') stmt.append(f"COMMENT '{self.comment}'") return stmt
async def unbind(cls): """ A coroutine that call `executer.close()` to unbind db""" if cls.executer is not None: await cls.executer.close() cls.executer = None else: Logger.warning('No binding db connection or closed') return True
def build_meta(): meta = {} for arg in TABLE_DEFAULT: if arg == '__table__': table_name = attrs.get(arg, None) if table_name is None: Logger.warning( "Did not give the table name by '__table__', use the model name" ) table_name = name meta[arg.strip('_')] = table_name else: meta[arg.strip('_')] = attrs.get(arg, None) or TABLE_DEFAULT[arg] if arg in attrs.keys(): attrs.pop(arg) return meta
def __init__(self, length, unsigned=False, allow_null=True, default=None, comment='', name=None): super().__init__(allow_null=allow_null, default=default, comment=comment, name=name) if not isinstance(length, (tuple)): raise ValueError('Length type error') self.length = length[0] if len(length) != 2: Logger.warning('Length format error') self.float_length = length[-1] else: self.float_length = length[1] self.unsigned = unsigned
def __init__( self, length, # auto_increase=False, unsigned=False, allow_null=True, primary_key=False, default=None, comment='', name=None): super().__init__(name=name, allow_null=allow_null, default=default, comment=comment, length=length, unsigned=unsigned) self.primary_key = primary_key if self.primary_key is True: if self.allow_null: Logger.warning('Primary_key is not allow null, use default') self.allow_null = False self.default = None
async def batch_drop(self, *models): """ A coroutine that batch drop same model. Args: models: one or more models Return: A list of droped table name For example: from tests.models import User, Order await db.batch_drop(User, Order) """ self._checker() succeed = [] if not models: Logger.warning("parameter 'models' is empty, 'batch_drop' nothing to do") return succeed for model in models: if not issubclass(model, self.Model): raise ValueError( 'drop model type must be {}, get {}'.format(self.Model, model) ) if await model.exist(): await model.drop() Logger.info( "dropped table '{table_name}' from db: '{db}'".format( table_name=model.__meta__.table, db=self.db_info.info.db.db ) ) succeed.append(model.__meta__.table) else: Logger.error( message="drop table '{}' does not exist".format(model.__meta__.table) ) return succeed
def qiniu_fetch_file(purl): max_retry = 5 if not is_url(purl): Logger.warning(task='qiniu_fetch', message='input url:%s' % purl) return '' purl = transform_to_http(purl) q_auth = qiniu.Auth(ACCESS_KEY, SECRET_KEY) bucket_path = qiniu.BucketManager(q_auth) for n in range(max_retry): ret = bucket_path.fetch(purl, BUCKET_NAME) if ret is None: continue elif isinstance(ret, tuple) and ret[0] is None: continue else: key = ret[0]['key'] url = DOMAIN + str(key) obj = urlparse.urlparse(url) return obj.geturl() else: Logger.error(task='qiniu_fetch', message='max retry exceed') return purl