--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/main.c Sat Dec 07 18:56:37 2024 +0100 @@ -0,0 +1,146 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2017 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBLIITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <sqlite3.h> + +#include <dbutils/dbutils.h> +#include <dbutils/sqlite.h> + +const char *sql_create_table_person = +"create table if not exists Person (" +"person_id integer primary key autoincrement, " +"name text, " +"email text, " +"age integer, " +"iscustomer integer , " +"hash integer);"; + +const char *sql_check_table = +"select person_id from Person limit 1;"; + +const char *sql_create_test_data = +"insert into person (name, email, age, iscustomer, hash) " +"values " +"('alice', 'alice@example.com', 30, 1, 123456789), " +"('bob', 'bob@example.com', 25, 0, 987654321);"; + +typedef struct Person { + int64_t person_id; + + cxmutstr name; + cxmutstr email; + int age; + bool iscustomer; + uint64_t hash; +} Person; + +static int create_test_data(sqlite3 *db); + +int main(int argc, char **argv) { + + + DBUContext *ctx = dbuContextCreate(); + + DBUClass *person = dbuRegisterClass(ctx, "person", Person, person_id); + dbuClassAdd(person, Person, name); + dbuClassAdd(person, Person, email); + dbuClassAdd(person, Person, age); + dbuClassAdd(person, Person, iscustomer); + dbuClassAdd(person, Person, hash); + + + + // Open or create the database + sqlite3 *db; + int rc = sqlite3_open("test.db", &db); + if(rc != SQLITE_OK) { + fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db)); + sqlite3_close(db); + return 1; + } + + if(create_test_data(db)) { + return 1; + } + + CxList *persons = dbuSQLiteQuerySingleTable(ctx, db, "person", "select * from Person;"); + if(persons) { + CxIterator i = cxListIterator(persons); + cx_foreach(Person *, p, i) { + printf("{ person_id = %" PRId64 ", name = \"%s\", email = \"%s\", age = %d, iscustomer = %s, hash = %" PRIu64 " }\n", + p->person_id, p->name.ptr, p->email.ptr, p->age, p->iscustomer ? "true" : "false", p->hash); + } + } else { + fprintf(stderr, "Error\n"); + } + + sqlite3_close(db); + + return 0; +} + +static int create_test_data(sqlite3 *db) { + char *err_msg = NULL; + int rc = sqlite3_exec(db, sql_create_table_person, 0, 0, &err_msg); + if(rc != SQLITE_OK) { + fprintf(stderr, "SQLite error: %s\n", err_msg); + sqlite3_free(err_msg); + sqlite3_close(db); + return 1; + } + + + sqlite3_stmt *stmt; + rc = sqlite3_prepare_v2(db, sql_check_table, -1, &stmt, 0); + if(rc != SQLITE_OK) { + fprintf(stderr, "SQLite error: %s\n", sqlite3_errmsg(db)); + sqlite3_close(db); + return 1; + } + + int exists = 0; + if(sqlite3_step(stmt) == SQLITE_ROW) { + exists = 1; + } + sqlite3_finalize(stmt); + + if(exists) { + return 0; + } + + rc = sqlite3_exec(db, sql_create_test_data, 0, 0, &err_msg); + if(rc != SQLITE_OK) { + fprintf(stderr, "SQLite error: %s\n", err_msg); + sqlite3_free(err_msg); + sqlite3_close(db); + return 1; + } + + return 0; +}