cursor.c.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*[clinic input]
  2. preserve
  3. [clinic start generated code]*/
  4. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  5. # include "pycore_gc.h" // PyGC_Head
  6. # include "pycore_runtime.h" // _Py_ID()
  7. #endif
  8. static int
  9. pysqlite_cursor_init_impl(pysqlite_Cursor *self,
  10. pysqlite_Connection *connection);
  11. static int
  12. pysqlite_cursor_init(PyObject *self, PyObject *args, PyObject *kwargs)
  13. {
  14. int return_value = -1;
  15. PyTypeObject *base_tp = clinic_state()->CursorType;
  16. pysqlite_Connection *connection;
  17. if ((Py_IS_TYPE(self, base_tp) ||
  18. Py_TYPE(self)->tp_new == base_tp->tp_new) &&
  19. !_PyArg_NoKeywords("Cursor", kwargs)) {
  20. goto exit;
  21. }
  22. if (!_PyArg_CheckPositional("Cursor", PyTuple_GET_SIZE(args), 1, 1)) {
  23. goto exit;
  24. }
  25. if (!PyObject_TypeCheck(PyTuple_GET_ITEM(args, 0), clinic_state()->ConnectionType)) {
  26. _PyArg_BadArgument("Cursor", "argument 1", (clinic_state()->ConnectionType)->tp_name, PyTuple_GET_ITEM(args, 0));
  27. goto exit;
  28. }
  29. connection = (pysqlite_Connection *)PyTuple_GET_ITEM(args, 0);
  30. return_value = pysqlite_cursor_init_impl((pysqlite_Cursor *)self, connection);
  31. exit:
  32. return return_value;
  33. }
  34. PyDoc_STRVAR(pysqlite_cursor_execute__doc__,
  35. "execute($self, sql, parameters=(), /)\n"
  36. "--\n"
  37. "\n"
  38. "Executes an SQL statement.");
  39. #define PYSQLITE_CURSOR_EXECUTE_METHODDEF \
  40. {"execute", _PyCFunction_CAST(pysqlite_cursor_execute), METH_FASTCALL, pysqlite_cursor_execute__doc__},
  41. static PyObject *
  42. pysqlite_cursor_execute_impl(pysqlite_Cursor *self, PyObject *sql,
  43. PyObject *parameters);
  44. static PyObject *
  45. pysqlite_cursor_execute(pysqlite_Cursor *self, PyObject *const *args, Py_ssize_t nargs)
  46. {
  47. PyObject *return_value = NULL;
  48. PyObject *sql;
  49. PyObject *parameters = NULL;
  50. if (!_PyArg_CheckPositional("execute", nargs, 1, 2)) {
  51. goto exit;
  52. }
  53. if (!PyUnicode_Check(args[0])) {
  54. _PyArg_BadArgument("execute", "argument 1", "str", args[0]);
  55. goto exit;
  56. }
  57. if (PyUnicode_READY(args[0]) == -1) {
  58. goto exit;
  59. }
  60. sql = args[0];
  61. if (nargs < 2) {
  62. goto skip_optional;
  63. }
  64. parameters = args[1];
  65. skip_optional:
  66. return_value = pysqlite_cursor_execute_impl(self, sql, parameters);
  67. exit:
  68. return return_value;
  69. }
  70. PyDoc_STRVAR(pysqlite_cursor_executemany__doc__,
  71. "executemany($self, sql, seq_of_parameters, /)\n"
  72. "--\n"
  73. "\n"
  74. "Repeatedly executes an SQL statement.");
  75. #define PYSQLITE_CURSOR_EXECUTEMANY_METHODDEF \
  76. {"executemany", _PyCFunction_CAST(pysqlite_cursor_executemany), METH_FASTCALL, pysqlite_cursor_executemany__doc__},
  77. static PyObject *
  78. pysqlite_cursor_executemany_impl(pysqlite_Cursor *self, PyObject *sql,
  79. PyObject *seq_of_parameters);
  80. static PyObject *
  81. pysqlite_cursor_executemany(pysqlite_Cursor *self, PyObject *const *args, Py_ssize_t nargs)
  82. {
  83. PyObject *return_value = NULL;
  84. PyObject *sql;
  85. PyObject *seq_of_parameters;
  86. if (!_PyArg_CheckPositional("executemany", nargs, 2, 2)) {
  87. goto exit;
  88. }
  89. if (!PyUnicode_Check(args[0])) {
  90. _PyArg_BadArgument("executemany", "argument 1", "str", args[0]);
  91. goto exit;
  92. }
  93. if (PyUnicode_READY(args[0]) == -1) {
  94. goto exit;
  95. }
  96. sql = args[0];
  97. seq_of_parameters = args[1];
  98. return_value = pysqlite_cursor_executemany_impl(self, sql, seq_of_parameters);
  99. exit:
  100. return return_value;
  101. }
  102. PyDoc_STRVAR(pysqlite_cursor_executescript__doc__,
  103. "executescript($self, sql_script, /)\n"
  104. "--\n"
  105. "\n"
  106. "Executes multiple SQL statements at once.");
  107. #define PYSQLITE_CURSOR_EXECUTESCRIPT_METHODDEF \
  108. {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_O, pysqlite_cursor_executescript__doc__},
  109. static PyObject *
  110. pysqlite_cursor_executescript_impl(pysqlite_Cursor *self,
  111. const char *sql_script);
  112. static PyObject *
  113. pysqlite_cursor_executescript(pysqlite_Cursor *self, PyObject *arg)
  114. {
  115. PyObject *return_value = NULL;
  116. const char *sql_script;
  117. if (!PyUnicode_Check(arg)) {
  118. _PyArg_BadArgument("executescript", "argument", "str", arg);
  119. goto exit;
  120. }
  121. Py_ssize_t sql_script_length;
  122. sql_script = PyUnicode_AsUTF8AndSize(arg, &sql_script_length);
  123. if (sql_script == NULL) {
  124. goto exit;
  125. }
  126. if (strlen(sql_script) != (size_t)sql_script_length) {
  127. PyErr_SetString(PyExc_ValueError, "embedded null character");
  128. goto exit;
  129. }
  130. return_value = pysqlite_cursor_executescript_impl(self, sql_script);
  131. exit:
  132. return return_value;
  133. }
  134. PyDoc_STRVAR(pysqlite_cursor_fetchone__doc__,
  135. "fetchone($self, /)\n"
  136. "--\n"
  137. "\n"
  138. "Fetches one row from the resultset.");
  139. #define PYSQLITE_CURSOR_FETCHONE_METHODDEF \
  140. {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS, pysqlite_cursor_fetchone__doc__},
  141. static PyObject *
  142. pysqlite_cursor_fetchone_impl(pysqlite_Cursor *self);
  143. static PyObject *
  144. pysqlite_cursor_fetchone(pysqlite_Cursor *self, PyObject *Py_UNUSED(ignored))
  145. {
  146. return pysqlite_cursor_fetchone_impl(self);
  147. }
  148. PyDoc_STRVAR(pysqlite_cursor_fetchmany__doc__,
  149. "fetchmany($self, /, size=1)\n"
  150. "--\n"
  151. "\n"
  152. "Fetches several rows from the resultset.\n"
  153. "\n"
  154. " size\n"
  155. " The default value is set by the Cursor.arraysize attribute.");
  156. #define PYSQLITE_CURSOR_FETCHMANY_METHODDEF \
  157. {"fetchmany", _PyCFunction_CAST(pysqlite_cursor_fetchmany), METH_FASTCALL|METH_KEYWORDS, pysqlite_cursor_fetchmany__doc__},
  158. static PyObject *
  159. pysqlite_cursor_fetchmany_impl(pysqlite_Cursor *self, int maxrows);
  160. static PyObject *
  161. pysqlite_cursor_fetchmany(pysqlite_Cursor *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  162. {
  163. PyObject *return_value = NULL;
  164. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  165. #define NUM_KEYWORDS 1
  166. static struct {
  167. PyGC_Head _this_is_not_used;
  168. PyObject_VAR_HEAD
  169. PyObject *ob_item[NUM_KEYWORDS];
  170. } _kwtuple = {
  171. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  172. .ob_item = { &_Py_ID(size), },
  173. };
  174. #undef NUM_KEYWORDS
  175. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  176. #else // !Py_BUILD_CORE
  177. # define KWTUPLE NULL
  178. #endif // !Py_BUILD_CORE
  179. static const char * const _keywords[] = {"size", NULL};
  180. static _PyArg_Parser _parser = {
  181. .keywords = _keywords,
  182. .fname = "fetchmany",
  183. .kwtuple = KWTUPLE,
  184. };
  185. #undef KWTUPLE
  186. PyObject *argsbuf[1];
  187. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  188. int maxrows = self->arraysize;
  189. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
  190. if (!args) {
  191. goto exit;
  192. }
  193. if (!noptargs) {
  194. goto skip_optional_pos;
  195. }
  196. maxrows = _PyLong_AsInt(args[0]);
  197. if (maxrows == -1 && PyErr_Occurred()) {
  198. goto exit;
  199. }
  200. skip_optional_pos:
  201. return_value = pysqlite_cursor_fetchmany_impl(self, maxrows);
  202. exit:
  203. return return_value;
  204. }
  205. PyDoc_STRVAR(pysqlite_cursor_fetchall__doc__,
  206. "fetchall($self, /)\n"
  207. "--\n"
  208. "\n"
  209. "Fetches all rows from the resultset.");
  210. #define PYSQLITE_CURSOR_FETCHALL_METHODDEF \
  211. {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS, pysqlite_cursor_fetchall__doc__},
  212. static PyObject *
  213. pysqlite_cursor_fetchall_impl(pysqlite_Cursor *self);
  214. static PyObject *
  215. pysqlite_cursor_fetchall(pysqlite_Cursor *self, PyObject *Py_UNUSED(ignored))
  216. {
  217. return pysqlite_cursor_fetchall_impl(self);
  218. }
  219. PyDoc_STRVAR(pysqlite_cursor_setinputsizes__doc__,
  220. "setinputsizes($self, sizes, /)\n"
  221. "--\n"
  222. "\n"
  223. "Required by DB-API. Does nothing in sqlite3.");
  224. #define PYSQLITE_CURSOR_SETINPUTSIZES_METHODDEF \
  225. {"setinputsizes", (PyCFunction)pysqlite_cursor_setinputsizes, METH_O, pysqlite_cursor_setinputsizes__doc__},
  226. PyDoc_STRVAR(pysqlite_cursor_setoutputsize__doc__,
  227. "setoutputsize($self, size, column=None, /)\n"
  228. "--\n"
  229. "\n"
  230. "Required by DB-API. Does nothing in sqlite3.");
  231. #define PYSQLITE_CURSOR_SETOUTPUTSIZE_METHODDEF \
  232. {"setoutputsize", _PyCFunction_CAST(pysqlite_cursor_setoutputsize), METH_FASTCALL, pysqlite_cursor_setoutputsize__doc__},
  233. static PyObject *
  234. pysqlite_cursor_setoutputsize_impl(pysqlite_Cursor *self, PyObject *size,
  235. PyObject *column);
  236. static PyObject *
  237. pysqlite_cursor_setoutputsize(pysqlite_Cursor *self, PyObject *const *args, Py_ssize_t nargs)
  238. {
  239. PyObject *return_value = NULL;
  240. PyObject *size;
  241. PyObject *column = Py_None;
  242. if (!_PyArg_CheckPositional("setoutputsize", nargs, 1, 2)) {
  243. goto exit;
  244. }
  245. size = args[0];
  246. if (nargs < 2) {
  247. goto skip_optional;
  248. }
  249. column = args[1];
  250. skip_optional:
  251. return_value = pysqlite_cursor_setoutputsize_impl(self, size, column);
  252. exit:
  253. return return_value;
  254. }
  255. PyDoc_STRVAR(pysqlite_cursor_close__doc__,
  256. "close($self, /)\n"
  257. "--\n"
  258. "\n"
  259. "Closes the cursor.");
  260. #define PYSQLITE_CURSOR_CLOSE_METHODDEF \
  261. {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS, pysqlite_cursor_close__doc__},
  262. static PyObject *
  263. pysqlite_cursor_close_impl(pysqlite_Cursor *self);
  264. static PyObject *
  265. pysqlite_cursor_close(pysqlite_Cursor *self, PyObject *Py_UNUSED(ignored))
  266. {
  267. return pysqlite_cursor_close_impl(self);
  268. }
  269. /*[clinic end generated code: output=1f82e3c9791bb9a5 input=a9049054013a1b77]*/