Exemplo n.º 1
0
def test_named():
    args = {
        "f_2": 1,
        "f1": 2,
    }
    new_query, vals = convert_paramstyle(
        "named", "SELECT sum(x)::decimal(5, 2) :f_2, :f1 FROM t WHERE a=:f_2",
        args)
    expected = "SELECT sum(x)::decimal(5, 2) $1, $2 FROM t WHERE a=$1"
    assert (new_query, vals) == (expected, (1, 2))
Exemplo n.º 2
0
def test_pyformat_format():
    """pyformat should support %s and an array, too:"""
    args = 1, 2, 3
    new_query, vals = convert_paramstyle(
        "pyformat",
        "SELECT %s, %s, \"f1_%%\", E'txt_%%' "
        "FROM t WHERE a=%s AND b='75%%'",
        args,
    )
    expected = "SELECT $1, $2, \"f1_%%\", E'txt_%%' FROM t WHERE a=$3 AND " "b='75%%'"
    assert (new_query, vals) == (expected, args)
Exemplo n.º 3
0
def test_py_format():
    args = {"f2": 1, "f1": 2, "f3": 3}

    new_query, vals = convert_paramstyle(
        "pyformat",
        "SELECT %(f2)s, %(f1)s, \"f1_%%\", E'txt_%%' "
        "FROM t WHERE a=%(f2)s AND b='75%%'",
        args,
    )
    expected = "SELECT $1, $2, \"f1_%%\", E'txt_%%' FROM t WHERE a=$1 AND " "b='75%%'"
    assert (new_query, vals) == (expected, (1, 2))
Exemplo n.º 4
0
    def execute(self, operation, args=(), stream=None):
        """Executes a database operation.  Parameters may be provided as a
        sequence, or as a mapping, depending upon the value of
        :data:`pg8000.paramstyle`.

        This method is part of the `DBAPI 2.0 specification
        <http://www.python.org/dev/peps/pep-0249/>`_.

        :param operation:
            The SQL statement to execute.

        :param args:
            If :data:`paramstyle` is ``qmark``, ``numeric``, or ``format``,
            this argument should be an array of parameters to bind into the
            statement.  If :data:`paramstyle` is ``named``, the argument should
            be a dict mapping of parameters.  If the :data:`paramstyle` is
            ``pyformat``, the argument value may be either an array or a
            mapping.

        :param stream: This is a pg8000 extension for use with the PostgreSQL
            `COPY
            <http://www.postgresql.org/docs/current/static/sql-copy.html>`_
            command. For a COPY FROM the parameter must be a readable file-like
            object, and for COPY TO it must be writable.

            .. versionadded:: 1.9.11
        """
        try:
            if not self._c.in_transaction and not self._c.autocommit:
                self._c.execute_unnamed("begin transaction")

            statement, vals = convert_paramstyle(self.paramstyle, operation,
                                                 args)

            self._context = self._c.execute_unnamed(
                statement,
                vals=vals,
                input_oids=self._input_oids,
                stream=stream)

            rows = [] if self._context.rows is None else self._context.rows
            self._row_iter = iter(rows)

            self._input_oids = None
        except AttributeError as e:
            if self._c is None:
                raise InterfaceError("Cursor closed")
            elif self._c._sock is None:
                raise InterfaceError("connection is closed")
            else:
                raise e
        except DatabaseError as e:
            msg = e.args[0]
            if isinstance(msg, dict):
                response_code = msg["C"]

                if response_code == "28000":
                    cls = InterfaceError
                elif response_code == "23505":
                    cls = IntegrityError
                else:
                    cls = ProgrammingError

                raise cls(msg)
            else:
                raise ProgrammingError(msg)

        self.input_types = []
        return self
Exemplo n.º 5
0
def test_format_unchanged(query):
    args = 1, 2, 3
    new_query, vals = convert_paramstyle("format", query, args)
    assert (new_query, vals) == (query, args)
Exemplo n.º 6
0
def test_format_changed(query, expected):
    args = 1, 2, 3
    new_query, vals = convert_paramstyle("format", query, args)
    assert (new_query, vals) == (expected, args)
Exemplo n.º 7
0
def test_numeric_unchanged(query):
    args = 1, 2, 3
    new_query, vals = convert_paramstyle("numeric", query, args)
    assert (new_query, vals) == (query, args)
Exemplo n.º 8
0
def test_numeric(query, expected):
    args = 1, 2, 3
    new_query, vals = convert_paramstyle("numeric", query, args)
    assert (new_query, vals) == (expected, args)
Exemplo n.º 9
0
def test_qmark(query, statement):
    args = 1, 2, 3
    new_query, vals = convert_paramstyle("qmark", query, args)
    assert (new_query, vals) == (statement, args)