From 1911df764dc401e854aa0308b6104ff5d4028425 Mon Sep 17 00:00:00 2001 From: Joel Anderson Date: Thu, 23 Nov 2023 07:30:48 -0500 Subject: [PATCH] continue adding to sqlite3 example --- docs/examples/sqlite3/README.md | 33 +++++++++++++++++++++++++++++++- test/function/target/sqlite3.cpp | 15 +++++++-------- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/docs/examples/sqlite3/README.md b/docs/examples/sqlite3/README.md index 251c89153..b1c7423ec 100644 --- a/docs/examples/sqlite3/README.md +++ b/docs/examples/sqlite3/README.md @@ -95,7 +95,7 @@ values. You might have noticed that the default SQL statement uses several named SQL parameters for the entry values. You can use these in your custom SQL as well! They are resolved by name (position is ignored) and if a name is not present -in the SQL then it is simply left out. In addition to the parameters listed in +in the SQL then it is simply left out. In addition to the parameters used in the default statement, there are a few more available if you need them: * `$facility` is the facility portion of the entry as an integer. This is @@ -109,8 +109,39 @@ For this example, lets say that you have your own log table with the following schema, and you want entries to go into it instead. ```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 +); +``` + +We've made a few adjustments to the default schema here. Of course, there is a +different table name to be more descriptive about the type of logs that are in +the table. We've also broken the prival into it's separate parts, so that it is +easier to filter entries using SQL without needing to parse the prival first. +Finally, we've cut the columns down to specific things that our application +cares about. + +The default insert statement won't work here of course, so we'll need to write +a new one to fit. This is pretty straightforward: +```sql +INSERT INTO card_logs ( facility, severity, timestamp, structured_data, + message ) +VALUES ( $facility, $severity, $timestamp, $structured_data, $message ) ``` +Now we can start putting logs into our new table! This time, we'll include some +structured data in our entry as well. + +```c + +``` + + # Custom Prepared SQL Statements TODO fill in diff --git a/test/function/target/sqlite3.cpp b/test/function/target/sqlite3.cpp index 449eb3a94..cb27da67f 100644 --- a/test/function/target/sqlite3.cpp +++ b/test/function/target/sqlite3.cpp @@ -402,14 +402,13 @@ namespace { "message TEXT)"; sqlite3_stmt *create_stmt = NULL; int sql_result; - const char *insert_sql = "INSERT INTO l (prival, version," - " timestamp, hostname, app_name," - " procid, msgid, structured_data," - " message) " - "VALUES ($prival, 1, $timestamp," - " $hostname, $app_name, $procid," - " $msgid, '-'," - " $message)"; + const char *insert_sql = "INSERT INTO l ( prival, version, timestamp," + " hostname, app_name, procid," + " msgid, structured_data," + " message ) " + "VALUES ( $prival, 1, $timestamp, $hostname," + " $app_name, $procid, $msgid, '-'," + " $message )"; const struct stumpless_target *result; const char *current_sql; int add_result;