src/risclog/sqlalchemy/cache.py
def _save_by_copy(self, cursor, objects):
        """
        Insert objects by using PostgreSQL's COPY command. This is
        database-specific but one of the most efficient ways to populate a
        table. Expects instances of one single model per call.
        See: https://www.postgresql.org/docs/current/sql-copy.html

        Args:
            cursor: A Psycopg2 cursor
            objects: SQLAlchemy ORM instances to save
        """
        if len(objects) == 0:
            return

        model = inspect(objects[0]).mapper
        for table in model.tables:
            file = io.StringIO()
            writer = csv.DictWriter(file, table.columns.keys())
            columns = [
                c for c in model.c.items()
                if c[1].table == table or c[1].primary_key
            ]

            for object in objects:
                row = {}
                for attr, column in columns:
                    value = getattr(object, attr)
                    if value is None:
                        if column.default is not None:
                            value = column.default.arg
                        else:
                            value = r'\N'
                    elif type(value) != column.type.python_type:
                        value = column.type.python_type(value)
                    row[column.key] = value
                writer.writerow(row)

            file.seek(0)

            columns_string = ','.join(
                [f'"{column}"' for column in table.columns.keys()]
            )
            cursor.copy_expert(
                f'COPY {table} ({columns_string}) '
                "FROM STDIN WITH CSV DELIMITER ',' NULL '\\N'",
                file,
            )
            cursor.connection.commit()

        Insert objects by using PostgreSQL's COPY command. This is
        database-specific but one of the most efficient ways to populate a
        table. Expects instances of one single model per call.
        See: https://www.postgresql.org/docs/current/sql-copy.html

        Args:
            cursor: A Psycopg2 cursor
            objects: SQLAlchemy ORM instances to save
        
Insert objects by using PostgreSQL's COPY command. This is
database-specific but one of the most efficient ways to populate a
table. Expects instances of one single model per call.
Insert objects by using PostgreSQL ' s COPY command . This is database - specific but one of the most efficient ways to populate a table . Expects instances of one single model per call .
def _save_by_copy(self, cursor, objects):
        if len(objects) == 0:
            return
        model = inspect(objects[0]).mapper
        for table in model.tables:
            file = io.StringIO()
            writer = csv.DictWriter(file, table.columns.keys())
            columns = [
                c for c in model.c.items()
                if c[1].table == table or c[1].primary_key
            ]
            for object in objects:
                row = {}
                for attr, column in columns:
                    value = getattr(object, attr)
                    if value is None:
                        if column.default is not None:
                            value = column.default.arg
                        else:
                            value = r'\N'
                    elif type(value) != column.type.python_type:
                        value = column.type.python_type(value)
                    row[column.key] = value
                writer.writerow(row)
            file.seek(0)
            columns_string = ','.join(
                [f'"{column}"' for column in table.columns.keys()]
            )
            cursor.copy_expert(
                f'COPY {table} ({columns_string}) '
                "FROM STDIN WITH CSV DELIMITER ',' NULL '\\N'",
                file,
            )
            cursor.connection.commit()
def _save_by_copy ( self , cursor , objects ) : if len ( objects ) == 0 : return model = inspect ( objects [ 0 ] ) . mapper for table in model . tables : file = io . StringIO ( ) writer = csv . DictWriter ( file , table . columns . keys ( ) ) columns = [ c for c in model . c . items ( ) if c [ 1 ] . table == table or c [ 1 ] . primary_key ] for object in objects : row = { } for attr , column in columns : value = getattr ( object , attr ) if value is None : if column . default is not None : value = column . default . arg else : value = r'\N' elif type ( value ) != column . type . python_type : value = column . type . python_type ( value ) row [ column . key ] = value writer . writerow ( row ) file . seek ( 0 ) columns_string = ',' . join ( [ f'"{column}"' for column in table . columns . keys ( ) ] ) cursor . copy_expert ( f'COPY {table} ({columns_string}) ' "FROM STDIN WITH CSV DELIMITER ',' NULL '\\N'" , file , ) cursor . connection . commit ( )
Insert objects by using PostgreSQL's COPY command.
Insert objects by using PostgreSQL ' s COPY command .
"""
        Insert objects by using PostgreSQL's COPY command. This is
        database-specific but one of the most efficient ways to populate a
        table. Expects instances of one single model per call.
        See: https://www.postgresql.org/docs/current/sql-copy.html

        Args:
            cursor: A Psycopg2 cursor
            objects: SQLAlchemy ORM instances to save
        """
self
cursor
A Psycopg2 cursor
A Psycopg2 cursor
objects
SQLAlchemy ORM instances to save
SQLAlchemy ORM instances to save