Created
August 16, 2017 08:44
-
-
Save danielgustafsson/23d907068e024595eb91f2af284d4583 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment