static PyObject * psyco_conn_set_isolation_level(connectionObject *self, PyObject *args) { int level = 1; EXC_IF_CONN_CLOSED(self); EXC_IF_CONN_ASYNC(self, set_isolation_level); EXC_IF_TPC_PREPARED(self, set_isolation_level); if (!PyArg_ParseTuple(args, "i", &level)) return NULL; if (level < 0 || level > 4) { PyErr_SetString(PyExc_ValueError, "isolation level must be between 0 and 4"); return NULL; } if (conn_switch_isolation_level(self, level) < 0) { return NULL; } Py_INCREF(Py_None); return Py_None; }
static PyObject * psyco_conn_tpc_prepare(connectionObject *self, PyObject *args) { EXC_IF_CONN_CLOSED(self); EXC_IF_CONN_ASYNC(self, tpc_prepare); EXC_IF_TPC_PREPARED(self, tpc_prepare); if (NULL == self->tpc_xid) { PyErr_SetString(ProgrammingError, "prepare must be called inside a two-phase transaction"); return NULL; } if (0 > conn_tpc_command(self, "PREPARE TRANSACTION", self->tpc_xid)) { return NULL; } /* transaction prepared: set the state so that no operation * can be performed until commit. */ self->status = CONN_STATUS_PREPARED; Py_INCREF(Py_None); return Py_None; }
static PyObject * psyco_conn_cancel(connectionObject *self) { char errbuf[256]; EXC_IF_CONN_CLOSED(self); EXC_IF_TPC_PREPARED(self, cancel); /* do not allow cancellation while the connection is being built */ Dprintf("psyco_conn_cancel: cancelling with key %p", self->cancel); if (self->status != CONN_STATUS_READY && self->status != CONN_STATUS_BEGIN) { PyErr_SetString(OperationalError, "asynchronous connection attempt underway"); return NULL; } if (PQcancel(self->cancel, errbuf, sizeof(errbuf)) == 0) { Dprintf("psyco_conn_cancel: cancelling failed: %s", errbuf); PyErr_SetString(OperationalError, errbuf); return NULL; } Py_RETURN_NONE; }
static PyObject * psyco_conn_cursor(connectionObject *self, PyObject *args, PyObject *kwargs) { PyObject *obj = NULL; PyObject *rv = NULL; PyObject *name = Py_None; PyObject *factory = Py_None; PyObject *withhold = Py_False; PyObject *scrollable = Py_None; static char *kwlist[] = { "name", "cursor_factory", "withhold", "scrollable", NULL}; EXC_IF_CONN_CLOSED(self); if (!PyArg_ParseTupleAndKeywords( args, kwargs, "|OOOO", kwlist, &name, &factory, &withhold, &scrollable)) { goto exit; } if (factory == Py_None) { if (self->cursor_factory && self->cursor_factory != Py_None) { factory = self->cursor_factory; } else { factory = (PyObject *)&cursorType; } } if (self->status != CONN_STATUS_READY && self->status != CONN_STATUS_BEGIN && self->status != CONN_STATUS_PREPARED) { PyErr_SetString(OperationalError, "asynchronous connection attempt underway"); goto exit; } if (name != Py_None && self->async == 1) { PyErr_SetString(ProgrammingError, "asynchronous connections " "cannot produce named cursors"); goto exit; } Dprintf("psyco_conn_cursor: new %s cursor for connection at %p", (name == Py_None ? "unnamed" : "named"), self); if (!(obj = PyObject_CallFunctionObjArgs(factory, self, name, NULL))) { goto exit; } if (PyObject_IsInstance(obj, (PyObject *)&cursorType) == 0) { PyErr_SetString(PyExc_TypeError, "cursor factory must be subclass of psycopg2.extensions.cursor"); goto exit; } if (0 != psyco_curs_withhold_set((cursorObject *)obj, withhold)) { goto exit; } if (0 != psyco_curs_scrollable_set((cursorObject *)obj, scrollable)) { goto exit; } Dprintf("psyco_conn_cursor: new cursor at %p: refcnt = " FORMAT_CODE_PY_SSIZE_T, obj, Py_REFCNT(obj) ); rv = obj; obj = NULL; exit: Py_XDECREF(obj); return rv; }
static PyObject * psyco_conn_lobject(connectionObject *self, PyObject *args, PyObject *keywds) { Oid oid=InvalidOid, new_oid=InvalidOid; char *smode = NULL, *new_file = NULL; int mode=0; PyObject *obj, *factory = NULL; static char *kwlist[] = {"oid", "mode", "new_oid", "new_file", "cursor_factory", NULL}; if (!PyArg_ParseTupleAndKeywords(args, keywds, "|izizO", kwlist, &oid, &smode, &new_oid, &new_file, &factory)) { return NULL; } EXC_IF_CONN_CLOSED(self); Dprintf("psyco_conn_lobject: new lobject for connection at %p", self); Dprintf("psyco_conn_lobject: parameters: oid = %d, mode = %s", oid, smode); Dprintf("psyco_conn_lobject: parameters: new_oid = %d, new_file = %s", new_oid, new_file); /* build a mode number out of the mode string: right now we only accept 'r', 'w' and 'rw' (but note that 'w' implies 'rw' because PostgreSQL backend does that. */ if (smode) { if (strncmp("rw", smode, 2) == 0) mode = INV_READ+INV_WRITE; else if (smode[0] == 'r') mode = INV_READ; else if (smode[0] == 'w') mode = INV_WRITE; else if (smode[0] == 'n') mode = -1; else { PyErr_SetString(PyExc_TypeError, "mode should be one of 'r', 'w' or 'rw'"); return NULL; } } if (factory == NULL) factory = (PyObject *)&lobjectType; if (new_file) obj = PyObject_CallFunction(factory, "Oiiis", self, oid, mode, new_oid, new_file); else obj = PyObject_CallFunction(factory, "Oiii", self, oid, mode, new_oid); if (obj == NULL) return NULL; if (PyObject_IsInstance(obj, (PyObject *)&lobjectType) == 0) { PyErr_SetString(PyExc_TypeError, "lobject factory must be subclass of psycoev._psycoev.lobject"); Py_DECREF(obj); return NULL; } Dprintf("psyco_conn_lobject: new lobject at %p: refcnt = " FORMAT_CODE_PY_SSIZE_T, obj, obj->ob_refcnt); return obj; }
static PyObject * psyco_conn_cursor(connectionObject *self, PyObject *args, PyObject *kwargs) { PyObject *obj; PyObject *name = Py_None; PyObject *factory = (PyObject *)&cursorType; PyObject *withhold = Py_False; static char *kwlist[] = {"name", "cursor_factory", "withhold", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOO", kwlist, &name, &factory, &withhold)) { return NULL; } if (PyObject_IsTrue(withhold) && (name == Py_None)) { PyErr_SetString(ProgrammingError, "'withhold=True can be specified only for named cursors"); return NULL; } EXC_IF_CONN_CLOSED(self); if (self->status != CONN_STATUS_READY && self->status != CONN_STATUS_BEGIN && self->status != CONN_STATUS_PREPARED) { PyErr_SetString(OperationalError, "asynchronous connection attempt underway"); return NULL; } if (name != Py_None && self->async == 1) { PyErr_SetString(ProgrammingError, "asynchronous connections " "cannot produce named cursors"); return NULL; } Dprintf("psyco_conn_cursor: new %s cursor for connection at %p", (name == Py_None ? "unnamed" : "named"), self); if (!(obj = PyObject_CallFunctionObjArgs(factory, self, name, NULL))) { return NULL; } if (PyObject_IsInstance(obj, (PyObject *)&cursorType) == 0) { PyErr_SetString(PyExc_TypeError, "cursor factory must be subclass of psycopg2._psycopg.cursor"); Py_DECREF(obj); return NULL; } if (PyObject_IsTrue(withhold)) ((cursorObject*)obj)->withhold = 1; Dprintf("psyco_conn_cursor: new cursor at %p: refcnt = " FORMAT_CODE_PY_SSIZE_T, obj, Py_REFCNT(obj) ); return obj; }