|
| 1 | +#include <libpq-fe.h> |
1 | 2 | #include <stdio.h>
|
2 | 3 | #include <stdlib.h>
|
3 |
| -#include <libpq-fe.h> |
4 | 4 |
|
5 | 5 | // PGresult *PQexec(PGconn*, const char* command);
|
6 | 6 | // PQgetvalue
|
7 |
| -// %7 = call @PQexec(%2, %6) : (memref<?x1xi8>, memref<?xi8>) -> memref<?x1xi8> |
8 |
| -#pragma lower_to(num_rows_fn, "sql.num_results") |
9 |
| -int num_rows_fn(size_t);// char* |
| 7 | +// %7 = call @PQexec(%2, %6) : (memref<?x1xi8>, memref<?xi8>) -> |
| 8 | +// memref<?x1xi8> |
| 9 | +// #pragma lower_to(num_rows_fn, "sql.num_results") |
| 10 | +// int num_rows_fn(size_t);// char* |
10 | 11 |
|
11 |
| -#pragma lower_to(get_value_fn_int, "sql.get_value") |
12 |
| -int get_value_fn_int(size_t, int, int); |
| 12 | +// #pragma lower_to(get_value_fn_int, "sql.get_value") |
| 13 | +// int get_value_fn_int(size_t, int, int); |
13 | 14 |
|
14 |
| -#pragma lower_to(get_value_fn_double, "sql.get_value") |
15 |
| -double get_value_fn_double(size_t, int, int); |
| 15 | +// #pragma lower_to(get_value_fn_double, "sql.get_value") |
| 16 | +// double get_value_fn_double(size_t, int, int); |
16 | 17 |
|
| 18 | +// #pragma lower_to(execute, "sql.execute") |
| 19 | +// PGresult* execute(size_t, char*); |
17 | 20 |
|
18 | 21 | void do_exit(PGconn *conn) {
|
19 |
| - PQfinish(conn); |
20 |
| - exit(1); |
| 22 | + PQfinish(conn); |
| 23 | + exit(1); |
21 | 24 | }
|
22 | 25 |
|
23 | 26 | int main() {
|
24 |
| - |
25 |
| - PGconn *conn = PQconnectdb("user=janbodnar dbname=testdb"); |
26 |
| - |
27 |
| - if (PQstatus(conn) == CONNECTION_BAD) { |
28 |
| - |
29 |
| - fprintf(stderr, "Connection to database failed: %s\n", |
30 |
| - PQerrorMessage(conn)); |
31 |
| - do_exit(conn); |
32 |
| - } |
33 | 27 |
|
34 |
| - PGresult *res = PQexec(conn, "SELECT VERSION()"); |
35 |
| - |
36 |
| - if (PQresultStatus(res) != PGRES_TUPLES_OK) { |
| 28 | + PGconn *conn = PQconnectdb("user=carl dbname=testdb"); |
37 | 29 |
|
38 |
| - printf("No data retrieved\n"); |
39 |
| - PQclear(res); |
40 |
| - do_exit(conn); |
41 |
| - } |
42 |
| - |
43 |
| - printf("%s\n", PQgetvalue(res, 0, 0)); |
44 |
| - printf("%d\n", get_value_fn_int((size_t)res, 0, 0)); |
45 |
| - printf("%d\n", num_rows_fn((size_t)res)); |
46 |
| - // res, 0, 0)); |
| 30 | + if (PQstatus(conn) == CONNECTION_BAD) { |
47 | 31 |
|
| 32 | + fprintf(stderr, "Connection to database failed: %s\n", |
| 33 | + PQerrorMessage(conn)); |
| 34 | + do_exit(conn); |
| 35 | + } |
| 36 | + |
| 37 | + // PGresult *res = PQexec(conn, "SELECT 17"); |
| 38 | + PGresult *res = PQexec(conn, "SELECT a FROM table1"); |
| 39 | + PGresult *res1 = PQexec(conn, "SELECT * FROM table1 WHERE b > 10 OR c < 10 AND a <= 20"); |
| 40 | + PGresult *res2 = PQexec(conn, "SELECT * FROM table1 WHERE b > 10 AND c < 10"); |
| 41 | + PGresult *res3 = PQexec(conn, "SELECT b, c FROM table1 WHERE a <= 10 LIMIT 10"); |
| 42 | + // PGresult *res3 = PQexec(conn, "SELECT b, c FROM table1 LIMIT ALL"); |
| 43 | + if (PQresultStatus(res) != PGRES_TUPLES_OK) { |
| 44 | + |
| 45 | + printf("No data retrieved\n"); |
48 | 46 | PQclear(res);
|
49 |
| - PQfinish(conn); |
| 47 | + do_exit(conn); |
| 48 | + } |
| 49 | + |
| 50 | + PQclear(res); |
| 51 | + PQclear(res1); |
| 52 | + PQclear(res2); |
| 53 | + PQclear(res3); |
| 54 | + PQfinish(conn); |
50 | 55 |
|
51 |
| - return 0; |
| 56 | + return 0; |
52 | 57 | }
|
0 commit comments