2 #include "sq3_log_db.hpp"
26 std::string sql(
"create table if not exists log(ts,msg TEXT)" );
32 this->
pragma(
"temp_store = MEMORY" );
38 return this->
execute(
"delete from log" );
41 static char const * LOG_DB_LOG_INSERT_SQL =
"insert into log (ts,msg) values(strftime('%Y-%m-%d %H:%M:%f','now'),?)";
50 if( msg.empty() )
return true;
51 statement st( *
this, LOG_DB_LOG_INSERT_SQL );
64 const int buffsz =
static_cast<int>( std::max( (
size_t) 2048, strlen(format) * 2 ) );
65 std::vector<char> buffer( buffsz,
'\0' );
67 va_start ( vargs, format );
71 int size = vsnprintf(&buffer[0], buffsz, format, vargs);
73 if (size > (buffsz-1))
77 for(
int i = buffsz-4; i < buffsz-1; ++i )
85 statement st( *
this, LOG_DB_LOG_INSERT_SQL );
86 st.
bind( 1, &buffer[0], size );
89 return SQLITE_DONE == rc;
94 #undef LOG_DB_LOG_INSERT_SQL
102 std::ostream & os = std::cout;
103 os <<
"sq3::log_db: most recent "
104 << count <<
" entries:\n";
107 os <<
"ERROR: Log database is not opened!";
110 std::ostringstream fmt;
113 fmt <<
"select /*DATETIME(ts)*/ts,msg from log "
114 <<
"order by ts desc, rowid desc"
120 fmt <<
"select /*DATETIME(ts)*/ts,msg from log "
121 <<
"order by ts asc, rowid asc"
125 std::string sql(fmt.str());
127 cursor r = st.get_cursor();
129 while( SQLITE_ROW == r.
step() )
143 std::ostringstream os;
144 os <<
"delete from log where rowid not in (select rowid from log order by ts desc, rowid desc limit "<<count<<
")";
145 std::string sql( os.str() );
146 if( SQLITE_OK == this->
execute( sql.c_str() ) )
Encapsulates a connection to an sqlite database.
virtual int clear()
Empties the log database.
int execute(const std::string &sql)
Functionally identical to execute(char const *).
int bind(int index)
Binds NULL to the given placeholder index (1-based, not 0-based!).
log_db()
Creates an unopened database.
This class represents a prepared database statement.
bool is_open() const
Returns true if this db is opened.
int vacuum()
Convenience wrapper around execute("vacuum").
virtual ~log_db()
Closes this db.
int step()
Uses sqlite3_step() to step through this object's data set by one step.
int get(int index, int &tgt)
If column index (0-based) is in bounds then this function assigns tgt to the value of the given colum...
bool log(std::string const &msg)
Logs a message to the log database.
virtual int open(char const *, long flags=0)
Creates/opens the given db file.
int pragma(char const *code)
This is a convenience wrapper for execute( "pragma ..." ).
virtual int on_open()
Called when open() succeeds.
virtual void show_last(int howMany)
Shows the last count entries using a subclass-specific method.
int execute()
Assumes this object's SQL statement is a single statement.
The sq3 namespace encapsulates an OO sqlite3 API very similar to the sqlite3x API, but this one uses no exception handling (i.e., it doesn't throw on errors).
bool rc_is_okay(int rc)
rc_is_okay() is an easy way to check if rc is one of SQLITE_OK, SQLITE_ROW, or SQLITE_DONE.
bool trim(int leaveThisMany)
Deletes all entries in the log except the leaveThisMany most recent.
This type is for stepping through a db query result.