static PyObject *
_pydatetime_string_delta(pydatetimeObject *self)
{
    PyDateTime_Delta *obj = (PyDateTime_Delta*)self->wrapped;

    char buffer[8];
    int i;
    int a = obj->microseconds;

    for (i=0; i < 6 ; i++) {
        buffer[5-i] = '0' + (a % 10);
        a /= 10;
    }
    buffer[6] = '\0';

    return Bytes_FromFormat("'%d days %d.%s seconds'::interval",
                            obj->days, obj->seconds, buffer);
}
Beispiel #2
0
static PyObject *
_pydatetime_string_delta(pydatetimeObject *self)
{
    PyDateTime_Delta *obj = (PyDateTime_Delta*)self->wrapped;

    char buffer[8];
    int i;
    int a = PyDateTime_DELTA_GET_MICROSECONDS(obj);

    for (i=0; i < 6 ; i++) {
        buffer[5-i] = '0' + (a % 10);
        a /= 10;
    }
    buffer[6] = '\0';

    return Bytes_FromFormat("'%d days %d.%s seconds'::interval",
                            PyDateTime_DELTA_GET_DAYS(obj),
                            PyDateTime_DELTA_GET_SECONDS(obj),
                            buffer);
}
Beispiel #3
0
static PyObject *
_pydatetime_string_date_time(pydatetimeObject *self)
{
    PyObject *rv = NULL;
    PyObject *iso = NULL;
    PyObject *tz;

    /* Select the right PG type to cast into. */
    char *fmt = NULL;
    switch (self->type) {
    case PSYCO_DATETIME_TIME:
        tz = PyObject_GetAttrString(self->wrapped, "tzinfo");
        if (!tz) { goto error; }
        fmt = (tz == Py_None) ? "'%s'::time" : "'%s'::timetz";
        Py_DECREF(tz);
        break;
    case PSYCO_DATETIME_DATE:
        fmt = "'%s'::date";
        break;
    case PSYCO_DATETIME_TIMESTAMP:
        tz = PyObject_GetAttrString(self->wrapped, "tzinfo");
        if (!tz) { goto error; }
        fmt = (tz == Py_None) ? "'%s'::timestamp" : "'%s'::timestamptz";
        Py_DECREF(tz);
        break;
    }

    if (!(iso = psycopg_ensure_bytes(
            PyObject_CallMethod(self->wrapped, "isoformat", NULL)))) {
        goto error;
    }

    rv = Bytes_FromFormat(fmt, Bytes_AsString(iso));

    Py_DECREF(iso);
    return rv;

error:
    Py_XDECREF(iso);
    return rv;
}
Beispiel #4
0
static PyObject *
binary_quote(binaryObject *self)
{
    char *to = NULL;
    const char *buffer = NULL;
    Py_ssize_t buffer_len;
    size_t len = 0;
    PyObject *rv = NULL;
#if HAS_MEMORYVIEW
    Py_buffer view;
    int got_view = 0;
#endif

    /* if we got a plain string or a buffer we escape it and save the buffer */

#if HAS_MEMORYVIEW
    if (PyObject_CheckBuffer(self->wrapped)) {
        if (0 > PyObject_GetBuffer(self->wrapped, &view, PyBUF_CONTIG_RO)) {
            goto exit;
        }
        got_view = 1;
        buffer = (const char *)(view.buf);
        buffer_len = view.len;
    }
#endif

#if HAS_BUFFER
    if (!buffer && (Bytes_Check(self->wrapped) || PyBuffer_Check(self->wrapped))) {
        if (PyObject_AsReadBuffer(self->wrapped, (const void **)&buffer,
                                  &buffer_len) < 0) {
            goto exit;
        }
    }
#endif

    if (!buffer) {
        goto exit;
    }

    /* escape and build quoted buffer */

    to = (char *)binary_escape((unsigned char*)buffer, (size_t) buffer_len,
        &len, self->conn ? ((connectionObject*)self->conn)->pgconn : NULL);
    if (to == NULL) {
        PyErr_NoMemory();
        goto exit;
    }

    if (len > 0)
        rv = Bytes_FromFormat(
            (self->conn && ((connectionObject*)self->conn)->equote)
                ? "E'%s'::bytea" : "'%s'::bytea" , to);
    else
        rv = Bytes_FromString("''::bytea");

exit:
    if (to) { PQfreemem(to); }
#if HAS_MEMORYVIEW
    if (got_view) { PyBuffer_Release(&view); }
#endif

    /* Allow Binary(None) to work */
    if (self->wrapped == Py_None) {
        Py_INCREF(psyco_null);
        rv = psyco_null;
    }

    /* if the wrapped object is not bytes or a buffer, this is an error */
    if (!rv && !PyErr_Occurred()) {
        PyErr_Format(PyExc_TypeError, "can't escape %s to binary",
            Py_TYPE(self->wrapped)->tp_name);
    }

    return rv;
}