Sophie

Sophie

distrib > Fedora > 14 > x86_64 > by-pkgid > c71168e7cd0acdcf2760741fe30d7fdb > files > 48

libsq3-devel-20071018-9.fc14.x86_64.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>libsq3: sq3_log_db.cpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.2-20100208 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div class="tabs">
    <ul>
      <li><a href="files.html"><span>File&nbsp;List</span></a></li>
    </ul>
  </div>
<h1>sq3_log_db.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00002"></a>00002 <span class="preprocessor">#include &quot;sq3_log_db.hpp&quot;</span>
<a name="l00003"></a>00003 <span class="preprocessor">#include &lt;iostream&gt;</span>
<a name="l00004"></a>00004 <span class="preprocessor">#include &lt;sstream&gt;</span>
<a name="l00005"></a><a class="code" href="classsq3_1_1log__db.html#a5de7c0cad428bed8f34eaa2f7c1b89ec">00005</a> <span class="preprocessor">#include &lt;vector&gt;</span>
<a name="l00006"></a>00006 <span class="preprocessor">#include &lt;cstdio&gt;</span> <span class="comment">// vsnprint()</span>
<a name="l00007"></a>00007 <span class="preprocessor">#include &lt;cstring&gt;</span>
<a name="l00008"></a>00008 <span class="keyword">namespace </span>sq3 {
<a name="l00009"></a>00009 
<a name="l00010"></a>00010     <a class="code" href="classsq3_1_1log__db.html#a5a013b51c7ebdba7ec6566755031a4c1" title="Creates an unopened database.">log_db::log_db</a>( std::string <span class="keyword">const</span> &amp; filename )
<a name="l00011"></a><a class="code" href="classsq3_1_1log__db.html#ab27be97ae871b3c2ece7913403d13e9b">00011</a>         : <a class="code" href="classsq3_1_1database.html" title="Encapsulates a connection to an sqlite database.">database</a>()
<a name="l00012"></a>00012     {
<a name="l00013"></a>00013         this-&gt;<a class="code" href="classsq3_1_1database.html#ab7b7609d8dfc4b773c72f80839c38572" title="Creates/opens the given db file.">database::open</a>(filename);
<a name="l00014"></a>00014     }
<a name="l00015"></a><a class="code" href="classsq3_1_1log__db.html#a1b654e2c000d3497f723848cd358f24e">00015</a> 
<a name="l00016"></a>00016     <a class="code" href="classsq3_1_1log__db.html#ab27be97ae871b3c2ece7913403d13e9b" title="Closes this db.">log_db::~log_db</a>()
<a name="l00017"></a>00017     {
<a name="l00018"></a>00018     }
<a name="l00019"></a>00019 
<a name="l00020"></a>00020     <span class="keywordtype">int</span> <a class="code" href="classsq3_1_1log__db.html#a1b654e2c000d3497f723848cd358f24e" title="Called when open() succeeds.">log_db::on_open</a>()
<a name="l00021"></a>00021     {
<a name="l00022"></a>00022         <span class="keywordflow">if</span>( ! this-&gt;<a class="code" href="classsq3_1_1database.html#af3b6b07c453a2ce5eaf9072e9cbe1f84" title="Returns true if this db is opened.">is_open</a>() )
<a name="l00023"></a>00023         {
<a name="l00024"></a>00024             <span class="keywordflow">return</span> SQLITE_ERROR;
<a name="l00025"></a>00025         }
<a name="l00026"></a>00026         std::string sql( <span class="stringliteral">&quot;create table if not exists log(ts,msg TEXT)&quot;</span> );
<a name="l00027"></a>00027         this-&gt;<a class="code" href="classsq3_1_1database.html#a65d5e801060b1dad34d539b191305130" title="Functionally identical to execute(char const *).">execute</a>( sql );<span class="comment"></span>
<a name="l00028"></a>00028 <span class="comment">        /**</span>
<a name="l00029"></a>00029 <span class="comment">           enable temp_store=MEMORY to speed this up considably on PocketPC</span>
<a name="l00030"></a>00030 <span class="comment">           devices writing to SD cards.</span>
<a name="l00031"></a><a class="code" href="classsq3_1_1log__db.html#af2c6103c4980d3ae24270c6ed618839d">00031</a> <span class="comment">        */</span>
<a name="l00032"></a>00032         this-&gt;<a class="code" href="classsq3_1_1database.html#ac9fda7f2a5e13df8a1e7cce2ecb13279" title="This is a convenience wrapper for execute( &amp;quot;pragma ...&amp;quot; ).">pragma</a>( <span class="stringliteral">&quot;temp_store = MEMORY&quot;</span> );
<a name="l00033"></a>00033         <span class="keywordflow">return</span> SQLITE_OK;
<a name="l00034"></a>00034     }
<a name="l00035"></a>00035 
<a name="l00036"></a>00036     <span class="keywordtype">int</span> <a class="code" href="classsq3_1_1log__db.html#af2c6103c4980d3ae24270c6ed618839d" title="Empties the log database.">log_db::clear</a>()
<a name="l00037"></a>00037     {
<a name="l00038"></a>00038         <span class="keywordflow">return</span> this-&gt;<a class="code" href="classsq3_1_1database.html#a65d5e801060b1dad34d539b191305130" title="Functionally identical to execute(char const *).">execute</a>( <span class="stringliteral">&quot;delete from log&quot;</span> );
<a name="l00039"></a><a class="code" href="classsq3_1_1log__db.html#a95f7d7c504f6bd1cb1d438add7479de5">00039</a>     }
<a name="l00040"></a>00040 
<a name="l00041"></a>00041     <span class="keyword">static</span> <span class="keywordtype">char</span> <span class="keyword">const</span> * LOG_DB_LOG_INSERT_SQL = <span class="stringliteral">&quot;insert into log (ts,msg) values(strftime(&#39;%Y-%m-%d %H:%M:%f&#39;,&#39;now&#39;),?)&quot;</span>;
<a name="l00042"></a>00042         <span class="comment">// &quot;insert into log (ts,msg) values(current_timestamp,?)&quot;</span>
<a name="l00043"></a>00043 
<a name="l00044"></a>00044     <span class="keywordtype">bool</span> <a class="code" href="classsq3_1_1log__db.html#a95f7d7c504f6bd1cb1d438add7479de5" title="Logs a message to the log database.">log_db::log</a>( std::string <span class="keyword">const</span> &amp; msg )
<a name="l00045"></a>00045     {
<a name="l00046"></a>00046         <span class="keywordflow">if</span>( ! this-&gt;<a class="code" href="classsq3_1_1database.html#af3b6b07c453a2ce5eaf9072e9cbe1f84" title="Returns true if this db is opened.">is_open</a>() )
<a name="l00047"></a>00047         {
<a name="l00048"></a>00048             <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00049"></a>00049         }
<a name="l00050"></a>00050         <span class="keywordflow">if</span>( msg.empty() ) <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00051"></a>00051         statement st( *<span class="keyword">this</span>, LOG_DB_LOG_INSERT_SQL );
<a name="l00052"></a>00052         st.bind( 1, msg );
<a name="l00053"></a><a class="code" href="classsq3_1_1log__db.html#a400663fbeb21bbf919d3c1b944ec3613">00053</a>         <span class="keywordtype">int</span> rc = st.execute();
<a name="l00054"></a>00054         <span class="keywordflow">return</span> <a class="code" href="namespacesq3.html#a080ee04ba6c41a38c4d3efbb1ccb741d" title="rc_is_okay() is an easy way to check if rc is one of SQLITE_OK, SQLITE_ROW, or SQLITE_DONE...">rc_is_okay</a>( rc );
<a name="l00055"></a>00055         <span class="comment">// In theory, if the count_changes PRAGMA is on then SQLITE_ROW will be returned from execute()</span>
<a name="l00056"></a>00056     }
<a name="l00057"></a>00057 
<a name="l00058"></a>00058     <span class="keywordtype">bool</span> <a class="code" href="classsq3_1_1log__db.html#a95f7d7c504f6bd1cb1d438add7479de5" title="Logs a message to the log database.">log_db::log</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *format,...)
<a name="l00059"></a>00059     {
<a name="l00060"></a>00060         <span class="keywordflow">if</span>( ! this-&gt;<a class="code" href="classsq3_1_1database.html#af3b6b07c453a2ce5eaf9072e9cbe1f84" title="Returns true if this db is opened.">is_open</a>() )
<a name="l00061"></a>00061         {
<a name="l00062"></a>00062             <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00063"></a>00063         }
<a name="l00064"></a>00064         <span class="keyword">const</span> <span class="keywordtype">int</span> buffsz = <span class="keyword">static_cast&lt;</span><span class="keywordtype">int</span><span class="keyword">&gt;</span>( std::max( (<span class="keywordtype">size_t</span>) 2048, strlen(format) * 2 ) );
<a name="l00065"></a>00065         std::vector&lt;char&gt; buffer( buffsz, <span class="charliteral">&#39;\0&#39;</span> );
<a name="l00066"></a>00066         va_list vargs;
<a name="l00067"></a>00067         va_start ( vargs, format );
<a name="l00068"></a>00068         <span class="keyword">using namespace </span>std;<span class="comment"></span>
<a name="l00069"></a>00069 <span class="comment">        /** In gcc, vsnprintf() is in the std namespace, but in MSVC it is not, so we use &#39;using&#39;</span>
<a name="l00070"></a>00070 <span class="comment">            to accomodate both cases. */</span>
<a name="l00071"></a>00071         <span class="keywordtype">int</span> size = vsnprintf(&amp;buffer[0], buffsz, format, vargs);
<a name="l00072"></a>00072         va_end( vargs );
<a name="l00073"></a>00073         <span class="keywordflow">if</span> (size &gt; (buffsz-1))
<a name="l00074"></a>00074         {
<a name="l00075"></a>00075             <span class="comment">// replace tail of msg with &quot;...&quot;</span>
<a name="l00076"></a>00076             size = buffsz-1;
<a name="l00077"></a>00077             <span class="keywordflow">for</span>( <span class="keywordtype">int</span> i = buffsz-4; i &lt; buffsz-1; ++i )
<a name="l00078"></a>00078             {
<a name="l00079"></a>00079                 buffer[i] = <span class="charliteral">&#39;.&#39;</span>;
<a name="l00080"></a>00080             }
<a name="l00081"></a>00081         }
<a name="l00082"></a>00082         buffer[size] = <span class="charliteral">&#39;\0&#39;</span>;
<a name="l00083"></a>00083         <span class="keywordflow">if</span>( size )
<a name="l00084"></a>00084         {
<a name="l00085"></a>00085             statement st( *<span class="keyword">this</span>, LOG_DB_LOG_INSERT_SQL );
<a name="l00086"></a>00086             st.bind( 1, &amp;buffer[0], size );
<a name="l00087"></a>00087             <span class="keywordtype">int</span> rc = st.execute();
<a name="l00088"></a>00088             <span class="comment">//std::cout &lt;&lt; &quot;FYI: rc from an INSERT is &quot; &lt;&lt; rc &lt;&lt; &#39;\n&#39;; // == SQLITE_DONE</span>
<a name="l00089"></a>00089             <span class="keywordflow">return</span> SQLITE_DONE == rc;
<a name="l00090"></a>00090         }
<a name="l00091"></a><a class="code" href="classsq3_1_1log__db.html#adb4bc83fbe16ee98160631e55eb3ce2e">00091</a>         <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00092"></a>00092     }
<a name="l00093"></a>00093 
<a name="l00094"></a>00094 <span class="preprocessor">#undef LOG_DB_LOG_INSERT_SQL</span>
<a name="l00095"></a>00095 <span class="preprocessor"></span>
<a name="l00096"></a>00096     <span class="keywordtype">void</span> <a class="code" href="classsq3_1_1log__db.html#adb4bc83fbe16ee98160631e55eb3ce2e" title="Shows the last count entries using a subclass-specific method.">log_db::show_last</a>( <span class="keywordtype">int</span> count )
<a name="l00097"></a>00097     {
<a name="l00098"></a>00098         <span class="keywordflow">if</span>( ! this-&gt;<a class="code" href="classsq3_1_1database.html#af3b6b07c453a2ce5eaf9072e9cbe1f84" title="Returns true if this db is opened.">is_open</a>() )
<a name="l00099"></a>00099         {
<a name="l00100"></a>00100             <span class="keywordflow">return</span>;
<a name="l00101"></a>00101         }
<a name="l00102"></a>00102         std::ostream &amp; os = std::cout;
<a name="l00103"></a>00103         os &lt;&lt; <span class="stringliteral">&quot;sq3::log_db: most recent &quot;</span>
<a name="l00104"></a>00104            &lt;&lt; count &lt;&lt; <span class="stringliteral">&quot; entries:\n&quot;</span>;
<a name="l00105"></a>00105         <span class="keywordflow">if</span>( ! this-&gt;<a class="code" href="classsq3_1_1database.html#af3b6b07c453a2ce5eaf9072e9cbe1f84" title="Returns true if this db is opened.">is_open</a>() )
<a name="l00106"></a>00106         {
<a name="l00107"></a>00107             os &lt;&lt; <span class="stringliteral">&quot;ERROR: Log database is not opened!&quot;</span>;
<a name="l00108"></a>00108             <span class="keywordflow">return</span>;
<a name="l00109"></a>00109         }
<a name="l00110"></a>00110         std::ostringstream fmt;
<a name="l00111"></a>00111         <span class="keywordflow">if</span>( 0 )
<a name="l00112"></a>00112         { <span class="comment">// newest entries at the top:</span>
<a name="l00113"></a>00113         fmt &lt;&lt; <span class="stringliteral">&quot;select /*DATETIME(ts)*/ts,msg from log &quot;</span>
<a name="l00114"></a>00114             &lt;&lt; <span class="stringliteral">&quot;order by ts desc, rowid desc&quot;</span>
<a name="l00115"></a>00115             &lt;&lt;<span class="stringliteral">&quot; limit &quot;</span> &lt;&lt; count
<a name="l00116"></a>00116             ;
<a name="l00117"></a>00117         }
<a name="l00118"></a>00118         <span class="keywordflow">else</span>
<a name="l00119"></a>00119         { <span class="comment">// in &quot;natural order&quot;:</span>
<a name="l00120"></a>00120             fmt &lt;&lt; <span class="stringliteral">&quot;select /*DATETIME(ts)*/ts,msg from log &quot;</span>
<a name="l00121"></a>00121                 &lt;&lt; <span class="stringliteral">&quot;order by ts asc, rowid asc&quot;</span>
<a name="l00122"></a>00122                 &lt;&lt;<span class="stringliteral">&quot; limit &quot;</span> &lt;&lt; count
<a name="l00123"></a>00123                 ;
<a name="l00124"></a>00124         }
<a name="l00125"></a>00125         std::string sql(fmt.str());
<a name="l00126"></a>00126         statement st( *<span class="keyword">this</span>, sql );
<a name="l00127"></a>00127         cursor r = st.get_cursor();
<a name="l00128"></a>00128         std::string buff;
<a name="l00129"></a>00129         <span class="keywordflow">while</span>( SQLITE_ROW == r.step() )
<a name="l00130"></a>00130         {
<a name="l00131"></a>00131             std::string tmp;
<a name="l00132"></a>00132             r.get( 0, tmp );
<a name="l00133"></a>00133             os &lt;&lt; tmp &lt;&lt; <span class="stringliteral">&quot;: &quot;</span>;
<a name="l00134"></a><a class="code" href="classsq3_1_1log__db.html#a77bbc0cc808134c178417564a8da2006">00134</a>             r.get( 1, tmp );
<a name="l00135"></a>00135             os &lt;&lt; tmp &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;
<a name="l00136"></a>00136         }
<a name="l00137"></a>00137     }
<a name="l00138"></a>00138 
<a name="l00139"></a>00139     <span class="keywordtype">bool</span> <a class="code" href="classsq3_1_1log__db.html#a77bbc0cc808134c178417564a8da2006" title="Deletes all entries in the log except the leaveThisMany most recent.">log_db::trim</a>( <span class="keywordtype">int</span> count )
<a name="l00140"></a>00140     {
<a name="l00141"></a>00141         <span class="keywordflow">if</span>( this-&gt;<a class="code" href="classsq3_1_1database.html#af3b6b07c453a2ce5eaf9072e9cbe1f84" title="Returns true if this db is opened.">is_open</a>() )
<a name="l00142"></a>00142         {
<a name="l00143"></a>00143             std::ostringstream os;
<a name="l00144"></a>00144             os &lt;&lt; <span class="stringliteral">&quot;delete from log where rowid not in (select rowid from log order by ts desc, rowid desc limit &quot;</span>&lt;&lt;count&lt;&lt;<span class="stringliteral">&quot;)&quot;</span>;
<a name="l00145"></a>00145             std::string sql( os.str() );
<a name="l00146"></a>00146             <span class="keywordflow">if</span>( SQLITE_OK == this-&gt;<a class="code" href="classsq3_1_1database.html#a65d5e801060b1dad34d539b191305130" title="Functionally identical to execute(char const *).">execute</a>( sql.c_str() ) )
<a name="l00147"></a>00147             {
<a name="l00148"></a>00148                 this-&gt;<a class="code" href="classsq3_1_1database.html#a1cd1358658597f872f849691dafc12ed" title="Convenience wrapper around execute(&amp;quot;vacuum&amp;quot;).">vacuum</a>();
<a name="l00149"></a>00149             }
<a name="l00150"></a>00150             <span class="keywordflow">return</span> <span class="keyword">true</span>; <span class="comment">// delete will fail if the db is empty, but we&#39;ll consider that to be success</span>
<a name="l00151"></a>00151         }
<a name="l00152"></a>00152         <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00153"></a>00153     }
<a name="l00154"></a>00154 
<a name="l00155"></a>00155 } <span class="comment">// namespace</span>
</pre></div></div>
<hr class="footer"/><address style="text-align: right;"><small>Generated by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2-20100208 </small></address>
</body>
</html>