chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
LIBPQ_CFLAGS := $(shell pkg-config --cflags libpq)
|
||||
LIBPQ_LDFLAGS := $(shell pkg-config --libs libpq)
|
||||
APR_CFLAGS := $(shell pkg-config --cflags apr-1 apr-util-1)
|
||||
APR_LDFLAGS := $(shell pkg-config --libs apr-1 apr-util-1)
|
||||
CFLAGS="$(apr-1-config --includes) $(apu-1-config --includes)"
|
||||
|
||||
all: postgres-c-connector-test libaprutil-test libdbi-test
|
||||
|
||||
postgres-c-connector-test: postgres-c-connector-test.c
|
||||
$(CC) $(LIBPQ_CFLAGS) -o $@ $^ $(LIBPQ_LDFLAGS)
|
||||
|
||||
libaprutil-test: libaprutil-test.c
|
||||
$(CC) $(APR_CFLAGS) -o $@ $^ $(APR_LDFLAGS)
|
||||
|
||||
libdbi-test: libdbi-test.c
|
||||
$(CC) -o $@ $^ -ldbi
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -f postgres-c-connector-test libaprutil-test libdbi-test
|
||||
@@ -0,0 +1,117 @@
|
||||
#include <apr.h>
|
||||
#include <apr_pools.h>
|
||||
#include <apr_dbd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static void fail(const char *op, const char *msg) {
|
||||
fprintf(stderr, "%s: %s\n", op, msg ? msg : "(no message)");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static void exec_query(const apr_dbd_driver_t *drv, apr_dbd_t *handle, const char *sql) {
|
||||
int nrows = 0;
|
||||
int rv = apr_dbd_query(drv, handle, &nrows, sql);
|
||||
if (rv != 0)
|
||||
fail(sql, apr_dbd_error(drv, handle, rv));
|
||||
}
|
||||
|
||||
static const char *exec_scalar(const apr_dbd_driver_t *drv, apr_pool_t *pool,
|
||||
apr_dbd_t *handle, const char *sql) {
|
||||
apr_dbd_results_t *res = NULL;
|
||||
int rv = apr_dbd_select(drv, pool, handle, &res, sql, 1);
|
||||
if (rv != 0)
|
||||
fail(sql, apr_dbd_error(drv, handle, rv));
|
||||
apr_dbd_row_t *row = NULL;
|
||||
if (apr_dbd_get_row(drv, pool, res, &row, -1) != 0)
|
||||
fail(sql, "no rows returned");
|
||||
return apr_dbd_get_entry(drv, row, 0);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 3) {
|
||||
fprintf(stderr, "Usage: %s <user> <port>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
const char *user = argv[1];
|
||||
const char *port = argv[2];
|
||||
|
||||
apr_initialize();
|
||||
apr_pool_t *pool;
|
||||
apr_pool_create(&pool, NULL);
|
||||
|
||||
apr_status_t rv = apr_dbd_init(pool);
|
||||
if (rv != APR_SUCCESS) {
|
||||
char buf[256];
|
||||
apr_strerror(rv, buf, sizeof(buf));
|
||||
fail("apr_dbd_init", buf);
|
||||
}
|
||||
|
||||
const apr_dbd_driver_t *driver;
|
||||
rv = apr_dbd_get_driver(pool, "pgsql", &driver);
|
||||
if (rv != APR_SUCCESS) {
|
||||
char buf[256];
|
||||
apr_strerror(rv, buf, sizeof(buf));
|
||||
fail("apr_dbd_get_driver(pgsql)", buf);
|
||||
}
|
||||
|
||||
char params[256];
|
||||
snprintf(params, sizeof(params),
|
||||
"host=localhost port=%s dbname=postgres user=%s password=password",
|
||||
port, user);
|
||||
apr_dbd_t *handle = NULL;
|
||||
const char *open_err = NULL;
|
||||
rv = apr_dbd_open_ex(driver, pool, params, &handle, &open_err);
|
||||
if (rv != APR_SUCCESS)
|
||||
fail("apr_dbd_open_ex", open_err);
|
||||
|
||||
const char *pk_str = exec_scalar(driver, pool, handle, "SELECT pk FROM test_table LIMIT 1");
|
||||
if (!pk_str || atoi(pk_str) != 1) {
|
||||
fprintf(stderr, "expected pk=1, got %s\n", pk_str ? pk_str : "NULL");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
exec_query(driver, handle, "INSERT INTO test_table VALUES (2)");
|
||||
|
||||
const char *count_str = exec_scalar(driver, pool, handle, "SELECT COUNT(*) FROM test_table");
|
||||
if (!count_str || atoi(count_str) != 2) {
|
||||
fprintf(stderr, "expected count=2, got %s\n", count_str ? count_str : "NULL");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
const char *dolt_queries[] = {
|
||||
"DROP TABLE IF EXISTS test",
|
||||
"CREATE TABLE test (pk int, value int, PRIMARY KEY(pk))",
|
||||
"INSERT INTO test (pk, value) VALUES (0, 0)",
|
||||
"SELECT dolt_add('-A')",
|
||||
"SELECT dolt_commit('-m', 'added table test')",
|
||||
"SELECT dolt_checkout('-b', 'mybranch')",
|
||||
"INSERT INTO test VALUES (1, 1)",
|
||||
"SELECT dolt_commit('-a', '-m', 'updated test')",
|
||||
"SELECT dolt_checkout('main')",
|
||||
"SELECT dolt_merge('mybranch')",
|
||||
NULL
|
||||
};
|
||||
for (int i = 0; dolt_queries[i]; i++)
|
||||
exec_query(driver, handle, dolt_queries[i]);
|
||||
|
||||
const char *log_count = exec_scalar(driver, pool, handle, "SELECT COUNT(*) FROM dolt_log");
|
||||
if (!log_count || atoi(log_count) != 4) {
|
||||
fprintf(stderr, "expected 4 dolt_log entries, got %s\n", log_count ? log_count : "NULL");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
const char *test_count = exec_scalar(driver, pool, handle, "SELECT COUNT(*) FROM test");
|
||||
if (!test_count || atoi(test_count) != 2) {
|
||||
fprintf(stderr, "expected 2 rows in test, got %s\n", test_count ? test_count : "NULL");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
apr_dbd_close(driver, handle);
|
||||
apr_pool_destroy(pool);
|
||||
apr_terminate();
|
||||
|
||||
printf("libaprutil apr_dbd test passed\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>English</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>com.apple.xcode.dsym.libaprutil-test</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>dSYM</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
</dict>
|
||||
</plist>
|
||||
BIN
Binary file not shown.
+5
@@ -0,0 +1,5 @@
|
||||
---
|
||||
triple: 'arm64-apple-darwin'
|
||||
binary-path: libaprutil-test
|
||||
relocations: []
|
||||
...
|
||||
@@ -0,0 +1,122 @@
|
||||
#include <dbi/dbi.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static void conn_fail(dbi_conn conn, const char *op) {
|
||||
const char *errstr = NULL;
|
||||
dbi_conn_error(conn, &errstr);
|
||||
fprintf(stderr, "%s: %s\n", op, errstr ? errstr : "(no message)");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static dbi_result exec_query(dbi_conn conn, const char *sql) {
|
||||
dbi_result res = dbi_conn_query(conn, sql);
|
||||
if (!res)
|
||||
conn_fail(conn, sql);
|
||||
return res;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 3) {
|
||||
fprintf(stderr, "Usage: %s <user> <port>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
const char *user = argv[1];
|
||||
int port = atoi(argv[2]);
|
||||
|
||||
dbi_inst dbi;
|
||||
if (dbi_initialize_r(NULL, &dbi) < 0) {
|
||||
fprintf(stderr, "dbi_initialize_r failed: no drivers found\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
dbi_conn conn = dbi_conn_new_r("pgsql", dbi);
|
||||
if (!conn) {
|
||||
fprintf(stderr, "dbi_conn_new_r(pgsql) failed: driver not installed?\n");
|
||||
dbi_shutdown_r(dbi);
|
||||
return 1;
|
||||
}
|
||||
|
||||
dbi_conn_set_option(conn, "host", "localhost");
|
||||
dbi_conn_set_option_numeric(conn, "port", (long)port);
|
||||
dbi_conn_set_option(conn, "dbname", "postgres");
|
||||
dbi_conn_set_option(conn, "username", user);
|
||||
dbi_conn_set_option(conn, "password", "password");
|
||||
|
||||
if (dbi_conn_connect(conn) < 0)
|
||||
conn_fail(conn, "dbi_conn_connect");
|
||||
|
||||
// SELECT pk from test_table (set up by bats setup())
|
||||
dbi_result res = exec_query(conn, "SELECT pk FROM test_table LIMIT 1");
|
||||
if (!dbi_result_next_row(res)) {
|
||||
fprintf(stderr, "expected at least one row in test_table\n");
|
||||
exit(1);
|
||||
}
|
||||
int pk = dbi_result_get_int(res, "pk");
|
||||
dbi_result_free(res);
|
||||
if (pk != 1) {
|
||||
fprintf(stderr, "expected pk=1, got %d\n", pk);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// INSERT
|
||||
res = exec_query(conn, "INSERT INTO test_table VALUES (2)");
|
||||
dbi_result_free(res);
|
||||
|
||||
// COUNT
|
||||
res = exec_query(conn, "SELECT COUNT(*) FROM test_table");
|
||||
if (!dbi_result_next_row(res)) {
|
||||
fprintf(stderr, "expected count row\n");
|
||||
exit(1);
|
||||
}
|
||||
long long count = dbi_result_get_longlong(res, "count");
|
||||
dbi_result_free(res);
|
||||
if (count != 2) {
|
||||
fprintf(stderr, "expected count=2, got %lld\n", count);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Dolt workflow
|
||||
const char *dolt_queries[] = {
|
||||
"DROP TABLE IF EXISTS test",
|
||||
"CREATE TABLE test (pk int, value int, PRIMARY KEY(pk))",
|
||||
"INSERT INTO test (pk, value) VALUES (0, 0)",
|
||||
"SELECT dolt_add('-A')",
|
||||
"SELECT dolt_commit('-m', 'added table test')",
|
||||
"SELECT dolt_checkout('-b', 'mybranch')",
|
||||
"INSERT INTO test VALUES (1, 1)",
|
||||
"SELECT dolt_commit('-a', '-m', 'updated test')",
|
||||
"SELECT dolt_checkout('main')",
|
||||
"SELECT dolt_merge('mybranch')",
|
||||
NULL
|
||||
};
|
||||
for (int i = 0; dolt_queries[i]; i++) {
|
||||
res = exec_query(conn, dolt_queries[i]);
|
||||
dbi_result_free(res);
|
||||
}
|
||||
|
||||
res = exec_query(conn, "SELECT COUNT(*) FROM dolt_log");
|
||||
dbi_result_next_row(res);
|
||||
long long log_count = dbi_result_get_longlong(res, "count");
|
||||
dbi_result_free(res);
|
||||
if (log_count != 4) {
|
||||
fprintf(stderr, "expected 4 dolt_log entries, got %lld\n", log_count);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
res = exec_query(conn, "SELECT COUNT(*) FROM test");
|
||||
dbi_result_next_row(res);
|
||||
long long test_count = dbi_result_get_longlong(res, "count");
|
||||
dbi_result_free(res);
|
||||
if (test_count != 2) {
|
||||
fprintf(stderr, "expected 2 rows in test, got %lld\n", test_count);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
dbi_conn_close(conn);
|
||||
dbi_shutdown_r(dbi);
|
||||
|
||||
printf("libdbi pgsql test passed\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <libpq-fe.h>
|
||||
|
||||
#define QUERIES_SIZE 13
|
||||
|
||||
char *queries[QUERIES_SIZE] = {
|
||||
"create table test (pk int, value int, d1 decimal(9, 3), f1 float, c1 char(10), t1 text, primary key(pk))",
|
||||
"select * from test",
|
||||
"insert into test (pk, value, d1, f1, c1, t1) values (0,0,0.0,0.0,'abc','a1')",
|
||||
"select * from test",
|
||||
"select dolt_add('-A');",
|
||||
"select dolt_commit('-m', 'my commit')",
|
||||
"select COUNT(*) FROM dolt.log",
|
||||
"select dolt_checkout('-b', 'mybranch')",
|
||||
"insert into test (pk, value, d1, f1, c1, t1) values (10,10, 123456.789, 420.42,'example','some text')",
|
||||
"select dolt_commit('-a', '-m', 'my commit2')",
|
||||
"select dolt_checkout('main')",
|
||||
"select dolt_merge('mybranch')",
|
||||
"select COUNT(*) FROM dolt.log",
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
char* user = argv[1];
|
||||
int port = atoi(argv[2]);
|
||||
|
||||
// Connect to the database
|
||||
// conninfo is a string of keywords and values separated by spaces.
|
||||
char conninfo[100];
|
||||
sprintf(conninfo, "dbname=postgres user=%s password=password host=localhost port=%d", user, port);
|
||||
|
||||
// Create a connection
|
||||
PGconn *conn = PQconnectdb(conninfo);
|
||||
|
||||
// Check if the connection is successful
|
||||
if (PQstatus(conn) != CONNECTION_OK) {
|
||||
// If not successful, print the error message and finish the connection
|
||||
printf("Error while connecting to the database server: %s\n", PQerrorMessage(conn));
|
||||
|
||||
// Finish the connection
|
||||
PQfinish(conn);
|
||||
|
||||
// Exit the program
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// We have successfully established a connection to the database server
|
||||
printf("Connection Established\n");
|
||||
printf("Port: %s\n", PQport(conn));
|
||||
printf("Host: %s\n", PQhost(conn));
|
||||
printf("DBName: %s\n", PQdb(conn));
|
||||
|
||||
for ( int i = 0; i < QUERIES_SIZE; i++ ) {
|
||||
// Submit the query and retrieve the result
|
||||
PGresult *res = PQexec(conn, queries[i]);
|
||||
|
||||
// Check the status of the query result
|
||||
ExecStatusType resStatus = PQresultStatus(res);
|
||||
|
||||
if (resStatus == PGRES_COMMAND_OK || resStatus == PGRES_TUPLES_OK) {
|
||||
// Successful completion of a command returning no data OR data (such as a SELECT or SHOW).
|
||||
|
||||
// Clear the result
|
||||
PQclear(res);
|
||||
} else {
|
||||
printf("QUERY FAILED: %s\n", queries[i]);
|
||||
// If not successful, print the error message and finish the connection
|
||||
printf("Error while executing the query: %s\n", PQerrorMessage(conn));
|
||||
|
||||
// Clear the result
|
||||
PQclear(res);
|
||||
|
||||
// Finish the connection
|
||||
PQfinish(conn);
|
||||
|
||||
// Exit the program
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Submit the query and retrieve the result
|
||||
PGresult *res = PQexec(conn, "SELECT * FROM test WHERE pk = 10");
|
||||
|
||||
// Check the status of the query result
|
||||
ExecStatusType resStatus = PQresultStatus(res);
|
||||
|
||||
if (resStatus != PGRES_TUPLES_OK) {
|
||||
printf("QUERY FAILED: %s\n", "SELECT * FROM test WHERE pk = 10");
|
||||
// If not successful, print the error message and finish the connection
|
||||
printf("Error while executing the query: %s\n", PQerrorMessage(conn));
|
||||
|
||||
// Clear the result
|
||||
PQclear(res);
|
||||
|
||||
// Finish the connection
|
||||
PQfinish(conn);
|
||||
|
||||
// Exit the program
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
// Get the number of columns in the query result
|
||||
int cols = PQnfields(res);
|
||||
printf("Number of cols: %d\n", cols);
|
||||
assert(cols == 6);
|
||||
|
||||
char *expectedCols[6] = {"pk", "value", "d1", "f1", "c1", "t1"};
|
||||
// Assert the column names
|
||||
for (int i = 0; i < cols; i++) {
|
||||
assert(strcmp(PQfname(res, i), expectedCols[i]) == 0);
|
||||
}
|
||||
|
||||
// Get the number of rows in the query result
|
||||
int rows = PQntuples(res);
|
||||
printf("Number of rows: %d\n", rows);
|
||||
assert(rows == 1);
|
||||
|
||||
char *expectedRowResults[6] = {"10", "10", "123456.789", "420.42", "example ", "some text"};
|
||||
// Assert query result
|
||||
for (int i = 0; i < rows; i++) {
|
||||
for (int j = 0; j < cols; j++) {
|
||||
char *actual = PQgetvalue(res, i, j);
|
||||
printf("EXPECTED: '%s'\n", expectedRowResults[j]);
|
||||
printf("ACTUAL: '%s'\n", actual);
|
||||
assert(strcmp(actual, expectedRowResults[j]) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
// Clear the result
|
||||
PQclear(res);
|
||||
|
||||
// Close the connection and free the memory
|
||||
PQfinish(conn);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user