LCOV - code coverage report
Current view: top level - src - remember_history.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 61.4 % 57 35
Test Date: 2026-07-12 01:01:34 Functions: 100.0 % 1 1
Branches: 56.2 % 32 18

             Branch data     Line data    Source code
       1                 :             : #include "precizer.h"
       2                 :             : 
       3                 :             : /**
       4                 :             :  * @brief Store a formatted log line in the TEMP remember_history table
       5                 :             :  *
       6                 :             :  * @details The table is created in db_init(). Empty messages are ignored. When
       7                 :             :  * the previous remembered row does not end with a newline, the new message is
       8                 :             :  * appended to that row so multi-part log output remains one remembered message
       9                 :             :  *
      10                 :             :  * @param[in] message Message bytes to remember
      11                 :             :  * @param[in] line_len Number of bytes to read from @p message
      12                 :             :  *
      13                 :             :  * @note The message buffer may be non-null-terminated; line_len is used to
      14                 :             :  *       bound the insert or append operation
      15                 :             :  */
      16                 :         143 : void rational_remember(
      17                 :             :         const char *message,
      18                 :             :         const int  line_len)
      19                 :             : {
      20         [ -  + ]:         143 :         if(message == NULL)
      21                 :             :         {
      22                 :           0 :                 return;
      23                 :             :         }
      24                 :             : 
      25         [ -  + ]:         143 :         if(line_len <= 0)
      26                 :             :         {
      27                 :           0 :                 return;
      28                 :             :         }
      29                 :             : 
      30                 :         143 :         sqlite3_stmt *lookup_stmt = NULL;
      31                 :         143 :         sqlite3_stmt *write_stmt = NULL;
      32                 :         143 :         sqlite3_int64 last_id = 0;
      33                 :         143 :         bool append_to_last = false;
      34                 :             : 
      35                 :         143 :         const char *select_sql =
      36                 :             :                 "SELECT id, message FROM temp.remember_history "
      37                 :             :                 "ORDER BY id DESC LIMIT 1;";
      38                 :             : 
      39                 :         143 :         int rc = sqlite3_prepare_v2(config->db,select_sql,-1,&lookup_stmt,NULL);
      40                 :             : 
      41         [ -  + ]:         143 :         if(rc != SQLITE_OK)
      42                 :             :         {
      43                 :           0 :                 log_sqlite_error(config->db,rc,NULL,
      44                 :             :                         "Failed to prepare remember_history lookup");
      45                 :             :         } else {
      46                 :         143 :                 rc = sqlite3_step(lookup_stmt);
      47                 :             : 
      48         [ +  + ]:         143 :                 if(rc == SQLITE_ROW)
      49                 :             :                 {
      50                 :          74 :                         const unsigned char *text = sqlite3_column_text(lookup_stmt,1);
      51                 :          74 :                         const int text_len = sqlite3_column_bytes(lookup_stmt,1);
      52                 :             : 
      53   [ +  -  +  -  :          74 :                         if(text != NULL && text_len > 0 && text[text_len - 1] != '\n')
                   +  + ]
      54                 :             :                         {
      55                 :          28 :                                 last_id = sqlite3_column_int64(lookup_stmt,0);
      56                 :          28 :                                 append_to_last = true;
      57                 :             :                         }
      58                 :             :                 }
      59                 :             :         }
      60                 :             : 
      61                 :         143 :         sqlite3_finalize(lookup_stmt);
      62                 :             : 
      63         [ +  + ]:         143 :         if(append_to_last)
      64                 :             :         {
      65                 :          28 :                 const char *update_sql =
      66                 :             :                         "UPDATE temp.remember_history "
      67                 :             :                         "SET message = message || ?1 WHERE id = ?2;";
      68                 :             : 
      69                 :          28 :                 rc = sqlite3_prepare_v2(config->db,update_sql,-1,&write_stmt,NULL);
      70                 :             : 
      71         [ -  + ]:          28 :                 if(rc != SQLITE_OK)
      72                 :             :                 {
      73                 :           0 :                         log_sqlite_error(config->db,rc,NULL,
      74                 :             :                                 "Failed to prepare remember_history update");
      75                 :           0 :                         sqlite3_finalize(write_stmt);
      76                 :           0 :                         return;
      77                 :             :                 }
      78                 :             : 
      79                 :          28 :                 rc = sqlite3_bind_text(write_stmt,1,message,line_len,SQLITE_TRANSIENT);
      80                 :             : 
      81         [ -  + ]:          28 :                 if(rc != SQLITE_OK)
      82                 :             :                 {
      83                 :           0 :                         log_sqlite_error(config->db,rc,NULL,
      84                 :             :                                 "Failed to bind remember_history update");
      85                 :           0 :                         sqlite3_finalize(write_stmt);
      86                 :           0 :                         return;
      87                 :             :                 }
      88                 :             : 
      89                 :          28 :                 rc = sqlite3_bind_int64(write_stmt,2,last_id);
      90                 :             : 
      91         [ -  + ]:          28 :                 if(rc != SQLITE_OK)
      92                 :             :                 {
      93                 :           0 :                         log_sqlite_error(config->db,rc,NULL,
      94                 :             :                                 "Failed to bind remember_history id");
      95                 :           0 :                         sqlite3_finalize(write_stmt);
      96                 :           0 :                         return;
      97                 :             :                 }
      98                 :             :         } else {
      99                 :         115 :                 const char *insert_sql =
     100                 :             :                         "INSERT INTO temp.remember_history (message) VALUES (?1);";
     101                 :             : 
     102                 :         115 :                 rc = sqlite3_prepare_v2(config->db,insert_sql,-1,&write_stmt,NULL);
     103                 :             : 
     104         [ -  + ]:         115 :                 if(rc != SQLITE_OK)
     105                 :             :                 {
     106                 :           0 :                         log_sqlite_error(config->db,rc,NULL,
     107                 :             :                                 "Failed to prepare remember_history insert");
     108                 :           0 :                         sqlite3_finalize(write_stmt);
     109                 :           0 :                         return;
     110                 :             :                 }
     111                 :             : 
     112                 :         115 :                 rc = sqlite3_bind_text(write_stmt,1,message,line_len,SQLITE_TRANSIENT);
     113                 :             :         }
     114                 :             : 
     115         [ -  + ]:         143 :         if(rc != SQLITE_OK)
     116                 :             :         {
     117                 :           0 :                 log_sqlite_error(config->db,rc,NULL,
     118                 :             :                         "Failed to bind remember_history message");
     119                 :           0 :                 sqlite3_finalize(write_stmt);
     120                 :           0 :                 return;
     121                 :             :         }
     122                 :             : 
     123                 :         143 :         rc = sqlite3_step(write_stmt);
     124                 :             : 
     125         [ -  + ]:         143 :         if(rc != SQLITE_DONE)
     126                 :             :         {
     127         [ #  # ]:           0 :                 if(append_to_last)
     128                 :             :                 {
     129                 :           0 :                         log_sqlite_error(config->db,rc,NULL,
     130                 :             :                                 "Failed to update remember_history record");
     131                 :             :                 } else {
     132                 :           0 :                         log_sqlite_error(config->db,rc,NULL,
     133                 :             :                                 "Failed to insert remember_history record");
     134                 :             :                 }
     135                 :             :         }
     136                 :             : 
     137                 :         143 :         sqlite3_finalize(write_stmt);
     138                 :             : 
     139         [ -  + ]:         143 :         if(rc != SQLITE_DONE)
     140                 :             :         {
     141                 :           0 :                 return;
     142                 :             :         }
     143                 :             : }
        

Generated by: LCOV version 2.0-1