Added an easy wrapper Column Database::execAndGet("query"), version number increased to 0.3.0

This commit is contained in:
Sébastien Rombauts 2012-04-16 07:19:54 +02:00
parent d516ea72c5
commit fb1b17bfff
7 changed files with 74 additions and 17 deletions

View File

@ -467,7 +467,11 @@
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
<storageModule moduleId="org.eclipse.cdt.core.language.mapping"/>
<storageModule moduleId="org.eclipse.cdt.internal.ui.text.commentOwnerProjectMappings"/>
<storageModule moduleId="org.eclipse.cdt.internal.ui.text.commentOwnerProjectMappings">
<doc-comment-owner id="org.eclipse.cdt.ui.doxygen">
<path value=""/>
</doc-comment-owner>
</storageModule>
</cconfiguration>
<cconfiguration id="cdt.managedbuild.config.gnu.exe.release.326780594">
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="cdt.managedbuild.config.gnu.exe.release.326780594" moduleId="org.eclipse.cdt.core.settings" name="Release">
@ -929,7 +933,11 @@
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.language.mapping"/>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
<storageModule moduleId="org.eclipse.cdt.internal.ui.text.commentOwnerProjectMappings"/>
<storageModule moduleId="org.eclipse.cdt.internal.ui.text.commentOwnerProjectMappings">
<doc-comment-owner id="org.eclipse.cdt.ui.doxygen">
<path value=""/>
</doc-comment-owner>
</storageModule>
</cconfiguration>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">

1
.gitignore vendored
View File

@ -5,3 +5,4 @@ Release
*.suo
*.user
*~
core

View File

@ -1,9 +1,8 @@
Add a comparison of others C++ wrappers (code style, C++ design, in code documentation, tests, online documentation, examples, license, UTF-16)
Missing features in v0.2.0:
- Statement::execStepAndGetScalar() easy wrapper
- Database::execScalar() easy wrapper like CppSqlite
Missing features in v0.3.0:
- Database::tableExists
- Blob
- getColumnByName ? std::map getRow() ?
Advanced missing features:

View File

@ -10,7 +10,6 @@
#include "Database.h"
#include "Statement.h"
#include "Column.h"
namespace SQLite
{
@ -50,6 +49,18 @@ int Database::exec(const char* apQueries) // throw(SQLite::Exception);
return sqlite3_changes(mpSQLite);
}
// Shortcut to execute a one step query and fetch the first column of the result.
// WARNING: Be very careful with this dangerous method: you have to
// make a COPY OF THE result, else it will be destroy before the next line
// (when the underlying temporary Statement and Column objects are destroyed)
Column Database::execAndGet(const char* apQuery) // throw(SQLite::Exception)
{
Statement query(*this, apQuery);
query.executeStep();
return query.getColumn(0);
}
// Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message
void Database::check(const int aRet) const // throw(SQLite::Exception)
{

View File

@ -11,14 +11,11 @@
#include <sqlite3.h>
#include "Exception.h"
#include "Column.h"
namespace SQLite
{
// Forward declarations
class Statement;
class Exception;
/**
* @brief RAII management of a SQLite Database Connection.
*
@ -68,6 +65,23 @@ public:
*/
int exec(const char* apQueries); // throw(SQLite::Exception);
/**
* @brief Shortcut to execute a one step query and fetch the first column of the result.
*
* This is a shortcut to execute a simple statement with a single result.
* This should be used only for non reusable queries (else you should use a Statement with bind()).
* This should be used only for queries with expected results (else an exception is fired).
*
* @warning WARNING: Be very careful with this dangerous method: you have to
* make a COPY OF THE result, else it will be destroy before the next line
* (when the underlying temporary Statement and Column objects are destroyed)
*
* @see also Statement class for handling queries with multiple results
*
* @param[in] apQuery a UTF-8 encoded SQL query
*/
Column execAndGet(const char* apQuery); // throw(SQLite::Exception);
/**
* @brief Set a busy handler that sleeps for a specified amount of time when a table is locked.
*

View File

@ -32,5 +32,5 @@
* with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
* numbers used in [SQLITECPP_VERSION].
*/
#define SQLITECPP_VERSION "0.2.0"
#define SQLITECPP_VERSION_NUMBER 0002000
#define SQLITECPP_VERSION "0.3.0"
#define SQLITECPP_VERSION_NUMBER 0003000

View File

@ -58,13 +58,19 @@ private:
int main (void)
{
// Basic example (1/4) :
// Basic example (1/5) :
try
{
// Open a database file
SQLite::Database db("example.db3");
std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n";
// TODO SRombauts:
//SQLite::Statement::Column col = db.execAndGet("SELECT value FROM test WHERE id=2");
//const char* pvalue = col;
std::string value = db.execAndGet("SELECT value FROM test WHERE id=2");
std::cout << "execAndGet=" << value << std::endl;
// Compile a SQL query, containing one parameter (index 1)
SQLite::Statement query(db, "SELECT * FROM test WHERE size > ?");
std::cout << "SQLite statement '" << query.getQuery().c_str() << "' compiled (" << query.getColumnCount () << " columns in the result)\n";
@ -101,7 +107,7 @@ int main (void)
}
////////////////////////////////////////////////////////////////////////////
// Object Oriented Basic example (2/4) :
// Object Oriented Basic example (2/5) :
try
{
// Open the database and compile the query
@ -117,8 +123,26 @@ int main (void)
std::cout << "SQLite exception: " << e.what() << std::endl;
}
// The execAndGet wrapper example (3/5) :
try
{
// Open a database file
SQLite::Database db("example.db3");
std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n";
// WARNING: Be very careful with this dangerous method: you have to
// make a COPY OF THE result, else it will be destroy before the next line
// (when the underlying temporary Statement and Column objects are destroyed)
std::string value = db.execAndGet("SELECT value FROM test WHERE id=2");
std::cout << "execAndGet=" << value << std::endl;
}
catch (std::exception& e)
{
std::cout << "SQLite exception: " << e.what() << std::endl;
}
////////////////////////////////////////////////////////////////////////////
// Simple batch queries example (3/4) :
// Simple batch queries example (4/5) :
try
{
// Open a database file
@ -141,7 +165,7 @@ int main (void)
remove("test.db3");
////////////////////////////////////////////////////////////////////////////
// RAII transaction example (4/4) :
// RAII transaction example (5/5) :
try
{
// Open a database file