<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>libsqlite3x: sqlite3x_cursor.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.5.9 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main 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 List</span></a></li> </ul> </div> <h1>sqlite3x_cursor.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span> <a name="l00002"></a>00002 <span class="comment"> Copyright (C) 2004-2005 Cory Nelson</span> <a name="l00003"></a>00003 <span class="comment"></span> <a name="l00004"></a>00004 <span class="comment"> This software is provided 'as-is', without any express or implied</span> <a name="l00005"></a>00005 <span class="comment"> warranty. In no event will the authors be held liable for any damages</span> <a name="l00006"></a>00006 <span class="comment"> arising from the use of this software.</span> <a name="l00007"></a>00007 <span class="comment"></span> <a name="l00008"></a>00008 <span class="comment"> Permission is granted to anyone to use this software for any purpose,</span> <a name="l00009"></a>00009 <span class="comment"> including commercial applications, and to alter it and redistribute it</span> <a name="l00010"></a>00010 <span class="comment"> freely, subject to the following restrictions:</span> <a name="l00011"></a>00011 <span class="comment"></span> <a name="l00012"></a>00012 <span class="comment"> 1. The origin of this software must not be misrepresented; you must not</span> <a name="l00013"></a>00013 <span class="comment"> claim that you wrote the original software. If you use this software</span> <a name="l00014"></a>00014 <span class="comment"> in a product, an acknowledgment in the product documentation would be</span> <a name="l00015"></a>00015 <span class="comment"> appreciated but is not required.</span> <a name="l00016"></a>00016 <span class="comment"> 2. Altered source versions must be plainly marked as such, and must not be</span> <a name="l00017"></a>00017 <span class="comment"> misrepresented as being the original software.</span> <a name="l00018"></a>00018 <span class="comment"> 3. This notice may not be removed or altered from any source distribution.</span> <a name="l00019"></a>00019 <span class="comment"> </span> <a name="l00020"></a>00020 <span class="comment">*/</span> <a name="l00021"></a>00021 <a name="l00022"></a>00022 <span class="preprocessor">#include <sqlite3.h></span> <a name="l00023"></a>00023 <span class="preprocessor">#include "sqlite3x.hpp"</span> <a name="l00024"></a>00024 <a name="l00025"></a>00025 <span class="keyword">namespace </span>sqlite3x { <a name="l00026"></a>00026 <a name="l00027"></a><a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#3761e4b35a2fe422bdb46d013b6e2a93">00027</a> <a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#3761e4b35a2fe422bdb46d013b6e2a93" title="Creates an empty cursor object, suitable only for use as the target of a copy/assignment...">sqlite3_cursor::sqlite3_cursor</a>() : cmd(NULL) {} <a name="l00028"></a>00028 <a name="l00029"></a><a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#e050295d1038d6cbcecf0915cb53b059">00029</a> <a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#3761e4b35a2fe422bdb46d013b6e2a93" title="Creates an empty cursor object, suitable only for use as the target of a copy/assignment...">sqlite3_cursor::sqlite3_cursor</a>(<span class="keyword">const</span> <a class="code" href="classsqlite3x_1_1sqlite3__cursor.html" title="A type for reading results from an sqlite3_command.">sqlite3_cursor</a> &copy) : cmd(copy.cmd) { <a name="l00030"></a>00030 <span class="keywordflow">if</span>(this->cmd) ++this->cmd->refs; <a name="l00031"></a>00031 } <a name="l00032"></a>00032 <a name="l00033"></a><a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#a48d3d71142b95cd9abe9717cdcbb1b7">00033</a> <a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#3761e4b35a2fe422bdb46d013b6e2a93" title="Creates an empty cursor object, suitable only for use as the target of a copy/assignment...">sqlite3_cursor::sqlite3_cursor</a>(<a class="code" href="classsqlite3x_1_1sqlite3__command.html" title="Encapsulates a command to send to an sqlite3_connection.">sqlite3_command</a> & cmd) : cmd(&cmd) { <a name="l00034"></a>00034 ++this->cmd->refs; <a name="l00035"></a>00035 } <a name="l00036"></a>00036 <a name="l00037"></a><a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#6f68791cf8fc8a4ee399619494d3251b">00037</a> <a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#6f68791cf8fc8a4ee399619494d3251b" title="Closes this cursor, freeing up db resources if this is the last cursor of a copied...">sqlite3_cursor::~sqlite3_cursor</a>() { <a name="l00038"></a>00038 this-><a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#4fa5692ec13fd4197e71e6c7f495dd4a" title="Closes this cursor.">close</a>(); <a name="l00039"></a>00039 } <a name="l00040"></a>00040 <a name="l00041"></a><a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#c2a3544f41845997e266edc71875f134">00041</a> <a class="code" href="classsqlite3x_1_1sqlite3__cursor.html" title="A type for reading results from an sqlite3_command.">sqlite3_cursor</a>& <a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#c2a3544f41845997e266edc71875f134" title="Copies the given cursor object.">sqlite3_cursor::operator=</a>(<span class="keyword">const</span> <a class="code" href="classsqlite3x_1_1sqlite3__cursor.html" title="A type for reading results from an sqlite3_command.">sqlite3_cursor</a> &copy) { <a name="l00042"></a>00042 this-><a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#4fa5692ec13fd4197e71e6c7f495dd4a" title="Closes this cursor.">close</a>(); <a name="l00043"></a>00043 <a name="l00044"></a>00044 this->cmd=copy.cmd; <a name="l00045"></a>00045 <span class="keywordflow">if</span>(this->cmd) ++this->cmd->refs; <a name="l00046"></a>00046 <a name="l00047"></a>00047 <span class="keywordflow">return</span> *<span class="keyword">this</span>; <a name="l00048"></a>00048 } <a name="l00049"></a>00049 <a name="l00050"></a><a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#98a12f884ca428595e692603ab863d9f">00050</a> <span class="keywordtype">int</span> <a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#98a12f884ca428595e692603ab863d9f" title="Returns the column count of the result set or throws on error.">sqlite3_cursor::colcount</a>() <a name="l00051"></a>00051 { <a name="l00052"></a>00052 <span class="keywordflow">if</span>( ! this->cmd ) <a name="l00053"></a>00053 { <a name="l00054"></a>00054 <span class="keywordflow">throw</span> <a class="code" href="classsqlite3x_1_1database__error.html" title="Exception type used by the sqlite3x classes.">database_error</a>(<span class="stringliteral">"sqlite3_cursor::colcount(): reader is closed"</span>); <a name="l00055"></a>00055 } <a name="l00056"></a>00056 <span class="keywordflow">return</span> this->cmd-><a class="code" href="classsqlite3x_1_1sqlite3__command.html#a9540e9132b3927cd44abb0de53f398e" title="Returns the column count of this object&#39;s query, or throws on error.">colcount</a>(); <a name="l00057"></a>00057 } <a name="l00058"></a>00058 <a name="l00059"></a><a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#b2c806b7049f1715b8e6f4106aa6af4b">00059</a> <span class="keywordtype">bool</span> <a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#b2c806b7049f1715b8e6f4106aa6af4b" title="Steps one step through the sql result set and returns true on SQLITE_ROW, false on...">sqlite3_cursor::step</a>() { <a name="l00060"></a>00060 <span class="keywordflow">if</span>(!this->cmd) <span class="keywordflow">throw</span> <a class="code" href="classsqlite3x_1_1database__error.html" title="Exception type used by the sqlite3x classes.">database_error</a>(<span class="stringliteral">"sqlite3_cursor::step(): reader is closed"</span>); <a name="l00061"></a>00061 <a name="l00062"></a>00062 <span class="keywordflow">switch</span>(sqlite3_step(this->cmd->stmt)) { <a name="l00063"></a>00063 <span class="keywordflow">case</span> SQLITE_ROW: <a name="l00064"></a>00064 <span class="keywordflow">return</span> <span class="keyword">true</span>; <a name="l00065"></a>00065 <span class="keywordflow">case</span> SQLITE_DONE: <a name="l00066"></a>00066 <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00067"></a>00067 <span class="keywordflow">default</span>: <a name="l00068"></a>00068 <span class="keywordflow">throw</span> <a class="code" href="classsqlite3x_1_1database__error.html" title="Exception type used by the sqlite3x classes.">database_error</a>(this->cmd->con); <a name="l00069"></a>00069 } <a name="l00070"></a>00070 } <a name="l00071"></a>00071 <a name="l00072"></a><a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#c456cea61aa21a64ab9b617ed58eae1f">00072</a> <span class="keywordtype">void</span> <a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#c456cea61aa21a64ab9b617ed58eae1f" title="Resets the underlying prepared statement of this cursor.">sqlite3_cursor::reset</a>() { <a name="l00073"></a>00073 <span class="keywordflow">if</span>(!this->cmd) <span class="keywordflow">throw</span> <a class="code" href="classsqlite3x_1_1database__error.html" title="Exception type used by the sqlite3x classes.">database_error</a>(<span class="stringliteral">"sqlite3_cursor::reset(): reader is closed"</span>); <a name="l00074"></a>00074 <a name="l00075"></a>00075 <span class="keywordflow">if</span>(! this->cmd-><a class="code" href="classsqlite3x_1_1sqlite3__command.html#1a2076c8b07432561a30e266569454ae" title="Resets this statement using sqlite3_reset().">reset</a>() ) <a name="l00076"></a>00076 { <a name="l00077"></a>00077 <span class="keywordflow">throw</span> <a class="code" href="classsqlite3x_1_1database__error.html" title="Exception type used by the sqlite3x classes.">database_error</a>(<span class="stringliteral">"sqlite3_cursor::reset() db error: %s"</span>, this->cmd->con.<a class="code" href="classsqlite3x_1_1sqlite3__connection.html#ac8cae93da50969fa503687566627308" title="Returns the equivalent of sqlite3_errmsg(), or an empty string if that function returns...">errormsg</a>().c_str() ); <a name="l00078"></a>00078 } <a name="l00079"></a>00079 } <a name="l00080"></a>00080 <a name="l00081"></a><a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#4fa5692ec13fd4197e71e6c7f495dd4a">00081</a> <span class="keywordtype">void</span> <a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#4fa5692ec13fd4197e71e6c7f495dd4a" title="Closes this cursor.">sqlite3_cursor::close</a>() { <a name="l00082"></a>00082 <span class="keywordflow">if</span>(this->cmd) { <a name="l00083"></a>00083 <span class="keywordflow">if</span>(--this->cmd->refs==0) { sqlite3_reset(this->cmd->stmt); } <a name="l00084"></a>00084 this->cmd=NULL; <a name="l00085"></a>00085 } <a name="l00086"></a>00086 } <a name="l00087"></a>00087 <a name="l00088"></a>00088 <span class="preprocessor">#define READER_CHECK(FUNC) \</span> <a name="l00089"></a>00089 <span class="preprocessor"> if( ! this->cmd ) throw database_error( "sqlite3_cursor::%s(%d): reader is closed", # FUNC, index ); \</span> <a name="l00090"></a>00090 <span class="preprocessor"> if( (index)>(this->cmd->argc-1)) throw database_error("sqlite3_cursor::%s(%d): index out of range", # FUNC, index );</span> <a name="l00091"></a>00091 <span class="preprocessor"></span> <a name="l00092"></a><a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#505869cfa78a1b5aeb516bbe99d2400e">00092</a> <span class="keywordtype">bool</span> <a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#505869cfa78a1b5aeb516bbe99d2400e" title="Check if the given field number is NULL.">sqlite3_cursor::isnull</a>(<span class="keywordtype">int</span> index) { <a name="l00093"></a>00093 READER_CHECK(<a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#505869cfa78a1b5aeb516bbe99d2400e" title="Check if the given field number is NULL.">isnull</a>); <a name="l00094"></a>00094 <span class="keywordflow">return</span> sqlite3_column_type(this->cmd->stmt, index) == SQLITE_NULL; <a name="l00095"></a>00095 } <a name="l00096"></a>00096 <a name="l00097"></a><a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#0c8263dc2db4054e38f4479a70a8dec8">00097</a> <span class="keywordtype">int</span> <a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#0c8263dc2db4054e38f4479a70a8dec8" title="Gets the integer value at the given field number.">sqlite3_cursor::getint</a>(<span class="keywordtype">int</span> index) { <a name="l00098"></a>00098 READER_CHECK(<a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#0c8263dc2db4054e38f4479a70a8dec8" title="Gets the integer value at the given field number.">getint</a>); <a name="l00099"></a>00099 <span class="keywordflow">return</span> sqlite3_column_int(this->cmd->stmt, index); <a name="l00100"></a>00100 } <a name="l00101"></a>00101 <a name="l00102"></a><a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#1f78949a7d2dd96b8677964368b2dc71">00102</a> <a class="code" href="namespacesqlite3x.html#006a0e90d7b40685893f6f6f766d9b48" title="64-bit integer type used by this code.">int64_t</a> <a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#1f78949a7d2dd96b8677964368b2dc71" title="Gets the (int64_t) value at the given field number.">sqlite3_cursor::getint64</a>(<span class="keywordtype">int</span> index) { <a name="l00103"></a>00103 READER_CHECK(<a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#1f78949a7d2dd96b8677964368b2dc71" title="Gets the (int64_t) value at the given field number.">getint64</a>); <a name="l00104"></a>00104 <span class="keywordflow">return</span> sqlite3_column_int64(this->cmd->stmt, index); <a name="l00105"></a>00105 } <a name="l00106"></a>00106 <a name="l00107"></a><a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#c6a6487bb07c7a7a864e9c971f4c773f">00107</a> <span class="keywordtype">double</span> <a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#c6a6487bb07c7a7a864e9c971f4c773f" title="Gets the double value at the given field number.">sqlite3_cursor::getdouble</a>(<span class="keywordtype">int</span> index) { <a name="l00108"></a>00108 READER_CHECK(<a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#c6a6487bb07c7a7a864e9c971f4c773f" title="Gets the double value at the given field number.">getdouble</a>); <a name="l00109"></a>00109 <span class="keywordflow">return</span> sqlite3_column_double(this->cmd->stmt, index); <a name="l00110"></a>00110 } <a name="l00111"></a>00111 <a name="l00112"></a><a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#2d323375d5cf1a0c3efc915179558db3">00112</a> std::string <a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#2d323375d5cf1a0c3efc915179558db3" title="Gets the string value at the given field number.">sqlite3_cursor::getstring</a>(<span class="keywordtype">int</span> index) { <a name="l00113"></a>00113 READER_CHECK(<span class="keywordtype">string</span>); <a name="l00114"></a>00114 <span class="keywordflow">return</span> std::string((<span class="keyword">const</span> <span class="keywordtype">char</span>*)sqlite3_column_text(this->cmd->stmt, index), sqlite3_column_bytes(this->cmd->stmt, index)); <a name="l00115"></a>00115 } <a name="l00116"></a>00116 <a name="l00117"></a><a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#c469fe6903f1d1af7623bba6b9cd2570">00117</a> <span class="keywordtype">char</span> <span class="keyword">const</span> * <a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#2d323375d5cf1a0c3efc915179558db3" title="Gets the string value at the given field number.">sqlite3_cursor::getstring</a>(<span class="keywordtype">int</span> index, <span class="keywordtype">int</span> & size) { <a name="l00118"></a>00118 READER_CHECK(<span class="keywordtype">string</span>); <a name="l00119"></a>00119 size = sqlite3_column_bytes(this->cmd->stmt, index); <a name="l00120"></a>00120 <span class="keywordflow">return</span> (<span class="keywordtype">char</span> <span class="keyword">const</span> *)sqlite3_column_text(this->cmd->stmt, index); <a name="l00121"></a>00121 } <a name="l00122"></a>00122 <a name="l00123"></a>00123 <span class="preprocessor">#if SQLITE3X_USE_WCHAR</span> <a name="l00124"></a>00124 <span class="preprocessor"></span> std::wstring sqlite3_cursor::getstring16(<span class="keywordtype">int</span> index) { <a name="l00125"></a>00125 READER_CHECK(wstring); <a name="l00126"></a>00126 <span class="keywordflow">return</span> std::wstring((<span class="keyword">const</span> <span class="keywordtype">wchar_t</span>*)sqlite3_column_text16(this->cmd->stmt, index), sqlite3_column_bytes16(this->cmd->stmt, index)/2); <a name="l00127"></a>00127 } <a name="l00128"></a>00128 <span class="preprocessor">#endif</span> <a name="l00129"></a>00129 <span class="preprocessor"></span> <a name="l00130"></a><a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#c645e030d1bbdfa13e88af4a61ed810e">00130</a> std::string <a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#c645e030d1bbdfa13e88af4a61ed810e" title="Gets the blob value at the given field number.">sqlite3_cursor::getblob</a>(<span class="keywordtype">int</span> index) { <a name="l00131"></a>00131 READER_CHECK(<span class="keywordtype">string</span>); <a name="l00132"></a>00132 <span class="keywordflow">return</span> std::string((<span class="keyword">const</span> <span class="keywordtype">char</span>*)sqlite3_column_blob(this->cmd->stmt, index), sqlite3_column_bytes(this->cmd->stmt, index)); <a name="l00133"></a>00133 } <a name="l00134"></a>00134 <a name="l00135"></a><a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#fbd6d623c832623904e529571732ca10">00135</a> <span class="keywordtype">void</span> <span class="keyword">const</span> * <a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#c645e030d1bbdfa13e88af4a61ed810e" title="Gets the blob value at the given field number.">sqlite3_cursor::getblob</a>(<span class="keywordtype">int</span> index, <span class="keywordtype">int</span> & size ) { <a name="l00136"></a>00136 READER_CHECK(<span class="keywordtype">string</span>); <a name="l00137"></a>00137 size = sqlite3_column_bytes(this->cmd->stmt, index); <a name="l00138"></a>00138 <span class="keywordflow">return</span> sqlite3_column_blob(this->cmd->stmt, index); <a name="l00139"></a>00139 } <a name="l00140"></a>00140 <a name="l00141"></a><a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#adfe96368032b5cdb54112eb93f6e3c5">00141</a> std::string <a class="code" href="classsqlite3x_1_1sqlite3__cursor.html#adfe96368032b5cdb54112eb93f6e3c5" title="Gets the column name for the given column index.">sqlite3_cursor::getcolname</a>(<span class="keywordtype">int</span> index) { <a name="l00142"></a>00142 READER_CHECK(<span class="keywordtype">string</span>); <a name="l00143"></a>00143 <span class="keywordtype">char</span> <span class="keyword">const</span> * cn = sqlite3_column_name(this->cmd->stmt, index); <a name="l00144"></a>00144 <span class="keywordflow">return</span> cn ? cn : <span class="stringliteral">""</span>; <a name="l00145"></a>00145 } <a name="l00146"></a>00146 <a name="l00147"></a>00147 <span class="comment">// char const * sqlite3_cursor::getcolname(int index) {</span> <a name="l00148"></a>00148 <span class="comment">// READER_CHECK(string);</span> <a name="l00149"></a>00149 <span class="comment">// char const * cn = sqlite3_column_name(this->cmd->stmt, index);</span> <a name="l00150"></a>00150 <span class="comment">// return cn ? cn : "";</span> <a name="l00151"></a>00151 <span class="comment">// }</span> <a name="l00152"></a>00152 <a name="l00153"></a>00153 <span class="preprocessor">#if SQLITE3X_USE_WCHAR</span> <a name="l00154"></a>00154 <span class="preprocessor"></span> std::wstring sqlite3_cursor::getcolname16(<span class="keywordtype">int</span> index) { <a name="l00155"></a>00155 READER_CHECK(wstring); <a name="l00156"></a>00156 <span class="keywordflow">return</span> (<span class="keyword">const</span> <span class="keywordtype">wchar_t</span>*)sqlite3_column_name16(this->cmd->stmt, index); <a name="l00157"></a>00157 } <a name="l00158"></a>00158 <span class="preprocessor">#endif</span> <a name="l00159"></a>00159 <span class="preprocessor"></span> <a name="l00160"></a>00160 <span class="preprocessor">#undef READER_CHECK</span> <a name="l00161"></a>00161 <span class="preprocessor"></span>} </pre></div></div> <hr size="1"><address style="text-align: right;"><small>Generated on Sun Jul 26 18:20:37 2009 for libsqlite3x by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.9 </small></address> </body> </html>