_mysql.c 83 KB

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