Ejemplo n.º 1
0
    def copy_from(self, file, table, sep='\t', null='\\N', size=8192,
                  columns=None):
        """Reads data from a file-like object appending them to a database
        table (COPY table FROM file syntax).

        The source file must have both read() and readline() method.

        TODO: Improve error handling

        """
        if columns:
            columns_str = '(%s)' % ','.join([column for column in columns])
        else:
            columns_str = ''

        query = util.unicode_to_bytes(
                "COPY %s%s FROM stdin WITH DELIMITER AS " % (
                    table, columns_str)) + \
                util.quote_string(self._conn, sep) + b' NULL AS ' + \
                util.quote_string(self._conn, null)

        self._copysize = size
        self._copyfile = file
        try:
            self._pq_execute(query)
        finally:
            self._copyfile = None
            self._copysize = None
Ejemplo n.º 2
0
    def copy_to(self, file, table, sep='\t', null='\\N', columns=None):
        """Writes the content of a table to a file-like object (COPY table
        TO file syntax).

        The target file must have a write() method.

        TODO: Improve error handling

        """
        if columns:
            columns_str = '(%s)' % ','.join([column for column in columns])
        else:
            columns_str = ''

        query = util.unicode_to_bytes(
                "COPY %s%s TO stdout WITH DELIMITER AS " % (
                    table, columns_str)) + \
                util.quote_string(self._conn, sep) + b' NULL AS ' + \
                util.quote_string(self._conn, null)

        self._copyfile = file
        try:
            self._pq_execute(query)
        finally:
            self._copyfile = None
Ejemplo n.º 3
0
    def execute(self, query, parameters=None):
        """Prepare and execute a database operation (query or command).

        Parameters may be provided as sequence or mapping and will be bound to
        variables in the operation.  Variables are specified in a
        database-specific notation (see the module's paramstyle attribute for
        details).

        A reference to the operation will be retained by the cursor.  If the
        same operation object is passed in again, then the cursor can optimize
        its behavior.  This is most effective for algorithms where the same
        operation is used, but different parameters are bound to it
        (many times).

        For maximum efficiency when reusing an operation, it is best to use
        the .setinputsizes() method to specify the parameter types and sizes
        ahead of time.  It is legal for a parameter to not match the
        predefined information; the implementation should compensate,
        possibly with a loss of efficiency.

        The parameters may also be specified as list of tuples to e.g. insert
        multiple rows in a single operation, but this kind of usage is
        deprecated: .executemany() should be used instead.

        Return values are not defined.

        """
        self._description = None
        conn = self._conn

        if self._name:
            if self._query:
                raise ProgrammingError(
                    "can't call .execute() on named cursors more than once")
            if self._conn.autocommit:
                raise ProgrammingError(
                    "can't use a named cursor outside of transactions")

        if isinstance(query, six.text_type):
            query = query.encode(self._conn._py_enc)

        if parameters is not None:
            self._query = _combine_cmd_params(query, parameters, conn)
        else:
            self._query = query

        scroll = ""
        if self._scrollable is not None:
            scroll = self._scrollable and "SCROLL " or "NO SCROLL "

        conn._begin_transaction()
        self._clear_pgres()

        if self._name:
            self._query = \
                util.unicode_to_bytes(
                        'DECLARE "%s" %sCURSOR %s HOLD FOR ' % (
                            self._name, scroll,
                            "WITH" if self._withhold else "WITHOUT")) \
                + self._query

        self._pq_execute(self._query, conn._async)