def getList(self, cls, entity_ids, **kwargs): db_conn = cls.dbName(**kwargs) connection = self.connection_manager.get(db_conn) query_db_ids = [] query_cache_ids = [] for entity_id in entity_ids: entity = self.getEntityInMemory(cls, entity_id) if not entity: query_cache_ids.append(entity_id) if query_cache_ids: if self.use_cache: keys = [ self.makeKey(cls, entity_id) for entity_id in query_cache_ids ] cache_name = cls._cache_name cache = self.cache_manager.get(cache_name) if not cache: raise ValueError( 'CACHE DOES NOT EXSITS WHEN USE_CACHE IS TRUE') entitys = cache.getList(keys) for entity_id, key in zip(query_cache_ids, keys): cache_dict = entitys.get(key) if cache_dict: entity = cls(**cache_dict) entity._is_new = False entity._is_delete = False entity._is_dirty = False entity._load_from_cache = True entity._db = 'default' entity._cache = cache_name self.register(entity) logging.debug("[XWEB] LOAD ENTITY %s FROM CACHE: %s" % (entity, cache_name)) else: query_db_ids.append(entity_id) else: query_db_ids = query_cache_ids entitys = connection.getEntityList(cls, query_db_ids) if not entitys: return [] first_entity = entitys[0] first_entity.setProps('entity_ids_in_query', entity_ids) for entity in entitys: self.register(entity) entity.setProps('first_entity_in_query', first_entity.id) logging.debug("[XWEB] LOAD ENTITY %s FROM DB: %s" % (entity, db_conn)) return [ self.getEntityInMemory(cls, entity_id) for entity_id in entity_ids if self.getEntityInMemory(cls, entity_id) ]
def fetchRows(self, sql, *args): cursor = self.connect().cursor() try: t = time.time() cnt = 0 while cnt < 2: cnt += 1 try: cursor.execute(sql, tuple(args)) row = cursor.fetchall() t = time.time() - t logging.debug( "[XWEB] SQL: \"%s\", PARAMS: %s, TIME: %.1fms" % (sql, str(args[:10]), t * 1000)) return row break except InterfaceError, OperationalError: logging.debug("[XWEB] MYSQL RECONNECT...") self.ping() finally: cursor.close() return None
def get(self, name='default', read_only=False): if read_only: name = "%s_read" % name if not self.conf.has_key(name): if not self.conf.has_key('default'): raise DefaultDBNotExists() return self.get('default') if not self.connections.has_key(name): conf = self.conf.get(name) driver = conf.get('driver') driver2cls = { 'mysql': MySQLDBConnection } cls = driver2cls.get(driver) or MySQLDBConnection conn = cls(name, conf) self.connections[name] = conn logging.debug("[XWEB] INIT DB CONNECTION: %s", conn) return self.connections.get(name)
def __getBelongsToEntity(self, name): ''' ''' foreign_key, foreign_class, foreign_id = self._getBelongsToInfo(name) unitofwork = self.getUnitOfWork() if not foreign_id: return None if self.disable_preload: return unitofwork.get(foreign_class, foreign_id) foreign_entity = unitofwork.getEntityInMemory(foreign_class, foreign_id) if foreign_entity: return foreign_entity # 多主键的实体禁用preload功能 if type(foreign_key) != str: return unitofwork.get(foreign_class, foreign_id) first_entity_id = self.getProps('first_entity_in_query') if not first_entity_id: return unitofwork.get(foreign_class, foreign_id) if first_entity_id == self.getId(): entity_ids_in_query = self.getProps('entity_ids_in_query', []) else: first_entity = unitofwork.getEntityInMemory(type(self), first_entity_id) if not first_entity: return unitofwork.get(foreign_class, foreign_id) entity_ids_in_query = first_entity.getProps('entity_ids_in_query', []) foreign_entity_infos = {} for current_entity_id in entity_ids_in_query: entity_in_query = unitofwork.getEntityInMemory(type(self), current_entity_id) if not entity_in_query: continue sub_foreign_class, sub_foreign_id = entity_in_query._getBelongsToInfo(name)[-2:] if not foreign_entity_infos.has_key(sub_foreign_class): foreign_entity_infos[sub_foreign_class] = [sub_foreign_id] else: foreign_entity_infos[sub_foreign_class].append(sub_foreign_id) if foreign_entity_infos: for sub_foreign_class, sub_foreign_ids in foreign_entity_infos.items(): foreign_entitys = unitofwork.getList(sub_foreign_class, sub_foreign_ids) logging.debug("[XWEB] PRELOAD %s in %s" % (sub_foreign_class.modelName(), str([foreign_entity.getId() for foreign_entity in foreign_entitys]))) return unitofwork.getEntityInMemory(foreign_class, foreign_id) return unitofwork.get(foreign_class, foreign_id)
def getList(self, cls, entity_ids, **kwargs): db_conn = cls.dbName(**kwargs) connection = self.connection_manager.get(db_conn) query_db_ids = [] query_cache_ids = [] for entity_id in entity_ids: entity = self.getEntityInMemory(cls, entity_id) if not entity: query_cache_ids.append(entity_id) if query_cache_ids: if self.use_cache: keys = [self.makeKey(cls, entity_id) for entity_id in query_cache_ids] cache_name = cls._cache_name cache = self.cache_manager.get(cache_name) if not cache: raise ValueError('CACHE DOES NOT EXSITS WHEN USE_CACHE IS TRUE') entitys = cache.getList(keys) for entity_id, key in zip(query_cache_ids, keys): cache_dict = entitys.get(key) if cache_dict: entity = cls(**cache_dict) entity._is_new = False entity._is_delete = False entity._is_dirty = False entity._load_from_cache = True entity._db = 'default' entity._cache = cache_name self.register(entity) logging.debug("[XWEB] LOAD ENTITY %s FROM CACHE: %s"%(entity, cache_name)) else: query_db_ids.append(entity_id) else: query_db_ids = query_cache_ids entitys = connection.getEntityList(cls, query_db_ids) if not entitys: return [] first_entity = entitys[0] first_entity.setProps('entity_ids_in_query', entity_ids) for entity in entitys: self.register(entity) entity.setProps('first_entity_in_query', first_entity.id) logging.debug("[XWEB] LOAD ENTITY %s FROM DB: %s"%(entity, db_conn)) return [self.getEntityInMemory(cls, entity_id) for entity_id in entity_ids if self.getEntityInMemory(cls, entity_id)]
def execute(self, sql, values): n = 0 cursor = self.connect().cursor() t = time.time() try: n = cursor.execute(sql, tuple(values)) finally: cursor.close() self.last_time = time.time() t= time.time() - t logging.debug("[XWEB] SQL: \"%s\", PARAMS: %s, ROWS: %s, TIME: %.1fms"%(sql, str(values[:10]), n, t*1000)) return n > 0
def fetchRows(self, sql, *args): cursor = self.connect().cursor() try: t = time.time() cursor.execute(sql, args) row = cursor.fetchall() t = time.time() - t logging.debug("[XWEB] SQL: \"%s\", PARAMS: %s, ROWS: %s, TIME: %.1fms"%(sql, str(args[:10]), len(row), t*1000)) return row finally: cursor.close() return None
def get(self, cls, entity_id, **kwargs): #@ReservedAssignment entity = self.getEntityInMemory(cls, entity_id) if entity: return entity key = self.makeKey(cls, entity_id) cache_name = cls._cache_name cache = self.cache_manager.get(cache_name) if self.use_cache and cache: cache_dict = cache.get(key) if cache_dict: entity = cls(**cache_dict) entity._is_new = False entity._is_delete = False entity._is_dirty = False entity._load_from_cache = True entity._db = 'default' entity._cache = cache_name self.register(entity) logging.debug("[XWEB] LOAD ENTITY %s FROM CACHE: %s" % (entity, cache_name)) return entity db_conn = cls.dbName(entity_id=entity_id, **kwargs) connection = self.connection_manager.get(db_conn) entity = connection.getEntity(cls, entity_id) if entity is None: return None self.register(entity) if cache: cache.set(key, entity.getCacheDict()) logging.debug("[XWEB] LOAD ENTITY %s FROM DB: %s" % (entity, db_conn)) return entity
def execute(self, sql, values=()): n = 0 cursor = self.connect().cursor() try: t = time.time() cnt = 0 while cnt < 2: cnt += 1 try: n = cursor.execute(sql, tuple(values)) t= time.time() - t logging.debug("[XWEB] FETCH ROWS SQL: \"%s\", PARAMS: %s, ROWS: %s, TIME: %.1fms"%(sql, str(values[:10]), n, t*1000)) return n except InterfaceError, OperationalError: logging.debug("[XWEB] MYSQL RECONNECT...") self.ping() finally: cursor.close() return False
def get(self, cls, entity_id, **kwargs): #@ReservedAssignment entity = self.getEntityInMemory(cls, entity_id) if entity: return entity key = self.makeKey(cls, entity_id) cache_name = cls._cache_name cache = self.cache_manager.get(cache_name) if self.use_cache and cache: cache_dict = cache.get(key) if cache_dict: entity = cls(**cache_dict) entity._is_new = False entity._is_delete = False entity._is_dirty = False entity._load_from_cache = True entity._db = 'default' entity._cache = cache_name self.register(entity) logging.debug("[XWEB] LOAD ENTITY %s FROM CACHE: %s"%(entity, cache_name)) return entity db_conn = cls.dbName(entity_id=entity_id, **kwargs) connection = self.connection_manager.get(db_conn) entity = connection.getEntity(cls, entity_id) if entity is None: return None self.register(entity) if cache: cache.set(key, entity.getCacheDict()) logging.debug("[XWEB] LOAD ENTITY %s FROM DB: %s"%(entity, db_conn)) return entity
def fetchRow(self, sql, *args): cursor = self.connect().cursor() try: t = time.time() cnt = 0 while cnt < 2: cnt += 1 try: cursor.execute(sql, tuple(args)) row = cursor.fetchone() t= time.time() - t logging.debug("[XWEB] FETCH ROW SQL: \"%s\", PARAMS: %s, TIME: %.1fms"%(sql, str(args[:10]), t*1000)) return row break except InterfaceError, OperationalError: logging.debug("[XWEB] MYSQL RECONNECT...") self.ping() finally: cursor.close() return None
def execute(self, sql, values=()): n = 0 cursor = self.connect().cursor() try: t = time.time() cnt = 0 while cnt < 2: cnt += 1 try: n = cursor.execute(sql, tuple(values)) t = time.time() - t logging.debug( "[XWEB] FETCH ROWS SQL: \"%s\", PARAMS: %s, ROWS: %s, TIME: %.1fms" % (sql, str(values[:10]), n, t * 1000)) return n except InterfaceError, OperationalError: logging.debug("[XWEB] MYSQL RECONNECT...") self.ping() finally: cursor.close() return False
#app = ProfilerMiddleware(app) return app def runApp(self, environ, start_response): response = None t = time.time() try: request = self.createRequest(environ) response = self.process(request) request.secure_cookies.save_cookie(response) except HTTPException, ex: response = ex t = (time.time() - t) * 1000 logging.debug("Request time: %.2f ms" % t) return response(environ, start_response) def createRewriteRules(self): self.rewrite_rules.extend([ XRewriteRule('/', {'c':'default', 'a':'index'}), XRewriteRule('/<c>/', {'a':'index'}), XRewriteRule('/<c>/<a>/', {}), ]) return self def buildRewrite(self, rules): self.rewrite_rules = [] for rule in rules:
def begin(self): if hasattr(self.connect(), 'begin'): self.connect().begin() else: self.execute("BEGIN") logging.debug("开启事务")
def __getBelongsToEntity(self, name): ''' ''' foreign_key, foreign_class, foreign_id = self._getBelongsToInfo(name) unitofwork = self.getUnitOfWork() if not foreign_id: return None if self.disable_preload: return unitofwork.get(foreign_class, foreign_id) foreign_entity = unitofwork.getEntityInMemory(foreign_class, foreign_id) if foreign_entity: return foreign_entity # 多主键的实体禁用preload功能 if type(foreign_key) != str: return unitofwork.get(foreign_class, foreign_id) first_entity_id = self.getProps('first_entity_in_query') if not first_entity_id: return unitofwork.get(foreign_class, foreign_id) if first_entity_id == self.getId(): entity_ids_in_query = self.getProps('entity_ids_in_query', []) else: first_entity = unitofwork.getEntityInMemory( type(self), first_entity_id) if not first_entity: return unitofwork.get(foreign_class, foreign_id) entity_ids_in_query = first_entity.getProps( 'entity_ids_in_query', []) foreign_entity_infos = {} for current_entity_id in entity_ids_in_query: entity_in_query = unitofwork.getEntityInMemory( type(self), current_entity_id) if not entity_in_query: continue sub_foreign_class, sub_foreign_id = entity_in_query._getBelongsToInfo( name)[-2:] if not foreign_entity_infos.has_key(sub_foreign_class): foreign_entity_infos[sub_foreign_class] = [sub_foreign_id] else: foreign_entity_infos[sub_foreign_class].append(sub_foreign_id) if foreign_entity_infos: for sub_foreign_class, sub_foreign_ids in foreign_entity_infos.items( ): foreign_entitys = unitofwork.getList(sub_foreign_class, sub_foreign_ids) logging.debug("[XWEB] PRELOAD %s in %s" % (sub_foreign_class.modelName(), str([ foreign_entity.getId() for foreign_entity in foreign_entitys ]))) return unitofwork.getEntityInMemory(foreign_class, foreign_id) return unitofwork.get(foreign_class, foreign_id)