Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <libpq-fe.h>
  3.  
  4. int
  5. main(int argc, char **argv)
  6. {
  7. int typmod;
  8. int ret = 1;
  9. int i;
  10. char * conninfo = "dbname=danielgustafsson user=test password=test";
  11. PGconn *conn;
  12. PGresult *res;
  13. ExecStatusType status;
  14.  
  15. conn = PQconnectdb(conninfo);
  16.  
  17. if (PQstatus(conn) != CONNECTION_OK)
  18. {
  19. printf("ERROR: Connection to database failed: %s\n",
  20. PQerrorMessage(conn));
  21. goto end;
  22. }
  23.  
  24. PQexec(conn, "create table ptest2("
  25. "col0 character(10), "
  26. "col1 char(5), "
  27. "col2 numeric(15,5), "
  28. "col3 integer, "
  29. "col4 bit) distributed by (col2)");
  30.  
  31. res = PQexec(conn, "select * from ptest2");
  32. if (PQresultStatus(res) != PGRES_TUPLES_OK)
  33. {
  34. printf("ERROR: Query failed: %s\n", PQerrorMessage(conn));
  35. PQclear(res);
  36. goto end;
  37. }
  38.  
  39. for (i = 0; i < PQnfields(res); i++)
  40. {
  41. printf("* col%d typmod: %d, size: %d, format: %s\n",
  42. i, PQfmod(res, i), PQfsize(res, i),
  43. PQfformat(res, i) ? "binary" : "text");
  44. }
  45.  
  46. /* Parse typmod for numeric(15,5) */
  47. typmod = PQfmod(res, 2);
  48. typmod -= sizeof(int);
  49. printf("* numeric(15,5): precision: %d, scale: %d\n", (typmod >> 16 & 0xffff), (typmod & 0xffff));
  50.  
  51. PQclear(res);
  52.  
  53. /* Test faulty insertion */
  54. res = PQexec(conn, "insert into ptest2 (col0) values ('Ab34567890123')");
  55. if ((status = PQresultStatus(res)) != PGRES_COMMAND_OK)
  56. {
  57. printf("* Insert failed\n\t result status:%s\n\t error: %s\t sqlstate: %s\n",
  58. PQresStatus(status),
  59. PQerrorMessage(conn),
  60. PQresultErrorField(res, PG_DIAG_SQLSTATE));
  61. }
  62.  
  63. PQexec(conn, "drop table ptest2");
  64.  
  65. ret = 0;
  66.  
  67. end:
  68. PQfinish(conn);
  69. return ret;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement