_mysql.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894
  1. /*
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2, or (at your option)
  5. any later version. Alternatively, you may use the original license
  6. reproduced below.
  7. Copyright 1999 by Comstar.net, Inc., Atlanta, GA, US.
  8. All Rights Reserved
  9. Permission to use, copy, modify, and distribute this software and its
  10. documentation for any purpose and without fee is hereby granted,
  11. provided that the above copyright notice appear in all copies and that
  12. both that copyright notice and this permission notice appear in
  13. supporting documentation, and that the name of Comstar.net, Inc.
  14. or COMSTAR not be used in advertising or publicity pertaining to
  15. distribution of the software without specific, written prior permission.
  16. COMSTAR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  17. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  18. EVENT SHALL COMSTAR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  19. CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  20. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  21. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  22. PERFORMANCE OF THIS SOFTWARE.
  23. */
  24. #include "pymemcompat.h"
  25. #include "structmember.h"
  26. #if defined(MS_WINDOWS)
  27. #include <winsock2.h>
  28. #include <windows.h>
  29. #include <config-win.h>
  30. #else
  31. #include "my_config.h"
  32. #endif
  33. #include "mysql.h"
  34. #include "mysqld_error.h"
  35. #include "errmsg.h"
  36. #if PY_VERSION_HEX < 0x02020000
  37. # define MyTuple_Resize(t,n,d) _PyTuple_Resize(t, n, d)
  38. # define MyMember(a,b,c,d,e) {a,b,c,d}
  39. # define MyMemberlist(x) struct memberlist x
  40. # define MyAlloc(s,t) PyObject_New(s,&t)
  41. # define MyFree(o) PyObject_Del(o)
  42. #else
  43. # define MyTuple_Resize(t,n,d) _PyTuple_Resize(t, n)
  44. # define MyMember(a,b,c,d,e) {a,b,c,d,e}
  45. # define MyMemberlist(x) struct PyMemberDef x
  46. # define MyAlloc(s,t) (s *) t.tp_alloc(&t,0)
  47. # define MyFree(ob) ob->ob_type->tp_free((PyObject *)ob)
  48. #endif
  49. #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
  50. typedef int Py_ssize_t;
  51. #define PY_SSIZE_T_MAX INT_MAX
  52. #define PY_SSIZE_T_MIN INT_MIN
  53. #endif
  54. static PyObject *_mysql_MySQLError;
  55. static PyObject *_mysql_Warning;
  56. static PyObject *_mysql_Error;
  57. static PyObject *_mysql_DatabaseError;
  58. static PyObject *_mysql_InterfaceError;
  59. static PyObject *_mysql_DataError;
  60. static PyObject *_mysql_OperationalError;
  61. static PyObject *_mysql_IntegrityError;
  62. static PyObject *_mysql_InternalError;
  63. static PyObject *_mysql_ProgrammingError;
  64. static PyObject *_mysql_NotSupportedError;
  65. typedef struct {
  66. PyObject_HEAD
  67. MYSQL connection;
  68. int open;
  69. PyObject *converter;
  70. } _mysql_ConnectionObject;
  71. #define check_connection(c) if (!(c->open)) return _mysql_Exception(c)
  72. #define result_connection(r) ((_mysql_ConnectionObject *)r->conn)
  73. #define check_result_connection(r) check_connection(result_connection(r))
  74. extern PyTypeObject _mysql_ConnectionObject_Type;
  75. typedef struct {
  76. PyObject_HEAD
  77. PyObject *conn;
  78. MYSQL_RES *result;
  79. int nfields;
  80. int use;
  81. PyObject *converter;
  82. } _mysql_ResultObject;
  83. extern PyTypeObject _mysql_ResultObject_Type;
  84. static int _mysql_server_init_done = 0;
  85. #if MYSQL_VERSION_ID >= 40000
  86. #define check_server_init(x) if (!_mysql_server_init_done) { if (mysql_server_init(0, NULL, NULL)) { _mysql_Exception(NULL); return x; } else { _mysql_server_init_done = 1;} }
  87. #else
  88. #define check_server_init(x) if (!_mysql_server_init_done) _mysql_server_init_done = 1
  89. #endif
  90. #if MYSQL_VERSION_ID >= 50500
  91. #define HAVE_OPENSSL 1
  92. #endif
  93. PyObject *
  94. _mysql_Exception(_mysql_ConnectionObject *c)
  95. {
  96. PyObject *t, *e;
  97. int merr;
  98. if (!(t = PyTuple_New(2))) return NULL;
  99. if (!_mysql_server_init_done) {
  100. e = _mysql_InternalError;
  101. PyTuple_SET_ITEM(t, 0, PyInt_FromLong(-1L));
  102. PyTuple_SET_ITEM(t, 1, PyString_FromString("server not initialized"));
  103. PyErr_SetObject(e, t);
  104. Py_DECREF(t);
  105. return NULL;
  106. }
  107. merr = mysql_errno(&(c->connection));
  108. if (!merr)
  109. e = _mysql_InterfaceError;
  110. else if (merr > CR_MAX_ERROR) {
  111. PyTuple_SET_ITEM(t, 0, PyInt_FromLong(-1L));
  112. PyTuple_SET_ITEM(t, 1, PyString_FromString("error totally whack"));
  113. PyErr_SetObject(_mysql_InterfaceError, t);
  114. Py_DECREF(t);
  115. return NULL;
  116. }
  117. else switch (merr) {
  118. case CR_COMMANDS_OUT_OF_SYNC:
  119. case ER_DB_CREATE_EXISTS:
  120. case ER_SYNTAX_ERROR:
  121. case ER_PARSE_ERROR:
  122. case ER_NO_SUCH_TABLE:
  123. case ER_WRONG_DB_NAME:
  124. case ER_WRONG_TABLE_NAME:
  125. case ER_FIELD_SPECIFIED_TWICE:
  126. case ER_INVALID_GROUP_FUNC_USE:
  127. case ER_UNSUPPORTED_EXTENSION:
  128. case ER_TABLE_MUST_HAVE_COLUMNS:
  129. #ifdef ER_CANT_DO_THIS_DURING_AN_TRANSACTION
  130. case ER_CANT_DO_THIS_DURING_AN_TRANSACTION:
  131. #endif
  132. e = _mysql_ProgrammingError;
  133. break;
  134. #ifdef WARN_DATA_TRUNCATED
  135. case WARN_DATA_TRUNCATED:
  136. #ifdef WARN_NULL_TO_NOTNULL
  137. case WARN_NULL_TO_NOTNULL:
  138. #endif
  139. #ifdef ER_WARN_DATA_OUT_OF_RANGE
  140. case ER_WARN_DATA_OUT_OF_RANGE:
  141. #endif
  142. #ifdef ER_NO_DEFAULT
  143. case ER_NO_DEFAULT:
  144. #endif
  145. #ifdef ER_PRIMARY_CANT_HAVE_NULL
  146. case ER_PRIMARY_CANT_HAVE_NULL:
  147. #endif
  148. #ifdef ER_DATA_TOO_LONG
  149. case ER_DATA_TOO_LONG:
  150. #endif
  151. #ifdef ER_DATETIME_FUNCTION_OVERFLOW
  152. case ER_DATETIME_FUNCTION_OVERFLOW:
  153. #endif
  154. e = _mysql_DataError;
  155. break;
  156. #endif
  157. case ER_DUP_ENTRY:
  158. #ifdef ER_DUP_UNIQUE
  159. case ER_DUP_UNIQUE:
  160. #endif
  161. #ifdef ER_NO_REFERENCED_ROW
  162. case ER_NO_REFERENCED_ROW:
  163. #endif
  164. #ifdef ER_NO_REFERENCED_ROW_2
  165. case ER_NO_REFERENCED_ROW_2:
  166. #endif
  167. #ifdef ER_ROW_IS_REFERENCED
  168. case ER_ROW_IS_REFERENCED:
  169. #endif
  170. #ifdef ER_ROW_IS_REFERENCED_2
  171. case ER_ROW_IS_REFERENCED_2:
  172. #endif
  173. #ifdef ER_CANNOT_ADD_FOREIGN
  174. case ER_CANNOT_ADD_FOREIGN:
  175. #endif
  176. e = _mysql_IntegrityError;
  177. break;
  178. #ifdef ER_WARNING_NOT_COMPLETE_ROLLBACK
  179. case ER_WARNING_NOT_COMPLETE_ROLLBACK:
  180. #endif
  181. #ifdef ER_NOT_SUPPORTED_YET
  182. case ER_NOT_SUPPORTED_YET:
  183. #endif
  184. #ifdef ER_FEATURE_DISABLED
  185. case ER_FEATURE_DISABLED:
  186. #endif
  187. #ifdef ER_UNKNOWN_STORAGE_ENGINE
  188. case ER_UNKNOWN_STORAGE_ENGINE:
  189. #endif
  190. e = _mysql_NotSupportedError;
  191. break;
  192. default:
  193. if (merr < 1000)
  194. e = _mysql_InternalError;
  195. else
  196. e = _mysql_OperationalError;
  197. break;
  198. }
  199. PyTuple_SET_ITEM(t, 0, PyInt_FromLong((long)merr));
  200. PyTuple_SET_ITEM(t, 1, PyString_FromString(mysql_error(&(c->connection))));
  201. PyErr_SetObject(e, t);
  202. Py_DECREF(t);
  203. return NULL;
  204. }
  205. static char _mysql_server_init__doc__[] =
  206. "Initialize embedded server. If this client is not linked against\n\
  207. the embedded server library, this function does nothing.\n\
  208. \n\
  209. args -- sequence of command-line arguments\n\
  210. groups -- sequence of groups to use in defaults files\n\
  211. ";
  212. static PyObject *_mysql_server_init(
  213. PyObject *self,
  214. PyObject *args,
  215. PyObject *kwargs) {
  216. static char *kwlist[] = {"args", "groups", NULL};
  217. char **cmd_args_c=NULL, **groups_c=NULL, *s;
  218. int cmd_argc=0, i, groupc;
  219. PyObject *cmd_args=NULL, *groups=NULL, *ret=NULL, *item;
  220. if (_mysql_server_init_done) {
  221. PyErr_SetString(_mysql_ProgrammingError,
  222. "already initialized");
  223. return NULL;
  224. }
  225. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO", kwlist,
  226. &cmd_args, &groups))
  227. return NULL;
  228. #if MYSQL_VERSION_ID >= 40000
  229. if (cmd_args) {
  230. if (!PySequence_Check(cmd_args)) {
  231. PyErr_SetString(PyExc_TypeError,
  232. "args must be a sequence");
  233. goto finish;
  234. }
  235. cmd_argc = PySequence_Size(cmd_args);
  236. if (cmd_argc == -1) {
  237. PyErr_SetString(PyExc_TypeError,
  238. "args could not be sized");
  239. goto finish;
  240. }
  241. cmd_args_c = (char **) PyMem_Malloc(cmd_argc*sizeof(char *));
  242. for (i=0; i< cmd_argc; i++) {
  243. item = PySequence_GetItem(cmd_args, i);
  244. s = PyString_AsString(item);
  245. Py_DECREF(item);
  246. if (!s) {
  247. PyErr_SetString(PyExc_TypeError,
  248. "args must contain strings");
  249. goto finish;
  250. }
  251. cmd_args_c[i] = s;
  252. }
  253. }
  254. if (groups) {
  255. if (!PySequence_Check(groups)) {
  256. PyErr_SetString(PyExc_TypeError,
  257. "groups must be a sequence");
  258. goto finish;
  259. }
  260. groupc = PySequence_Size(groups);
  261. if (groupc == -1) {
  262. PyErr_SetString(PyExc_TypeError,
  263. "groups could not be sized");
  264. goto finish;
  265. }
  266. groups_c = (char **) PyMem_Malloc((1+groupc)*sizeof(char *));
  267. for (i=0; i< groupc; i++) {
  268. item = PySequence_GetItem(groups, i);
  269. s = PyString_AsString(item);
  270. Py_DECREF(item);
  271. if (!s) {
  272. PyErr_SetString(PyExc_TypeError,
  273. "groups must contain strings");
  274. goto finish;
  275. }
  276. groups_c[i] = s;
  277. }
  278. groups_c[groupc] = (char *)NULL;
  279. }
  280. /* even though this may block, don't give up the interpreter lock
  281. so that the server can't be initialized multiple times. */
  282. if (mysql_server_init(cmd_argc, cmd_args_c, groups_c)) {
  283. _mysql_Exception(NULL);
  284. goto finish;
  285. }
  286. #endif
  287. ret = Py_None;
  288. Py_INCREF(Py_None);
  289. _mysql_server_init_done = 1;
  290. finish:
  291. PyMem_Free(groups_c);
  292. PyMem_Free(cmd_args_c);
  293. return ret;
  294. }
  295. static char _mysql_server_end__doc__[] =
  296. "Shut down embedded server. If not using an embedded server, this\n\
  297. does nothing.";
  298. static PyObject *_mysql_server_end(
  299. PyObject *self,
  300. PyObject *args) {
  301. if (_mysql_server_init_done) {
  302. #if MYSQL_VERSION_ID >= 40000
  303. mysql_server_end();
  304. #endif
  305. _mysql_server_init_done = 0;
  306. Py_INCREF(Py_None);
  307. return Py_None;
  308. }
  309. return _mysql_Exception(NULL);
  310. }
  311. #if MYSQL_VERSION_ID >= 32314
  312. static char _mysql_thread_safe__doc__[] =
  313. "Indicates whether the client is compiled as thread-safe.";
  314. static PyObject *_mysql_thread_safe(
  315. PyObject *self,
  316. PyObject *args) {
  317. PyObject *flag;
  318. if (!PyArg_ParseTuple(args, "")) return NULL;
  319. check_server_init(NULL);
  320. if (!(flag=PyInt_FromLong((long)mysql_thread_safe()))) return NULL;
  321. return flag;
  322. }
  323. #endif
  324. static char _mysql_ResultObject__doc__[] =
  325. "result(connection, use=0, converter={}) -- Result set from a query.\n\
  326. \n\
  327. Creating instances of this class directly is an excellent way to\n\
  328. shoot yourself in the foot. If using _mysql.connection directly,\n\
  329. use connection.store_result() or connection.use_result() instead.\n\
  330. If using MySQLdb.Connection, this is done by the cursor class.\n\
  331. Just forget you ever saw this. Forget... FOR-GET...";
  332. static int
  333. _mysql_ResultObject_Initialize(
  334. _mysql_ResultObject *self,
  335. PyObject *args,
  336. PyObject *kwargs)
  337. {
  338. static char *kwlist[] = {"connection", "use", "converter", NULL};
  339. MYSQL_RES *result;
  340. _mysql_ConnectionObject *conn=NULL;
  341. int use=0;
  342. PyObject *conv=NULL;
  343. int n, i;
  344. MYSQL_FIELD *fields;
  345. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|iO", kwlist,
  346. &conn, &use, &conv))
  347. return -1;
  348. if (!conv) conv = PyDict_New();
  349. if (!conv) return -1;
  350. self->conn = (PyObject *) conn;
  351. Py_INCREF(conn);
  352. self->use = use;
  353. Py_BEGIN_ALLOW_THREADS ;
  354. if (use)
  355. result = mysql_use_result(&(conn->connection));
  356. else
  357. result = mysql_store_result(&(conn->connection));
  358. self->result = result;
  359. Py_END_ALLOW_THREADS ;
  360. if (!result) {
  361. self->converter = PyTuple_New(0);
  362. return 0;
  363. }
  364. n = mysql_num_fields(result);
  365. self->nfields = n;
  366. if (!(self->converter = PyTuple_New(n))) return -1;
  367. fields = mysql_fetch_fields(result);
  368. for (i=0; i<n; i++) {
  369. PyObject *tmp, *fun;
  370. tmp = PyInt_FromLong((long) fields[i].type);
  371. if (!tmp) return -1;
  372. fun = PyObject_GetItem(conv, tmp);
  373. Py_DECREF(tmp);
  374. if (!fun) {
  375. PyErr_Clear();
  376. fun = Py_None;
  377. Py_INCREF(Py_None);
  378. }
  379. if (PySequence_Check(fun)) {
  380. int j, n2=PySequence_Size(fun);
  381. PyObject *fun2=NULL;
  382. for (j=0; j<n2; j++) {
  383. PyObject *t = PySequence_GetItem(fun, j);
  384. if (!t) continue;
  385. if (!PyTuple_Check(t)) goto cleanup;
  386. if (PyTuple_GET_SIZE(t) == 2) {
  387. long mask;
  388. PyObject *pmask=NULL;
  389. pmask = PyTuple_GET_ITEM(t, 0);
  390. fun2 = PyTuple_GET_ITEM(t, 1);
  391. if (PyInt_Check(pmask)) {
  392. mask = PyInt_AS_LONG(pmask);
  393. if (mask & fields[i].flags) {
  394. Py_DECREF(t);
  395. break;
  396. }
  397. else {
  398. goto cleanup;
  399. }
  400. } else {
  401. Py_DECREF(t);
  402. break;
  403. }
  404. }
  405. cleanup:
  406. Py_DECREF(t);
  407. }
  408. if (!fun2) fun2 = Py_None;
  409. Py_INCREF(fun2);
  410. Py_DECREF(fun);
  411. fun = fun2;
  412. }
  413. PyTuple_SET_ITEM(self->converter, i, fun);
  414. }
  415. return 0;
  416. }
  417. #if PY_VERSION_HEX >= 0x02020000
  418. static int _mysql_ResultObject_traverse(
  419. _mysql_ResultObject *self,
  420. visitproc visit,
  421. void *arg)
  422. {
  423. int r;
  424. if (self->converter) {
  425. if (!(r = visit(self->converter, arg))) return r;
  426. }
  427. if (self->conn)
  428. return visit(self->conn, arg);
  429. return 0;
  430. }
  431. #endif
  432. static int _mysql_ResultObject_clear(
  433. _mysql_ResultObject *self)
  434. {
  435. Py_XDECREF(self->converter);
  436. self->converter = NULL;
  437. Py_XDECREF(self->conn);
  438. self->conn = NULL;
  439. return 0;
  440. }
  441. static int
  442. _mysql_ConnectionObject_Initialize(
  443. _mysql_ConnectionObject *self,
  444. PyObject *args,
  445. PyObject *kwargs)
  446. {
  447. MYSQL *conn = NULL;
  448. PyObject *conv = NULL;
  449. PyObject *ssl = NULL;
  450. #if HAVE_OPENSSL
  451. char *key = NULL, *cert = NULL, *ca = NULL,
  452. *capath = NULL, *cipher = NULL;
  453. #endif
  454. char *host = NULL, *user = NULL, *passwd = NULL,
  455. *db = NULL, *unix_socket = NULL;
  456. unsigned int port = 0;
  457. unsigned int client_flag = 0;
  458. static char *kwlist[] = { "host", "user", "passwd", "db", "port",
  459. "unix_socket", "conv",
  460. "connect_timeout", "compress",
  461. "named_pipe", "init_command",
  462. "read_default_file", "read_default_group",
  463. "client_flag", "ssl",
  464. "local_infile",
  465. NULL } ;
  466. int connect_timeout = 0;
  467. int compress = -1, named_pipe = -1, local_infile = -1;
  468. char *init_command=NULL,
  469. *read_default_file=NULL,
  470. *read_default_group=NULL;
  471. self->converter = NULL;
  472. self->open = 0;
  473. check_server_init(-1);
  474. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ssssisOiiisssiOi:connect",
  475. kwlist,
  476. &host, &user, &passwd, &db,
  477. &port, &unix_socket, &conv,
  478. &connect_timeout,
  479. &compress, &named_pipe,
  480. &init_command, &read_default_file,
  481. &read_default_group,
  482. &client_flag, &ssl,
  483. &local_infile /* DO NOT PATCH FOR RECONNECT, IDIOTS
  484. IF YOU DO THIS, I WILL NOT SUPPORT YOUR PACKAGES. */
  485. ))
  486. return -1;
  487. if (!conv)
  488. conv = PyDict_New();
  489. #if PY_VERSION_HEX > 0x02000100
  490. else
  491. Py_INCREF(conv);
  492. #endif
  493. if (!conv)
  494. return -1;
  495. self->converter = conv;
  496. #define _stringsuck(d,t,s) {t=PyMapping_GetItemString(s,#d);\
  497. if(t){d=PyString_AsString(t);Py_DECREF(t);}\
  498. PyErr_Clear();}
  499. if (ssl) {
  500. #if HAVE_OPENSSL
  501. PyObject *value = NULL;
  502. _stringsuck(ca, value, ssl);
  503. _stringsuck(capath, value, ssl);
  504. _stringsuck(cert, value, ssl);
  505. _stringsuck(key, value, ssl);
  506. _stringsuck(cipher, value, ssl);
  507. #else
  508. PyErr_SetString(_mysql_NotSupportedError,
  509. "client library does not have SSL support");
  510. return -1;
  511. #endif
  512. }
  513. Py_BEGIN_ALLOW_THREADS ;
  514. conn = mysql_init(&(self->connection));
  515. if (connect_timeout) {
  516. unsigned int timeout = connect_timeout;
  517. mysql_options(&(self->connection), MYSQL_OPT_CONNECT_TIMEOUT,
  518. (char *)&timeout);
  519. }
  520. if (compress != -1) {
  521. mysql_options(&(self->connection), MYSQL_OPT_COMPRESS, 0);
  522. client_flag |= CLIENT_COMPRESS;
  523. }
  524. if (named_pipe != -1)
  525. mysql_options(&(self->connection), MYSQL_OPT_NAMED_PIPE, 0);
  526. if (init_command != NULL)
  527. mysql_options(&(self->connection), MYSQL_INIT_COMMAND, init_command);
  528. if (read_default_file != NULL)
  529. mysql_options(&(self->connection), MYSQL_READ_DEFAULT_FILE, read_default_file);
  530. if (read_default_group != NULL)
  531. mysql_options(&(self->connection), MYSQL_READ_DEFAULT_GROUP, read_default_group);
  532. if (local_infile != -1)
  533. mysql_options(&(self->connection), MYSQL_OPT_LOCAL_INFILE, (char *) &local_infile);
  534. #if HAVE_OPENSSL
  535. if (ssl)
  536. mysql_ssl_set(&(self->connection),
  537. key, cert, ca, capath, cipher);
  538. #endif
  539. conn = mysql_real_connect(&(self->connection), host, user, passwd, db,
  540. port, unix_socket, client_flag);
  541. Py_END_ALLOW_THREADS ;
  542. if (!conn) {
  543. _mysql_Exception(self);
  544. return -1;
  545. }
  546. /*
  547. PyType_GenericAlloc() automatically sets up GC allocation and
  548. tracking for GC objects, at least in 2.2.1, so it does not need to
  549. be done here. tp_dealloc still needs to call PyObject_GC_UnTrack(),
  550. however.
  551. */
  552. self->open = 1;
  553. return 0;
  554. }
  555. static char _mysql_connect__doc__[] =
  556. "Returns a MYSQL connection object. Exclusive use of\n\
  557. keyword parameters strongly recommended. Consult the\n\
  558. MySQL C API documentation for more details.\n\
  559. \n\
  560. host\n\
  561. string, host to connect\n\
  562. \n\
  563. user\n\
  564. string, user to connect as\n\
  565. \n\
  566. passwd\n\
  567. string, password to use\n\
  568. \n\
  569. db\n\
  570. string, database to use\n\
  571. \n\
  572. port\n\
  573. integer, TCP/IP port to connect to\n\
  574. \n\
  575. unix_socket\n\
  576. string, location of unix_socket (UNIX-ish only)\n\
  577. \n\
  578. conv\n\
  579. mapping, maps MySQL FIELD_TYPE.* to Python functions which\n\
  580. convert a string to the appropriate Python type\n\
  581. \n\
  582. connect_timeout\n\
  583. number of seconds to wait before the connection\n\
  584. attempt fails.\n\
  585. \n\
  586. compress\n\
  587. if set, gzip compression is enabled\n\
  588. \n\
  589. named_pipe\n\
  590. if set, connect to server via named pipe (Windows only)\n\
  591. \n\
  592. init_command\n\
  593. command which is run once the connection is created\n\
  594. \n\
  595. read_default_file\n\
  596. see the MySQL documentation for mysql_options()\n\
  597. \n\
  598. read_default_group\n\
  599. see the MySQL documentation for mysql_options()\n\
  600. \n\
  601. client_flag\n\
  602. client flags from MySQLdb.constants.CLIENT\n\
  603. \n\
  604. load_infile\n\
  605. int, non-zero enables LOAD LOCAL INFILE, zero disables\n\
  606. \n\
  607. ";
  608. static PyObject *
  609. _mysql_connect(
  610. PyObject *self,
  611. PyObject *args,
  612. PyObject *kwargs)
  613. {
  614. _mysql_ConnectionObject *c=NULL;
  615. c = MyAlloc(_mysql_ConnectionObject, _mysql_ConnectionObject_Type);
  616. if (c == NULL) return NULL;
  617. if (_mysql_ConnectionObject_Initialize(c, args, kwargs)) {
  618. Py_DECREF(c);
  619. c = NULL;
  620. }
  621. return (PyObject *) c;
  622. }
  623. #if PY_VERSION_HEX >= 0x02020000
  624. static int _mysql_ConnectionObject_traverse(
  625. _mysql_ConnectionObject *self,
  626. visitproc visit,
  627. void *arg)
  628. {
  629. if (self->converter)
  630. return visit(self->converter, arg);
  631. return 0;
  632. }
  633. #endif
  634. static int _mysql_ConnectionObject_clear(
  635. _mysql_ConnectionObject *self)
  636. {
  637. Py_XDECREF(self->converter);
  638. self->converter = NULL;
  639. return 0;
  640. }
  641. static char _mysql_ConnectionObject_close__doc__[] =
  642. "Close the connection. No further activity possible.";
  643. static PyObject *
  644. _mysql_ConnectionObject_close(
  645. _mysql_ConnectionObject *self,
  646. PyObject *args)
  647. {
  648. if (args) {
  649. if (!PyArg_ParseTuple(args, "")) return NULL;
  650. }
  651. if (self->open) {
  652. Py_BEGIN_ALLOW_THREADS
  653. mysql_close(&(self->connection));
  654. Py_END_ALLOW_THREADS
  655. self->open = 0;
  656. } else {
  657. PyErr_SetString(_mysql_ProgrammingError,
  658. "closing a closed connection");
  659. return NULL;
  660. }
  661. _mysql_ConnectionObject_clear(self);
  662. Py_INCREF(Py_None);
  663. return Py_None;
  664. }
  665. static char _mysql_ConnectionObject_affected_rows__doc__ [] =
  666. "Return number of rows affected by the last query.\n\
  667. Non-standard. Use Cursor.rowcount.\n\
  668. ";
  669. static PyObject *
  670. _mysql_ConnectionObject_affected_rows(
  671. _mysql_ConnectionObject *self,
  672. PyObject *args)
  673. {
  674. if (!PyArg_ParseTuple(args, "")) return NULL;
  675. check_connection(self);
  676. return PyLong_FromUnsignedLongLong(mysql_affected_rows(&(self->connection)));
  677. }
  678. static char _mysql_debug__doc__[] =
  679. "Does a DBUG_PUSH with the given string.\n\
  680. mysql_debug() uses the Fred Fish debug library.\n\
  681. To use this function, you must compile the client library to\n\
  682. support debugging.\n\
  683. ";
  684. static PyObject *
  685. _mysql_debug(
  686. PyObject *self,
  687. PyObject *args)
  688. {
  689. char *debug;
  690. if (!PyArg_ParseTuple(args, "s", &debug)) return NULL;
  691. mysql_debug(debug);
  692. Py_INCREF(Py_None);
  693. return Py_None;
  694. }
  695. static char _mysql_ConnectionObject_dump_debug_info__doc__[] =
  696. "Instructs the server to write some debug information to the\n\
  697. log. The connected user must have the process privilege for\n\
  698. this to work. Non-standard.\n\
  699. ";
  700. static PyObject *
  701. _mysql_ConnectionObject_dump_debug_info(
  702. _mysql_ConnectionObject *self,
  703. PyObject *args)
  704. {
  705. int err;
  706. if (!PyArg_ParseTuple(args, "")) return NULL;
  707. check_connection(self);
  708. Py_BEGIN_ALLOW_THREADS
  709. err = mysql_dump_debug_info(&(self->connection));
  710. Py_END_ALLOW_THREADS
  711. if (err) return _mysql_Exception(self);
  712. Py_INCREF(Py_None);
  713. return Py_None;
  714. }
  715. static char _mysql_ConnectionObject_autocommit__doc__[] =
  716. "Set the autocommit mode. True values enable; False value disable.\n\
  717. ";
  718. static PyObject *
  719. _mysql_ConnectionObject_autocommit(
  720. _mysql_ConnectionObject *self,
  721. PyObject *args)
  722. {
  723. int flag, err;
  724. if (!PyArg_ParseTuple(args, "i", &flag)) return NULL;
  725. Py_BEGIN_ALLOW_THREADS
  726. #if MYSQL_VERSION_ID >= 40100
  727. err = mysql_autocommit(&(self->connection), flag);
  728. #else
  729. {
  730. char query[256];
  731. snprintf(query, 256, "SET AUTOCOMMIT=%d", flag);
  732. err = mysql_query(&(self->connection), query);
  733. }
  734. #endif
  735. Py_END_ALLOW_THREADS
  736. if (err) return _mysql_Exception(self);
  737. Py_INCREF(Py_None);
  738. return Py_None;
  739. }
  740. static char _mysql_ConnectionObject_commit__doc__[] =
  741. "Commits the current transaction\n\
  742. ";
  743. static PyObject *
  744. _mysql_ConnectionObject_commit(
  745. _mysql_ConnectionObject *self,
  746. PyObject *args)
  747. {
  748. int err;
  749. if (!PyArg_ParseTuple(args, "")) return NULL;
  750. Py_BEGIN_ALLOW_THREADS
  751. #if MYSQL_VERSION_ID >= 40100
  752. err = mysql_commit(&(self->connection));
  753. #else
  754. err = mysql_query(&(self->connection), "COMMIT");
  755. #endif
  756. Py_END_ALLOW_THREADS
  757. if (err) return _mysql_Exception(self);
  758. Py_INCREF(Py_None);
  759. return Py_None;
  760. }
  761. static char _mysql_ConnectionObject_rollback__doc__[] =
  762. "Rolls backs the current transaction\n\
  763. ";
  764. static PyObject *
  765. _mysql_ConnectionObject_rollback(
  766. _mysql_ConnectionObject *self,
  767. PyObject *args)
  768. {
  769. int err;
  770. if (!PyArg_ParseTuple(args, "")) return NULL;
  771. Py_BEGIN_ALLOW_THREADS
  772. #if MYSQL_VERSION_ID >= 40100
  773. err = mysql_rollback(&(self->connection));
  774. #else
  775. err = mysql_query(&(self->connection), "ROLLBACK");
  776. #endif
  777. Py_END_ALLOW_THREADS
  778. if (err) return _mysql_Exception(self);
  779. Py_INCREF(Py_None);
  780. return Py_None;
  781. }
  782. static char _mysql_ConnectionObject_next_result__doc__[] =
  783. "If more query results exist, next_result() reads the next query\n\
  784. results and returns the status back to application.\n\
  785. \n\
  786. After calling next_result() the state of the connection is as if\n\
  787. you had called query() for the next query. This means that you can\n\
  788. now call store_result(), warning_count(), affected_rows()\n\
  789. , and so forth. \n\
  790. \n\
  791. Returns 0 if there are more results; -1 if there are no more results\n\
  792. \n\
  793. Non-standard.\n\
  794. ";
  795. static PyObject *
  796. _mysql_ConnectionObject_next_result(
  797. _mysql_ConnectionObject *self,
  798. PyObject *args)
  799. {
  800. int err;
  801. if (!PyArg_ParseTuple(args, "")) return NULL;
  802. Py_BEGIN_ALLOW_THREADS
  803. #if MYSQL_VERSION_ID >= 40100
  804. err = mysql_next_result(&(self->connection));
  805. #else
  806. err = -1;
  807. #endif
  808. Py_END_ALLOW_THREADS
  809. if (err > 0) return _mysql_Exception(self);
  810. return PyInt_FromLong(err);
  811. }
  812. #if MYSQL_VERSION_ID >= 40100
  813. static char _mysql_ConnectionObject_set_server_option__doc__[] =
  814. "set_server_option(option) -- Enables or disables an option\n\
  815. for the connection.\n\
  816. \n\
  817. Non-standard.\n\
  818. ";
  819. static PyObject *
  820. _mysql_ConnectionObject_set_server_option(
  821. _mysql_ConnectionObject *self,
  822. PyObject *args)
  823. {
  824. int err, flags=0;
  825. if (!PyArg_ParseTuple(args, "i", &flags))
  826. return NULL;
  827. Py_BEGIN_ALLOW_THREADS
  828. err = mysql_set_server_option(&(self->connection), flags);
  829. Py_END_ALLOW_THREADS
  830. if (err) return _mysql_Exception(self);
  831. return PyInt_FromLong(err);
  832. }
  833. static char _mysql_ConnectionObject_sqlstate__doc__[] =
  834. "Returns a string containing the SQLSTATE error code\n\
  835. for the last error. The error code consists of five characters.\n\
  836. '00000' means \"no error.\" The values are specified by ANSI SQL\n\
  837. and ODBC. For a list of possible values, see section 23\n\
  838. Error Handling in MySQL in the MySQL Manual.\n\
  839. \n\
  840. Note that not all MySQL errors are yet mapped to SQLSTATE's.\n\
  841. The value 'HY000' (general error) is used for unmapped errors.\n\
  842. \n\
  843. Non-standard.\n\
  844. ";
  845. static PyObject *
  846. _mysql_ConnectionObject_sqlstate(
  847. _mysql_ConnectionObject *self,
  848. PyObject *args)
  849. {
  850. if (!PyArg_ParseTuple(args, "")) return NULL;
  851. return PyString_FromString(mysql_sqlstate(&(self->connection)));
  852. }
  853. static char _mysql_ConnectionObject_warning_count__doc__[] =
  854. "Returns the number of warnings generated during execution\n\
  855. of the previous SQL statement.\n\
  856. \n\
  857. Non-standard.\n\
  858. ";
  859. static PyObject *
  860. _mysql_ConnectionObject_warning_count(
  861. _mysql_ConnectionObject *self,
  862. PyObject *args)
  863. {
  864. if (!PyArg_ParseTuple(args, "")) return NULL;
  865. return PyInt_FromLong(mysql_warning_count(&(self->connection)));
  866. }
  867. #endif
  868. static char _mysql_ConnectionObject_errno__doc__[] =
  869. "Returns the error code for the most recently invoked API function\n\
  870. that can succeed or fail. A return value of zero means that no error\n\
  871. occurred.\n\
  872. ";
  873. static PyObject *
  874. _mysql_ConnectionObject_errno(
  875. _mysql_ConnectionObject *self,
  876. PyObject *args)
  877. {
  878. if (!PyArg_ParseTuple(args, "")) return NULL;
  879. check_connection(self);
  880. return PyInt_FromLong((long)mysql_errno(&(self->connection)));
  881. }
  882. static char _mysql_ConnectionObject_error__doc__[] =
  883. "Returns the error message for the most recently invoked API function\n\
  884. that can succeed or fail. An empty string ("") is returned if no error\n\
  885. occurred.\n\
  886. ";
  887. static PyObject *
  888. _mysql_ConnectionObject_error(
  889. _mysql_ConnectionObject *self,
  890. PyObject *args)
  891. {
  892. if (!PyArg_ParseTuple(args, "")) return NULL;
  893. check_connection(self);
  894. return PyString_FromString(mysql_error(&(self->connection)));
  895. }
  896. static char _mysql_escape_string__doc__[] =
  897. "escape_string(s) -- quote any SQL-interpreted characters in string s.\n\
  898. \n\
  899. Use connection.escape_string(s), if you use it at all.\n\
  900. _mysql.escape_string(s) cannot handle character sets. You are\n\
  901. probably better off using connection.escape(o) instead, since\n\
  902. it will escape entire sequences as well as strings.";
  903. static PyObject *
  904. _mysql_escape_string(
  905. _mysql_ConnectionObject *self,
  906. PyObject *args)
  907. {
  908. PyObject *str;
  909. char *in, *out;
  910. int len, size;
  911. if (!PyArg_ParseTuple(args, "s#:escape_string", &in, &size)) return NULL;
  912. str = PyString_FromStringAndSize((char *) NULL, size*2+1);
  913. if (!str) return PyErr_NoMemory();
  914. out = PyString_AS_STRING(str);
  915. #if MYSQL_VERSION_ID < 32321
  916. len = mysql_escape_string(out, in, size);
  917. #else
  918. check_server_init(NULL);
  919. if (self && self->open)
  920. len = mysql_real_escape_string(&(self->connection), out, in, size);
  921. else
  922. len = mysql_escape_string(out, in, size);
  923. #endif
  924. if (_PyString_Resize(&str, len) < 0) return NULL;
  925. return (str);
  926. }
  927. static char _mysql_string_literal__doc__[] =
  928. "string_literal(obj) -- converts object obj into a SQL string literal.\n\
  929. This means, any special SQL characters are escaped, and it is enclosed\n\
  930. within single quotes. In other words, it performs:\n\
  931. \n\
  932. \"'%s'\" % escape_string(str(obj))\n\
  933. \n\
  934. Use connection.string_literal(obj), if you use it at all.\n\
  935. _mysql.string_literal(obj) cannot handle character sets.";
  936. static PyObject *
  937. _mysql_string_literal(
  938. _mysql_ConnectionObject *self,
  939. PyObject *args)
  940. {
  941. PyObject *str, *s, *o, *d;
  942. char *in, *out;
  943. int len, size;
  944. if (!PyArg_ParseTuple(args, "O|O:string_literal", &o, &d)) return NULL;
  945. s = PyObject_Str(o);
  946. if (!s) return NULL;
  947. in = PyString_AsString(s);
  948. size = PyString_GET_SIZE(s);
  949. str = PyString_FromStringAndSize((char *) NULL, size*2+3);
  950. if (!str) return PyErr_NoMemory();
  951. out = PyString_AS_STRING(str);
  952. #if MYSQL_VERSION_ID < 32321
  953. len = mysql_escape_string(out+1, in, size);
  954. #else
  955. check_server_init(NULL);
  956. if (self && self->open)
  957. len = mysql_real_escape_string(&(self->connection), out+1, in, size);
  958. else
  959. len = mysql_escape_string(out+1, in, size);
  960. #endif
  961. *out = *(out+len+1) = '\'';
  962. if (_PyString_Resize(&str, len+2) < 0) return NULL;
  963. Py_DECREF(s);
  964. return (str);
  965. }
  966. static PyObject *_mysql_NULL;
  967. static PyObject *
  968. _escape_item(
  969. PyObject *item,
  970. PyObject *d)
  971. {
  972. PyObject *quoted=NULL, *itemtype, *itemconv;
  973. if (!(itemtype = PyObject_Type(item)))
  974. goto error;
  975. itemconv = PyObject_GetItem(d, itemtype);
  976. Py_DECREF(itemtype);
  977. if (!itemconv) {
  978. PyErr_Clear();
  979. itemconv = PyObject_GetItem(d,
  980. (PyObject *) &PyString_Type);
  981. }
  982. if (!itemconv) {
  983. PyErr_SetString(PyExc_TypeError,
  984. "no default type converter defined");
  985. goto error;
  986. }
  987. quoted = PyObject_CallFunction(itemconv, "OO", item, d);
  988. Py_DECREF(itemconv);
  989. error:
  990. return quoted;
  991. }
  992. static char _mysql_escape__doc__[] =
  993. "escape(obj, dict) -- escape any special characters in object obj\n\
  994. using mapping dict to provide quoting functions for each type.\n\
  995. Returns a SQL literal string.";
  996. static PyObject *
  997. _mysql_escape(
  998. PyObject *self,
  999. PyObject *args)
  1000. {
  1001. PyObject *o=NULL, *d=NULL;
  1002. if (!PyArg_ParseTuple(args, "O|O:escape", &o, &d))
  1003. return NULL;
  1004. if (d) {
  1005. if (!PyMapping_Check(d)) {
  1006. PyErr_SetString(PyExc_TypeError,
  1007. "argument 2 must be a mapping");
  1008. return NULL;
  1009. }
  1010. return _escape_item(o, d);
  1011. } else {
  1012. if (!self) {
  1013. PyErr_SetString(PyExc_TypeError,
  1014. "argument 2 must be a mapping");
  1015. return NULL;
  1016. }
  1017. return _escape_item(o,
  1018. ((_mysql_ConnectionObject *) self)->converter);
  1019. }
  1020. }
  1021. static char _mysql_escape_sequence__doc__[] =
  1022. "escape_sequence(seq, dict) -- escape any special characters in sequence\n\
  1023. seq using mapping dict to provide quoting functions for each type.\n\
  1024. Returns a tuple of escaped items.";
  1025. static PyObject *
  1026. _mysql_escape_sequence(
  1027. PyObject *self,
  1028. PyObject *args)
  1029. {
  1030. PyObject *o=NULL, *d=NULL, *r=NULL, *item, *quoted;
  1031. int i, n;
  1032. if (!PyArg_ParseTuple(args, "OO:escape_sequence", &o, &d))
  1033. goto error;
  1034. if (!PyMapping_Check(d)) {
  1035. PyErr_SetString(PyExc_TypeError,
  1036. "argument 2 must be a mapping");
  1037. return NULL;
  1038. }
  1039. if ((n = PyObject_Length(o)) == -1) goto error;
  1040. if (!(r = PyTuple_New(n))) goto error;
  1041. for (i=0; i<n; i++) {
  1042. item = PySequence_GetItem(o, i);
  1043. if (!item) goto error;
  1044. quoted = _escape_item(item, d);
  1045. Py_DECREF(item);
  1046. if (!quoted) goto error;
  1047. PyTuple_SET_ITEM(r, i, quoted);
  1048. }
  1049. return r;
  1050. error:
  1051. Py_XDECREF(r);
  1052. return NULL;
  1053. }
  1054. static char _mysql_escape_dict__doc__[] =
  1055. "escape_sequence(d, dict) -- escape any special characters in\n\
  1056. dictionary d using mapping dict to provide quoting functions for each type.\n\
  1057. Returns a dictionary of escaped items.";
  1058. static PyObject *
  1059. _mysql_escape_dict(
  1060. PyObject *self,
  1061. PyObject *args)
  1062. {
  1063. PyObject *o=NULL, *d=NULL, *r=NULL, *item, *quoted, *pkey;
  1064. Py_ssize_t ppos = 0;
  1065. if (!PyArg_ParseTuple(args, "O!O:escape_dict", &PyDict_Type, &o, &d))
  1066. goto error;
  1067. if (!PyMapping_Check(d)) {
  1068. PyErr_SetString(PyExc_TypeError,
  1069. "argument 2 must be a mapping");
  1070. return NULL;
  1071. }
  1072. if (!(r = PyDict_New())) goto error;
  1073. while (PyDict_Next(o, &ppos, &pkey, &item)) {
  1074. quoted = _escape_item(item, d);
  1075. if (!quoted) goto error;
  1076. if (PyDict_SetItem(r, pkey, quoted)==-1) goto error;
  1077. Py_DECREF(quoted);
  1078. }
  1079. return r;
  1080. error:
  1081. Py_XDECREF(r);
  1082. return NULL;
  1083. }
  1084. static char _mysql_ResultObject_describe__doc__[] =
  1085. "Returns the sequence of 7-tuples required by the DB-API for\n\
  1086. the Cursor.description attribute.\n\
  1087. ";
  1088. static PyObject *
  1089. _mysql_ResultObject_describe(
  1090. _mysql_ResultObject *self,
  1091. PyObject *args)
  1092. {
  1093. PyObject *d;
  1094. MYSQL_FIELD *fields;
  1095. unsigned int i, n;
  1096. if (!PyArg_ParseTuple(args, "")) return NULL;
  1097. check_result_connection(self);
  1098. n = mysql_num_fields(self->result);
  1099. fields = mysql_fetch_fields(self->result);
  1100. if (!(d = PyTuple_New(n))) return NULL;
  1101. for (i=0; i<n; i++) {
  1102. PyObject *t;
  1103. t = Py_BuildValue("(siiiiii)",
  1104. fields[i].name,
  1105. (long) fields[i].type,
  1106. (long) fields[i].max_length,
  1107. (long) fields[i].length,
  1108. (long) fields[i].length,
  1109. (long) fields[i].decimals,
  1110. (long) !(IS_NOT_NULL(fields[i].flags)));
  1111. if (!t) goto error;
  1112. PyTuple_SET_ITEM(d, i, t);
  1113. }
  1114. return d;
  1115. error:
  1116. Py_XDECREF(d);
  1117. return NULL;
  1118. }
  1119. static char _mysql_ResultObject_field_flags__doc__[] =
  1120. "Returns a tuple of field flags, one for each column in the result.\n\
  1121. " ;
  1122. static PyObject *
  1123. _mysql_ResultObject_field_flags(
  1124. _mysql_ResultObject *self,
  1125. PyObject *args)
  1126. {
  1127. PyObject *d;
  1128. MYSQL_FIELD *fields;
  1129. unsigned int i, n;
  1130. if (!PyArg_ParseTuple(args, "")) return NULL;
  1131. check_result_connection(self);
  1132. n = mysql_num_fields(self->result);
  1133. fields = mysql_fetch_fields(self->result);
  1134. if (!(d = PyTuple_New(n))) return NULL;
  1135. for (i=0; i<n; i++) {
  1136. PyObject *f;
  1137. if (!(f = PyInt_FromLong((long)fields[i].flags))) goto error;
  1138. PyTuple_SET_ITEM(d, i, f);
  1139. }
  1140. return d;
  1141. error:
  1142. Py_XDECREF(d);
  1143. return NULL;
  1144. }
  1145. static PyObject *
  1146. _mysql_field_to_python(
  1147. PyObject *converter,
  1148. char *rowitem,
  1149. unsigned long length)
  1150. {
  1151. PyObject *v;
  1152. if (rowitem) {
  1153. if (converter != Py_None)
  1154. v = PyObject_CallFunction(converter,
  1155. "s#",
  1156. rowitem,
  1157. (int)length);
  1158. else
  1159. v = PyString_FromStringAndSize(rowitem,
  1160. (int)length);
  1161. if (!v)
  1162. return NULL;
  1163. } else {
  1164. Py_INCREF(Py_None);
  1165. v = Py_None;
  1166. }
  1167. return v;
  1168. }
  1169. static PyObject *
  1170. _mysql_row_to_tuple(
  1171. _mysql_ResultObject *self,
  1172. MYSQL_ROW row)
  1173. {
  1174. unsigned int n, i;
  1175. unsigned long *length;
  1176. PyObject *r, *c;
  1177. n = mysql_num_fields(self->result);
  1178. if (!(r = PyTuple_New(n))) return NULL;
  1179. length = mysql_fetch_lengths(self->result);
  1180. for (i=0; i<n; i++) {
  1181. PyObject *v;
  1182. c = PyTuple_GET_ITEM(self->converter, i);
  1183. v = _mysql_field_to_python(c, row[i], length[i]);
  1184. if (!v) goto error;
  1185. PyTuple_SET_ITEM(r, i, v);
  1186. }
  1187. return r;
  1188. error:
  1189. Py_XDECREF(r);
  1190. return NULL;
  1191. }
  1192. static PyObject *
  1193. _mysql_row_to_dict(
  1194. _mysql_ResultObject *self,
  1195. MYSQL_ROW row)
  1196. {
  1197. unsigned int n, i;
  1198. unsigned long *length;
  1199. PyObject *r, *c;
  1200. MYSQL_FIELD *fields;
  1201. n = mysql_num_fields(self->result);
  1202. if (!(r = PyDict_New())) return NULL;
  1203. length = mysql_fetch_lengths(self->result);
  1204. fields = mysql_fetch_fields(self->result);
  1205. for (i=0; i<n; i++) {
  1206. PyObject *v;
  1207. c = PyTuple_GET_ITEM(self->converter, i);
  1208. v = _mysql_field_to_python(c, row[i], length[i]);
  1209. if (!v) goto error;
  1210. if (!PyMapping_HasKeyString(r, fields[i].name)) {
  1211. PyMapping_SetItemString(r, fields[i].name, v);
  1212. } else {
  1213. int len;
  1214. char buf[256];
  1215. strncpy(buf, fields[i].table, 256);
  1216. len = strlen(buf);
  1217. strncat(buf, ".", 256-len);
  1218. len = strlen(buf);
  1219. strncat(buf, fields[i].name, 256-len);
  1220. PyMapping_SetItemString(r, buf, v);
  1221. }
  1222. Py_DECREF(v);
  1223. }
  1224. return r;
  1225. error:
  1226. Py_XDECREF(r);
  1227. return NULL;
  1228. }
  1229. static PyObject *
  1230. _mysql_row_to_dict_old(
  1231. _mysql_ResultObject *self,
  1232. MYSQL_ROW row)
  1233. {
  1234. unsigned int n, i;
  1235. unsigned long *length;
  1236. PyObject *r, *c;
  1237. MYSQL_FIELD *fields;
  1238. n = mysql_num_fields(self->result);
  1239. if (!(r = PyDict_New())) return NULL;
  1240. length = mysql_fetch_lengths(self->result);
  1241. fields = mysql_fetch_fields(self->result);
  1242. for (i=0; i<n; i++) {
  1243. PyObject *v;
  1244. c = PyTuple_GET_ITEM(self->converter, i);
  1245. v = _mysql_field_to_python(c, row[i], length[i]);
  1246. if (!v) goto error;
  1247. {
  1248. int len=0;
  1249. char buf[256]="";
  1250. if (strlen(fields[i].table)) {
  1251. strncpy(buf, fields[i].table, 256);
  1252. len = strlen(buf);
  1253. strncat(buf, ".", 256-len);
  1254. len = strlen(buf);
  1255. }
  1256. strncat(buf, fields[i].name, 256-len);
  1257. PyMapping_SetItemString(r, buf, v);
  1258. }
  1259. Py_DECREF(v);
  1260. }
  1261. return r;
  1262. error:
  1263. Py_XDECREF(r);
  1264. return NULL;
  1265. }
  1266. typedef PyObject *_PYFUNC(_mysql_ResultObject *, MYSQL_ROW);
  1267. int
  1268. _mysql__fetch_row(
  1269. _mysql_ResultObject *self,
  1270. PyObject **r,
  1271. int skiprows,
  1272. int maxrows,
  1273. _PYFUNC *convert_row)
  1274. {
  1275. unsigned int i;
  1276. MYSQL_ROW row;
  1277. for (i = skiprows; i<(skiprows+maxrows); i++) {
  1278. PyObject *v;
  1279. if (!self->use)
  1280. row = mysql_fetch_row(self->result);
  1281. else {
  1282. Py_BEGIN_ALLOW_THREADS;
  1283. row = mysql_fetch_row(self->result);
  1284. Py_END_ALLOW_THREADS;
  1285. }
  1286. if (!row && mysql_errno(&(((_mysql_ConnectionObject *)(self->conn))->connection))) {
  1287. _mysql_Exception((_mysql_ConnectionObject *)self->conn);
  1288. goto error;
  1289. }
  1290. if (!row) {
  1291. if (MyTuple_Resize(r, i, 0) == -1) goto error;
  1292. break;
  1293. }
  1294. v = convert_row(self, row);
  1295. if (!v) goto error;
  1296. PyTuple_SET_ITEM(*r, i, v);
  1297. }
  1298. return i-skiprows;
  1299. error:
  1300. return -1;
  1301. }
  1302. static char _mysql_ResultObject_fetch_row__doc__[] =
  1303. "fetch_row([maxrows, how]) -- Fetches up to maxrows as a tuple.\n\
  1304. The rows are formatted according to how:\n\
  1305. \n\
  1306. 0 -- tuples (default)\n\
  1307. 1 -- dictionaries, key=column or table.column if duplicated\n\
  1308. 2 -- dictionaries, key=table.column\n\
  1309. ";
  1310. static PyObject *
  1311. _mysql_ResultObject_fetch_row(
  1312. _mysql_ResultObject *self,
  1313. PyObject *args,
  1314. PyObject *kwargs)
  1315. {
  1316. typedef PyObject *_PYFUNC(_mysql_ResultObject *, MYSQL_ROW);
  1317. static char *kwlist[] = { "maxrows", "how", NULL };
  1318. static _PYFUNC *row_converters[] =
  1319. {
  1320. _mysql_row_to_tuple,
  1321. _mysql_row_to_dict,
  1322. _mysql_row_to_dict_old
  1323. };
  1324. _PYFUNC *convert_row;
  1325. unsigned int maxrows=1, how=0, skiprows=0, rowsadded;
  1326. PyObject *r=NULL;
  1327. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii:fetch_row", kwlist,
  1328. &maxrows, &how))
  1329. return NULL;
  1330. check_result_connection(self);
  1331. if (how < 0 || how >= sizeof(row_converters)) {
  1332. PyErr_SetString(PyExc_ValueError, "how out of range");
  1333. return NULL;
  1334. }
  1335. convert_row = row_converters[how];
  1336. if (maxrows) {
  1337. if (!(r = PyTuple_New(maxrows))) goto error;
  1338. rowsadded = _mysql__fetch_row(self, &r, skiprows, maxrows,
  1339. convert_row);
  1340. if (rowsadded == -1) goto error;
  1341. } else {
  1342. if (self->use) {
  1343. maxrows = 1000;
  1344. if (!(r = PyTuple_New(maxrows))) goto error;
  1345. while (1) {
  1346. rowsadded = _mysql__fetch_row(self, &r, skiprows,
  1347. maxrows, convert_row);
  1348. if (rowsadded == -1) goto error;
  1349. skiprows += rowsadded;
  1350. if (rowsadded < maxrows) break;
  1351. if (MyTuple_Resize(&r, skiprows+maxrows, 0) == -1)
  1352. goto error;
  1353. }
  1354. } else {
  1355. /* XXX if overflow, maxrows<0? */
  1356. maxrows = (int) mysql_num_rows(self->result);
  1357. if (!(r = PyTuple_New(maxrows))) goto error;
  1358. rowsadded = _mysql__fetch_row(self, &r, 0,
  1359. maxrows, convert_row);
  1360. if (rowsadded == -1) goto error;
  1361. }
  1362. }
  1363. return r;
  1364. error:
  1365. Py_XDECREF(r);
  1366. return NULL;
  1367. }
  1368. #if MYSQL_VERSION_ID >= 32303
  1369. static char _mysql_ConnectionObject_change_user__doc__[] =
  1370. "Changes the user and causes the database specified by db to\n\
  1371. become the default (current) database on the connection\n\
  1372. specified by mysql. In subsequent queries, this database is\n\
  1373. the default for table references that do not include an\n\
  1374. explicit database specifier.\n\
  1375. \n\
  1376. This function was introduced in MySQL Version 3.23.3.\n\
  1377. \n\
  1378. Fails unless the connected user can be authenticated or if he\n\
  1379. doesn't have permission to use the database. In this case the\n\
  1380. user and database are not changed.\n\
  1381. \n\
  1382. The db parameter may be set to None if you don't want to have\n\
  1383. a default database.\n\
  1384. ";
  1385. static PyObject *
  1386. _mysql_ConnectionObject_change_user(
  1387. _mysql_ConnectionObject *self,
  1388. PyObject *args,
  1389. PyObject *kwargs)
  1390. {
  1391. char *user, *pwd=NULL, *db=NULL;
  1392. int r;
  1393. static char *kwlist[] = { "user", "passwd", "db", NULL } ;
  1394. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ss:change_user",
  1395. kwlist, &user, &pwd, &db))
  1396. return NULL;
  1397. check_connection(self);
  1398. Py_BEGIN_ALLOW_THREADS
  1399. r = mysql_change_user(&(self->connection), user, pwd, db);
  1400. Py_END_ALLOW_THREADS
  1401. if (r) return _mysql_Exception(self);
  1402. Py_INCREF(Py_None);
  1403. return Py_None;
  1404. }
  1405. #endif
  1406. static char _mysql_ConnectionObject_character_set_name__doc__[] =
  1407. "Returns the default character set for the current connection.\n\
  1408. Non-standard.\n\
  1409. ";
  1410. static PyObject *
  1411. _mysql_ConnectionObject_character_set_name(
  1412. _mysql_ConnectionObject *self,
  1413. PyObject *args)
  1414. {
  1415. const char *s;
  1416. if (!PyArg_ParseTuple(args, "")) return NULL;
  1417. check_connection(self);
  1418. #if MYSQL_VERSION_ID >= 32321
  1419. s = mysql_character_set_name(&(self->connection));
  1420. #else
  1421. s = "latin1";
  1422. #endif
  1423. return PyString_FromString(s);
  1424. }
  1425. #if MYSQL_VERSION_ID >= 50007
  1426. static char _mysql_ConnectionObject_set_character_set__doc__[] =
  1427. "Sets the default character set for the current connection.\n\
  1428. Non-standard.\n\
  1429. ";
  1430. static PyObject *
  1431. _mysql_ConnectionObject_set_character_set(
  1432. _mysql_ConnectionObject *self,
  1433. PyObject *args)
  1434. {
  1435. const char *s;
  1436. int err;
  1437. if (!PyArg_ParseTuple(args, "s", &s)) return NULL;
  1438. check_connection(self);
  1439. Py_BEGIN_ALLOW_THREADS
  1440. err = mysql_set_character_set(&(self->connection), s);
  1441. Py_END_ALLOW_THREADS
  1442. if (err) return _mysql_Exception(self);
  1443. Py_INCREF(Py_None);
  1444. return Py_None;
  1445. }
  1446. #endif
  1447. #if MYSQL_VERSION_ID >= 50010
  1448. static char _mysql_ConnectionObject_get_character_set_info__doc__[] =
  1449. "Returns a dict with information about the current character set:\n\
  1450. \n\
  1451. collation\n\
  1452. collation name\n\
  1453. name\n\
  1454. character set name\n\
  1455. comment\n\
  1456. comment or descriptive name\n\
  1457. dir\n\
  1458. character set directory\n\
  1459. mbminlen\n\
  1460. min. length for multibyte string\n\
  1461. mbmaxlen\n\
  1462. max. length for multibyte string\n\
  1463. \n\
  1464. Not all keys may be present, particularly dir.\n\
  1465. \n\
  1466. Non-standard.\n\
  1467. ";
  1468. static PyObject *
  1469. _mysql_ConnectionObject_get_character_set_info(
  1470. _mysql_ConnectionObject *self,
  1471. PyObject *args)
  1472. {
  1473. PyObject *result;
  1474. MY_CHARSET_INFO cs;
  1475. if (!PyArg_ParseTuple(args, "")) return NULL;
  1476. check_connection(self);
  1477. mysql_get_character_set_info(&(self->connection), &cs);
  1478. if (!(result = PyDict_New())) return NULL;
  1479. if (cs.csname)
  1480. PyDict_SetItemString(result, "name", PyString_FromString(cs.csname));
  1481. if (cs.name)
  1482. PyDict_SetItemString(result, "collation", PyString_FromString(cs.name));
  1483. if (cs.comment)
  1484. PyDict_SetItemString(result, "comment", PyString_FromString(cs.comment));
  1485. if (cs.dir)
  1486. PyDict_SetItemString(result, "dir", PyString_FromString(cs.dir));
  1487. PyDict_SetItemString(result, "mbminlen", PyInt_FromLong(cs.mbminlen));
  1488. PyDict_SetItemString(result, "mbmaxlen", PyInt_FromLong(cs.mbmaxlen));
  1489. return result;
  1490. }
  1491. #endif
  1492. static char _mysql_get_client_info__doc__[] =
  1493. "get_client_info() -- Returns a string that represents\n\
  1494. the client library version.";
  1495. static PyObject *
  1496. _mysql_get_client_info(
  1497. PyObject *self,
  1498. PyObject *args)
  1499. {
  1500. if (!PyArg_ParseTuple(args, "")) return NULL;
  1501. check_server_init(NULL);
  1502. return PyString_FromString(mysql_get_client_info());
  1503. }
  1504. static char _mysql_ConnectionObject_get_host_info__doc__[] =
  1505. "Returns a string that represents the MySQL client library\n\
  1506. version. Non-standard.\n\
  1507. ";
  1508. static PyObject *
  1509. _mysql_ConnectionObject_get_host_info(
  1510. _mysql_ConnectionObject *self,
  1511. PyObject *args)
  1512. {
  1513. if (!PyArg_ParseTuple(args, "")) return NULL;
  1514. check_connection(self);
  1515. return PyString_FromString(mysql_get_host_info(&(self->connection)));
  1516. }
  1517. static char _mysql_ConnectionObject_get_proto_info__doc__[] =
  1518. "Returns an unsigned integer representing the protocol version\n\
  1519. used by the current connection. Non-standard.\n\
  1520. ";
  1521. static PyObject *
  1522. _mysql_ConnectionObject_get_proto_info(
  1523. _mysql_ConnectionObject *self,
  1524. PyObject *args)
  1525. {
  1526. if (!PyArg_ParseTuple(args, "")) return NULL;
  1527. check_connection(self);
  1528. return PyInt_FromLong((long)mysql_get_proto_info(&(self->connection)));
  1529. }
  1530. static char _mysql_ConnectionObject_get_server_info__doc__[] =
  1531. "Returns a string that represents the server version number.\n\
  1532. Non-standard.\n\
  1533. ";
  1534. static PyObject *
  1535. _mysql_ConnectionObject_get_server_info(
  1536. _mysql_ConnectionObject *self,
  1537. PyObject *args)
  1538. {
  1539. if (!PyArg_ParseTuple(args, "")) return NULL;
  1540. check_connection(self);
  1541. return PyString_FromString(mysql_get_server_info(&(self->connection)));
  1542. }
  1543. static char _mysql_ConnectionObject_info__doc__[] =
  1544. "Retrieves a string providing information about the most\n\
  1545. recently executed query. Non-standard. Use messages or\n\
  1546. Cursor.messages.\n\
  1547. ";
  1548. static PyObject *
  1549. _mysql_ConnectionObject_info(
  1550. _mysql_ConnectionObject *self,
  1551. PyObject *args)
  1552. {
  1553. const char *s;
  1554. if (!PyArg_ParseTuple(args, "")) return NULL;
  1555. check_connection(self);
  1556. s = mysql_info(&(self->connection));
  1557. if (s) return PyString_FromString(s);
  1558. Py_INCREF(Py_None);
  1559. return Py_None;
  1560. }
  1561. static char _mysql_ConnectionObject_insert_id__doc__[] =
  1562. "Returns the ID generated for an AUTO_INCREMENT column by the previous\n\
  1563. query. Use this function after you have performed an INSERT query into a\n\
  1564. table that contains an AUTO_INCREMENT field.\n\
  1565. \n\
  1566. Note that this returns 0 if the previous query does not\n\
  1567. generate an AUTO_INCREMENT value. If you need to save the value for\n\
  1568. later, be sure to call this immediately after the query\n\
  1569. that generates the value.\n\
  1570. \n\
  1571. The ID is updated after INSERT and UPDATE statements that generate\n\
  1572. an AUTO_INCREMENT value or that set a column value to\n\
  1573. LAST_INSERT_ID(expr). See section 6.3.5.2 Miscellaneous Functions\n\
  1574. in the MySQL documentation.\n\
  1575. \n\
  1576. Also note that the value of the SQL LAST_INSERT_ID() function always\n\
  1577. contains the most recently generated AUTO_INCREMENT value, and is not\n\
  1578. reset between queries because the value of that function is maintained\n\
  1579. in the server.\n\
  1580. " ;
  1581. static PyObject *
  1582. _mysql_ConnectionObject_insert_id(
  1583. _mysql_ConnectionObject *self,
  1584. PyObject *args)
  1585. {
  1586. my_ulonglong r;
  1587. if (!PyArg_ParseTuple(args, "")) return NULL;
  1588. check_connection(self);
  1589. Py_BEGIN_ALLOW_THREADS
  1590. r = mysql_insert_id(&(self->connection));
  1591. Py_END_ALLOW_THREADS
  1592. return PyLong_FromUnsignedLongLong(r);
  1593. }
  1594. static char _mysql_ConnectionObject_kill__doc__[] =
  1595. "Asks the server to kill the thread specified by pid.\n\
  1596. Non-standard.";
  1597. static PyObject *
  1598. _mysql_ConnectionObject_kill(
  1599. _mysql_ConnectionObject *self,
  1600. PyObject *args)
  1601. {
  1602. unsigned long pid;
  1603. int r;
  1604. if (!PyArg_ParseTuple(args, "i:kill", &pid)) return NULL;
  1605. check_connection(self);
  1606. Py_BEGIN_ALLOW_THREADS
  1607. r = mysql_kill(&(self->connection), pid);
  1608. Py_END_ALLOW_THREADS
  1609. if (r) return _mysql_Exception(self);
  1610. Py_INCREF(Py_None);
  1611. return Py_None;
  1612. }
  1613. static char _mysql_ConnectionObject_field_count__doc__[] =
  1614. "Returns the number of columns for the most recent query on the\n\
  1615. connection. Non-standard. Will probably give you bogus results\n\
  1616. on most cursor classes. Use Cursor.rowcount.\n\
  1617. ";
  1618. static PyObject *
  1619. _mysql_ConnectionObject_field_count(
  1620. _mysql_ConnectionObject *self,
  1621. PyObject *args)
  1622. {
  1623. if (!PyArg_ParseTuple(args, "")) return NULL;
  1624. check_connection(self);
  1625. #if MYSQL_VERSION_ID < 32224
  1626. return PyInt_FromLong((long)mysql_num_fields(&(self->connection)));
  1627. #else
  1628. return PyInt_FromLong((long)mysql_field_count(&(self->connection)));
  1629. #endif
  1630. }
  1631. static char _mysql_ResultObject_num_fields__doc__[] =
  1632. "Returns the number of fields (column) in the result." ;
  1633. static PyObject *
  1634. _mysql_ResultObject_num_fields(
  1635. _mysql_ResultObject *self,
  1636. PyObject *args)
  1637. {
  1638. if (!PyArg_ParseTuple(args, "")) return NULL;
  1639. check_result_connection(self);
  1640. return PyInt_FromLong((long)mysql_num_fields(self->result));
  1641. }
  1642. static char _mysql_ResultObject_num_rows__doc__[] =
  1643. "Returns the number of rows in the result set. Note that if\n\
  1644. use=1, this will not return a valid value until the entire result\n\
  1645. set has been read.\n\
  1646. ";
  1647. static PyObject *
  1648. _mysql_ResultObject_num_rows(
  1649. _mysql_ResultObject *self,
  1650. PyObject *args)
  1651. {
  1652. if (!PyArg_ParseTuple(args, "")) return NULL;
  1653. check_result_connection(self);
  1654. return PyLong_FromUnsignedLongLong(mysql_num_rows(self->result));
  1655. }
  1656. static char _mysql_ConnectionObject_ping__doc__[] =
  1657. "Checks whether or not the connection to the server is\n\
  1658. working. If it has gone down, an automatic reconnection is\n\
  1659. attempted.\n\
  1660. \n\
  1661. This function can be used by clients that remain idle for a\n\
  1662. long while, to check whether or not the server has closed the\n\
  1663. connection and reconnect if necessary.\n\
  1664. \n\
  1665. New in 1.2.2: Accepts an optional reconnect parameter. If True,\n\
  1666. then the client will attempt reconnection. Note that this setting\n\
  1667. is persistent. By default, this is on in MySQL<5.0.3, and off\n\
  1668. thereafter.\n\
  1669. \n\
  1670. Non-standard. You should assume that ping() performs an\n\
  1671. implicit rollback; use only when starting a new transaction.\n\
  1672. You have been warned.\n\
  1673. ";
  1674. static PyObject *
  1675. _mysql_ConnectionObject_ping(
  1676. _mysql_ConnectionObject *self,
  1677. PyObject *args)
  1678. {
  1679. int r, reconnect = -1;
  1680. if (!PyArg_ParseTuple(args, "|I", &reconnect)) return NULL;
  1681. check_connection(self);
  1682. if ( reconnect != -1 ) self->connection.reconnect = reconnect;
  1683. Py_BEGIN_ALLOW_THREADS
  1684. r = mysql_ping(&(self->connection));
  1685. Py_END_ALLOW_THREADS
  1686. if (r) return _mysql_Exception(self);
  1687. Py_INCREF(Py_None);
  1688. return Py_None;
  1689. }
  1690. static char _mysql_ConnectionObject_query__doc__[] =
  1691. "Execute a query. store_result() or use_result() will get the\n\
  1692. result set, if any. Non-standard. Use cursor() to create a cursor,\n\
  1693. then cursor.execute().\n\
  1694. " ;
  1695. static PyObject *
  1696. _mysql_ConnectionObject_query(
  1697. _mysql_ConnectionObject *self,
  1698. PyObject *args)
  1699. {
  1700. char *query;
  1701. int len, r;
  1702. if (!PyArg_ParseTuple(args, "s#:query", &query, &len)) return NULL;
  1703. check_connection(self);
  1704. Py_BEGIN_ALLOW_THREADS
  1705. r = mysql_real_query(&(self->connection), query, len);
  1706. Py_END_ALLOW_THREADS
  1707. if (r) return _mysql_Exception(self);
  1708. Py_INCREF(Py_None);
  1709. return Py_None;
  1710. }
  1711. static char _mysql_ConnectionObject_select_db__doc__[] =
  1712. "Causes the database specified by db to become the default\n\
  1713. (current) database on the connection specified by mysql. In subsequent\n\
  1714. queries, this database is the default for table references that do not\n\
  1715. include an explicit database specifier.\n\
  1716. \n\
  1717. Fails unless the connected user can be authenticated as having\n\
  1718. permission to use the database.\n\
  1719. \n\
  1720. Non-standard.\n\
  1721. ";
  1722. static PyObject *
  1723. _mysql_ConnectionObject_select_db(
  1724. _mysql_ConnectionObject *self,
  1725. PyObject *args)
  1726. {
  1727. char *db;
  1728. int r;
  1729. if (!PyArg_ParseTuple(args, "s:select_db", &db)) return NULL;
  1730. check_connection(self);
  1731. Py_BEGIN_ALLOW_THREADS
  1732. r = mysql_select_db(&(self->connection), db);
  1733. Py_END_ALLOW_THREADS
  1734. if (r) return _mysql_Exception(self);
  1735. Py_INCREF(Py_None);
  1736. return Py_None;
  1737. }
  1738. static char _mysql_ConnectionObject_shutdown__doc__[] =
  1739. "Asks the database server to shut down. The connected user must\n\
  1740. have shutdown privileges. Non-standard.\n\
  1741. ";
  1742. static PyObject *
  1743. _mysql_ConnectionObject_shutdown(
  1744. _mysql_ConnectionObject *self,
  1745. PyObject *args)
  1746. {
  1747. int r;
  1748. if (!PyArg_ParseTuple(args, "")) return NULL;
  1749. check_connection(self);
  1750. Py_BEGIN_ALLOW_THREADS
  1751. r = mysql_shutdown(&(self->connection)
  1752. #if MYSQL_VERSION_ID >= 40103
  1753. , SHUTDOWN_DEFAULT
  1754. #endif
  1755. );
  1756. Py_END_ALLOW_THREADS
  1757. if (r) return _mysql_Exception(self);
  1758. Py_INCREF(Py_None);
  1759. return Py_None;
  1760. }
  1761. static char _mysql_ConnectionObject_stat__doc__[] =
  1762. "Returns a character string containing information similar to\n\
  1763. that provided by the mysqladmin status command. This includes\n\
  1764. uptime in seconds and the number of running threads,\n\
  1765. questions, reloads, and open tables. Non-standard.\n\
  1766. ";
  1767. static PyObject *
  1768. _mysql_ConnectionObject_stat(
  1769. _mysql_ConnectionObject *self,
  1770. PyObject *args)
  1771. {
  1772. const char *s;
  1773. if (!PyArg_ParseTuple(args, "")) return NULL;
  1774. check_connection(self);
  1775. Py_BEGIN_ALLOW_THREADS
  1776. s = mysql_stat(&(self->connection));
  1777. Py_END_ALLOW_THREADS
  1778. if (!s) return _mysql_Exception(self);
  1779. return PyString_FromString(s);
  1780. }
  1781. static char _mysql_ConnectionObject_store_result__doc__[] =
  1782. "Returns a result object acquired by mysql_store_result\n\
  1783. (results stored in the client). If no results are available,\n\
  1784. None is returned. Non-standard.\n\
  1785. ";
  1786. static PyObject *
  1787. _mysql_ConnectionObject_store_result(
  1788. _mysql_ConnectionObject *self,
  1789. PyObject *args)
  1790. {
  1791. PyObject *arglist=NULL, *kwarglist=NULL, *result=NULL;
  1792. _mysql_ResultObject *r=NULL;
  1793. if (!PyArg_ParseTuple(args, "")) return NULL;
  1794. check_connection(self);
  1795. arglist = Py_BuildValue("(OiO)", self, 0, self->converter);
  1796. if (!arglist) goto error;
  1797. kwarglist = PyDict_New();
  1798. if (!kwarglist) goto error;
  1799. r = MyAlloc(_mysql_ResultObject, _mysql_ResultObject_Type);
  1800. if (!r) goto error;
  1801. if (_mysql_ResultObject_Initialize(r, arglist, kwarglist))
  1802. goto error;
  1803. result = (PyObject *) r;
  1804. if (!(r->result)) {
  1805. Py_DECREF(result);
  1806. Py_INCREF(Py_None);
  1807. result = Py_None;
  1808. }
  1809. error:
  1810. Py_XDECREF(arglist);
  1811. Py_XDECREF(kwarglist);
  1812. return result;
  1813. }
  1814. static char _mysql_ConnectionObject_thread_id__doc__[] =
  1815. "Returns the thread ID of the current connection. This value\n\
  1816. can be used as an argument to kill() to kill the thread.\n\
  1817. \n\
  1818. If the connection is lost and you reconnect with ping(), the\n\
  1819. thread ID will change. This means you should not get the\n\
  1820. thread ID and store it for later. You should get it when you\n\
  1821. need it.\n\
  1822. \n\
  1823. Non-standard.";
  1824. static PyObject *
  1825. _mysql_ConnectionObject_thread_id(
  1826. _mysql_ConnectionObject *self,
  1827. PyObject *args)
  1828. {
  1829. unsigned long pid;
  1830. if (!PyArg_ParseTuple(args, "")) return NULL;
  1831. check_connection(self);
  1832. Py_BEGIN_ALLOW_THREADS
  1833. pid = mysql_thread_id(&(self->connection));
  1834. Py_END_ALLOW_THREADS
  1835. return PyInt_FromLong((long)pid);
  1836. }
  1837. static char _mysql_ConnectionObject_use_result__doc__[] =
  1838. "Returns a result object acquired by mysql_use_result\n\
  1839. (results stored in the server). If no results are available,\n\
  1840. None is returned. Non-standard.\n\
  1841. ";
  1842. static PyObject *
  1843. _mysql_ConnectionObject_use_result(
  1844. _mysql_ConnectionObject *self,
  1845. PyObject *args)
  1846. {
  1847. PyObject *arglist=NULL, *kwarglist=NULL, *result=NULL;
  1848. _mysql_ResultObject *r=NULL;
  1849. if (!PyArg_ParseTuple(args, "")) return NULL;
  1850. check_connection(self);
  1851. arglist = Py_BuildValue("(OiO)", self, 1, self->converter);
  1852. if (!arglist) return NULL;
  1853. kwarglist = PyDict_New();
  1854. if (!kwarglist) goto error;
  1855. r = MyAlloc(_mysql_ResultObject, _mysql_ResultObject_Type);
  1856. if (!r) goto error;
  1857. result = (PyObject *) r;
  1858. if (_mysql_ResultObject_Initialize(r, arglist, kwarglist))
  1859. goto error;
  1860. if (!(r->result)) {
  1861. Py_DECREF(result);
  1862. Py_INCREF(Py_None);
  1863. result = Py_None;
  1864. }
  1865. error:
  1866. Py_DECREF(arglist);
  1867. Py_XDECREF(kwarglist);
  1868. return result;
  1869. }
  1870. static void
  1871. _mysql_ConnectionObject_dealloc(
  1872. _mysql_ConnectionObject *self)
  1873. {
  1874. PyObject *o;
  1875. PyObject_GC_UnTrack(self);
  1876. if (self->open) {
  1877. o = _mysql_ConnectionObject_close(self, NULL);
  1878. Py_XDECREF(o);
  1879. }
  1880. MyFree(self);
  1881. }
  1882. static PyObject *
  1883. _mysql_ConnectionObject_repr(
  1884. _mysql_ConnectionObject *self)
  1885. {
  1886. char buf[300];
  1887. if (self->open)
  1888. sprintf(buf, "<_mysql.connection open to '%.256s' at %lx>",
  1889. self->connection.host,
  1890. (long)self);
  1891. else
  1892. sprintf(buf, "<_mysql.connection closed at %lx>",
  1893. (long)self);
  1894. return PyString_FromString(buf);
  1895. }
  1896. static char _mysql_ResultObject_data_seek__doc__[] =
  1897. "data_seek(n) -- seek to row n of result set";
  1898. static PyObject *
  1899. _mysql_ResultObject_data_seek(
  1900. _mysql_ResultObject *self,
  1901. PyObject *args)
  1902. {
  1903. unsigned int row;
  1904. if (!PyArg_ParseTuple(args, "i:data_seek", &row)) return NULL;
  1905. check_result_connection(self);
  1906. mysql_data_seek(self->result, row);
  1907. Py_INCREF(Py_None);
  1908. return Py_None;
  1909. }
  1910. static char _mysql_ResultObject_row_seek__doc__[] =
  1911. "row_seek(n) -- seek by offset n rows of result set";
  1912. static PyObject *
  1913. _mysql_ResultObject_row_seek(
  1914. _mysql_ResultObject *self,
  1915. PyObject *args)
  1916. {
  1917. int offset;
  1918. MYSQL_ROW_OFFSET r;
  1919. if (!PyArg_ParseTuple(args, "i:row_seek", &offset)) return NULL;
  1920. check_result_connection(self);
  1921. if (self->use) {
  1922. PyErr_SetString(_mysql_ProgrammingError,
  1923. "cannot be used with connection.use_result()");
  1924. return NULL;
  1925. }
  1926. r = mysql_row_tell(self->result);
  1927. mysql_row_seek(self->result, r+offset);
  1928. Py_INCREF(Py_None);
  1929. return Py_None;
  1930. }
  1931. static char _mysql_ResultObject_row_tell__doc__[] =
  1932. "row_tell() -- return the current row number of the result set.";
  1933. static PyObject *
  1934. _mysql_ResultObject_row_tell(
  1935. _mysql_ResultObject *self,
  1936. PyObject *args)
  1937. {
  1938. MYSQL_ROW_OFFSET r;
  1939. if (!PyArg_ParseTuple(args, "")) return NULL;
  1940. check_result_connection(self);
  1941. if (self->use) {
  1942. PyErr_SetString(_mysql_ProgrammingError,
  1943. "cannot be used with connection.use_result()");
  1944. return NULL;
  1945. }
  1946. r = mysql_row_tell(self->result);
  1947. return PyInt_FromLong(r-self->result->data->data);
  1948. }
  1949. static void
  1950. _mysql_ResultObject_dealloc(
  1951. _mysql_ResultObject *self)
  1952. {
  1953. PyObject_GC_UnTrack((PyObject *)self);
  1954. mysql_free_result(self->result);
  1955. _mysql_ResultObject_clear(self);
  1956. MyFree(self);
  1957. }
  1958. static PyObject *
  1959. _mysql_ResultObject_repr(
  1960. _mysql_ResultObject *self)
  1961. {
  1962. char buf[300];
  1963. sprintf(buf, "<_mysql.result object at %lx>",
  1964. (long)self);
  1965. return PyString_FromString(buf);
  1966. }
  1967. static PyMethodDef _mysql_ConnectionObject_methods[] = {
  1968. {
  1969. "affected_rows",
  1970. (PyCFunction)_mysql_ConnectionObject_affected_rows,
  1971. METH_VARARGS,
  1972. _mysql_ConnectionObject_affected_rows__doc__
  1973. },
  1974. {
  1975. "autocommit",
  1976. (PyCFunction)_mysql_ConnectionObject_autocommit,
  1977. METH_VARARGS,
  1978. _mysql_ConnectionObject_autocommit__doc__
  1979. },
  1980. {
  1981. "commit",
  1982. (PyCFunction)_mysql_ConnectionObject_commit,
  1983. METH_VARARGS,
  1984. _mysql_ConnectionObject_commit__doc__
  1985. },
  1986. {
  1987. "rollback",
  1988. (PyCFunction)_mysql_ConnectionObject_rollback,
  1989. METH_VARARGS,
  1990. _mysql_ConnectionObject_rollback__doc__
  1991. },
  1992. {
  1993. "next_result",
  1994. (PyCFunction)_mysql_ConnectionObject_next_result,
  1995. METH_VARARGS,
  1996. _mysql_ConnectionObject_next_result__doc__
  1997. },
  1998. #if MYSQL_VERSION_ID >= 40100
  1999. {
  2000. "set_server_option",
  2001. (PyCFunction)_mysql_ConnectionObject_set_server_option,
  2002. METH_VARARGS,
  2003. _mysql_ConnectionObject_set_server_option__doc__
  2004. },
  2005. {
  2006. "sqlstate",
  2007. (PyCFunction)_mysql_ConnectionObject_sqlstate,
  2008. METH_VARARGS,
  2009. _mysql_ConnectionObject_sqlstate__doc__
  2010. },
  2011. {
  2012. "warning_count",
  2013. (PyCFunction)_mysql_ConnectionObject_warning_count,
  2014. METH_VARARGS,
  2015. _mysql_ConnectionObject_warning_count__doc__
  2016. },
  2017. #endif
  2018. #if MYSQL_VERSION_ID >= 32303
  2019. {
  2020. "change_user",
  2021. (PyCFunction)_mysql_ConnectionObject_change_user,
  2022. METH_VARARGS | METH_KEYWORDS,
  2023. _mysql_ConnectionObject_change_user__doc__
  2024. },
  2025. #endif
  2026. {
  2027. "character_set_name",
  2028. (PyCFunction)_mysql_ConnectionObject_character_set_name,
  2029. METH_VARARGS,
  2030. _mysql_ConnectionObject_character_set_name__doc__
  2031. },
  2032. #if MYSQL_VERSION_ID >= 50007
  2033. {
  2034. "set_character_set",
  2035. (PyCFunction)_mysql_ConnectionObject_set_character_set,
  2036. METH_VARARGS,
  2037. _mysql_ConnectionObject_set_character_set__doc__
  2038. },
  2039. #endif
  2040. #if MYSQL_VERSION_ID >= 50010
  2041. {
  2042. "get_character_set_info",
  2043. (PyCFunction)_mysql_ConnectionObject_get_character_set_info,
  2044. METH_VARARGS,
  2045. _mysql_ConnectionObject_get_character_set_info__doc__
  2046. },
  2047. #endif
  2048. {
  2049. "close",
  2050. (PyCFunction)_mysql_ConnectionObject_close,
  2051. METH_VARARGS,
  2052. _mysql_ConnectionObject_close__doc__
  2053. },
  2054. {
  2055. "dump_debug_info",
  2056. (PyCFunction)_mysql_ConnectionObject_dump_debug_info,
  2057. METH_VARARGS,
  2058. _mysql_ConnectionObject_dump_debug_info__doc__
  2059. },
  2060. {
  2061. "escape",
  2062. (PyCFunction)_mysql_escape,
  2063. METH_VARARGS,
  2064. _mysql_escape__doc__
  2065. },
  2066. {
  2067. "escape_string",
  2068. (PyCFunction)_mysql_escape_string,
  2069. METH_VARARGS,
  2070. _mysql_escape_string__doc__
  2071. },
  2072. {
  2073. "error",
  2074. (PyCFunction)_mysql_ConnectionObject_error,
  2075. METH_VARARGS,
  2076. _mysql_ConnectionObject_error__doc__
  2077. },
  2078. {
  2079. "errno",
  2080. (PyCFunction)_mysql_ConnectionObject_errno,
  2081. METH_VARARGS,
  2082. _mysql_ConnectionObject_errno__doc__
  2083. },
  2084. {
  2085. "field_count",
  2086. (PyCFunction)_mysql_ConnectionObject_field_count,
  2087. METH_VARARGS,
  2088. _mysql_ConnectionObject_field_count__doc__
  2089. },
  2090. {
  2091. "get_host_info",
  2092. (PyCFunction)_mysql_ConnectionObject_get_host_info,
  2093. METH_VARARGS,
  2094. _mysql_ConnectionObject_get_host_info__doc__
  2095. },
  2096. {
  2097. "get_proto_info",
  2098. (PyCFunction)_mysql_ConnectionObject_get_proto_info,
  2099. METH_VARARGS,
  2100. _mysql_ConnectionObject_get_proto_info__doc__
  2101. },
  2102. {
  2103. "get_server_info",
  2104. (PyCFunction)_mysql_ConnectionObject_get_server_info,
  2105. METH_VARARGS,
  2106. _mysql_ConnectionObject_get_server_info__doc__
  2107. },
  2108. {
  2109. "info",
  2110. (PyCFunction)_mysql_ConnectionObject_info,
  2111. METH_VARARGS,
  2112. _mysql_ConnectionObject_info__doc__
  2113. },
  2114. {
  2115. "insert_id",
  2116. (PyCFunction)_mysql_ConnectionObject_insert_id,
  2117. METH_VARARGS,
  2118. _mysql_ConnectionObject_insert_id__doc__
  2119. },
  2120. {
  2121. "kill",
  2122. (PyCFunction)_mysql_ConnectionObject_kill,
  2123. METH_VARARGS,
  2124. _mysql_ConnectionObject_kill__doc__
  2125. },
  2126. {
  2127. "ping",
  2128. (PyCFunction)_mysql_ConnectionObject_ping,
  2129. METH_VARARGS,
  2130. _mysql_ConnectionObject_ping__doc__
  2131. },
  2132. {
  2133. "query",
  2134. (PyCFunction)_mysql_ConnectionObject_query,
  2135. METH_VARARGS,
  2136. _mysql_ConnectionObject_query__doc__
  2137. },
  2138. {
  2139. "select_db",
  2140. (PyCFunction)_mysql_ConnectionObject_select_db,
  2141. METH_VARARGS,
  2142. _mysql_ConnectionObject_select_db__doc__
  2143. },
  2144. {
  2145. "shutdown",
  2146. (PyCFunction)_mysql_ConnectionObject_shutdown,
  2147. METH_VARARGS,
  2148. _mysql_ConnectionObject_shutdown__doc__
  2149. },
  2150. {
  2151. "stat",
  2152. (PyCFunction)_mysql_ConnectionObject_stat,
  2153. METH_VARARGS,
  2154. _mysql_ConnectionObject_stat__doc__
  2155. },
  2156. {
  2157. "store_result",
  2158. (PyCFunction)_mysql_ConnectionObject_store_result,
  2159. METH_VARARGS,
  2160. _mysql_ConnectionObject_store_result__doc__
  2161. },
  2162. {
  2163. "string_literal",
  2164. (PyCFunction)_mysql_string_literal,
  2165. METH_VARARGS,
  2166. _mysql_string_literal__doc__},
  2167. {
  2168. "thread_id",
  2169. (PyCFunction)_mysql_ConnectionObject_thread_id,
  2170. METH_VARARGS,
  2171. _mysql_ConnectionObject_thread_id__doc__
  2172. },
  2173. {
  2174. "use_result",
  2175. (PyCFunction)_mysql_ConnectionObject_use_result,
  2176. METH_VARARGS,
  2177. _mysql_ConnectionObject_use_result__doc__
  2178. },
  2179. {NULL, NULL} /* sentinel */
  2180. };
  2181. static MyMemberlist(_mysql_ConnectionObject_memberlist)[] = {
  2182. MyMember(
  2183. "open",
  2184. T_INT,
  2185. offsetof(_mysql_ConnectionObject,open),
  2186. RO,
  2187. "True if connection is open"
  2188. ),
  2189. MyMember(
  2190. "converter",
  2191. T_OBJECT,
  2192. offsetof(_mysql_ConnectionObject,converter),
  2193. 0,
  2194. "Type conversion mapping"
  2195. ),
  2196. MyMember(
  2197. "server_capabilities",
  2198. T_UINT,
  2199. offsetof(_mysql_ConnectionObject,connection.server_capabilities),
  2200. RO,
  2201. "Capabilites of server; consult MySQLdb.constants.CLIENT"
  2202. ),
  2203. MyMember(
  2204. "port",
  2205. T_UINT,
  2206. offsetof(_mysql_ConnectionObject,connection.port),
  2207. RO,
  2208. "TCP/IP port of the server connection"
  2209. ),
  2210. MyMember(
  2211. "client_flag",
  2212. T_UINT,
  2213. RO,
  2214. offsetof(_mysql_ConnectionObject,connection.client_flag),
  2215. "Client flags; refer to MySQLdb.constants.CLIENT"
  2216. ),
  2217. {NULL} /* Sentinel */
  2218. };
  2219. static PyMethodDef _mysql_ResultObject_methods[] = {
  2220. {
  2221. "data_seek",
  2222. (PyCFunction)_mysql_ResultObject_data_seek,
  2223. METH_VARARGS,
  2224. _mysql_ResultObject_data_seek__doc__
  2225. },
  2226. {
  2227. "row_seek",
  2228. (PyCFunction)_mysql_ResultObject_row_seek,
  2229. METH_VARARGS,
  2230. _mysql_ResultObject_row_seek__doc__
  2231. },
  2232. {
  2233. "row_tell",
  2234. (PyCFunction)_mysql_ResultObject_row_tell,
  2235. METH_VARARGS,
  2236. _mysql_ResultObject_row_tell__doc__
  2237. },
  2238. {
  2239. "describe",
  2240. (PyCFunction)_mysql_ResultObject_describe,
  2241. METH_VARARGS,
  2242. _mysql_ResultObject_describe__doc__
  2243. },
  2244. {
  2245. "fetch_row",
  2246. (PyCFunction)_mysql_ResultObject_fetch_row,
  2247. METH_VARARGS | METH_KEYWORDS,
  2248. _mysql_ResultObject_fetch_row__doc__
  2249. },
  2250. {
  2251. "field_flags",
  2252. (PyCFunction)_mysql_ResultObject_field_flags,
  2253. METH_VARARGS,
  2254. _mysql_ResultObject_field_flags__doc__
  2255. },
  2256. {
  2257. "num_fields",
  2258. (PyCFunction)_mysql_ResultObject_num_fields,
  2259. METH_VARARGS,
  2260. _mysql_ResultObject_num_fields__doc__
  2261. },
  2262. {
  2263. "num_rows",
  2264. (PyCFunction)_mysql_ResultObject_num_rows,
  2265. METH_VARARGS,
  2266. _mysql_ResultObject_num_rows__doc__
  2267. },
  2268. {NULL, NULL} /* sentinel */
  2269. };
  2270. static MyMemberlist(_mysql_ResultObject_memberlist)[] = {
  2271. MyMember(
  2272. "converter",
  2273. T_OBJECT,
  2274. offsetof(_mysql_ResultObject,converter),
  2275. RO,
  2276. "Type conversion mapping"
  2277. ),
  2278. {NULL} /* Sentinel */
  2279. };
  2280. static PyObject *
  2281. _mysql_ConnectionObject_getattr(
  2282. _mysql_ConnectionObject *self,
  2283. char *name)
  2284. {
  2285. PyObject *res;
  2286. res = Py_FindMethod(_mysql_ConnectionObject_methods, (PyObject *)self, name);
  2287. if (res != NULL)
  2288. return res;
  2289. PyErr_Clear();
  2290. if (strcmp(name, "closed") == 0)
  2291. return PyInt_FromLong((long)!(self->open));
  2292. #if PY_VERSION_HEX < 0x02020000
  2293. return PyMember_Get((char *)self, _mysql_ConnectionObject_memberlist, name);
  2294. #else
  2295. {
  2296. MyMemberlist(*l);
  2297. for (l = _mysql_ConnectionObject_memberlist; l->name != NULL; l++) {
  2298. if (strcmp(l->name, name) == 0)
  2299. return PyMember_GetOne((char *)self, l);
  2300. }
  2301. PyErr_SetString(PyExc_AttributeError, name);
  2302. return NULL;
  2303. }
  2304. #endif
  2305. }
  2306. static PyObject *
  2307. _mysql_ResultObject_getattr(
  2308. _mysql_ResultObject *self,
  2309. char *name)
  2310. {
  2311. PyObject *res;
  2312. res = Py_FindMethod(_mysql_ResultObject_methods, (PyObject *)self, name);
  2313. if (res != NULL)
  2314. return res;
  2315. PyErr_Clear();
  2316. #if PY_VERSION_HEX < 0x02020000
  2317. return PyMember_Get((char *)self, _mysql_ResultObject_memberlist, name);
  2318. #else
  2319. {
  2320. MyMemberlist(*l);
  2321. for (l = _mysql_ResultObject_memberlist; l->name != NULL; l++) {
  2322. if (strcmp(l->name, name) == 0)
  2323. return PyMember_GetOne((char *)self, l);
  2324. }
  2325. PyErr_SetString(PyExc_AttributeError, name);
  2326. return NULL;
  2327. }
  2328. #endif
  2329. }
  2330. static int
  2331. _mysql_ConnectionObject_setattr(
  2332. _mysql_ConnectionObject *self,
  2333. char *name,
  2334. PyObject *v)
  2335. {
  2336. if (v == NULL) {
  2337. PyErr_SetString(PyExc_AttributeError,
  2338. "can't delete connection attributes");
  2339. return -1;
  2340. }
  2341. #if PY_VERSION_HEX < 0x02020000
  2342. return PyMember_Set((char *)self, _mysql_ConnectionObject_memberlist, name, v);
  2343. #else
  2344. {
  2345. MyMemberlist(*l);
  2346. for (l = _mysql_ConnectionObject_memberlist; l->name != NULL; l++)
  2347. if (strcmp(l->name, name) == 0)
  2348. return PyMember_SetOne((char *)self, l, v);
  2349. }
  2350. PyErr_SetString(PyExc_AttributeError, name);
  2351. return -1;
  2352. #endif
  2353. }
  2354. static int
  2355. _mysql_ResultObject_setattr(
  2356. _mysql_ResultObject *self,
  2357. char *name,
  2358. PyObject *v)
  2359. {
  2360. if (v == NULL) {
  2361. PyErr_SetString(PyExc_AttributeError,
  2362. "can't delete connection attributes");
  2363. return -1;
  2364. }
  2365. #if PY_VERSION_HEX < 0x02020000
  2366. return PyMember_Set((char *)self, _mysql_ResultObject_memberlist, name, v);
  2367. #else
  2368. {
  2369. MyMemberlist(*l);
  2370. for (l = _mysql_ResultObject_memberlist; l->name != NULL; l++)
  2371. if (strcmp(l->name, name) == 0)
  2372. return PyMember_SetOne((char *)self, l, v);
  2373. }
  2374. PyErr_SetString(PyExc_AttributeError, name);
  2375. return -1;
  2376. #endif
  2377. }
  2378. PyTypeObject _mysql_ConnectionObject_Type = {
  2379. PyObject_HEAD_INIT(NULL)
  2380. 0,
  2381. "_mysql.connection", /* (char *)tp_name For printing */
  2382. sizeof(_mysql_ConnectionObject),
  2383. 0,
  2384. (destructor)_mysql_ConnectionObject_dealloc, /* tp_dealloc */
  2385. 0, /*tp_print*/
  2386. (getattrfunc)_mysql_ConnectionObject_getattr, /* tp_getattr */
  2387. (setattrfunc)_mysql_ConnectionObject_setattr, /* tp_setattr */
  2388. 0, /*tp_compare*/
  2389. (reprfunc)_mysql_ConnectionObject_repr, /* tp_repr */
  2390. /* Method suites for standard classes */
  2391. 0, /* (PyNumberMethods *) tp_as_number */
  2392. 0, /* (PySequenceMethods *) tp_as_sequence */
  2393. 0, /* (PyMappingMethods *) tp_as_mapping */
  2394. /* More standard operations (here for binary compatibility) */
  2395. 0, /* (hashfunc) tp_hash */
  2396. 0, /* (ternaryfunc) tp_call */
  2397. 0, /* (reprfunc) tp_str */
  2398. 0, /* (getattrofunc) tp_getattro */
  2399. 0, /* (setattrofunc) tp_setattro */
  2400. /* Functions to access object as input/output buffer */
  2401. 0, /* (PyBufferProcs *) tp_as_buffer */
  2402. /* Flags to define presence of optional/expanded features */
  2403. #if PY_VERSION_HEX < 0x02020000
  2404. Py_TPFLAGS_DEFAULT, /* (long) tp_flags */
  2405. #else
  2406. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE,
  2407. #endif
  2408. _mysql_connect__doc__, /* (char *) tp_doc Documentation string */
  2409. #if PY_VERSION_HEX >= 0x02000000
  2410. /* Assigned meaning in release 2.0 */
  2411. #if PY_VERSION_HEX >= 0x02020000
  2412. /* call function for all accessible objects */
  2413. (traverseproc) _mysql_ConnectionObject_traverse, /* tp_traverse */
  2414. /* delete references to contained objects */
  2415. (inquiry) _mysql_ConnectionObject_clear, /* tp_clear */
  2416. #else
  2417. /* not supporting pre-2.2 GC */
  2418. 0,
  2419. 0,
  2420. #endif
  2421. #if PY_VERSION_HEX >= 0x02010000
  2422. /* Assigned meaning in release 2.1 */
  2423. /* rich comparisons */
  2424. 0, /* (richcmpfunc) tp_richcompare */
  2425. /* weak reference enabler */
  2426. 0, /* (long) tp_weaklistoffset */
  2427. #if PY_VERSION_HEX >= 0x02020000
  2428. /* Added in release 2.2 */
  2429. /* Iterators */
  2430. 0, /* (getiterfunc) tp_iter */
  2431. 0, /* (iternextfunc) tp_iternext */
  2432. /* Attribute descriptor and subclassing stuff */
  2433. (struct PyMethodDef *)_mysql_ConnectionObject_methods, /* tp_methods */
  2434. (MyMemberlist(*))_mysql_ConnectionObject_memberlist, /* tp_members */
  2435. 0, /* (struct getsetlist *) tp_getset; */
  2436. 0, /* (struct _typeobject *) tp_base; */
  2437. 0, /* (PyObject *) tp_dict */
  2438. 0, /* (descrgetfunc) tp_descr_get */
  2439. 0, /* (descrsetfunc) tp_descr_set */
  2440. 0, /* (long) tp_dictoffset */
  2441. (initproc)_mysql_ConnectionObject_Initialize, /* tp_init */
  2442. NULL, /* tp_alloc */
  2443. NULL, /* tp_new */
  2444. NULL, /* tp_free Low-level free-memory routine */
  2445. 0, /* (PyObject *) tp_bases */
  2446. 0, /* (PyObject *) tp_mro method resolution order */
  2447. 0, /* (PyObject *) tp_defined */
  2448. #endif /* python 2.2 */
  2449. #endif /* python 2.1 */
  2450. #endif /* python 2.0 */
  2451. } ;
  2452. PyTypeObject _mysql_ResultObject_Type = {
  2453. PyObject_HEAD_INIT(NULL)
  2454. 0,
  2455. "_mysql.result",
  2456. sizeof(_mysql_ResultObject),
  2457. 0,
  2458. (destructor)_mysql_ResultObject_dealloc, /* tp_dealloc */
  2459. 0, /*tp_print*/
  2460. (getattrfunc)_mysql_ResultObject_getattr, /* tp_getattr */
  2461. (setattrfunc)_mysql_ResultObject_setattr, /* tp_setattr */
  2462. 0, /*tp_compare*/
  2463. (reprfunc)_mysql_ResultObject_repr, /* tp_repr */
  2464. /* Method suites for standard classes */
  2465. 0, /* (PyNumberMethods *) tp_as_number */
  2466. 0, /* (PySequenceMethods *) tp_as_sequence */
  2467. 0, /* (PyMappingMethods *) tp_as_mapping */
  2468. /* More standard operations (here for binary compatibility) */
  2469. 0, /* (hashfunc) tp_hash */
  2470. 0, /* (ternaryfunc) tp_call */
  2471. 0, /* (reprfunc) tp_str */
  2472. 0, /* (getattrofunc) tp_getattro */
  2473. 0, /* (setattrofunc) tp_setattro */
  2474. /* Functions to access object as input/output buffer */
  2475. 0, /* (PyBufferProcs *) tp_as_buffer */
  2476. /* Flags to define presence of optional/expanded features */
  2477. #if PY_VERSION_HEX < 0x02020000
  2478. Py_TPFLAGS_DEFAULT, /* (long) tp_flags */
  2479. #else
  2480. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE,
  2481. #endif
  2482. _mysql_ResultObject__doc__, /* (char *) tp_doc Documentation string */
  2483. #if PY_VERSION_HEX >= 0x02000000
  2484. /* Assigned meaning in release 2.0 */
  2485. #if PY_VERSION_HEX >= 0x02020000
  2486. /* call function for all accessible objects */
  2487. (traverseproc) _mysql_ResultObject_traverse, /* tp_traverse */
  2488. /* delete references to contained objects */
  2489. (inquiry) _mysql_ResultObject_clear, /* tp_clear */
  2490. #else
  2491. /* not supporting pre-2.2 GC */
  2492. 0,
  2493. 0,
  2494. #endif
  2495. #if PY_VERSION_HEX >= 0x02010000
  2496. /* Assigned meaning in release 2.1 */
  2497. /* rich comparisons */
  2498. 0, /* (richcmpfunc) tp_richcompare */
  2499. /* weak reference enabler */
  2500. 0, /* (long) tp_weaklistoffset */
  2501. #if PY_VERSION_HEX >= 0x02020000
  2502. /* Added in release 2.2 */
  2503. /* Iterators */
  2504. 0, /* (getiterfunc) tp_iter */
  2505. 0, /* (iternextfunc) tp_iternext */
  2506. /* Attribute descriptor and subclassing stuff */
  2507. (struct PyMethodDef *) _mysql_ResultObject_methods, /* tp_methods */
  2508. (MyMemberlist(*)) _mysql_ResultObject_memberlist, /*tp_members */
  2509. 0, /* (struct getsetlist *) tp_getset; */
  2510. 0, /* (struct _typeobject *) tp_base; */
  2511. 0, /* (PyObject *) tp_dict */
  2512. 0, /* (descrgetfunc) tp_descr_get */
  2513. 0, /* (descrsetfunc) tp_descr_set */
  2514. 0, /* (long) tp_dictoffset */
  2515. (initproc)_mysql_ResultObject_Initialize, /* tp_init */
  2516. NULL, /* tp_alloc */
  2517. NULL, /* tp_new */
  2518. NULL, /* tp_free Low-level free-memory routine */
  2519. 0, /* (PyObject *) tp_bases */
  2520. 0, /* (PyObject *) tp_mro method resolution order */
  2521. 0, /* (PyObject *) tp_defined */
  2522. #endif /* python 2.2 */
  2523. #endif /* python 2.1 */
  2524. #endif /* python 2.0 */
  2525. };
  2526. static PyMethodDef
  2527. _mysql_methods[] = {
  2528. {
  2529. "connect",
  2530. (PyCFunction)_mysql_connect,
  2531. METH_VARARGS | METH_KEYWORDS,
  2532. _mysql_connect__doc__
  2533. },
  2534. {
  2535. "debug",
  2536. (PyCFunction)_mysql_debug,
  2537. METH_VARARGS,
  2538. _mysql_debug__doc__
  2539. },
  2540. {
  2541. "escape",
  2542. (PyCFunction)_mysql_escape,
  2543. METH_VARARGS,
  2544. _mysql_escape__doc__
  2545. },
  2546. {
  2547. "escape_sequence",
  2548. (PyCFunction)_mysql_escape_sequence,
  2549. METH_VARARGS,
  2550. _mysql_escape_sequence__doc__
  2551. },
  2552. {
  2553. "escape_dict",
  2554. (PyCFunction)_mysql_escape_dict,
  2555. METH_VARARGS,
  2556. _mysql_escape_dict__doc__
  2557. },
  2558. {
  2559. "escape_string",
  2560. (PyCFunction)_mysql_escape_string,
  2561. METH_VARARGS,
  2562. _mysql_escape_string__doc__
  2563. },
  2564. {
  2565. "string_literal",
  2566. (PyCFunction)_mysql_string_literal,
  2567. METH_VARARGS,
  2568. _mysql_string_literal__doc__
  2569. },
  2570. {
  2571. "get_client_info",
  2572. (PyCFunction)_mysql_get_client_info,
  2573. METH_VARARGS,
  2574. _mysql_get_client_info__doc__
  2575. },
  2576. #if MYSQL_VERSION_ID >= 32314
  2577. {
  2578. "thread_safe",
  2579. (PyCFunction)_mysql_thread_safe,
  2580. METH_VARARGS,
  2581. _mysql_thread_safe__doc__
  2582. },
  2583. #endif
  2584. {
  2585. "server_init",
  2586. (PyCFunction)_mysql_server_init,
  2587. METH_VARARGS | METH_KEYWORDS,
  2588. _mysql_server_init__doc__
  2589. },
  2590. {
  2591. "server_end",
  2592. (PyCFunction)_mysql_server_end,
  2593. METH_VARARGS,
  2594. _mysql_server_end__doc__
  2595. },
  2596. {NULL, NULL} /* sentinel */
  2597. };
  2598. static PyObject *
  2599. _mysql_NewException(
  2600. PyObject *dict,
  2601. PyObject *edict,
  2602. char *name)
  2603. {
  2604. PyObject *e;
  2605. if (!(e = PyDict_GetItemString(edict, name)))
  2606. return NULL;
  2607. if (PyDict_SetItemString(dict, name, e)) return NULL;
  2608. return e;
  2609. }
  2610. #define QUOTE(X) _QUOTE(X)
  2611. #define _QUOTE(X) #X
  2612. static char _mysql___doc__[] =
  2613. "an adaptation of the MySQL C API (mostly)\n\
  2614. \n\
  2615. You probably are better off using MySQLdb instead of using this\n\
  2616. module directly.\n\
  2617. \n\
  2618. In general, renaming goes from mysql_* to _mysql.*. _mysql.connect()\n\
  2619. returns a connection object (MYSQL). Functions which expect MYSQL * as\n\
  2620. an argument are now methods of the connection object. A number of things\n\
  2621. return result objects (MYSQL_RES). Functions which expect MYSQL_RES * as\n\
  2622. an argument are now methods of the result object. Deprecated functions\n\
  2623. (as of 3.23) are NOT implemented.\n\
  2624. ";
  2625. DL_EXPORT(void)
  2626. init_mysql(void)
  2627. {
  2628. PyObject *dict, *module, *emod, *edict;
  2629. module = Py_InitModule4("_mysql", _mysql_methods, _mysql___doc__,
  2630. (PyObject *)NULL, PYTHON_API_VERSION);
  2631. if (!module) return; /* this really should never happen */
  2632. _mysql_ConnectionObject_Type.ob_type = &PyType_Type;
  2633. _mysql_ResultObject_Type.ob_type = &PyType_Type;
  2634. #if PY_VERSION_HEX >= 0x02020000
  2635. _mysql_ConnectionObject_Type.tp_alloc = PyType_GenericAlloc;
  2636. _mysql_ConnectionObject_Type.tp_new = PyType_GenericNew;
  2637. _mysql_ConnectionObject_Type.tp_free = _PyObject_GC_Del;
  2638. _mysql_ResultObject_Type.tp_alloc = PyType_GenericAlloc;
  2639. _mysql_ResultObject_Type.tp_new = PyType_GenericNew;
  2640. _mysql_ResultObject_Type.tp_free = _PyObject_GC_Del;
  2641. #endif
  2642. if (!(dict = PyModule_GetDict(module))) goto error;
  2643. if (PyDict_SetItemString(dict, "version_info",
  2644. PyRun_String(QUOTE(version_info), Py_eval_input,
  2645. dict, dict)))
  2646. goto error;
  2647. if (PyDict_SetItemString(dict, "__version__",
  2648. PyString_FromString(QUOTE(__version__))))
  2649. goto error;
  2650. if (PyDict_SetItemString(dict, "connection",
  2651. (PyObject *)&_mysql_ConnectionObject_Type))
  2652. goto error;
  2653. Py_INCREF(&_mysql_ConnectionObject_Type);
  2654. if (PyDict_SetItemString(dict, "result",
  2655. (PyObject *)&_mysql_ResultObject_Type))
  2656. goto error;
  2657. Py_INCREF(&_mysql_ResultObject_Type);
  2658. if (!(emod = PyImport_ImportModule("_mysql_exceptions")))
  2659. goto error;
  2660. if (!(edict = PyModule_GetDict(emod))) goto error;
  2661. if (!(_mysql_MySQLError =
  2662. _mysql_NewException(dict, edict, "MySQLError")))
  2663. goto error;
  2664. if (!(_mysql_Warning =
  2665. _mysql_NewException(dict, edict, "Warning")))
  2666. goto error;
  2667. if (!(_mysql_Error =
  2668. _mysql_NewException(dict, edict, "Error")))
  2669. goto error;
  2670. if (!(_mysql_InterfaceError =
  2671. _mysql_NewException(dict, edict, "InterfaceError")))
  2672. goto error;
  2673. if (!(_mysql_DatabaseError =
  2674. _mysql_NewException(dict, edict, "DatabaseError")))
  2675. goto error;
  2676. if (!(_mysql_DataError =
  2677. _mysql_NewException(dict, edict, "DataError")))
  2678. goto error;
  2679. if (!(_mysql_OperationalError =
  2680. _mysql_NewException(dict, edict, "OperationalError")))
  2681. goto error;
  2682. if (!(_mysql_IntegrityError =
  2683. _mysql_NewException(dict, edict, "IntegrityError")))
  2684. goto error;
  2685. if (!(_mysql_InternalError =
  2686. _mysql_NewException(dict, edict, "InternalError")))
  2687. goto error;
  2688. if (!(_mysql_ProgrammingError =
  2689. _mysql_NewException(dict, edict, "ProgrammingError")))
  2690. goto error;
  2691. if (!(_mysql_NotSupportedError =
  2692. _mysql_NewException(dict, edict, "NotSupportedError")))
  2693. goto error;
  2694. Py_DECREF(emod);
  2695. if (!(_mysql_NULL = PyString_FromString("NULL")))
  2696. goto error;
  2697. if (PyDict_SetItemString(dict, "NULL", _mysql_NULL)) goto error;
  2698. error:
  2699. if (PyErr_Occurred())
  2700. PyErr_SetString(PyExc_ImportError,
  2701. "_mysql: init failed");
  2702. return;
  2703. }