Adding a Column::getName() function

- thanks to a patch provided by Nellis Willers,
- requires the SQLITE_ENABLE_COLUMN_METADATA preprocessor macro to be also defined at compile times of the SQLite library
- v0.5.1
This commit is contained in:
Sébastien Rombauts 2013-04-07 18:24:44 +02:00
parent 1f55ddbbdb
commit 7669bcbf90
5 changed files with 136975 additions and 136942 deletions

View File

@ -1,339 +1,338 @@
/** /**
* @file main.cpp * @file main.cpp
* @brief A few short examples in a row. * @brief A few short examples in a row.
* *
* Demonstrate how-to use the SQLite++ wrapper * Demonstrate how-to use the SQLite++ wrapper
* *
* Copyright (c) 2012-2013 Sebastien Rombauts (sebastien.rombauts@gmail.com) * Copyright (c) 2012-2013 Sebastien Rombauts (sebastien.rombauts@gmail.com)
* *
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
* or copy at http://opensource.org/licenses/MIT) * or copy at http://opensource.org/licenses/MIT)
*/ */
#include <iostream> #include <iostream>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "../../src/Database.h" #include "../../src/SQLiteC++.h"
#include "../../src/Statement.h"
#include "../../src/Column.h"
#include "../../src/Transaction.h" static const char* filename_example_db3 = "examples/example1/example.db3";
static const char* filename_logo_png = "examples/example1/logo.png";
static const char* filename_example_db3 = "examples/example1/example.db3";
static const char* filename_logo_png = "examples/example1/logo.png"; /// Object Oriented Basic example
class Example
{
/// Object Oriented Basic example public:
class Example // Constructor
{ Example(void) :
public: mDb(filename_example_db3), // Open a database file in readonly mode
// Constructor mQuery(mDb, "SELECT * FROM test WHERE weight > :min_weight")// Compile a SQL query, containing one parameter (index 1)
Example(void) : {
mDb(filename_example_db3), // Open a database file in readonly mode }
mQuery(mDb, "SELECT * FROM test WHERE weight > :min_weight")// Compile a SQL query, containing one parameter (index 1) virtual ~Example(void)
{ {
} }
virtual ~Example(void)
{ /// List the rows where the "weight" column is greater than the provided aParamValue
} void ListGreaterThan (const int aParamValue)
{
/// List the rows where the "weight" column is greater than the provided aParamValue std::cout << "ListGreaterThan (" << aParamValue << ")\n";
void ListGreaterThan (const int aParamValue)
{ // Bind the integer value provided to the first parameter of the SQL query
std::cout << "ListGreaterThan (" << aParamValue << ")\n"; mQuery.bind(":min_weight", aParamValue); // same as mQuery.bind(1, aParamValue);
// Bind the integer value provided to the first parameter of the SQL query // Loop to execute the query step by step, to get one a row of results at a time
mQuery.bind(":min_weight", aParamValue); // same as mQuery.bind(1, aParamValue); while (mQuery.executeStep())
{
// Loop to execute the query step by step, to get one a row of results at a time std::cout << "row : (" << mQuery.getColumn(0) << ", " << mQuery.getColumn(1) << ", " << mQuery.getColumn(2) << ")\n";
while (mQuery.executeStep()) }
{
std::cout << "row : (" << mQuery.getColumn(0) << ", " << mQuery.getColumn(1) << ", " << mQuery.getColumn(2) << ")\n"; // Reset the query to be able to use it again later
} mQuery.reset();
}
// Reset the query to be able to use it again later
mQuery.reset(); private:
} SQLite::Database mDb; ///< Database connection
SQLite::Statement mQuery; ///< Database prepared SQL query
private: };
SQLite::Database mDb; ///< Database connection
SQLite::Statement mQuery; ///< Database prepared SQL query
}; int main (void)
{
// Basic example (1/6) :
int main (void) try
{ {
// Basic example (1/6) : // Open a database file in readonly mode
try SQLite::Database db(filename_example_db3); // SQLITE_OPEN_READONLY
{ std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n";
// Open a database file in readonly mode
SQLite::Database db(filename_example_db3); // SQLITE_OPEN_READONLY // Test if the 'test' table exists
std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n"; bool bExists = db.tableExists("test");
std::cout << "SQLite table 'test' exists=" << bExists << "\n";
// Test if the 'test' table exists
bool bExists = db.tableExists("test"); // Get a single value result with an easy to use shortcut
std::cout << "SQLite table 'test' exists=" << bExists << "\n"; std::string value = db.execAndGet("SELECT value FROM test WHERE id=2");
std::cout << "execAndGet=" << value.c_str() << std::endl;
// Get a single value result with an easy to use shortcut
std::string value = db.execAndGet("SELECT value FROM test WHERE id=2"); // Compile a SQL query, containing one parameter (index 1)
std::cout << "execAndGet=" << value.c_str() << std::endl; SQLite::Statement query(db, "SELECT * FROM test WHERE weight > ?");
std::cout << "SQLite statement '" << query.getQuery().c_str() << "' compiled (" << query.getColumnCount () << " columns in the result)\n";
// Compile a SQL query, containing one parameter (index 1) // Bind the integer value 2 to the first parameter of the SQL query
SQLite::Statement query(db, "SELECT * FROM test WHERE weight > ?"); query.bind(1, 2);
std::cout << "SQLite statement '" << query.getQuery().c_str() << "' compiled (" << query.getColumnCount () << " columns in the result)\n"; std::cout << "binded with integer value 2 :\n";
// Bind the integer value 2 to the first parameter of the SQL query
query.bind(1, 2); // Loop to execute the query step by step, to get one a row of results at a time
std::cout << "binded with integer value 2 :\n"; while (query.executeStep())
{
// Loop to execute the query step by step, to get one a row of results at a time // Demonstrate how to get some typed column value (and the equivalent explicit call)
while (query.executeStep()) int id = query.getColumn(0); // = query.getColumn(0).getInt()
{ //const char* pvalue = query.getColumn(1); // = query.getColumn(1).getText()
// Demonstrate how to get some typed column value (and the equivalent explicit call) std::string value2 = query.getColumn(1); // = query.getColumn(1).getText()
int id = query.getColumn(0); // = query.getColumn(0).getInt() int bytes = query.getColumn(1).getBytes();
//const char* pvalue = query.getColumn(1); // = query.getColumn(1).getText() double weight = query.getColumn(2); // = query.getColumn(2).getInt()
std::string value2 = query.getColumn(1); // = query.getColumn(1).getText()
int bytes = query.getColumn(1).getBytes(); std::string name(query.getColumn(0).getName());
double weight = query.getColumn(2); // = query.getColumn(2).getInt()
std::cout << "row : (" << id << " [" << name.c_str() << "], \"" << value2.c_str() << "\" " << bytes << "B, " << weight << ")\n";
std::cout << "row : (" << id << ", \"" << value2.c_str() << "\" " << bytes << "B, " << weight << ")\n"; }
}
// Reset the query to use it again
// Reset the query to use it again query.reset();
query.reset(); std::cout << "SQLite statement '" << query.getQuery().c_str() << "' reseted (" << query.getColumnCount () << " columns in the result)\n";
std::cout << "SQLite statement '" << query.getQuery().c_str() << "' reseted (" << query.getColumnCount () << " columns in the result)\n"; // Bind the string value "6" to the first parameter of the SQL query
// Bind the string value "6" to the first parameter of the SQL query query.bind(1, "6");
query.bind(1, "6"); std::cout << "binded with string value \"6\" :\n";
std::cout << "binded with string value \"6\" :\n";
while (query.executeStep())
while (query.executeStep()) {
{ // Demonstrate that inserting column value in a std:ostream is natural
// Demonstrate that inserting column value in a std:ostream is natural std::cout << "row : (" << query.getColumn(0) << ", " << query.getColumn(1) << ", " << query.getColumn(2) << ")\n";
std::cout << "row : (" << query.getColumn(0) << ", " << query.getColumn(1) << ", " << query.getColumn(2) << ")\n"; }
} }
} catch (std::exception& e)
catch (std::exception& e) {
{ std::cout << "SQLite exception: " << e.what() << std::endl;
std::cout << "SQLite exception: " << e.what() << std::endl; abort(); // unexpected error : abort the example program
abort(); // unexpected error : abort the example program }
}
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////// // Object Oriented Basic example (2/6) :
// Object Oriented Basic example (2/6) : try
try {
{ // Open the database and compile the query
// Open the database and compile the query Example example;
Example example;
// Demonstrate the way to use the same query with different parameter values
// Demonstrate the way to use the same query with different parameter values example.ListGreaterThan(8);
example.ListGreaterThan(8); example.ListGreaterThan(6);
example.ListGreaterThan(6); example.ListGreaterThan(2);
example.ListGreaterThan(2); }
} catch (std::exception& e)
catch (std::exception& e) {
{ std::cout << "SQLite exception: " << e.what() << std::endl;
std::cout << "SQLite exception: " << e.what() << std::endl; abort(); // unexpected error : abort the example program
abort(); // unexpected error : abort the example program }
}
// The execAndGet wrapper example (3/6) :
// The execAndGet wrapper example (3/6) : try
try {
{ // Open a database file in readonly mode
// Open a database file in readonly mode SQLite::Database db(filename_example_db3); // SQLITE_OPEN_READONLY
SQLite::Database db(filename_example_db3); // SQLITE_OPEN_READONLY std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n";
std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n";
// WARNING: Be very careful with this dangerous method: you have to
// 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
// 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)
// (when the underlying temporary Statement and Column objects are destroyed) std::string value = db.execAndGet("SELECT value FROM test WHERE id=2");
std::string value = db.execAndGet("SELECT value FROM test WHERE id=2"); std::cout << "execAndGet=" << value.c_str() << std::endl;
std::cout << "execAndGet=" << value.c_str() << std::endl; }
} catch (std::exception& e)
catch (std::exception& e) {
{ std::cout << "SQLite exception: " << e.what() << std::endl;
std::cout << "SQLite exception: " << e.what() << std::endl; abort(); // unexpected error : abort the example program
abort(); // unexpected error : abort the example program }
}
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////// // Simple batch queries example (4/6) :
// Simple batch queries example (4/6) : try
try {
{ // Open a database file in create/write mode
// Open a database file in create/write mode SQLite::Database db("test.db3", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
SQLite::Database db("test.db3", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n";
std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n";
db.exec("DROP TABLE IF EXISTS test");
db.exec("DROP TABLE IF EXISTS test");
db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)");
db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)");
// first row
// first row int nb = db.exec("INSERT INTO test VALUES (NULL, \"test\")");
int nb = db.exec("INSERT INTO test VALUES (NULL, \"test\")"); std::cout << "INSERT INTO test VALUES (NULL, \"test\")\", returned " << nb << std::endl;
std::cout << "INSERT INTO test VALUES (NULL, \"test\")\", returned " << nb << std::endl;
// second row
// second row nb = db.exec("INSERT INTO test VALUES (NULL, \"second\")");
nb = db.exec("INSERT INTO test VALUES (NULL, \"second\")"); std::cout << "INSERT INTO test VALUES (NULL, \"second\")\", returned " << nb << std::endl;
std::cout << "INSERT INTO test VALUES (NULL, \"second\")\", returned " << nb << std::endl;
// update the second row
// update the second row nb = db.exec("UPDATE test SET value=\"second-updated\" WHERE id='2'");
nb = db.exec("UPDATE test SET value=\"second-updated\" WHERE id='2'"); std::cout << "UPDATE test SET value=\"second-updated\" WHERE id='2', returned " << nb << std::endl;
std::cout << "UPDATE test SET value=\"second-updated\" WHERE id='2', returned " << nb << std::endl;
// Check the results : expect two row of result
// Check the results : expect two row of result SQLite::Statement query(db, "SELECT * FROM test");
SQLite::Statement query(db, "SELECT * FROM test"); std::cout << "SELECT * FROM test :\n";
std::cout << "SELECT * FROM test :\n"; while (query.executeStep())
while (query.executeStep()) {
{ std::cout << "row : (" << query.getColumn(0) << ", " << query.getColumn(1) << ")\n";
std::cout << "row : (" << query.getColumn(0) << ", " << query.getColumn(1) << ")\n"; }
}
db.exec("DROP TABLE test");
db.exec("DROP TABLE test"); }
} catch (std::exception& e)
catch (std::exception& e) {
{ std::cout << "SQLite exception: " << e.what() << std::endl;
std::cout << "SQLite exception: " << e.what() << std::endl; abort(); // unexpected error : abort the example program
abort(); // unexpected error : abort the example program }
} remove("test.db3");
remove("test.db3");
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////// // RAII transaction example (5/6) :
// RAII transaction example (5/6) : try
try {
{ // Open a database file in create/write mode
// Open a database file in create/write mode SQLite::Database db("transaction.db3", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
SQLite::Database db("transaction.db3", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n";
std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n";
db.exec("DROP TABLE IF EXISTS test");
db.exec("DROP TABLE IF EXISTS test");
// Exemple of a successful transaction :
// Exemple of a successful transaction : try
try {
{ // Begin transaction
// Begin transaction SQLite::Transaction transaction(db);
SQLite::Transaction transaction(db);
db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)");
db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)");
int nb = db.exec("INSERT INTO test VALUES (NULL, \"test\")");
int nb = db.exec("INSERT INTO test VALUES (NULL, \"test\")"); std::cout << "INSERT INTO test VALUES (NULL, \"test\")\", returned " << nb << std::endl;
std::cout << "INSERT INTO test VALUES (NULL, \"test\")\", returned " << nb << std::endl;
// Commit transaction
// Commit transaction transaction.commit();
transaction.commit(); }
} catch (std::exception& e)
catch (std::exception& e) {
{ std::cout << "SQLite exception: " << e.what() << std::endl;
std::cout << "SQLite exception: " << e.what() << std::endl; abort(); // unexpected error : abort the example program
abort(); // unexpected error : abort the example program }
}
// Exemple of a rollbacked transaction :
// Exemple of a rollbacked transaction : try
try {
{ // Begin transaction
// Begin transaction SQLite::Transaction transaction(db);
SQLite::Transaction transaction(db);
int nb = db.exec("INSERT INTO test VALUES (NULL, \"second\")");
int nb = db.exec("INSERT INTO test VALUES (NULL, \"second\")"); std::cout << "INSERT INTO test VALUES (NULL, \"second\")\", returned " << nb << std::endl;
std::cout << "INSERT INTO test VALUES (NULL, \"second\")\", returned " << nb << std::endl;
nb = db.exec("INSERT INTO test ObviousError");
nb = db.exec("INSERT INTO test ObviousError"); std::cout << "INSERT INTO test \"error\", returned " << nb << std::endl;
std::cout << "INSERT INTO test \"error\", returned " << nb << std::endl;
abort(); // unexpected success : abort the example program
abort(); // unexpected success : abort the example program
// Commit transaction
// Commit transaction transaction.commit();
transaction.commit(); }
} catch (std::exception& e)
catch (std::exception& e) {
{ std::cout << "SQLite exception: " << e.what() << std::endl;
std::cout << "SQLite exception: " << e.what() << std::endl; // expected error, see above
// expected error, see above }
}
// Check the results (expect only one row of result, as the second one has been rollbacked by the error)
// Check the results (expect only one row of result, as the second one has been rollbacked by the error) SQLite::Statement query(db, "SELECT * FROM test");
SQLite::Statement query(db, "SELECT * FROM test"); std::cout << "SELECT * FROM test :\n";
std::cout << "SELECT * FROM test :\n"; while (query.executeStep())
while (query.executeStep()) {
{ std::cout << "row : (" << query.getColumn(0) << ", " << query.getColumn(1) << ")\n";
std::cout << "row : (" << query.getColumn(0) << ", " << query.getColumn(1) << ")\n"; }
} }
} catch (std::exception& e)
catch (std::exception& e) {
{ std::cout << "SQLite exception: " << e.what() << std::endl;
std::cout << "SQLite exception: " << e.what() << std::endl; abort(); // unexpected error : abort the example program
abort(); // unexpected error : abort the example program }
} remove("transaction.db3");
remove("transaction.db3");
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////// // Binary blob and in-memory database example (6/6) :
// Binary blob and in-memory database example (6/6) : try
try {
{ // Open a database file in create/write mode
// Open a database file in create/write mode SQLite::Database db(":memory:", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
SQLite::Database db(":memory:", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n";
std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n";
db.exec("DROP TABLE IF EXISTS test");
db.exec("DROP TABLE IF EXISTS test"); db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value BLOB)");
db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value BLOB)");
FILE* fp = fopen(filename_logo_png, "rb");
FILE* fp = fopen(filename_logo_png, "rb"); if (NULL != fp)
if (NULL != fp) {
{ char buffer[16*1024];
char buffer[16*1024]; void* blob = &buffer;
void* blob = &buffer; size_t size = fread(blob, 1, 16*1024, fp);
size_t size = fread(blob, 1, 16*1024, fp); buffer[size] = '\0';
buffer[size] = '\0'; fclose (fp);
fclose (fp); std::cout << "blob size=" << size << " :\n";
std::cout << "blob size=" << size << " :\n";
// Insert query
// Insert query SQLite::Statement query(db, "INSERT INTO test VALUES (NULL, ?)");
SQLite::Statement query(db, "INSERT INTO test VALUES (NULL, ?)"); // Bind the blob value to the first parameter of the SQL query
// Bind the blob value to the first parameter of the SQL query query.bind(1, blob, size);
query.bind(1, blob, size); std::cout << "blob binded successfully\n";
std::cout << "blob binded successfully\n";
// Execute the one-step query to insert the blob
// Execute the one-step query to insert the blob int nb = query.exec ();
int nb = query.exec (); std::cout << "INSERT INTO test VALUES (NULL, ?)\", returned " << nb << std::endl;
std::cout << "INSERT INTO test VALUES (NULL, ?)\", returned " << nb << std::endl; }
} else
else {
{ std::cout << "file " << filename_logo_png << " not found !\n";
std::cout << "file " << filename_logo_png << " not found !\n"; abort(); // unexpected error : abort the example program
abort(); // unexpected error : abort the example program }
}
fp = fopen("out.png", "wb");
fp = fopen("out.png", "wb"); if (NULL != fp)
if (NULL != fp) {
{ const void* blob = NULL;
const void* blob = NULL; size_t size;
size_t size;
SQLite::Statement query(db, "SELECT * FROM test");
SQLite::Statement query(db, "SELECT * FROM test"); std::cout << "SELECT * FROM test :\n";
std::cout << "SELECT * FROM test :\n"; if (query.executeStep())
if (query.executeStep()) {
{ SQLite::Column colBlob = query.getColumn(1);
SQLite::Column colBlob = query.getColumn(1); blob = colBlob.getBlob ();
blob = colBlob.getBlob (); size = colBlob.getBytes ();
size = colBlob.getBytes (); std::cout << "row : (" << query.getColumn(0) << ", size=" << size << ")\n";
std::cout << "row : (" << query.getColumn(0) << ", size=" << size << ")\n"; size_t sizew = fwrite(blob, 1, size, fp);
size_t sizew = fwrite(blob, 1, size, fp); assert(sizew == size);
assert(sizew == size); fclose (fp);
fclose (fp); }
} }
} else
else {
{ std::cout << "file out.png not created !\n";
std::cout << "file out.png not created !\n"; abort(); // unexpected error : abort the example program
abort(); // unexpected error : abort the example program }
} }
} catch (std::exception& e)
catch (std::exception& e) {
{ std::cout << "SQLite exception: " << e.what() << std::endl;
std::cout << "SQLite exception: " << e.what() << std::endl; abort(); // unexpected error : abort the example program
abort(); // unexpected error : abort the example program }
} remove("out.png");
remove("out.png");
std::cout << "everything ok, quitting\n";
std::cout << "everything ok, quitting\n";
return 0;
return 0; }
}

File diff suppressed because it is too large Load Diff

View File

@ -1,81 +1,91 @@
/** /**
* @file Column.cpp * @file Column.cpp
* @ingroup SQLiteCpp * @ingroup SQLiteCpp
* @brief Encapsulation of a Column in a row of the result pointed by the prepared SQLite::Statement. * @brief Encapsulation of a Column in a row of the result pointed by the prepared SQLite::Statement.
* *
* Copyright (c) 2012-2013 Sebastien Rombauts (sebastien.rombauts@gmail.com) * Copyright (c) 2012-2013 Sebastien Rombauts (sebastien.rombauts@gmail.com)
* *
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
* or copy at http://opensource.org/licenses/MIT) * or copy at http://opensource.org/licenses/MIT)
*/ */
#include "Column.h" #include "Column.h"
#include <iostream> #include <iostream>
namespace SQLite
{ namespace SQLite
{
// Encapsulation of a Column in a row of the result pointed by the prepared Statement.
Column::Column(Statement::Ptr& aStmtPtr, int aIndex) throw() : // nothrow
mStmtPtr (aStmtPtr), // Encapsulation of a Column in a row of the result pointed by the prepared Statement.
mIndex (aIndex) Column::Column(Statement::Ptr& aStmtPtr, int aIndex) throw() : // nothrow
{ mStmtPtr (aStmtPtr),
} mIndex (aIndex)
{
// Finalize and unregister the SQL query from the SQLite Database Connection. }
Column::~Column(void) throw() // nothrow
{ // Finalize and unregister the SQL query from the SQLite Database Connection.
// the finalization will be done by the destructor of the last shared pointer Column::~Column(void) throw() // nothrow
} {
// the finalization will be done by the destructor of the last shared pointer
// Return the integer value of the column specified by its index starting at 0 }
int Column::getInt(void) const throw() // nothrow
{ #ifdef SQLITE_ENABLE_COLUMN_METADATA
return sqlite3_column_int(mStmtPtr, mIndex); // Return the name of the column
} const char * Column::getName(void) const throw() // nothrow
{
// Return the 64bits integer value of the column specified by its index starting at 0 return sqlite3_column_origin_name(mStmtPtr, mIndex);
sqlite3_int64 Column::getInt64(void) const throw() // nothrow }
{ #endif
return sqlite3_column_int64(mStmtPtr, mIndex);
} // Return the integer value of the column specified by its index starting at 0
int Column::getInt(void) const throw() // nothrow
// Return the double value of the column specified by its index starting at 0 {
double Column::getDouble(void) const throw() // nothrow return sqlite3_column_int(mStmtPtr, mIndex);
{ }
return sqlite3_column_double(mStmtPtr, mIndex);
} // Return the 64bits integer value of the column specified by its index starting at 0
sqlite3_int64 Column::getInt64(void) const throw() // nothrow
// Return a pointer to the text value (NULL terminated string) of the column specified by its index starting at 0 {
const char* Column::getText(void) const throw() // nothrow return sqlite3_column_int64(mStmtPtr, mIndex);
{ }
return (const char*)sqlite3_column_text(mStmtPtr, mIndex);
} // Return the double value of the column specified by its index starting at 0
double Column::getDouble(void) const throw() // nothrow
// Return a pointer to the text value (NULL terminated string) of the column specified by its index starting at 0 {
const void* Column::getBlob(void) const throw() // nothrow return sqlite3_column_double(mStmtPtr, mIndex);
{ }
return sqlite3_column_blob(mStmtPtr, mIndex);
} // Return a pointer to the text value (NULL terminated string) of the column specified by its index starting at 0
const char* Column::getText(void) const throw() // nothrow
// Return the type of the value of the column {
int Column::getType(void) const throw() // nothrow return (const char*)sqlite3_column_text(mStmtPtr, mIndex);
{ }
return sqlite3_column_type(mStmtPtr, mIndex);
} // Return a pointer to the text value (NULL terminated string) of the column specified by its index starting at 0
const void* Column::getBlob(void) const throw() // nothrow
// Return the number of bytes used by the text value of the column {
int Column::getBytes(void) const throw() // nothrow return sqlite3_column_blob(mStmtPtr, mIndex);
{ }
return sqlite3_column_bytes(mStmtPtr, mIndex);
} // Return the type of the value of the column
int Column::getType(void) const throw() // nothrow
{
// Standard std::ostream inserter return sqlite3_column_type(mStmtPtr, mIndex);
std::ostream& operator<<(std::ostream& aStream, const Column& aColumn) }
{
aStream << aColumn.getText(); // Return the number of bytes used by the text value of the column
return aStream; int Column::getBytes(void) const throw() // nothrow
} {
return sqlite3_column_bytes(mStmtPtr, mIndex);
} // namespace SQLite }
// Standard std::ostream inserter
std::ostream& operator<<(std::ostream& aStream, const Column& aColumn)
{
aStream << aColumn.getText();
return aStream;
}
} // namespace SQLite

View File

@ -1,188 +1,210 @@
/** /**
* @file Column.h * @file Column.h
* @ingroup SQLiteCpp * @ingroup SQLiteCpp
* @brief Encapsulation of a Column in a row of the result pointed by the prepared SQLite::Statement. * @brief Encapsulation of a Column in a row of the result pointed by the prepared SQLite::Statement.
* *
* Copyright (c) 2012-2013 Sebastien Rombauts (sebastien.rombauts@gmail.com) * Copyright (c) 2012-2013 Sebastien Rombauts (sebastien.rombauts@gmail.com)
* *
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
* or copy at http://opensource.org/licenses/MIT) * or copy at http://opensource.org/licenses/MIT)
*/ */
#pragma once #pragma once
#include <sqlite3.h> /**
#include "Exception.h" * @brief Enable APIs that provide convenient access to meta-data about tables and queries.
#include "Statement.h" *
* @see #getName()
namespace SQLite *
{ * @warning Requires this SQLITE_ENABLE_COLUMN_METADATA preprocessor macro to be also defined at compile times of the SQLite library.
*/
#define SQLITE_ENABLE_COLUMN_METADATA
/** #include <sqlite3.h>
* @brief Encapsulation of a Column in a row of the result pointed by the prepared Statement.
* #include "Exception.h"
* A Column is a particular field of SQLite data in the current row of result #include "Statement.h"
* of the Statement : it points to a single cell.
*
* Its value can be expressed as a text, and, when applicable, as a numeric namespace SQLite
* (integer or floating point) or a binary blob. {
*/
class Column
{ /**
public: * @brief Encapsulation of a Column in a row of the result pointed by the prepared Statement.
/** *
* @brief Encapsulation of a Column in a Row of the result. * A Column is a particular field of SQLite data in the current row of result
* * of the Statement : it points to a single cell.
* @param[in] aStmtPtr Shared pointer to the prepared SQLite Statement Object. *
* @param[in] aIndex Index of the column in the row of result * Its value can be expressed as a text, and, when applicable, as a numeric
*/ * (integer or floating point) or a binary blob.
Column(Statement::Ptr& aStmtPtr, int aIndex) throw(); // nothrow */
/// @brief Simple destructor class Column
virtual ~Column(void) throw(); // nothrow {
public:
// default copy constructor and assignment operator are perfectly suited : /**
// they copy the Statement::Ptr which in turn increments the reference counter. * @brief Encapsulation of a Column in a Row of the result.
*
/// @brief Return the integer value of the column. * @param[in] aStmtPtr Shared pointer to the prepared SQLite Statement Object.
int getInt (void) const throw(); // nothrow * @param[in] aIndex Index of the column in the row of result
/// @brief Return the 64bits integer value of the column. */
sqlite3_int64 getInt64 (void) const throw(); // nothrow Column(Statement::Ptr& aStmtPtr, int aIndex) throw(); // nothrow
/// @brief Return the double (64bits float) value of the column. /// @brief Simple destructor
double getDouble(void) const throw(); // nothrow virtual ~Column(void) throw(); // nothrow
/**
* @brief Return a pointer to the text value (NULL terminated string) of the column. // default copy constructor and assignment operator are perfectly suited :
* // they copy the Statement::Ptr which in turn increments the reference counter.
* @warning The value pointed at is only valid while the statement is valid (ie. not finalized),
* thus you must copy it before using it beyond its scope (to a std::string for instance). #ifdef SQLITE_ENABLE_COLUMN_METADATA
*/ /**
const char* getText (void) const throw(); // nothrow * @brief Return a pointer to the column name
/** *
* @brief Return a pointer to the binary blob value of the column. * Require definition of the SQLITE_ENABLE_COLUMN_METADATA preprocessor macro :
* * - for compilation of the SQLite library,
* @warning The value pointed at is only valid while the statement is valid (ie. not finalized), * - and also when compiling this wrapper.
* thus you must copy it before using it beyond its scope (to a std::string for instance). */
*/ const char* getName (void) const throw(); // nothrow
const void* getBlob (void) const throw(); // nothrow #endif
/** /// @brief Return the integer value of the column.
* @brief Return the type of the value of the column int getInt (void) const throw(); // nothrow
* /// @brief Return the 64bits integer value of the column.
* Return either SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL. sqlite3_int64 getInt64 (void) const throw(); // nothrow
* /// @brief Return the double (64bits float) value of the column.
* @warning After a type conversion (by a call to a getXxx on a Column of a Yyy type), double getDouble(void) const throw(); // nothrow
* the value returned by sqlite3_column_type() is undefined. /**
*/ * @brief Return a pointer to the text value (NULL terminated string) of the column.
int getType(void) const throw(); // nothrow *
* @warning The value pointed at is only valid while the statement is valid (ie. not finalized),
/// @brief Test if the column is an integer type value (meaningful only before any conversion) * thus you must copy it before using it beyond its scope (to a std::string for instance).
inline bool isInteger(void) const throw() // nothrow */
{ const char* getText (void) const throw(); // nothrow
return (SQLITE_INTEGER == getType()); /**
} * @brief Return a pointer to the binary blob value of the column.
/// @brief Test if the column is a floating point type value (meaningful only before any conversion) *
inline bool isFloat(void) const throw() // nothrow * @warning The value pointed at is only valid while the statement is valid (ie. not finalized),
{ * thus you must copy it before using it beyond its scope (to a std::string for instance).
return (SQLITE_FLOAT == getType()); */
} const void* getBlob (void) const throw(); // nothrow
/// @brief Test if the column is a text type value (meaningful only before any conversion)
inline bool isText(void) const throw() // nothrow /**
{ * @brief Return the type of the value of the column
return (SQLITE_TEXT == getType()); *
} * Return either SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL.
/// @brief Test if the column is a binary blob type value (meaningful only before any conversion) *
inline bool isBlob(void) const throw() // nothrow * @warning After a type conversion (by a call to a getXxx on a Column of a Yyy type),
{ * the value returned by sqlite3_column_type() is undefined.
return (SQLITE_BLOB == getType()); */
} int getType(void) const throw(); // nothrow
/// @brief Test if the column is NULL (meaningful only before any conversion)
inline bool isNull(void) const throw() // nothrow /// @brief Test if the column is an integer type value (meaningful only before any conversion)
{ inline bool isInteger(void) const throw() // nothrow
return (SQLITE_NULL == getType()); {
} return (SQLITE_INTEGER == getType());
}
/** /// @brief Test if the column is a floating point type value (meaningful only before any conversion)
* @brief Return the number of bytes used by the text (or blob) value of the column inline bool isFloat(void) const throw() // nothrow
* {
* Return either : return (SQLITE_FLOAT == getType());
* - size in bytes (not in characters) of the string returned by getText() without the '\0' terminator }
* - size in bytes of the string representation of the numerical value (integer or double) /// @brief Test if the column is a text type value (meaningful only before any conversion)
* - size in bytes of the binary blob returned by getBlob() inline bool isText(void) const throw() // nothrow
* - 0 for a NULL value {
*/ return (SQLITE_TEXT == getType());
int getBytes(void) const throw(); }
/// @brief Test if the column is a binary blob type value (meaningful only before any conversion)
/// @brief Alias returning the number of bytes used by the text (or blob) value of the column inline bool isBlob(void) const throw() // nothrow
inline int size(void) const throw() {
{ return (SQLITE_BLOB == getType());
return getBytes (); }
} /// @brief Test if the column is NULL (meaningful only before any conversion)
inline bool isNull(void) const throw() // nothrow
/// @brief Inline cast operator to int {
inline operator int() const return (SQLITE_NULL == getType());
{ }
return getInt();
} /**
/// @brief Inline cast operator to 64bits integer * @brief Return the number of bytes used by the text (or blob) value of the column
inline operator sqlite3_int64() const *
{ * Return either :
return getInt64(); * - size in bytes (not in characters) of the string returned by getText() without the '\0' terminator
} * - size in bytes of the string representation of the numerical value (integer or double)
/// @brief Inline cast operator to double * - size in bytes of the binary blob returned by getBlob()
inline operator double() const * - 0 for a NULL value
{ */
return getDouble(); int getBytes(void) const throw();
}
/** /// @brief Alias returning the number of bytes used by the text (or blob) value of the column
* @brief Inline cast operator to char* inline int size(void) const throw()
* {
* @see getText return getBytes ();
*/ }
inline operator const char*() const
{ /// @brief Inline cast operator to int
return getText(); inline operator int() const
} {
/** return getInt();
* @brief Inline cast operator to void* }
* /// @brief Inline cast operator to 64bits integer
* @see getBlob inline operator sqlite3_int64() const
*/ {
inline operator const void*() const return getInt64();
{ }
return getBlob(); /// @brief Inline cast operator to double
} inline operator double() const
#ifdef __GNUC__ {
// NOTE : the following is required by GCC to cast a Column result in a std::string return getDouble();
// (error: conversion from SQLite::Column to non-scalar type std::string {aka std::basic_string<char>} requested) }
// but is not working under Microsoft Visual Studio 2010 and 2012 /**
// (error C2440: 'initializing' : cannot convert from 'SQLite::Column' to 'std::basic_string<_Elem,_Traits,_Ax>' * @brief Inline cast operator to char*
// [...] constructor overload resolution was ambiguous) *
/// Inline cast operator to std::string * @see getText
inline operator const std::string() const */
{ inline operator const char*() const
return getText(); {
} return getText();
#endif }
/**
/// @brief Return UTF-8 encoded English language explanation of the most recent error. * @brief Inline cast operator to void*
inline const char* errmsg(void) const *
{ * @see getBlob
return sqlite3_errmsg(mStmtPtr); */
} inline operator const void*() const
private: {
Statement::Ptr mStmtPtr; //!< Shared Pointer to the prepared SQLite Statement Object return getBlob();
int mIndex; //!< Index of the column in the row of result }
}; #ifdef __GNUC__
// NOTE : the following is required by GCC to cast a Column result in a std::string
/** // (error: conversion from SQLite::Column to non-scalar type std::string {aka std::basic_string<char>} requested)
* @brief Standard std::ostream text inserter // but is not working under Microsoft Visual Studio 2010 and 2012
* // (error C2440: 'initializing' : cannot convert from 'SQLite::Column' to 'std::basic_string<_Elem,_Traits,_Ax>'
* Insert the text value of the Column object, using getText(), into the provided stream. // [...] constructor overload resolution was ambiguous)
* /// Inline cast operator to std::string
* @param[in] aStream Stream to use inline operator const std::string() const
* @param[in] aColumn Column object to insert into the provided stream {
* return getText();
* @return Reference to the stream used }
*/ #endif
std::ostream& operator<<(std::ostream& aStream, const Column& aColumn);
/// @brief Return UTF-8 encoded English language explanation of the most recent error.
} // namespace SQLite inline const char* errmsg(void) const
{
return sqlite3_errmsg(mStmtPtr);
}
private:
Statement::Ptr mStmtPtr; //!< Shared Pointer to the prepared SQLite Statement Object
int mIndex; //!< Index of the column in the row of result
};
/**
* @brief Standard std::ostream text inserter
*
* Insert the text value of the Column object, using getText(), into the provided stream.
*
* @param[in] aStream Stream to use
* @param[in] aColumn Column object to insert into the provided stream
*
* @return Reference to the stream used
*/
std::ostream& operator<<(std::ostream& aStream, const Column& aColumn);
} // namespace SQLite

View File

@ -37,5 +37,5 @@
* with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same * with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
* numbers used in [SQLITECPP_VERSION]. * numbers used in [SQLITECPP_VERSION].
*/ */
#define SQLITECPP_VERSION "0.5.0" #define SQLITECPP_VERSION "0.5.1"
#define SQLITECPP_VERSION_NUMBER 0005000 #define SQLITECPP_VERSION_NUMBER 0005001