Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <libpq-fe.h>
- int
- main(int argc, char **argv)
- {
- int typmod;
- int ret = 1;
- int i;
- char * conninfo = "dbname=danielgustafsson user=test password=test";
- PGconn *conn;
- PGresult *res;
- ExecStatusType status;
- conn = PQconnectdb(conninfo);
- if (PQstatus(conn) != CONNECTION_OK)
- {
- printf("ERROR: Connection to database failed: %s\n",
- PQerrorMessage(conn));
- goto end;
- }
- PQexec(conn, "create table ptest2("
- "col0 character(10), "
- "col1 char(5), "
- "col2 numeric(15,5), "
- "col3 integer, "
- "col4 bit) distributed by (col2)");
- res = PQexec(conn, "select * from ptest2");
- if (PQresultStatus(res) != PGRES_TUPLES_OK)
- {
- printf("ERROR: Query failed: %s\n", PQerrorMessage(conn));
- PQclear(res);
- goto end;
- }
- for (i = 0; i < PQnfields(res); i++)
- {
- printf("* col%d typmod: %d, size: %d, format: %s\n",
- i, PQfmod(res, i), PQfsize(res, i),
- PQfformat(res, i) ? "binary" : "text");
- }
- /* Parse typmod for numeric(15,5) */
- typmod = PQfmod(res, 2);
- typmod -= sizeof(int);
- printf("* numeric(15,5): precision: %d, scale: %d\n", (typmod >> 16 & 0xffff), (typmod & 0xffff));
- PQclear(res);
- /* Test faulty insertion */
- res = PQexec(conn, "insert into ptest2 (col0) values ('Ab34567890123')");
- if ((status = PQresultStatus(res)) != PGRES_COMMAND_OK)
- {
- printf("* Insert failed\n\t result status:%s\n\t error: %s\t sqlstate: %s\n",
- PQresStatus(status),
- PQerrorMessage(conn),
- PQresultErrorField(res, PG_DIAG_SQLSTATE));
- }
- PQexec(conn, "drop table ptest2");
- ret = 0;
- end:
- PQfinish(conn);
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement