/* Performs very basic validation on an incoming SQL string. * Returns a new reference to a str instance on success; NULL on failure, * after having set an exception. */ PyObject * curs_validate_sql_basic(cursorObject *self, PyObject *sql) { PyObject *rv = NULL; PyObject *comp = NULL; int iscomp; if (!sql || !PyObject_IsTrue(sql)) { psyco_set_error(ProgrammingError, self, "can't execute an empty query"); goto exit; } if (Bytes_Check(sql)) { /* Necessary for ref-count symmetry with the unicode case: */ Py_INCREF(sql); rv = sql; } else if (PyUnicode_Check(sql)) { if (!(rv = conn_encode(self->conn, sql))) { goto exit; } } else if (0 != (iscomp = _curs_is_composible(sql))) { if (iscomp < 0) { goto exit; } if (!(comp = PyObject_CallMethod(sql, "as_string", "O", self->conn))) { goto exit; } if (Bytes_Check(comp)) { rv = comp; comp = NULL; } else if (PyUnicode_Check(comp)) { if (!(rv = conn_encode(self->conn, comp))) { goto exit; } } else { PyErr_Format(PyExc_TypeError, "as_string() should return a string: got %s instead", Py_TYPE(comp)->tp_name); goto exit; } } else { /* the is not unicode or string, raise an error */ PyErr_Format(PyExc_TypeError, "argument 1 must be a string or unicode object: got %s instead", Py_TYPE(sql)->tp_name); goto exit; } exit: Py_XDECREF(comp); return rv; }
PyObject * microprotocol_getquoted(PyObject *obj, connectionObject *conn) { PyObject *res = NULL; PyObject *prepare = NULL; PyObject *adapted; if (!(adapted = microprotocols_adapt(obj, (PyObject*)&isqlquoteType, NULL))) { goto exit; } Dprintf("microprotocol_getquoted: adapted to %s", Py_TYPE(adapted)->tp_name); /* if requested prepare the object passing it the connection */ if (conn) { if ((prepare = PyObject_GetAttrString(adapted, "prepare"))) { res = PyObject_CallFunctionObjArgs( prepare, (PyObject *)conn, NULL); if (res) { Py_DECREF(res); res = NULL; } else { goto exit; } } else { /* adapted.prepare not found */ PyErr_Clear(); } } /* call the getquoted method on adapted (that should exist because we adapted to the right protocol) */ res = PyObject_CallMethod(adapted, "getquoted", NULL); /* Convert to bytes. */ if (res && PyUnicode_CheckExact(res)) { PyObject *b; b = conn_encode(conn, res); Py_DECREF(res); res = b; } exit: Py_XDECREF(adapted); Py_XDECREF(prepare); /* we return res with one extra reference, the caller shall free it */ return res; }