/
populate.c
127 lines (100 loc) · 2.72 KB
/
populate.c
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
#include <stdio.h>
#include <stdlib.h>
#include "libpq-fe.h"
/*
* create table mrow (time bigint, id bigint, value float);
* insert into mrow values (1,1,1) (3,2,5) (4,5,5);
* insert into mrow values (1,1,1);
*/
static void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
int
main(int argc, char **argv)
{
const char *conninfo;
PGconn *conn;
PGresult *res;
int nFields;
int i,
j;
if (argc > 1)
conninfo = argv[1];
else
conninfo = "host=localhost port=5432 user=korisk dbname=test";
/* Make a connection to the database */
conn = PQconnectdb(conninfo);
/* Check to see that the backend connection was successfully made */
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Connection to database failed: %s",
PQerrorMessage(conn));
exit_nicely(conn);
}
/* Start a transaction block */
res = PQexec(conn, "BEGIN");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
PQclear(res);
/*
* * Should PQclear PGresult whenever it is no longer needed to avoid memory
* * leaks
* */
res = PQprepare(conn, "qu", "insert into mrow values ($1::bigint, $2::bigint, $3::float);",3, NULL);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "Prepare query failed: %s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
PQclear(res);
const char *paramValues[3];
int paramLengths[3];
int paramFormats[3];
long time = 0, _time = 0;
long id = 0, _id = 0;
union{
double f;
long i;
} _value, value;
_value.f = 1.0;
value.f = 1.0;
paramValues[0] = (char*)(&time);
paramLengths[0] = sizeof(time);
paramFormats[0] = 1;
paramValues[1] = (char*)(&id);
paramLengths[1] = sizeof(id);
paramFormats[1] = 1;
paramValues[2] = (char*)(&value);
paramLengths[2] = sizeof(value);
paramFormats[2] = 1;
for(i=0; i < 3; i++){
time = htobe64(_time);
id = htobe64(_id);
value.i = htobe64(_value.i);
_time++;
_id++;
_value.f += 0.1;
res = PQexecPrepared(conn, "qu", 3, paramValues, paramLengths, paramFormats, 1);
if (PQresultStatus(res) != PGRES_COMMAND_OK){
fprintf(stderr, "Exec Prepared query failed: %s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
PQclear(res);
}
/*
end the transaction */
res = PQexec(conn, "END");
PQclear(res);
/* close the connection to the database and cleanup */
PQfinish(conn);
return 0;
}