예제 #1
0
    def page(self, pager: EntityPager) -> DataPage:
        page_size = pager.pageable.pageSize

        table = self.find_table(pager.name)
        statement = select(func.count()).select_from(table)
        statement = self.build_criteria_for_statement([table], statement,
                                                      pager.criteria)
        count, empty_page = self.execute_page_count(statement, page_size)
        if count == 0:
            return empty_page

        page_number, max_page_number = self.compute_page(
            count, page_size, pager.pageable.pageNumber)

        statement = select(table)
        statement = self.build_criteria_for_statement([table], statement,
                                                      pager.criteria)
        statement = self.build_sort_for_statement(statement, pager.sort)
        offset = page_size * (page_number - 1)
        statement = statement.offset(offset).limit(page_size)
        results = self.connection.execute(statement).mappings().all()
        entity_list = ArrayHelper(results).map(lambda x: dict(x)).map(
            pager.shaper.deserialize).to_list()
        return DataPage(data=entity_list,
                        pageNumber=page_number,
                        pageSize=page_size,
                        itemCount=count,
                        pageCount=max_page_number)
예제 #2
0
	def free_aggregate_page(self, pager: FreeAggregatePager) -> DataPage:
		page_size = pager.pageable.pageSize

		_, data_statement = self.build_aggregate_statement(pager)

		aggregated = self.has_aggregate_column(pager.highOrderAggregateColumns)
		has_group_by = self.has_group_by_column(pager.highOrderAggregateColumns)
		if aggregated and not has_group_by:
			count = 1
		else:
			sub_query = data_statement.subquery()
			count_statement = select(func.count()).select_from(sub_query)
			count, empty_page = self.execute_page_count(count_statement, page_size)
			if count == 0:
				return empty_page

		data_statement = self.build_sort_for_statement(data_statement, pager.highOrderSortColumns)
		page_number, max_page_number = self.compute_page(count, page_size, pager.pageable.pageNumber)
		offset = page_size * (page_number - 1)
		data_statement = data_statement.offset(offset).limit(page_size)
		results = self.connection.execute(data_statement).mappings().all()

		def deserialize(row: Dict[str, Any]) -> Dict[str, Any]:
			return self.deserialize_from_auto_generated_aggregate_columns(row, pager.highOrderAggregateColumns)

		results = ArrayHelper(results).map(lambda x: deserialize(x)).to_list()

		return DataPage(
			data=results,
			pageNumber=page_number,
			pageSize=page_size,
			itemCount=count,
			pageCount=max_page_number
		)
예제 #3
0
	def free_page(self, pager: FreePager) -> DataPage:
		page_size = pager.pageable.pageSize

		data_statement = self.build_free_find_statement(pager)

		aggregated, aggregate_columns = self.fake_aggregate_columns(pager.columns)
		has_group_by = len(aggregate_columns) != len(pager.columns) and len(aggregate_columns) != 0
		if aggregated and not has_group_by:
			# all columns are aggregated, there is one row exactly
			count = 1
		else:
			sub_query = data_statement.subquery()
			count_statement = select(func.count()).select_from(sub_query)
			count, empty_page = self.execute_page_count(count_statement, page_size)
			if count == 0:
				return empty_page

		page_number, max_page_number = self.compute_page(count, page_size, pager.pageable.pageNumber)
		offset = page_size * (page_number - 1)
		data_statement = data_statement.offset(offset).limit(page_size)
		results = self.connection.execute(data_statement).mappings().all()

		results = ArrayHelper(results) \
			.map(lambda x: self.deserialize_from_auto_generated_columns(x, pager.columns)) \
			.to_list()

		return DataPage(
			data=results,
			pageNumber=page_number,
			pageSize=page_size,
			itemCount=count,
			pageCount=max_page_number
		)
    def free_aggregate_page(self, pager: FreeAggregatePager) -> DataPage:
        page_size = pager.pageable.pageSize

        _, data_sql = self.build_aggregate_statement(pager)

        aggregated = self.has_aggregate_column(pager.highOrderAggregateColumns)
        has_group_by = self.has_group_by_column(
            pager.highOrderAggregateColumns)
        if aggregated and not has_group_by:
            count = 1
        else:
            cursor = self.connection.cursor()
            count_sql = f'SELECT COUNT(*) FROM ({data_sql}) AS CQ'
            self.log_sql(count_sql)
            cursor.execute(count_sql)
            result = cursor.fetchall()
            if result:
                count = result[0][0]
            else:
                count = 0
            if count == 0:
                return DataPage(data=[],
                                pageNumber=1,
                                pageSize=page_size,
                                itemCount=0,
                                pageCount=0)

        order_by = self.build_aggregate_order_by(pager.highOrderSortColumns)
        if order_by is not None:
            data_sql = f'{data_sql} ORDER BY {order_by}'
        page_number, max_page_number = self.compute_page(
            count, page_size, pager.pageable.pageNumber)
        offset = page_size * (page_number - 1)
        data_sql = f'{data_sql} OFFSET {offset} LIMIT {page_size}'
        cursor = self.connection.cursor()
        self.log_sql(data_sql)
        cursor.execute(data_sql)
        rows = cursor.fetchall()

        results = ArrayHelper(rows) \
         .map(lambda x: self.deserialize_from_aggregate_row(x, pager.highOrderAggregateColumns)).to_list()

        return DataPage(data=results,
                        pageNumber=page_number,
                        pageSize=page_size,
                        itemCount=count,
                        pageCount=max_page_number)
    def free_page(self, pager: FreePager) -> DataPage:
        page_size = pager.pageable.pageSize

        data_sql = self.build_find_sql(pager)

        aggregated, aggregate_columns = self.fake_aggregate_columns(
            pager.columns)
        has_group_by = len(aggregate_columns) != 0 and len(
            aggregate_columns) != len(pager.columns)
        if aggregated and not has_group_by:
            count = 1
        else:
            cursor = self.connection.cursor()
            count_sql = f'SELECT COUNT(1) FROM ({data_sql}) as CQ'
            self.log_sql(count_sql)
            cursor.execute(count_sql)
            count = cursor.fetchall()[0][0]

        if count == 0:
            return DataPage(data=[],
                            pageNumber=1,
                            pageSize=page_size,
                            itemCount=0,
                            pageCount=0)

        page_number, max_page_number = self.compute_page(
            count, page_size, pager.pageable.pageNumber)

        offset = page_size * (page_number - 1)
        data_sql = f'{data_sql} OFFSET {offset} LIMIT {page_size}'
        cursor = self.connection.cursor()
        self.log_sql(data_sql)
        cursor.execute(data_sql)
        rows = cursor.fetchall()

        results = ArrayHelper(rows).map(
            lambda x: self.deserialize_from_row(x, pager.columns)).to_list()

        return DataPage(data=results,
                        pageNumber=page_number,
                        pageSize=page_size,
                        itemCount=count,
                        pageCount=max_page_number)
예제 #6
0
    def page(self, pager: EntityPager) -> DataPage:
        document = self.find_document(pager.name)
        where = build_criteria_for_statement([document], pager.criteria)
        page_size = pager.pageable.pageSize
        count = self.connection.count(document, where)
        if count == 0:
            return self.create_empty_page(page_size)

        page_number, max_page_number = self.compute_page(
            count, page_size, pager.pageable.pageNumber)
        offset = page_size * (page_number - 1)
        sort = build_sort_for_statement(pager.sort)
        results = self.connection.page(document, where, offset, page_size,
                                       sort)
        entities = ArrayHelper(results) \
         .map(self.remove_object_id) \
         .map(pager.shaper.deserialize) \
         .to_list()
        return DataPage(data=entities,
                        pageNumber=page_number,
                        pageSize=page_size,
                        itemCount=count,
                        pageCount=max_page_number)
예제 #7
0
 def create_empty_page(self, page_size: int) -> DataPage:
     return DataPage(data=[],
                     pageNumber=1,
                     pageSize=page_size,
                     itemCount=0,
                     pageCount=0)