예제 #1
0
    def exists(self, connection: Connection, cursor: Cursor) -> bool:
        """ Checks if this view exists """

        exists_sql, exists_params = self._exists_sql(connection)
        the_view = self._execute(connection, cursor, exists_sql, exists_params)

        # check if the value returned is not None
        return cursor.fetchone() is not None
예제 #2
0
    def _quote_values(self, connection: Connection, cursor: Cursor,
                      values: list[Any]) -> list[str]:
        """ Quotes a list of values for inclusion in a different sql value """

        # make a statement SELECT quote(%s), quote(%s), ... for each of the values
        sql = "SELECT {}".format(", ".join(["quote(%s)"] * len(values)))
        cursor.execute(sql, values)
        return cursor.fetchone()
예제 #3
0
def do_batch_update(
    cursor: CursorWrapper,
    table: str,
    assignments: List[Composable],
    batch_size: int = 10000,
    sleep: float = 0.1,
) -> None:  # nocoverage
    # The string substitution below is complicated by our need to
    # support multiple PostgreSQL versions.
    stmt = SQL("""
        UPDATE {}
        SET {}
        WHERE id >= %s AND id < %s
    """).format(
        Identifier(table),
        SQL(", ").join(assignments),
    )

    cursor.execute(
        SQL("SELECT MIN(id), MAX(id) FROM {}").format(Identifier(table)))
    (min_id, max_id) = cursor.fetchone()
    if min_id is None:
        return

    print(f"\n    Range of rows to update: [{min_id}, {max_id}]")
    while min_id <= max_id:
        lower = min_id
        upper = min_id + batch_size
        print(f"    Updating range [{lower},{upper})")
        cursor.execute(stmt, [lower, upper])

        min_id = upper
        time.sleep(sleep)

        # Once we've finished, check if any new rows were inserted to the table
        if min_id > max_id:
            cursor.execute(
                SQL("SELECT MAX(id) FROM {}").format(Identifier(table)))
            (max_id, ) = cursor.fetchone()

    print("    Finishing...", end="")