Changeset 870 for trunk/pgmodule.c
 Timestamp:
 Mar 26, 2016, 1:22:18 PM (3 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/pgmodule.c
r844 r870 473 473 case PYGRES_INT: 474 474 n = sizeof(buf)/sizeof(buf[0])  1; 475 if ( size < n) n =size;475 if ((int)size < n) n = (int)size; 476 476 for (i = 0, t = buf; i < n; ++i) *t++ = *s++; 477 477 *t = '\0'; … … 481 481 case PYGRES_LONG: 482 482 n = sizeof(buf)/sizeof(buf[0])  1; 483 if ( size < n) n =size;483 if ((int)size < n) n = (int)size; 484 484 for (i = 0, t = buf; i < n; ++i) *t++ = *s++; 485 485 *t = '\0'; … … 1461 1461 const int n = PQnfields(res); 1462 1462 1463 if (n <= 0) 1464 return PyStr_FromString("(nothing selected)"); 1465 1466 char * const aligns = (char *) PyMem_Malloc(n * sizeof(char)); 1467 int * const sizes = (int *) PyMem_Malloc(n * sizeof(int)); 1468 1469 if (!aligns  !sizes) 1470 { 1471 PyMem_Free(aligns); PyMem_Free(sizes); return PyErr_NoMemory(); 1472 } 1473 1474 const int m = PQntuples(res); 1475 int i, j; 1476 size_t size; 1477 char *buffer; 1478 1479 /* calculate sizes and alignments */ 1480 for (j = 0; j < n; ++j) 1481 { 1482 const char * const s = PQfname(res, j); 1483 const int format = PQfformat(res, j); 1484 1485 sizes[j] = s ? (int)strlen(s) : 0; 1486 if (format) 1463 if (n > 0) 1464 { 1465 char * const aligns = (char *) PyMem_Malloc(n * sizeof(char)); 1466 int * const sizes = (int *) PyMem_Malloc(n * sizeof(int)); 1467 1468 if (aligns && sizes) 1487 1469 { 1488 aligns[j] = '\0'; 1489 if (m && sizes[j] < 8) 1490 /* "<binary>" must fit */ 1491 sizes[j] = 8; 1470 const int m = PQntuples(res); 1471 int i, j; 1472 size_t size; 1473 char *buffer; 1474 1475 /* calculate sizes and alignments */ 1476 for (j = 0; j < n; ++j) 1477 { 1478 const char * const s = PQfname(res, j); 1479 const int format = PQfformat(res, j); 1480 1481 sizes[j] = s ? (int)strlen(s) : 0; 1482 if (format) 1483 { 1484 aligns[j] = '\0'; 1485 if (m && sizes[j] < 8) 1486 /* "<binary>" must fit */ 1487 sizes[j] = 8; 1488 } 1489 else 1490 { 1491 const Oid ftype = PQftype(res, j); 1492 1493 switch (ftype) 1494 { 1495 case INT2OID: 1496 case INT4OID: 1497 case INT8OID: 1498 case FLOAT4OID: 1499 case FLOAT8OID: 1500 case NUMERICOID: 1501 case OIDOID: 1502 case XIDOID: 1503 case CIDOID: 1504 case CASHOID: 1505 aligns[j] = 'r'; 1506 break; 1507 default: 1508 aligns[j] = 'l'; 1509 } 1510 } 1511 } 1512 for (i = 0; i < m; ++i) 1513 { 1514 for (j = 0; j < n; ++j) 1515 { 1516 if (aligns[j]) 1517 { 1518 const int k = PQgetlength(res, i, j); 1519 1520 if (sizes[j] < k) 1521 /* value must fit */ 1522 sizes[j] = k; 1523 } 1524 } 1525 } 1526 size = 0; 1527 /* size of one row */ 1528 for (j = 0; j < n; ++j) size += sizes[j] + 1; 1529 /* times number of rows incl. heading */ 1530 size *= (m + 2); 1531 /* plus size of footer */ 1532 size += 40; 1533 /* is the buffer size that needs to be allocated */ 1534 buffer = (char *) PyMem_Malloc(size); 1535 if (buffer) 1536 { 1537 char *p = buffer; 1538 PyObject *result; 1539 1540 /* create the header */ 1541 for (j = 0; j < n; ++j) 1542 { 1543 const char * const s = PQfname(res, j); 1544 const int k = sizes[j]; 1545 const int h = (k  (int)strlen(s)) / 2; 1546 1547 sprintf(p, "%*s", h, ""); 1548 sprintf(p + h, "%*s", k  h, s); 1549 p += k; 1550 if (j + 1 < n) 1551 *p++ = ''; 1552 } 1553 *p++ = '\n'; 1554 for (j = 0; j < n; ++j) 1555 { 1556 int k = sizes[j]; 1557 1558 while (k) 1559 *p++ = ''; 1560 if (j + 1 < n) 1561 *p++ = '+'; 1562 } 1563 *p++ = '\n'; 1564 /* create the body */ 1565 for (i = 0; i < m; ++i) 1566 { 1567 for (j = 0; j < n; ++j) 1568 { 1569 const char align = aligns[j]; 1570 const int k = sizes[j]; 1571 1572 if (align) 1573 { 1574 sprintf(p, align == 'r' ? 1575 "%*s" : "%*s", k, 1576 PQgetvalue(res, i, j)); 1577 } 1578 else 1579 { 1580 sprintf(p, "%*s", k, 1581 PQgetisnull(res, i, j) ? 1582 "" : "<binary>"); 1583 } 1584 p += k; 1585 if (j + 1 < n) 1586 *p++ = ''; 1587 } 1588 *p++ = '\n'; 1589 } 1590 /* free memory */ 1591 PyMem_Free(aligns); PyMem_Free(sizes); 1592 /* create the footer */ 1593 sprintf(p, "(%d row%s)", m, m == 1 ? "" : "s"); 1594 /* return the result */ 1595 result = PyStr_FromString(buffer); 1596 PyMem_Free(buffer); 1597 return result; 1598 } 1599 else 1600 { 1601 PyMem_Free(aligns); PyMem_Free(sizes); return PyErr_NoMemory(); 1602 } 1492 1603 } 1493 1604 else 1494 1605 { 1495 const Oid ftype = PQftype(res, j); 1496 1497 switch (ftype) 1498 { 1499 case INT2OID: 1500 case INT4OID: 1501 case INT8OID: 1502 case FLOAT4OID: 1503 case FLOAT8OID: 1504 case NUMERICOID: 1505 case OIDOID: 1506 case XIDOID: 1507 case CIDOID: 1508 case CASHOID: 1509 aligns[j] = 'r'; 1510 break; 1511 default: 1512 aligns[j] = 'l'; 1513 } 1514 } 1515 } 1516 for (i = 0; i < m; ++i) 1517 { 1518 for (j = 0; j < n; ++j) 1519 { 1520 if (aligns[j]) 1521 { 1522 const int k = PQgetlength(res, i, j); 1523 1524 if (sizes[j] < k) 1525 /* value must fit */ 1526 sizes[j] = k; 1527 } 1528 } 1529 } 1530 size = 0; 1531 /* size of one row */ 1532 for (j = 0; j < n; ++j) size += sizes[j] + 1; 1533 /* times number of rows incl. heading */ 1534 size *= (m + 2); 1535 /* plus size of footer */ 1536 size += 40; 1537 /* is the buffer size that needs to be allocated */ 1538 buffer = (char *) PyMem_Malloc(size); 1539 if (!buffer) 1540 { 1541 PyMem_Free(aligns); PyMem_Free(sizes); return PyErr_NoMemory(); 1542 } 1543 char *p = buffer; 1544 PyObject *result; 1545 1546 /* create the header */ 1547 for (j = 0; j < n; ++j) 1548 { 1549 const char * const s = PQfname(res, j); 1550 const int k = sizes[j]; 1551 const int h = (k  (int)strlen(s)) / 2; 1552 1553 sprintf(p, "%*s", h, ""); 1554 sprintf(p + h, "%*s", k  h, s); 1555 p += k; 1556 if (j + 1 < n) 1557 *p++ = ''; 1558 } 1559 *p++ = '\n'; 1560 for (j = 0; j < n; ++j) 1561 { 1562 int k = sizes[j]; 1563 1564 while (k) 1565 *p++ = ''; 1566 if (j + 1 < n) 1567 *p++ = '+'; 1568 } 1569 *p++ = '\n'; 1570 /* create the body */ 1571 for (i = 0; i < m; ++i) 1572 { 1573 for (j = 0; j < n; ++j) 1574 { 1575 const char align = aligns[j]; 1576 const int k = sizes[j]; 1577 1578 if (align) 1579 { 1580 sprintf(p, align == 'r' ? 1581 "%*s" : "%*s", k, 1582 PQgetvalue(res, i, j)); 1583 } 1584 else 1585 { 1586 sprintf(p, "%*s", k, 1587 PQgetisnull(res, i, j) ? 1588 "" : "<binary>"); 1589 } 1590 p += k; 1591 if (j + 1 < n) 1592 *p++ = ''; 1593 } 1594 *p++ = '\n'; 1595 } 1596 /* free memory */ 1597 PyMem_Free(aligns); PyMem_Free(sizes); 1598 /* create the footer */ 1599 sprintf(p, "(%d row%s)", m, m == 1 ? "" : "s"); 1600 /* return the result */ 1601 result = PyStr_FromString(buffer); 1602 PyMem_Free(buffer); 1603 return result; 1606 PyMem_Free(aligns); PyMem_Free(sizes); return PyErr_NoMemory(); 1607 } 1608 } 1609 else 1610 return PyStr_FromString("(nothing selected)"); 1604 1611 } 1605 1612 … … 2630 2637 return NULL; /* pass the UnicodeEncodeError */ 2631 2638 } 2632 const char* t = PyBytes_AsString(s); 2633 while (*t && bufsiz) 2639 else 2634 2640 { 2635 if (*t == '\\'  *t == '\t'  *t == '\n') 2641 const char* t = PyBytes_AsString(s); 2642 while (*t && bufsiz) 2636 2643 { 2637 *bufpt++ = '\\'; bufsiz; 2638 if (!bufsiz) break; 2644 if (*t == '\\'  *t == '\t'  *t == '\n') 2645 { 2646 *bufpt++ = '\\'; bufsiz; 2647 if (!bufsiz) break; 2648 } 2649 *bufpt++ = *t++; bufsiz; 2639 2650 } 2640 *bufpt++ = *t++; bufsiz;2651 Py_DECREF(s); 2641 2652 } 2642 Py_DECREF(s);2643 2653 } 2644 2654 else if (PyInt_Check(item)  PyLong_Check(item))
Note: See TracChangeset
for help on using the changeset viewer.