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 1001 1002 1003 1004
|
/*
* PyGreSQL - a Python interface for the PostgreSQL database.
*
* The query object - this file is part a of the C extension module.
*
* Copyright (c) 2024 by the PyGreSQL Development Team
*
* Please see the LICENSE.TXT file for specific restrictions.
*/
/* Deallocate the query object. */
static void
query_dealloc(queryObject *self)
{
Py_XDECREF(self->pgcnx);
if (self->col_types) {
PyMem_Free(self->col_types);
}
if (self->result) {
PQclear(self->result);
}
PyObject_Del(self);
}
/* Return query as string in human readable form. */
static PyObject *
query_str(queryObject *self)
{
return format_result(self->result);
}
/* Return length of a query object. */
static Py_ssize_t
query_len(PyObject *self)
{
PyObject *tmp;
Py_ssize_t len;
tmp = PyLong_FromLong(((queryObject *)self)->max_row);
len = PyLong_AsSsize_t(tmp);
Py_DECREF(tmp);
return len;
}
/* Return the value in the given column of the current row. */
static PyObject *
_query_value_in_column(queryObject *self, int column)
{
char *s;
int type;
if (PQgetisnull(self->result, self->current_row, column)) {
Py_INCREF(Py_None);
return Py_None;
}
/* get the string representation of the value */
/* note: this is always null-terminated text format */
s = PQgetvalue(self->result, self->current_row, column);
/* get the PyGreSQL type of the column */
type = self->col_types[column];
/* cast the string representation into a Python object */
if (type & PYGRES_ARRAY)
return cast_array(s,
PQgetlength(self->result, self->current_row, column),
self->encoding, type, NULL, 0);
if (type == PYGRES_BYTEA)
return cast_bytea_text(s);
if (type == PYGRES_OTHER)
return cast_other(s,
PQgetlength(self->result, self->current_row, column),
self->encoding, PQftype(self->result, column),
self->pgcnx->cast_hook);
if (type & PYGRES_TEXT)
return cast_sized_text(
s, PQgetlength(self->result, self->current_row, column),
self->encoding, type);
return cast_unsized_simple(s, type);
}
/* Return the current row as a tuple. */
static PyObject *
_query_row_as_tuple(queryObject *self)
{
PyObject *row_tuple = NULL;
int j;
if (!(row_tuple = PyTuple_New(self->num_fields))) {
return NULL;
}
for (j = 0; j < self->num_fields; ++j) {
PyObject *val = _query_value_in_column(self, j);
if (!val) {
Py_DECREF(row_tuple);
return NULL;
}
PyTuple_SET_ITEM(row_tuple, j, val);
}
return row_tuple;
}
/* Fetch the result if this is an asynchronous query and it has not yet
been fetched in this round-trip. Also mark whether the result should
be kept for this round-trip (e.g. to be used in an iterator).
If this is a normal query result, the query itself will be returned,
otherwise a result value will be returned that shall be passed on. */
static PyObject *
_get_async_result(queryObject *self, int keep)
{
int fetch = 0;
if (self->async) {
if (self->async == 1) {
fetch = 1;
if (keep) {
/* mark query as fetched, do not fetch again */
self->async = 2;
}
}
else if (!keep) {
self->async = 1;
}
}
if (fetch) {
int status;
if (!self->pgcnx) {
PyErr_SetString(PyExc_TypeError, "Connection is not valid");
return NULL;
}
Py_BEGIN_ALLOW_THREADS
if (self->result) {
PQclear(self->result);
}
self->result = PQgetResult(self->pgcnx->cnx);
Py_END_ALLOW_THREADS
if (!self->result) {
/* end of result set, return None */
self->max_row = 0;
self->num_fields = 0;
self->col_types = NULL;
Py_INCREF(Py_None);
return Py_None;
}
if ((status = PQresultStatus(self->result)) != PGRES_TUPLES_OK) {
PyObject *result =
_conn_non_query_result(status, self->result, self->pgcnx->cnx);
self->result = NULL; /* since this has been already cleared */
if (!result) {
/* Raise an error. We need to call PQgetResult() to clear the
connection state. This should return NULL the first time. */
self->result = PQgetResult(self->pgcnx->cnx);
while (self->result) {
PQclear(self->result);
self->result = PQgetResult(self->pgcnx->cnx);
Py_DECREF(self->pgcnx);
self->pgcnx = NULL;
}
}
else if (result == Py_None) {
/* It would be confusing to return None here because the
caller has to call again until we return None. We can't
just consume that final None because we don't know if there
are additional statements following this one, so we return
an empty string where query() would return None. */
Py_DECREF(result);
result = PyUnicode_FromString("");
}
return result;
}
self->max_row = PQntuples(self->result);
self->num_fields = PQnfields(self->result);
self->col_types = get_col_types(self->result, self->num_fields);
if (!self->col_types) {
Py_DECREF(self);
Py_DECREF(self);
return NULL;
}
}
else if (self->async == 2 && !self->max_row && !self->num_fields &&
!self->col_types) {
Py_INCREF(Py_None);
return Py_None;
}
/* return the query object itself as sentinel for a normal query result */
return (PyObject *)self;
}
/* Return given item from a query object. */
static PyObject *
query_getitem(PyObject *self, Py_ssize_t i)
{
queryObject *q = (queryObject *)self;
PyObject *tmp;
long row;
if ((tmp = _get_async_result(q, 0)) != (PyObject *)self)
return tmp;
tmp = PyLong_FromSize_t((size_t)i);
row = PyLong_AsLong(tmp);
Py_DECREF(tmp);
if (row < 0 || row >= q->max_row) {
PyErr_SetNone(PyExc_IndexError);
return NULL;
}
q->current_row = (int)row;
return _query_row_as_tuple(q);
}
/* __iter__() method of the queryObject:
Returns the default iterator yielding rows as tuples. */
static PyObject *
query_iter(queryObject *self)
{
PyObject *res;
if ((res = _get_async_result(self, 0)) != (PyObject *)self)
return res;
self->current_row = 0;
Py_INCREF(self);
return (PyObject *)self;
}
/* __next__() method of the queryObject:
Returns the current row as a tuple and moves to the next one. */
static PyObject *
query_next(queryObject *self, PyObject *noargs)
{
PyObject *row_tuple = NULL;
if (self->current_row >= self->max_row) {
PyErr_SetNone(PyExc_StopIteration);
return NULL;
}
row_tuple = _query_row_as_tuple(self);
if (row_tuple)
++self->current_row;
return row_tuple;
}
/* Get number of bytes allocated for PGresult object */
static char query_memsize__doc__[] =
"memsize() -- return number of bytes allocated by query result";
static PyObject *
query_memsize(queryObject *self, PyObject *noargs)
{
#ifdef MEMORY_SIZE
return PyLong_FromSize_t(PQresultMemorySize(self->result));
#else
set_error_msg(NotSupportedError, "Memory size functions not supported");
return NULL;
#endif /* MEMORY_SIZE */
}
/* List field names from query result. */
static char query_listfields__doc__[] =
"listfields() -- List field names from result";
static PyObject *
query_listfields(queryObject *self, PyObject *noargs)
{
int i;
char *name;
PyObject *fieldstuple, *str;
/* builds tuple */
fieldstuple = PyTuple_New(self->num_fields);
if (fieldstuple) {
for (i = 0; i < self->num_fields; ++i) {
name = PQfname(self->result, i);
str = PyUnicode_FromString(name);
PyTuple_SET_ITEM(fieldstuple, i, str);
}
}
return fieldstuple;
}
/* Get field name from number in last result. */
static char query_fieldname__doc__[] =
"fieldname(num) -- return name of field from result from its position";
static PyObject *
query_fieldname(queryObject *self, PyObject *args)
{
int i;
char *name;
/* gets args */
if (!PyArg_ParseTuple(args, "i", &i)) {
PyErr_SetString(PyExc_TypeError,
"Method fieldname() takes an integer as argument");
return NULL;
}
/* checks number validity */
if (i >= self->num_fields) {
PyErr_SetString(PyExc_ValueError, "Invalid field number");
return NULL;
}
/* gets fields name and builds object */
name = PQfname(self->result, i);
return PyUnicode_FromString(name);
}
/* Get field number from name in last result. */
static char query_fieldnum__doc__[] =
"fieldnum(name) -- return position in query for field from its name";
static PyObject *
query_fieldnum(queryObject *self, PyObject *args)
{
int num;
char *name;
/* gets args */
if (!PyArg_ParseTuple(args, "s", &name)) {
PyErr_SetString(PyExc_TypeError,
"Method fieldnum() takes a string as argument");
return NULL;
}
/* gets field number */
if ((num = PQfnumber(self->result, name)) == -1) {
PyErr_SetString(PyExc_ValueError, "Unknown field");
return NULL;
}
return PyLong_FromLong(num);
}
/* Build a tuple with info for query field with given number. */
static PyObject *
_query_build_field_info(PGresult *res, int col_num)
{
PyObject *info;
info = PyTuple_New(4);
if (info) {
PyTuple_SET_ITEM(info, 0, PyUnicode_FromString(PQfname(res, col_num)));
PyTuple_SET_ITEM(info, 1,
PyLong_FromLong((long)PQftype(res, col_num)));
PyTuple_SET_ITEM(info, 2, PyLong_FromLong(PQfsize(res, col_num)));
PyTuple_SET_ITEM(info, 3, PyLong_FromLong(PQfmod(res, col_num)));
}
return info;
}
/* Get information on one or all fields of the query result. */
static char query_fieldinfo__doc__[] =
"fieldinfo([name]) -- return information about field(s) in query result";
static PyObject *
query_fieldinfo(queryObject *self, PyObject *args)
{
PyObject *result, *field = NULL;
int num;
/* gets args */
if (!PyArg_ParseTuple(args, "|O", &field)) {
PyErr_SetString(PyExc_TypeError,
"Method fieldinfo() takes one optional argument only");
return NULL;
}
/* check optional field arg */
if (field) {
/* gets field number */
if (PyBytes_Check(field)) {
num = PQfnumber(self->result, PyBytes_AsString(field));
}
else if (PyUnicode_Check(field)) {
PyObject *tmp = get_encoded_string(field, self->encoding);
if (!tmp)
return NULL;
num = PQfnumber(self->result, PyBytes_AsString(tmp));
Py_DECREF(tmp);
}
else if (PyLong_Check(field)) {
num = (int)PyLong_AsLong(field);
}
else {
PyErr_SetString(PyExc_TypeError,
"Field should be given as column number or name");
return NULL;
}
if (num < 0 || num >= self->num_fields) {
PyErr_SetString(PyExc_IndexError, "Unknown field");
return NULL;
}
return _query_build_field_info(self->result, num);
}
if (!(result = PyTuple_New(self->num_fields))) {
return NULL;
}
for (num = 0; num < self->num_fields; ++num) {
PyObject *info = _query_build_field_info(self->result, num);
if (!info) {
Py_DECREF(result);
return NULL;
}
PyTuple_SET_ITEM(result, num, info);
}
return result;
}
/* Retrieve one row from the result as a tuple. */
static char query_one__doc__[] =
"one() -- Get one row from the result of a query\n\n"
"Only one row from the result is returned as a tuple of fields.\n"
"This method can be called multiple times to return more rows.\n"
"It returns None if the result does not contain one more row.\n";
static PyObject *
query_one(queryObject *self, PyObject *noargs)
{
PyObject *row_tuple;
if ((row_tuple = _get_async_result(self, 0)) == (PyObject *)self) {
if (self->current_row >= self->max_row) {
Py_INCREF(Py_None);
return Py_None;
}
row_tuple = _query_row_as_tuple(self);
if (row_tuple)
++self->current_row;
}
return row_tuple;
}
/* Retrieve the single row from the result as a tuple. */
static char query_single__doc__[] =
"single() -- Get the result of a query as single row\n\n"
"The single row from the query result is returned as a tuple of fields.\n"
"This method returns the same single row when called multiple times.\n"
"It raises an InvalidResultError if the result doesn't have exactly one "
"row,\n"
"which will be of type NoResultError or MultipleResultsError "
"specifically.\n";
static PyObject *
query_single(queryObject *self, PyObject *noargs)
{
PyObject *row_tuple;
if ((row_tuple = _get_async_result(self, 0)) == (PyObject *)self) {
if (self->max_row != 1) {
if (self->max_row)
set_error_msg(MultipleResultsError, "Multiple results found");
else
set_error_msg(NoResultError, "No result found");
return NULL;
}
self->current_row = 0;
row_tuple = _query_row_as_tuple(self);
if (row_tuple)
++self->current_row;
}
return row_tuple;
}
/* Retrieve the last query result as a list of tuples. */
static char query_getresult__doc__[] =
"getresult() -- Get the result of a query\n\n"
"The result is returned as a list of rows, each one a tuple of fields\n"
"in the order returned by the server.\n";
static PyObject *
query_getresult(queryObject *self, PyObject *noargs)
{
PyObject *result_list;
int i;
if ((result_list = _get_async_result(self, 0)) == (PyObject *)self) {
if (!(result_list = PyList_New(self->max_row))) {
return NULL;
}
for (i = self->current_row = 0; i < self->max_row; ++i) {
PyObject *row_tuple = query_next(self, noargs);
if (!row_tuple) {
Py_DECREF(result_list);
return NULL;
}
PyList_SET_ITEM(result_list, i, row_tuple);
}
}
return result_list;
}
/* Return the current row as a dict. */
static PyObject *
_query_row_as_dict(queryObject *self)
{
PyObject *row_dict = NULL;
int j;
if (!(row_dict = PyDict_New())) {
return NULL;
}
for (j = 0; j < self->num_fields; ++j) {
PyObject *val = _query_value_in_column(self, j);
if (!val) {
Py_DECREF(row_dict);
return NULL;
}
PyDict_SetItemString(row_dict, PQfname(self->result, j), val);
Py_DECREF(val);
}
return row_dict;
}
/* Return the current row as a dict and move to the next one. */
static PyObject *
query_next_dict(queryObject *self, PyObject *noargs)
{
PyObject *row_dict = NULL;
if (self->current_row >= self->max_row) {
PyErr_SetNone(PyExc_StopIteration);
return NULL;
}
row_dict = _query_row_as_dict(self);
if (row_dict)
++self->current_row;
return row_dict;
}
/* Retrieve one row from the result as a dictionary. */
static char query_onedict__doc__[] =
"onedict() -- Get one row from the result of a query\n\n"
"Only one row from the result is returned as a dictionary with\n"
"the field names used as the keys.\n"
"This method can be called multiple times to return more rows.\n"
"It returns None if the result does not contain one more row.\n";
static PyObject *
query_onedict(queryObject *self, PyObject *noargs)
{
PyObject *row_dict;
if ((row_dict = _get_async_result(self, 0)) == (PyObject *)self) {
if (self->current_row >= self->max_row) {
Py_INCREF(Py_None);
return Py_None;
}
row_dict = _query_row_as_dict(self);
if (row_dict)
++self->current_row;
}
return row_dict;
}
/* Retrieve the single row from the result as a dictionary. */
static char query_singledict__doc__[] =
"singledict() -- Get the result of a query as single row\n\n"
"The single row from the query result is returned as a dictionary with\n"
"the field names used as the keys.\n"
"This method returns the same single row when called multiple times.\n"
"It raises an InvalidResultError if the result doesn't have exactly one "
"row,\n"
"which will be of type NoResultError or MultipleResultsError "
"specifically.\n";
static PyObject *
query_singledict(queryObject *self, PyObject *noargs)
{
PyObject *row_dict;
if ((row_dict = _get_async_result(self, 0)) == (PyObject *)self) {
if (self->max_row != 1) {
if (self->max_row)
set_error_msg(MultipleResultsError, "Multiple results found");
else
set_error_msg(NoResultError, "No result found");
return NULL;
}
self->current_row = 0;
row_dict = _query_row_as_dict(self);
if (row_dict)
++self->current_row;
}
return row_dict;
}
/* Retrieve the last query result as a list of dictionaries. */
static char query_dictresult__doc__[] =
"dictresult() -- Get the result of a query\n\n"
"The result is returned as a list of rows, each one a dictionary with\n"
"the field names used as the keys.\n";
static PyObject *
query_dictresult(queryObject *self, PyObject *noargs)
{
PyObject *result_list;
int i;
if ((result_list = _get_async_result(self, 0)) == (PyObject *)self) {
if (!(result_list = PyList_New(self->max_row))) {
return NULL;
}
for (i = self->current_row = 0; i < self->max_row; ++i) {
PyObject *row_dict = query_next_dict(self, noargs);
if (!row_dict) {
Py_DECREF(result_list);
return NULL;
}
PyList_SET_ITEM(result_list, i, row_dict);
}
}
return result_list;
}
/* Retrieve last result as iterator of dictionaries. */
static char query_dictiter__doc__[] =
"dictiter() -- Get the result of a query\n\n"
"The result is returned as an iterator of rows, each one a a dictionary\n"
"with the field names used as the keys.\n";
static PyObject *
query_dictiter(queryObject *self, PyObject *noargs)
{
PyObject *res;
if (!dictiter) {
return query_dictresult(self, noargs);
}
if ((res = _get_async_result(self, 1)) != (PyObject *)self)
return res;
return PyObject_CallFunction(dictiter, "(O)", self);
}
/* Retrieve one row from the result as a named tuple. */
static char query_onenamed__doc__[] =
"onenamed() -- Get one row from the result of a query\n\n"
"Only one row from the result is returned as a named tuple of fields.\n"
"This method can be called multiple times to return more rows.\n"
"It returns None if the result does not contain one more row.\n";
static PyObject *
query_onenamed(queryObject *self, PyObject *noargs)
{
PyObject *res;
if (!namednext) {
return query_one(self, noargs);
}
if ((res = _get_async_result(self, 1)) != (PyObject *)self)
return res;
if (self->current_row >= self->max_row) {
Py_INCREF(Py_None);
return Py_None;
}
return PyObject_CallFunction(namednext, "(O)", self);
}
/* Retrieve the single row from the result as a tuple. */
static char query_singlenamed__doc__[] =
"singlenamed() -- Get the result of a query as single row\n\n"
"The single row from the query result is returned as named tuple of "
"fields.\n"
"This method returns the same single row when called multiple times.\n"
"It raises an InvalidResultError if the result doesn't have exactly one "
"row,\n"
"which will be of type NoResultError or MultipleResultsError "
"specifically.\n";
static PyObject *
query_singlenamed(queryObject *self, PyObject *noargs)
{
PyObject *res;
if (!namednext) {
return query_single(self, noargs);
}
if ((res = _get_async_result(self, 1)) != (PyObject *)self)
return res;
if (self->max_row != 1) {
if (self->max_row)
set_error_msg(MultipleResultsError, "Multiple results found");
else
set_error_msg(NoResultError, "No result found");
return NULL;
}
self->current_row = 0;
return PyObject_CallFunction(namednext, "(O)", self);
}
/* Retrieve last result as list of named tuples. */
static char query_namedresult__doc__[] =
"namedresult() -- Get the result of a query\n\n"
"The result is returned as a list of rows, each one a named tuple of "
"fields\n"
"in the order returned by the server.\n";
static PyObject *
query_namedresult(queryObject *self, PyObject *noargs)
{
PyObject *res, *res_list;
if (!namediter) {
return query_getresult(self, noargs);
}
if ((res_list = _get_async_result(self, 1)) == (PyObject *)self) {
res = PyObject_CallFunction(namediter, "(O)", self);
if (!res)
return NULL;
if (PyList_Check(res))
return res;
res_list = PySequence_List(res);
Py_DECREF(res);
}
return res_list;
}
/* Retrieve last result as iterator of named tuples. */
static char query_namediter__doc__[] =
"namediter() -- Get the result of a query\n\n"
"The result is returned as an iterator of rows, each one a named tuple\n"
"of fields in the order returned by the server.\n";
static PyObject *
query_namediter(queryObject *self, PyObject *noargs)
{
PyObject *res, *res_iter;
if (!namediter) {
return query_iter(self);
}
if ((res_iter = _get_async_result(self, 1)) == (PyObject *)self) {
res = PyObject_CallFunction(namediter, "(O)", self);
if (!res)
return NULL;
if (!PyList_Check(res))
return res;
res_iter = (Py_TYPE(res)->tp_iter)((PyObject *)self);
Py_DECREF(res);
}
return res_iter;
}
/* Retrieve the last query result as a list of scalar values. */
static char query_scalarresult__doc__[] =
"scalarresult() -- Get query result as scalars\n\n"
"The result is returned as a list of scalar values where the values\n"
"are the first fields of the rows in the order returned by the server.\n";
static PyObject *
query_scalarresult(queryObject *self, PyObject *noargs)
{
PyObject *result_list;
if ((result_list = _get_async_result(self, 0)) == (PyObject *)self) {
if (!self->num_fields) {
set_error_msg(ProgrammingError, "No fields in result");
return NULL;
}
if (!(result_list = PyList_New(self->max_row))) {
return NULL;
}
for (self->current_row = 0; self->current_row < self->max_row;
++self->current_row) {
PyObject *value = _query_value_in_column(self, 0);
if (!value) {
Py_DECREF(result_list);
return NULL;
}
PyList_SET_ITEM(result_list, self->current_row, value);
}
}
return result_list;
}
/* Retrieve the last query result as iterator of scalar values. */
static char query_scalariter__doc__[] =
"scalariter() -- Get query result as scalars\n\n"
"The result is returned as an iterator of scalar values where the values\n"
"are the first fields of the rows in the order returned by the server.\n";
static PyObject *
query_scalariter(queryObject *self, PyObject *noargs)
{
PyObject *res;
if (!scalariter) {
return query_scalarresult(self, noargs);
}
if ((res = _get_async_result(self, 1)) != (PyObject *)self)
return res;
if (!self->num_fields) {
set_error_msg(ProgrammingError, "No fields in result");
return NULL;
}
return PyObject_CallFunction(scalariter, "(O)", self);
}
/* Retrieve one result as scalar value. */
static char query_onescalar__doc__[] =
"onescalar() -- Get one scalar value from the result of a query\n\n"
"Returns the first field of the next row from the result as a scalar "
"value.\n"
"This method can be called multiple times to return more rows as "
"scalars.\n"
"It returns None if the result does not contain one more row.\n";
static PyObject *
query_onescalar(queryObject *self, PyObject *noargs)
{
PyObject *value;
if ((value = _get_async_result(self, 0)) == (PyObject *)self) {
if (!self->num_fields) {
set_error_msg(ProgrammingError, "No fields in result");
return NULL;
}
if (self->current_row >= self->max_row) {
Py_INCREF(Py_None);
return Py_None;
}
value = _query_value_in_column(self, 0);
if (value)
++self->current_row;
}
return value;
}
/* Retrieves the single row from the result as a tuple. */
static char query_singlescalar__doc__[] =
"singlescalar() -- Get scalar value from single result of a query\n\n"
"Returns the first field of the next row from the result as a scalar "
"value.\n"
"This method returns the same single row when called multiple times.\n"
"It raises an InvalidResultError if the result doesn't have exactly one "
"row,\n"
"which will be of type NoResultError or MultipleResultsError "
"specifically.\n";
static PyObject *
query_singlescalar(queryObject *self, PyObject *noargs)
{
PyObject *value;
if ((value = _get_async_result(self, 0)) == (PyObject *)self) {
if (!self->num_fields) {
set_error_msg(ProgrammingError, "No fields in result");
return NULL;
}
if (self->max_row != 1) {
if (self->max_row)
set_error_msg(MultipleResultsError, "Multiple results found");
else
set_error_msg(NoResultError, "No result found");
return NULL;
}
self->current_row = 0;
value = _query_value_in_column(self, 0);
if (value)
++self->current_row;
}
return value;
}
/* Query sequence protocol methods */
static PySequenceMethods query_sequence_methods = {
(lenfunc)query_len, /* sq_length */
0, /* sq_concat */
0, /* sq_repeat */
(ssizeargfunc)query_getitem, /* sq_item */
0, /* sq_ass_item */
0, /* sq_contains */
0, /* sq_inplace_concat */
0, /* sq_inplace_repeat */
};
/* Query object methods */
static struct PyMethodDef query_methods[] = {
{"getresult", (PyCFunction)query_getresult, METH_NOARGS,
query_getresult__doc__},
{"dictresult", (PyCFunction)query_dictresult, METH_NOARGS,
query_dictresult__doc__},
{"dictiter", (PyCFunction)query_dictiter, METH_NOARGS,
query_dictiter__doc__},
{"namedresult", (PyCFunction)query_namedresult, METH_NOARGS,
query_namedresult__doc__},
{"namediter", (PyCFunction)query_namediter, METH_NOARGS,
query_namediter__doc__},
{"one", (PyCFunction)query_one, METH_NOARGS, query_one__doc__},
{"single", (PyCFunction)query_single, METH_NOARGS, query_single__doc__},
{"onedict", (PyCFunction)query_onedict, METH_NOARGS, query_onedict__doc__},
{"singledict", (PyCFunction)query_singledict, METH_NOARGS,
query_singledict__doc__},
{"onenamed", (PyCFunction)query_onenamed, METH_NOARGS,
query_onenamed__doc__},
{"singlenamed", (PyCFunction)query_singlenamed, METH_NOARGS,
query_singlenamed__doc__},
{"scalarresult", (PyCFunction)query_scalarresult, METH_NOARGS,
query_scalarresult__doc__},
{"scalariter", (PyCFunction)query_scalariter, METH_NOARGS,
query_scalariter__doc__},
{"onescalar", (PyCFunction)query_onescalar, METH_NOARGS,
query_onescalar__doc__},
{"singlescalar", (PyCFunction)query_singlescalar, METH_NOARGS,
query_singlescalar__doc__},
{"fieldname", (PyCFunction)query_fieldname, METH_VARARGS,
query_fieldname__doc__},
{"fieldnum", (PyCFunction)query_fieldnum, METH_VARARGS,
query_fieldnum__doc__},
{"listfields", (PyCFunction)query_listfields, METH_NOARGS,
query_listfields__doc__},
{"fieldinfo", (PyCFunction)query_fieldinfo, METH_VARARGS,
query_fieldinfo__doc__},
{"memsize", (PyCFunction)query_memsize, METH_NOARGS, query_memsize__doc__},
{NULL, NULL}};
static char query__doc__[] = "PyGreSQL query object";
/* Query type definition */
static PyTypeObject queryType = {
PyVarObject_HEAD_INIT(NULL, 0) "pg.Query", /* tp_name */
sizeof(queryObject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor)query_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
&query_sequence_methods, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
(reprfunc)query_str, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
query__doc__, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
(getiterfunc)query_iter, /* tp_iter */
(iternextfunc)query_next, /* tp_iternext */
query_methods, /* tp_methods */
};
|