-
Notifications
You must be signed in to change notification settings - Fork 46
/
_mysql.c
2921 lines (2707 loc) · 74.2 KB
/
_mysql.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version. Alternatively, you may use the original license
reproduced below.
Copyright 1999 by Comstar.net, Inc., Atlanta, GA, US.
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Comstar.net, Inc.
or COMSTAR not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.
COMSTAR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
EVENT SHALL COMSTAR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "Python.h"
#if PY_VERSION_HEX > 0x02060000
#include "bytesobject.h"
#endif
#include "pymemcompat.h"
#include "structmember.h"
#include "my_config.h"
#include "mysql.h"
#include "mysqld_error.h"
#include "errmsg.h"
# define MyTuple_Resize(t,n,d) _PyTuple_Resize(t, n)
# define MyMember(a,b,c,d,e) {a,b,c,d,e}
# define MyMemberlist(x) struct PyMemberDef x
# define MyAlloc(s,t) (s *) t.tp_alloc(&t,0)
# define MyFree(ob) ob->ob_base.ob_type->tp_free((PyObject *)ob)
static PyObject *_mysql_MySQLError;
static PyObject *_mysql_Warning;
static PyObject *_mysql_Error;
static PyObject *_mysql_DatabaseError;
static PyObject *_mysql_InterfaceError;
static PyObject *_mysql_DataError;
static PyObject *_mysql_OperationalError;
static PyObject *_mysql_IntegrityError;
static PyObject *_mysql_InternalError;
static PyObject *_mysql_ProgrammingError;
static PyObject *_mysql_NotSupportedError;
typedef struct {
PyObject_HEAD
MYSQL connection;
int open;
PyObject *converter;
} _mysql_ConnectionObject;
#define check_connection(c) if (!(c->open)) return _mysql_Exception(c)
#define result_connection(r) ((_mysql_ConnectionObject *)r->conn)
#define check_result_connection(r) check_connection(result_connection(r))
extern PyTypeObject _mysql_ConnectionObject_Type;
typedef struct {
PyObject_HEAD
_mysql_ConnectionObject *conn;
MYSQL_RES *result;
int nfields;
int use;
PyObject *converter;
} _mysql_ResultObject;
extern PyTypeObject _mysql_ResultObject_Type;
static int _mysql_server_init_done = 0;
#if MYSQL_VERSION_ID >= 40000
#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;} }
#else
#define check_server_init(x) if (!_mysql_server_init_done) _mysql_server_init_done = 1
#endif
#if MYSQL_VERSION_ID >= 50500
#define HAVE_OPENSSL 1
#endif
/* According to https://dev.mysql.com/doc/refman/5.1/en/mysql-options.html
The MYSQL_OPT_READ_TIMEOUT apear in the version 5.1.12 */
#if MYSQL_VERSION_ID > 50112
#define HAVE_MYSQL_OPT_TIMEOUTS 1
#endif
PyObject * _mysql_Exception(_mysql_ConnectionObject *c)
{
PyObject *t, *e, *code, *message;
int merr;
if (!(t = PyTuple_New(2))) return NULL;
if (!_mysql_server_init_done) {
e = _mysql_InternalError;
PyTuple_SET_ITEM(t, 0, PyLong_FromLong(-1L));
PyTuple_SET_ITEM(t, 1, PyUnicode_FromString("server not initialized"));
PyErr_SetObject(e, t);
Py_DECREF(t);
return NULL;
}
merr = mysql_errno(&(c->connection));
if (!merr)
e = _mysql_InterfaceError;
else if (merr > CR_MAX_ERROR) {
PyTuple_SET_ITEM(t, 0, PyLong_FromLong(-1L));
PyTuple_SET_ITEM(t, 1, PyUnicode_FromString("error totally whack"));
PyErr_SetObject(_mysql_InterfaceError, t);
Py_DECREF(t);
return NULL;
}
else switch (merr) {
case CR_COMMANDS_OUT_OF_SYNC:
case ER_DB_CREATE_EXISTS:
case ER_SYNTAX_ERROR:
case ER_PARSE_ERROR:
case ER_NO_SUCH_TABLE:
case ER_WRONG_DB_NAME:
case ER_WRONG_TABLE_NAME:
case ER_FIELD_SPECIFIED_TWICE:
case ER_INVALID_GROUP_FUNC_USE:
case ER_UNSUPPORTED_EXTENSION:
case ER_TABLE_MUST_HAVE_COLUMNS:
#ifdef ER_CANT_DO_THIS_DURING_AN_TRANSACTION
case ER_CANT_DO_THIS_DURING_AN_TRANSACTION:
#endif
e = _mysql_ProgrammingError;
break;
#ifdef WARN_DATA_TRUNCATED
case WARN_DATA_TRUNCATED:
#ifdef WARN_NULL_TO_NOTNULL
case WARN_NULL_TO_NOTNULL:
#endif
#ifdef ER_WARN_DATA_OUT_OF_RANGE
case ER_WARN_DATA_OUT_OF_RANGE:
#endif
#ifdef ER_NO_DEFAULT
case ER_NO_DEFAULT:
#endif
#ifdef ER_PRIMARY_CANT_HAVE_NULL
case ER_PRIMARY_CANT_HAVE_NULL:
#endif
#ifdef ER_DATA_TOO_LONG
case ER_DATA_TOO_LONG:
#endif
#ifdef ER_DATETIME_FUNCTION_OVERFLOW
case ER_DATETIME_FUNCTION_OVERFLOW:
#endif
e = _mysql_DataError;
break;
#endif
case ER_DUP_ENTRY:
#ifdef ER_DUP_UNIQUE
case ER_DUP_UNIQUE:
#endif
#ifdef ER_NO_REFERENCED_ROW
case ER_NO_REFERENCED_ROW:
#endif
#ifdef ER_NO_REFERENCED_ROW_2
case ER_NO_REFERENCED_ROW_2:
#endif
#ifdef ER_ROW_IS_REFERENCED
case ER_ROW_IS_REFERENCED:
#endif
#ifdef ER_ROW_IS_REFERENCED_2
case ER_ROW_IS_REFERENCED_2:
#endif
#ifdef ER_CANNOT_ADD_FOREIGN
case ER_CANNOT_ADD_FOREIGN:
#endif
e = _mysql_IntegrityError;
break;
#ifdef ER_WARNING_NOT_COMPLETE_ROLLBACK
case ER_WARNING_NOT_COMPLETE_ROLLBACK:
#endif
#ifdef ER_NOT_SUPPORTED_YET
case ER_NOT_SUPPORTED_YET:
#endif
#ifdef ER_FEATURE_DISABLED
case ER_FEATURE_DISABLED:
#endif
#ifdef ER_UNKNOWN_STORAGE_ENGINE
case ER_UNKNOWN_STORAGE_ENGINE:
#endif
e = _mysql_NotSupportedError;
break;
default:
if (merr < 1000)
e = _mysql_InternalError;
else
e = _mysql_OperationalError;
break;
}
code = PyLong_FromLong((long)merr);
message = PyUnicode_FromString(mysql_error(&(c->connection)));
PyTuple_SET_ITEM(t, 0, code );
PyTuple_SET_ITEM(t, 1, message );
PyObject_SetAttrString(e, "code", code );
PyObject_SetAttrString(e, "message", message );
PyErr_SetObject(e, t);
Py_DECREF(t);
return NULL;
}
static char _mysql_server_init__doc__[] =
"Initialize embedded server. If this client is not linked against\n\
the embedded server library, this function does nothing.\n\
\n\
args -- sequence of command-line arguments\n\
groups -- sequence of groups to use in defaults files\n\
";
static PyObject *_mysql_server_init(
PyObject *self,
PyObject *args,
PyObject *kwargs) {
static char *kwlist[] = {"args", "groups", NULL};
char **cmd_args_c=NULL, **groups_c=NULL, *s;
int cmd_argc=0, i, groupc;
PyObject *cmd_args=NULL, *groups=NULL, *ret=NULL, *item;
if (_mysql_server_init_done) {
PyErr_SetString(_mysql_ProgrammingError,
"already initialized");
return NULL;
}
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO", kwlist,
&cmd_args, &groups))
return NULL;
#if MYSQL_VERSION_ID >= 40000
if (cmd_args) {
if (!PySequence_Check(cmd_args)) {
PyErr_SetString(PyExc_TypeError,
"args must be a sequence");
goto finish;
}
cmd_argc = PySequence_Size(cmd_args);
if (cmd_argc == -1) {
PyErr_SetString(PyExc_TypeError,
"args could not be sized");
goto finish;
}
cmd_args_c = (char **) PyMem_Malloc(cmd_argc*sizeof(char *));
for (i=0; i< cmd_argc; i++) {
item = PySequence_GetItem(cmd_args, i);
s = PyBytes_AsString(item);
Py_DECREF(item);
if (!s) {
PyErr_SetString(PyExc_TypeError,
"args must contain strings");
goto finish;
}
cmd_args_c[i] = s;
}
}
if (groups) {
if (!PySequence_Check(groups)) {
PyErr_SetString(PyExc_TypeError,
"groups must be a sequence");
goto finish;
}
groupc = PySequence_Size(groups);
if (groupc == -1) {
PyErr_SetString(PyExc_TypeError,
"groups could not be sized");
goto finish;
}
groups_c = (char **) PyMem_Malloc((1+groupc)*sizeof(char *));
for (i=0; i< groupc; i++) {
item = PySequence_GetItem(groups, i);
s = PyBytes_AsString(item);
Py_DECREF(item);
if (!s) {
PyErr_SetString(PyExc_TypeError,
"groups must contain strings");
goto finish;
}
groups_c[i] = s;
}
groups_c[groupc] = (char *)NULL;
}
/* even though this may block, don't give up the interpreter lock
so that the server can't be initialized multiple times. */
if (mysql_server_init(cmd_argc, cmd_args_c, groups_c)) {
_mysql_Exception(NULL);
goto finish;
}
#endif
ret = Py_None;
Py_INCREF(Py_None);
_mysql_server_init_done = 1;
finish:
PyMem_Free(groups_c);
PyMem_Free(cmd_args_c);
return ret;
}
static char _mysql_server_end__doc__[] =
"Shut down embedded server. If not using an embedded server, this\n\
does nothing.";
static PyObject *_mysql_server_end(
PyObject *self,
PyObject *args) {
if (_mysql_server_init_done) {
#if MYSQL_VERSION_ID >= 40000
mysql_server_end();
#endif
_mysql_server_init_done = 0;
Py_INCREF(Py_None);
return Py_None;
}
return _mysql_Exception(NULL);
}
#if MYSQL_VERSION_ID >= 32314
static char _mysql_thread_safe__doc__[] =
"Indicates whether the client is compiled as thread-safe.";
static PyObject *_mysql_thread_safe(
PyObject *self,
PyObject *args) {
PyObject *flag;
if (!PyArg_ParseTuple(args, "")) return NULL;
check_server_init(NULL);
if (!(flag=PyLong_FromLong((long)mysql_thread_safe()))) return NULL;
return flag;
}
#endif
static char _mysql_ResultObject__doc__[] =
"result(connection, use=0, converter={}) -- Result set from a query.\n\
\n\
Creating instances of this class directly is an excellent way to\n\
shoot yourself in the foot. If using _mysql.connection directly,\n\
use connection.store_result() or connection.use_result() instead.\n\
If using MySQLdb.Connection, this is done by the cursor class.\n\
Just forget you ever saw this. Forget... FOR-GET...";
static int
_mysql_ResultObject_Initialize(
_mysql_ResultObject *self,
PyObject *args,
PyObject *kwargs)
{
static char *kwlist[] = {"connection", "use", "converter", NULL};
MYSQL_RES *result;
_mysql_ConnectionObject *conn=NULL;
int use=0;
PyObject *conv=NULL;
int n, i;
MYSQL_FIELD *fields;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|iO", kwlist,
&conn, &use, &conv))
return -1;
if (!conv) {
if (!(conv = PyDict_New()))
return -1;
}
else
Py_INCREF(conv);
self->conn = (PyObject *) conn;
Py_INCREF(conn);
self->use = use;
Py_BEGIN_ALLOW_THREADS ;
if (use)
result = mysql_use_result(&(conn->connection));
else
result = mysql_store_result(&(conn->connection));
self->result = result;
Py_END_ALLOW_THREADS ;
if (!result) {
if (mysql_field_count(&(conn->connection)) > 0) {
_mysql_Exception(conn);
return -1;
}
self->converter = PyTuple_New(0);
Py_DECREF(conv);
return 0;
}
n = mysql_num_fields(result);
self->nfields = n;
if (!(self->converter = PyTuple_New(n))) {
Py_DECREF(conv);
return -1;
}
fields = mysql_fetch_fields(result);
for (i=0; i<n; i++) {
PyObject *tmp, *fun;
tmp = PyLong_FromLong((long) fields[i].type);
if (!tmp) {
Py_DECREF(conv);
return -1;
}
fun = PyObject_GetItem(conv, tmp);
Py_DECREF(tmp);
if (!fun) {
if (PyErr_Occurred()) {
if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
Py_DECREF(conv);
return -1;
}
PyErr_Clear();
}
fun = Py_None;
Py_INCREF(Py_None);
}
else if (PySequence_Check(fun)) {
int j, n2=PySequence_Size(fun);
PyObject *fun2=NULL;
for (j=0; j<n2; j++) {
PyObject *t = PySequence_GetItem(fun, j);
if (!t) {
Py_DECREF(fun);
Py_DECREF(conv);
return -1;
}
if (PyTuple_Check(t) && PyTuple_GET_SIZE(t) == 2) {
long mask, flags;
PyObject *pmask=NULL;
pmask = PyTuple_GET_ITEM(t, 0);
fun2 = PyTuple_GET_ITEM(t, 1);
Py_XINCREF(fun2);
if (PyNumber_Check(pmask)) {
mask = PyLong_AS_LONG(pmask);
flags = fields[i].flags;
if (fields[i].charsetnr != 63) { /* maaagic */
flags &= ~BINARY_FLAG;
}
if (mask & flags) {
Py_DECREF(t);
break;
}
else {
fun2 = NULL;
}
} else {
Py_DECREF(t);
break;
}
}
Py_DECREF(t);
}
if (!fun2) {
fun2 = Py_None;
Py_INCREF(fun2);
}
Py_DECREF(fun);
fun = fun2;
}
PyTuple_SET_ITEM(self->converter, i, fun);
}
Py_DECREF(conv);
return 0;
}
static int _mysql_ResultObject_traverse(
_mysql_ResultObject *self,
visitproc visit,
void *arg)
{
int r;
if (self->converter) {
if (!(r = visit(self->converter, arg))) return r;
}
if (self->conn)
return visit((PyObject *)self->conn, arg);
return 0;
}
static int _mysql_ResultObject_clear(
_mysql_ResultObject *self)
{
Py_XDECREF(self->converter);
self->converter = NULL;
Py_XDECREF(self->conn);
self->conn = NULL;
return 0;
}
static int
_mysql_ConnectionObject_Initialize(
_mysql_ConnectionObject *self,
PyObject *args,
PyObject *kwargs)
{
MYSQL *conn = NULL;
PyObject *conv = NULL;
PyObject *ssl = NULL;
#if HAVE_OPENSSL
char *key = NULL, *cert = NULL, *ca = NULL,
*capath = NULL, *cipher = NULL;
#endif
char *host = NULL, *user = NULL, *passwd = NULL,
*db = NULL, *unix_socket = NULL;
unsigned int port = 0;
unsigned int client_flag = 0;
static char *kwlist[] = { "host", "user", "passwd", "db", "port",
"unix_socket", "conv",
"connect_timeout", "compress",
"named_pipe", "init_command",
"read_default_file", "read_default_group",
"client_flag", "ssl",
"local_infile",
#ifdef HAVE_MYSQL_OPT_TIMEOUTS
"read_timeout",
"write_timeout",
#endif
NULL } ;
int connect_timeout = 0;
#ifdef HAVE_MYSQL_OPT_TIMEOUTS
int read_timeout = 0;
int write_timeout = 0;
#endif
int compress = -1, named_pipe = -1, local_infile = -1;
char *init_command=NULL,
*read_default_file=NULL,
*read_default_group=NULL;
self->converter = NULL;
self->open = 0;
check_server_init(-1);
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
#ifdef HAVE_MYSQL_OPT_TIMEOUTS
"|ssssisOiiisssiOiii:connect",
#else
"|ssssisOiiisssiOi:connect",
#endif
kwlist,
&host, &user, &passwd, &db,
&port, &unix_socket, &conv,
&connect_timeout,
&compress, &named_pipe,
&init_command, &read_default_file,
&read_default_group,
&client_flag, &ssl,
&local_infile
#ifdef HAVE_MYSQL_OPT_TIMEOUTS
, &read_timeout
, &write_timeout
#endif
))
return -1;
#define _stringsuck(d,t,s) {t=PyMapping_GetItemString(s,#d);\
if(t){d=PyUnicode_AS_DATA(t);Py_DECREF(t);}\
PyErr_Clear();}
if (ssl) {
#if HAVE_OPENSSL
PyObject *value = NULL;
_stringsuck(ca, value, ssl);
_stringsuck(capath, value, ssl);
_stringsuck(cert, value, ssl);
_stringsuck(key, value, ssl);
_stringsuck(cipher, value, ssl);
#else
PyErr_SetString(_mysql_NotSupportedError,
"client library does not have SSL support");
return -1;
#endif
}
Py_BEGIN_ALLOW_THREADS ;
conn = mysql_init(&(self->connection));
if (connect_timeout) {
unsigned int timeout = connect_timeout;
mysql_options(&(self->connection), MYSQL_OPT_CONNECT_TIMEOUT,
(char *)&timeout);
}
#ifdef HAVE_MYSQL_OPT_TIMEOUTS
if (read_timeout) {
unsigned int timeout = read_timeout;
mysql_options(&(self->connection), MYSQL_OPT_READ_TIMEOUT,
(char *)&timeout);
}
if (write_timeout) {
unsigned int timeout = write_timeout;
mysql_options(&(self->connection), MYSQL_OPT_WRITE_TIMEOUT,
(char *)&timeout);
}
#endif
if (compress != -1) {
mysql_options(&(self->connection), MYSQL_OPT_COMPRESS, 0);
client_flag |= CLIENT_COMPRESS;
}
if (named_pipe != -1)
mysql_options(&(self->connection), MYSQL_OPT_NAMED_PIPE, 0);
if (init_command != NULL)
mysql_options(&(self->connection), MYSQL_INIT_COMMAND, init_command);
if (read_default_file != NULL)
mysql_options(&(self->connection), MYSQL_READ_DEFAULT_FILE, read_default_file);
if (read_default_group != NULL)
mysql_options(&(self->connection), MYSQL_READ_DEFAULT_GROUP, read_default_group);
if (local_infile != -1)
mysql_options(&(self->connection), MYSQL_OPT_LOCAL_INFILE, (char *) &local_infile);
#if HAVE_OPENSSL
if (ssl)
mysql_ssl_set(&(self->connection),
key, cert, ca, capath, cipher);
#endif
conn = mysql_real_connect(&(self->connection), host, user, passwd, db,
port, unix_socket, client_flag);
Py_END_ALLOW_THREADS ;
if (!conn) {
_mysql_Exception(self);
return -1;
}
/* Internal references to python-land objects */
if (!conv)
conv = PyDict_New();
else
Py_INCREF(conv);
if (!conv)
return -1;
self->converter = conv;
/*
PyType_GenericAlloc() automatically sets up GC allocation and
tracking for GC objects, at least in 2.2.1, so it does not need to
be done here. tp_dealloc still needs to call PyObject_GC_UnTrack(),
however.
*/
self->open = 1;
return 0;
}
static char _mysql_connect__doc__[] =
"Returns a MYSQL connection object. Exclusive use of\n\
keyword parameters strongly recommended. Consult the\n\
MySQL C API documentation for more details.\n\
\n\
host\n\
string, host to connect\n\
\n\
user\n\
string, user to connect as\n\
\n\
passwd\n\
string, password to use\n\
\n\
db\n\
string, database to use\n\
\n\
port\n\
integer, TCP/IP port to connect to\n\
\n\
unix_socket\n\
string, location of unix_socket (UNIX-ish only)\n\
\n\
conv\n\
mapping, maps MySQL FIELD_TYPE.* to Python functions which\n\
convert a string to the appropriate Python type\n\
\n\
connect_timeout\n\
number of seconds to wait before the connection\n\
attempt fails.\n\
\n\
compress\n\
if set, gzip compression is enabled\n\
\n\
named_pipe\n\
if set, connect to server via named pipe (Windows only)\n\
\n\
init_command\n\
command which is run once the connection is created\n\
\n\
read_default_file\n\
see the MySQL documentation for mysql_options()\n\
\n\
read_default_group\n\
see the MySQL documentation for mysql_options()\n\
\n\
client_flag\n\
client flags from MySQLdb.constants.CLIENT\n\
\n\
load_infile\n\
int, non-zero enables LOAD LOCAL INFILE, zero disables\n\
\n\
";
static PyObject *
_mysql_connect(
PyObject *self,
PyObject *args,
PyObject *kwargs)
{
_mysql_ConnectionObject *c=NULL;
c = MyAlloc(_mysql_ConnectionObject, _mysql_ConnectionObject_Type);
if (c == NULL) return NULL;
if (_mysql_ConnectionObject_Initialize(c, args, kwargs)) {
Py_DECREF(c);
c = NULL;
}
return (PyObject *) c;
}
static int _mysql_ConnectionObject_traverse(
_mysql_ConnectionObject *self,
visitproc visit,
void *arg)
{
if (self->converter)
return visit(self->converter, arg);
return 0;
}
static int _mysql_ConnectionObject_clear(
_mysql_ConnectionObject *self)
{
Py_XDECREF(self->converter);
self->converter = NULL;
return 0;
}
static char _mysql_ConnectionObject_close__doc__[] =
"Close the connection. No further activity possible.";
static PyObject *
_mysql_ConnectionObject_close(
_mysql_ConnectionObject *self,
PyObject *args)
{
if (args) {
if (!PyArg_ParseTuple(args, "")) return NULL;
}
if (self->open) {
Py_BEGIN_ALLOW_THREADS
mysql_close(&(self->connection));
Py_END_ALLOW_THREADS
self->open = 0;
} else {
PyErr_SetString(_mysql_ProgrammingError,
"closing a closed connection");
return NULL;
}
_mysql_ConnectionObject_clear(self);
Py_INCREF(Py_None);
return Py_None;
}
static char _mysql_ConnectionObject_affected_rows__doc__ [] =
"Return number of rows affected by the last query.\n\
Non-standard. Use Cursor.rowcount.\n\
";
static PyObject *
_mysql_ConnectionObject_affected_rows(
_mysql_ConnectionObject *self,
PyObject *args)
{
if (!PyArg_ParseTuple(args, "")) return NULL;
check_connection(self);
return PyLong_FromUnsignedLongLong(mysql_affected_rows(&(self->connection)));
}
static char _mysql_debug__doc__[] =
"Does a DBUG_PUSH with the given string.\n\
mysql_debug() uses the Fred Fish debug library.\n\
To use this function, you must compile the client library to\n\
support debugging.\n\
";
static PyObject *
_mysql_debug(
PyObject *self,
PyObject *args)
{
char *debug;
if (!PyArg_ParseTuple(args, "s", &debug)) return NULL;
mysql_debug(debug);
Py_INCREF(Py_None);
return Py_None;
}
static char _mysql_ConnectionObject_dump_debug_info__doc__[] =
"Instructs the server to write some debug information to the\n\
log. The connected user must have the process privilege for\n\
this to work. Non-standard.\n\
";
static PyObject *
_mysql_ConnectionObject_dump_debug_info(
_mysql_ConnectionObject *self,
PyObject *args)
{
int err;
if (!PyArg_ParseTuple(args, "")) return NULL;
check_connection(self);
Py_BEGIN_ALLOW_THREADS
err = mysql_dump_debug_info(&(self->connection));
Py_END_ALLOW_THREADS
if (err) return _mysql_Exception(self);
Py_INCREF(Py_None);
return Py_None;
}
static char _mysql_ConnectionObject_autocommit__doc__[] =
"Set the autocommit mode. True values enable; False value disable.\n\
";
static PyObject *
_mysql_ConnectionObject_autocommit(
_mysql_ConnectionObject *self,
PyObject *args)
{
int flag, err;
if (!PyArg_ParseTuple(args, "i", &flag)) return NULL;
Py_BEGIN_ALLOW_THREADS
#if MYSQL_VERSION_ID >= 40100
err = mysql_autocommit(&(self->connection), flag);
#else
{
char query[256];
snprintf(query, 256, "SET AUTOCOMMIT=%d", flag);
err = mysql_query(&(self->connection), query);
}
#endif
Py_END_ALLOW_THREADS
if (err) return _mysql_Exception(self);
Py_INCREF(Py_None);
return Py_None;
}
static char _mysql_ConnectionObject_get_autocommit__doc__[] =
"Get the autocommit mode. True when enable; False when disable.\n";
static PyObject *
_mysql_ConnectionObject_get_autocommit(
_mysql_ConnectionObject *self,
PyObject *args)
{
if (self->connection.server_status & SERVER_STATUS_AUTOCOMMIT) {
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
}
static char _mysql_ConnectionObject_commit__doc__[] =
"Commits the current transaction\n\
";
static PyObject *
_mysql_ConnectionObject_commit(
_mysql_ConnectionObject *self,
PyObject *args)
{
int err;
if (!PyArg_ParseTuple(args, "")) return NULL;
Py_BEGIN_ALLOW_THREADS
#if MYSQL_VERSION_ID >= 40100
err = mysql_commit(&(self->connection));
#else
err = mysql_query(&(self->connection), "COMMIT");
#endif
Py_END_ALLOW_THREADS
if (err) return _mysql_Exception(self);
Py_INCREF(Py_None);
return Py_None;
}
static char _mysql_ConnectionObject_rollback__doc__[] =
"Rolls backs the current transaction\n\
";
static PyObject *
_mysql_ConnectionObject_rollback(
_mysql_ConnectionObject *self,
PyObject *args)
{
int err;
if (!PyArg_ParseTuple(args, "")) return NULL;
Py_BEGIN_ALLOW_THREADS
#if MYSQL_VERSION_ID >= 40100
err = mysql_rollback(&(self->connection));
#else
err = mysql_query(&(self->connection), "ROLLBACK");
#endif
Py_END_ALLOW_THREADS
if (err) return _mysql_Exception(self);
Py_INCREF(Py_None);
return Py_None;
}
static char _mysql_ConnectionObject_next_result__doc__[] =
"If more query results exist, next_result() reads the next query\n\
results and returns the status back to application.\n\
\n\
After calling next_result() the state of the connection is as if\n\
you had called query() for the next query. This means that you can\n\
now call store_result(), warning_count(), affected_rows()\n\
, and so forth. \n\
\n\
Returns 0 if there are more results; -1 if there are no more results\n\
\n\
Non-standard.\n\
";
static PyObject *
_mysql_ConnectionObject_next_result(
_mysql_ConnectionObject *self,
PyObject *args)
{
int err;
if (!PyArg_ParseTuple(args, "")) return NULL;
Py_BEGIN_ALLOW_THREADS
#if MYSQL_VERSION_ID >= 40100
err = mysql_next_result(&(self->connection));
#else
err = -1;
#endif
Py_END_ALLOW_THREADS
if (err > 0) return _mysql_Exception(self);
return PyLong_FromLong(err);
}
#if MYSQL_VERSION_ID >= 40100
static char _mysql_ConnectionObject_set_server_option__doc__[] =
"set_server_option(option) -- Enables or disables an option\n\
for the connection.\n\
\n\
Non-standard.\n\
";
static PyObject *
_mysql_ConnectionObject_set_server_option(
_mysql_ConnectionObject *self,
PyObject *args)
{
int err, flags=0;
if (!PyArg_ParseTuple(args, "i", &flags))
return NULL;
Py_BEGIN_ALLOW_THREADS
err = mysql_set_server_option(&(self->connection), flags);
Py_END_ALLOW_THREADS
if (err) return _mysql_Exception(self);
return PyLong_FromLong(err);
}
static char _mysql_ConnectionObject_sqlstate__doc__[] =
"Returns a string containing the SQLSTATE error code\n\
for the last error. The error code consists of five characters.\n\
'00000' means \"no error.\" The values are specified by ANSI SQL\n\
and ODBC. For a list of possible values, see section 23\n\
Error Handling in MySQL in the MySQL Manual.\n\
\n\
Note that not all MySQL errors are yet mapped to SQLSTATE's.\n\
The value 'HY000' (general error) is used for unmapped errors.\n\
\n\
Non-standard.\n\
";
static PyObject *
_mysql_ConnectionObject_sqlstate(
_mysql_ConnectionObject *self,
PyObject *args)
{
if (!PyArg_ParseTuple(args, "")) return NULL;
return PyUnicode_FromString(mysql_sqlstate(&(self->connection)));
}
static char _mysql_ConnectionObject_warning_count__doc__[] =
"Returns the number of warnings generated during execution\n\
of the previous SQL statement.\n\
\n\
Non-standard.\n\
";
static PyObject *
_mysql_ConnectionObject_warning_count(
_mysql_ConnectionObject *self,
PyObject *args)
{
if (!PyArg_ParseTuple(args, "")) return NULL;
return PyLong_FromLong(mysql_warning_count(&(self->connection)));
}
#endif
static char _mysql_ConnectionObject_errno__doc__[] =
"Returns the error code for the most recently invoked API function\n\