comparison src/results.c @ 73:24fa6a40c706 MySQLdb

Added a simple_fetch_row() to the result object that simply returns one row as a tuple of strings (or None for NULL values). Result objects are also now iterators which internally use simple_fetch_row().
author adustman
date Fri, 19 Feb 2010 02:21:11 +0000
parents 98d968f5af11
children 3b03cb566032
comparison
equal deleted inserted replaced
72:c0c00294239b 73:24fa6a40c706
427 error: 427 error:
428 Py_XDECREF(r); 428 Py_XDECREF(r);
429 return NULL; 429 return NULL;
430 } 430 }
431 431
432 static char _mysql_ResultObject_simple_fetch_row__doc__[] =
433 "simple_fetchrow()\n\
434 Fetches one row as a tuple of strings.\n\
435 NULL is returned as None.\n\
436 ";
437
438 static PyObject *
439 _mysql_ResultObject_simple_fetch_row(
440 _mysql_ResultObject *self,
441 PyObject *unused)
442 {
443 unsigned int n, i;
444 unsigned long *length;
445 PyObject *r=NULL;
446 MYSQL_ROW row;
447
448 check_result_connection(self);
449
450 if (!self->use)
451 row = mysql_fetch_row(self->result);
452 else {
453 Py_BEGIN_ALLOW_THREADS;
454 row = mysql_fetch_row(self->result);
455 Py_END_ALLOW_THREADS;
456 }
457 if (!row && mysql_errno(&(((_mysql_ConnectionObject *)(self->conn))->connection))) {
458 _mysql_Exception((_mysql_ConnectionObject *)self->conn);
459 goto error;
460 }
461 if (!row) {
462 Py_INCREF(Py_None);
463 return Py_None;
464 }
465
466 n = mysql_num_fields(self->result);
467 if (!(r = PyTuple_New(n))) return NULL;
468 length = mysql_fetch_lengths(self->result);
469 for (i=0; i<n; i++) {
470 PyObject *v;
471 if (row[i]) {
472 v = PyString_FromStringAndSize(row[i], length[i]);
473 if (!v) goto error;
474 } else /* NULL */ {
475 v = Py_None;
476 Py_INCREF(v);
477 }
478 PyTuple_SET_ITEM(r, i, v);
479 }
480 return r;
481 error:
482 Py_XDECREF(r);
483 return NULL;
484 }
485
486 static PyObject *
487 _mysql_ResultObject__iter__(
488 _mysql_ResultObject *self,
489 PyObject *unused)
490 {
491 check_result_connection(self);
492 Py_INCREF(self);
493 return (PyObject *)self;
494 }
495
496 static PyObject *
497 _mysql_ResultObject_next(
498 _mysql_ResultObject *self,
499 PyObject *unused)
500 {
501 PyObject *row;
502 check_result_connection(self);
503 row = _mysql_ResultObject_simple_fetch_row(self, NULL);
504 if (row == Py_None) {
505 Py_DECREF(row);
506 PyErr_SetString(PyExc_StopIteration, "");
507 return NULL;
508 }
509 return row;
510 }
432 511
433 static char _mysql_ResultObject_num_fields__doc__[] = 512 static char _mysql_ResultObject_num_fields__doc__[] =
434 "Returns the number of fields (column) in the result." ; 513 "Returns the number of fields (column) in the result." ;
435 514
436 static PyObject * 515 static PyObject *
564 (PyCFunction)_mysql_ResultObject_fetch_row, 643 (PyCFunction)_mysql_ResultObject_fetch_row,
565 METH_VARARGS | METH_KEYWORDS, 644 METH_VARARGS | METH_KEYWORDS,
566 _mysql_ResultObject_fetch_row__doc__ 645 _mysql_ResultObject_fetch_row__doc__
567 }, 646 },
568 { 647 {
648 "simple_fetch_row",
649 (PyCFunction)_mysql_ResultObject_simple_fetch_row,
650 METH_VARARGS | METH_KEYWORDS,
651 _mysql_ResultObject_simple_fetch_row__doc__
652 },
653
654 {
569 "field_flags", 655 "field_flags",
570 (PyCFunction)_mysql_ResultObject_field_flags, 656 (PyCFunction)_mysql_ResultObject_field_flags,
571 METH_NOARGS, 657 METH_NOARGS,
572 _mysql_ResultObject_field_flags__doc__ 658 _mysql_ResultObject_field_flags__doc__
573 }, 659 },
605 "fields", 691 "fields",
606 T_OBJECT, 692 T_OBJECT,
607 offsetof(_mysql_ResultObject, fields), 693 offsetof(_mysql_ResultObject, fields),
608 RO, 694 RO,
609 "Field metadata for result set" 695 "Field metadata for result set"
610 }, {NULL} /* Sentinel */ 696 },
697 {
698 "use",
699 T_INT,
700 offsetof(_mysql_ResultObject, use),
701 RO,
702 "True if mysql_use_result() was used; False if mysql_store_result() was used"
703 },
704 {NULL} /* Sentinel */
611 }; 705 };
612 706
613 static PyObject * 707 static PyObject *
614 _mysql_ResultObject_getattr( 708 _mysql_ResultObject_getattr(
615 _mysql_ResultObject *self, 709 _mysql_ResultObject *self,
698 792
699 /* weak reference enabler */ 793 /* weak reference enabler */
700 0, /* (long) tp_weaklistoffset */ 794 0, /* (long) tp_weaklistoffset */
701 795
702 /* Iterators */ 796 /* Iterators */
703 0, /* (getiterfunc) tp_iter */ 797 (getiterfunc) _mysql_ResultObject__iter__, /* (getiterfunc) tp_iter */
704 0, /* (iternextfunc) tp_iternext */ 798 (iternextfunc) _mysql_ResultObject_next, /* (iternextfunc) tp_iternext */
705 799
706 /* Attribute descriptor and subclassing stuff */ 800 /* Attribute descriptor and subclassing stuff */
707 (struct PyMethodDef *)_mysql_ResultObject_methods, /* tp_methods */ 801 (struct PyMethodDef *)_mysql_ResultObject_methods, /* tp_methods */
708 (struct PyMemberDef *)_mysql_ResultObject_memberlist, /*tp_members */ 802 (struct PyMemberDef *)_mysql_ResultObject_memberlist, /*tp_members */
709 0, /* (struct getsetlist *) tp_getset; */ 803 0, /* (struct getsetlist *) tp_getset; */