Demonstrates how to work with a sqlite3 target.
Demonstrates how to work with a sqlite3 target.
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
static sqlite3_stmt *card_stmts[2] = { NULL, NULL };
void *
void *data,
size_t *count ) {
sqlite3 *db = data;
const char *card_insert_sql = "INSERT INTO played_cards ( suit, rank ) "
"VALUES ( $suite, $rank )";
int sql_result;
const char *player_insert_sql = "INSERT INTO taken_turns ( player_name ) "
"VALUES ( $name )";
const char *suit;
const char *rank;
const char *name;
if( !card_stmts[0] ) {
sql_result = sqlite3_prepare_v2( db,
card_insert_sql,
-1,
&card_stmts[0],
NULL );
if( sql_result != SQLITE_OK ) {
printf( "couldn't prepare the played_cards insert stmt: %s\n",
sqlite3_errmsg( db ) );
return NULL;
}
} else {
sqlite3_reset( card_stmts[0] );
}
if( !card_stmts[1] ) {
sql_result = sqlite3_prepare_v2( db,
player_insert_sql,
-1,
&card_stmts[1],
NULL );
if( sql_result != SQLITE_OK ) {
printf( "couldn't prepare the taken_turns insert stmt: %s\n",
sqlite3_errmsg( db ) );
return NULL;
}
} else {
sqlite3_reset( card_stmts[1] );
}
sql_result = sqlite3_bind_text( card_stmts[0],
1,
suit,
-1,
SQLITE_TRANSIENT );
if( sql_result != SQLITE_OK ) {
printf( "couldn't bind the card suit: %s\n",
sqlite3_errmsg( db ) );
return NULL;
}
sql_result = sqlite3_bind_text( card_stmts[0],
2,
rank,
-1,
SQLITE_TRANSIENT );
if( sql_result != SQLITE_OK ) {
printf( "couldn't bind the card rank: %s\n",
sqlite3_errmsg( db ) );
return NULL;
}
sql_result = sqlite3_bind_text( card_stmts[1],
1,
name,
-1,
SQLITE_TRANSIENT );
if( sql_result != SQLITE_OK ) {
printf( "couldn't bind the player name: %s\n",
sqlite3_errmsg( db ) );
return NULL;
}
free( ( char * ) suit );
free( ( char * ) rank );
free( ( char * ) name );
*count = 2;
return &card_stmts;
}
int
main( int argc, char **argv ) {
int result = EXIT_SUCCESS;
sqlite3 *db;
const char *card_logs_create_sql = "CREATE TABLE card_logs ("
" log_id INTEGER PRIMARY KEY,"
" facility INTEGER NOT NULL,"
" severity INTEGER NOT NULL,"
" timestamp TEXT,"
" structured_data TEXT,"
" message TEXT )";
sqlite3_stmt *card_logs_create_stmt = NULL ;
int sql_result;
const char *card_logs_insert = "INSERT INTO card_logs ( facility, severity,"
" timestamp,"
" structured_data,"
" message )"
"VALUES ( $facility, $severity, $timestamp,"
" $structured_data, $message )";
const char *played_cards_create_sql = "CREATE TABLE played_cards ("
" played_card_id INTEGER PRIMARY KEY,"
" suit TEXT,"
" rank TEXT"
")";
sqlite3_stmt *played_cards_create_stmt = NULL;
const char *taken_turns_create_sql = "CREATE TABLE taken_turns ("
" taken_turn_id INTEGER PRIMARY KEY,"
" player_name TEXT"
")";
sqlite3_stmt *taken_turns_create_stmt = NULL;
if( !db_target ) {
result = EXIT_FAILURE;
goto cleanup_and_finish;
}
puts( "could not create default table, perhaps it already exists?" );
}
result = EXIT_FAILURE;
goto cleanup_and_finish;
}
if( !db ) {
result = EXIT_FAILURE;
goto cleanup_and_finish;
}
sql_result = sqlite3_prepare_v2( db,
card_logs_create_sql,
-1,
&card_logs_create_stmt,
NULL );
if( sql_result != SQLITE_OK ) {
puts( "could not create card_logs table, perhaps it already exists?" );
} else {
sql_result = sqlite3_step( card_logs_create_stmt );
if( sql_result != SQLITE_DONE ) {
printf( "couldn't create the card_logs table: %s\n",
sqlite3_errmsg( db ) );
result = EXIT_FAILURE;
goto cleanup_and_finish;
}
}
result = EXIT_FAILURE;
goto cleanup_and_finish;
}
STUMPLESS_SEVERITY_INFO,
"card-counter",
"card-played",
"a card was played" );
sql_result = sqlite3_prepare_v2( db,
played_cards_create_sql,
-1,
&played_cards_create_stmt,
NULL );
if( sql_result != SQLITE_OK ) {
puts( "could not create played_cards table, perhaps it already exists?" );
} else {
sql_result = sqlite3_step( played_cards_create_stmt );
if( sql_result != SQLITE_DONE ) {
printf( "couldn't create the played_cards table: %s\n",
sqlite3_errmsg( db ) );
result = EXIT_FAILURE;
goto cleanup_and_finish;
}
}
sql_result = sqlite3_prepare_v2( db,
taken_turns_create_sql,
-1,
&taken_turns_create_stmt,
NULL );
if( sql_result != SQLITE_OK ) {
puts( "could not create taken_turns table, perhaps it already exists?" );
} else {
sql_result = sqlite3_step( taken_turns_create_stmt );
if( sql_result != SQLITE_DONE ) {
printf( "couldn't create the taken_turns table: %s\n",
sqlite3_errmsg( db ) );
result = EXIT_FAILURE;
goto cleanup_and_finish;
}
}
result = EXIT_FAILURE;
goto cleanup_and_finish;
}
cleanup_and_finish:
sqlite3_finalize( card_logs_create_stmt );
sqlite3_finalize( played_cards_create_stmt );
sqlite3_finalize( taken_turns_create_stmt );
sqlite3_finalize( card_stmts[0] );
sqlite3_finalize( card_stmts[1] );
return result;
}
STUMPLESS_PUBLIC_FUNCTION void stumpless_destroy_entry_and_contents(const struct stumpless_entry *entry)
Destroys an entry as well as all elements and params that it contains, freeing any allocated memory.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_entry * stumpless_add_new_param_to_entry(struct stumpless_entry *entry, const char *element_name, const char *param_name, const char *param_value)
Creates a new param and adds it to the given element in the given entry.
STUMPLESS_PUBLIC_FUNCTION const char * stumpless_get_entry_param_value_by_name(const struct stumpless_entry *entry, const char *element_name, const char *param_name)
Gets the value of the first param from the element with the given name in an entry.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_entry * stumpless_new_entry(enum stumpless_facility facility, enum stumpless_severity severity, const char *app_name, const char *msgid, const char *message,...)
Creates a new entry with the given characteristics.
STUMPLESS_PUBLIC_FUNCTION void stumpless_perror(const char *prefix)
Prints information about the current error to the error stream.
STUMPLESS_PUBLIC_FUNCTION bool stumpless_has_error(void)
True if the last call to a stumpless function encountered an error.
STUMPLESS_PUBLIC_FUNCTION void stumpless_free_all(void)
Closes the default target if it has been opened, frees all memory allocated internally,...
SQLite3 targets allow logs to be sent to a SQLite3 database.
STUMPLESS_PUBLIC_FUNCTION bool stumpless_close_sqlite3_target_and_db(const struct stumpless_target *target)
Closes a SQLite3 target and its database handle.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_target * stumpless_set_sqlite3_insert_sql(struct stumpless_target *target, const char *sql)
Sets the SQL statement used to insert entries into the database.
STUMPLESS_PUBLIC_FUNCTION void * stumpless_get_sqlite3_db(const struct stumpless_target *target)
Gets the SQLite3 database handle used by the target.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_target * stumpless_create_default_sqlite3_table(struct stumpless_target *target)
Creates a table in the target's database for use with the default SQLite3 insertion behavior.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_target * stumpless_set_sqlite3_prepare(struct stumpless_target *target, stumpless_sqlite3_prepare_func_t preparer, void *data)
Set the function used to prepare statements for entries to this target.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_target * stumpless_open_sqlite3_target(const char *name)
Opens a SQLite3 target.
A log entry.
Definition entry.h:60
A target that log entries can be sent to.
Definition target.h:140
The main header file for the stumpless logging library.
STUMPLESS_PUBLIC_FUNCTION int stumpless_add_message(struct stumpless_target *target, const char *message,...)
Adds a message to a given target.
STUMPLESS_PUBLIC_FUNCTION int stumpless_add_entry(struct stumpless_target *target, const struct stumpless_entry *entry)
Adds an entry into a given target.