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

File diff suppressed because it is too large Load Diff

View File

@ -1,81 +1,91 @@
/**
* @file Column.cpp
* @ingroup SQLiteCpp
* @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)
*
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
* or copy at http://opensource.org/licenses/MIT)
*/
#include "Column.h"
#include <iostream>
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),
mIndex (aIndex)
{
}
// Finalize and unregister the SQL query from the SQLite Database Connection.
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
{
return sqlite3_column_int(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 sqlite3_column_int64(mStmtPtr, mIndex);
}
// Return the double value of the column specified by its index starting at 0
double Column::getDouble(void) const throw() // nothrow
{
return sqlite3_column_double(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 (const char*)sqlite3_column_text(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 sqlite3_column_blob(mStmtPtr, mIndex);
}
// Return the type of the value of the column
int Column::getType(void) const throw() // nothrow
{
return sqlite3_column_type(mStmtPtr, mIndex);
}
// Return the number of bytes used by the text value of the column
int Column::getBytes(void) const throw() // nothrow
{
return sqlite3_column_bytes(mStmtPtr, mIndex);
}
// Standard std::ostream inserter
std::ostream& operator<<(std::ostream& aStream, const Column& aColumn)
{
aStream << aColumn.getText();
return aStream;
}
} // namespace SQLite
/**
* @file Column.cpp
* @ingroup SQLiteCpp
* @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)
*
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
* or copy at http://opensource.org/licenses/MIT)
*/
#include "Column.h"
#include <iostream>
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),
mIndex (aIndex)
{
}
// Finalize and unregister the SQL query from the SQLite Database Connection.
Column::~Column(void) throw() // nothrow
{
// the finalization will be done by the destructor of the last shared pointer
}
#ifdef SQLITE_ENABLE_COLUMN_METADATA
// Return the name of the column
const char * Column::getName(void) const throw() // nothrow
{
return sqlite3_column_origin_name(mStmtPtr, mIndex);
}
#endif
// Return the integer value of the column specified by its index starting at 0
int Column::getInt(void) const throw() // nothrow
{
return sqlite3_column_int(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 sqlite3_column_int64(mStmtPtr, mIndex);
}
// Return the double value of the column specified by its index starting at 0
double Column::getDouble(void) const throw() // nothrow
{
return sqlite3_column_double(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 (const char*)sqlite3_column_text(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 sqlite3_column_blob(mStmtPtr, mIndex);
}
// Return the type of the value of the column
int Column::getType(void) const throw() // nothrow
{
return sqlite3_column_type(mStmtPtr, mIndex);
}
// Return the number of bytes used by the text value of the column
int Column::getBytes(void) const throw() // nothrow
{
return sqlite3_column_bytes(mStmtPtr, mIndex);
}
// 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
* @ingroup SQLiteCpp
* @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)
*
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
* or copy at http://opensource.org/licenses/MIT)
*/
#pragma once
#include <sqlite3.h>
#include "Exception.h"
#include "Statement.h"
namespace SQLite
{
/**
* @brief Encapsulation of a Column in a row of the result pointed by the prepared Statement.
*
* A Column is a particular field of SQLite data in the current row of result
* of the Statement : it points to a single cell.
*
* Its value can be expressed as a text, and, when applicable, as a numeric
* (integer or floating point) or a binary blob.
*/
class Column
{
public:
/**
* @brief Encapsulation of a Column in a Row of the result.
*
* @param[in] aStmtPtr Shared pointer to the prepared SQLite Statement Object.
* @param[in] aIndex Index of the column in the row of result
*/
Column(Statement::Ptr& aStmtPtr, int aIndex) throw(); // nothrow
/// @brief Simple destructor
virtual ~Column(void) throw(); // nothrow
// default copy constructor and assignment operator are perfectly suited :
// they copy the Statement::Ptr which in turn increments the reference counter.
/// @brief Return the integer value of the column.
int getInt (void) const throw(); // nothrow
/// @brief Return the 64bits integer value of the column.
sqlite3_int64 getInt64 (void) const throw(); // nothrow
/// @brief Return the double (64bits float) value of the column.
double getDouble(void) const throw(); // nothrow
/**
* @brief Return a pointer to the text value (NULL terminated string) of the column.
*
* @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).
*/
const char* getText (void) const throw(); // nothrow
/**
* @brief Return a pointer to the binary blob value of the column.
*
* @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).
*/
const void* getBlob (void) const throw(); // nothrow
/**
* @brief Return the type of the value of the column
*
* Return either SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL.
*
* @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.
*/
int getType(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_INTEGER == getType());
}
/// @brief Test if the column is a floating point type value (meaningful only before any conversion)
inline bool isFloat(void) const throw() // nothrow
{
return (SQLITE_FLOAT == getType());
}
/// @brief Test if the column is a text type value (meaningful only before any conversion)
inline bool isText(void) const throw() // nothrow
{
return (SQLITE_TEXT == getType());
}
/// @brief Test if the column is a binary blob type value (meaningful only before any conversion)
inline bool isBlob(void) const throw() // nothrow
{
return (SQLITE_BLOB == getType());
}
/// @brief Test if the column is NULL (meaningful only before any conversion)
inline bool isNull(void) const throw() // nothrow
{
return (SQLITE_NULL == getType());
}
/**
* @brief Return the number of bytes used by the text (or blob) value of the column
*
* Return either :
* - 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)
* - size in bytes of the binary blob returned by getBlob()
* - 0 for a NULL value
*/
int getBytes(void) const throw();
/// @brief Alias returning the number of bytes used by the text (or blob) value of the column
inline int size(void) const throw()
{
return getBytes ();
}
/// @brief Inline cast operator to int
inline operator int() const
{
return getInt();
}
/// @brief Inline cast operator to 64bits integer
inline operator sqlite3_int64() const
{
return getInt64();
}
/// @brief Inline cast operator to double
inline operator double() const
{
return getDouble();
}
/**
* @brief Inline cast operator to char*
*
* @see getText
*/
inline operator const char*() const
{
return getText();
}
/**
* @brief Inline cast operator to void*
*
* @see getBlob
*/
inline operator const void*() const
{
return getBlob();
}
#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)
// 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>'
// [...] constructor overload resolution was ambiguous)
/// Inline cast operator to std::string
inline operator const std::string() const
{
return getText();
}
#endif
/// @brief Return UTF-8 encoded English language explanation of the most recent error.
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
/**
* @file Column.h
* @ingroup SQLiteCpp
* @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)
*
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
* or copy at http://opensource.org/licenses/MIT)
*/
#pragma once
/**
* @brief Enable APIs that provide convenient access to meta-data about tables and queries.
*
* @see #getName()
*
* @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>
#include "Exception.h"
#include "Statement.h"
namespace SQLite
{
/**
* @brief Encapsulation of a Column in a row of the result pointed by the prepared Statement.
*
* A Column is a particular field of SQLite data in the current row of result
* of the Statement : it points to a single cell.
*
* Its value can be expressed as a text, and, when applicable, as a numeric
* (integer or floating point) or a binary blob.
*/
class Column
{
public:
/**
* @brief Encapsulation of a Column in a Row of the result.
*
* @param[in] aStmtPtr Shared pointer to the prepared SQLite Statement Object.
* @param[in] aIndex Index of the column in the row of result
*/
Column(Statement::Ptr& aStmtPtr, int aIndex) throw(); // nothrow
/// @brief Simple destructor
virtual ~Column(void) throw(); // nothrow
// default copy constructor and assignment operator are perfectly suited :
// they copy the Statement::Ptr which in turn increments the reference counter.
#ifdef SQLITE_ENABLE_COLUMN_METADATA
/**
* @brief Return a pointer to the column name
*
* Require definition of the SQLITE_ENABLE_COLUMN_METADATA preprocessor macro :
* - for compilation of the SQLite library,
* - and also when compiling this wrapper.
*/
const char* getName (void) const throw(); // nothrow
#endif
/// @brief Return the integer value of the column.
int getInt (void) const throw(); // nothrow
/// @brief Return the 64bits integer value of the column.
sqlite3_int64 getInt64 (void) const throw(); // nothrow
/// @brief Return the double (64bits float) value of the column.
double getDouble(void) const throw(); // nothrow
/**
* @brief Return a pointer to the text value (NULL terminated string) of the column.
*
* @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).
*/
const char* getText (void) const throw(); // nothrow
/**
* @brief Return a pointer to the binary blob value of the column.
*
* @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).
*/
const void* getBlob (void) const throw(); // nothrow
/**
* @brief Return the type of the value of the column
*
* Return either SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL.
*
* @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.
*/
int getType(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_INTEGER == getType());
}
/// @brief Test if the column is a floating point type value (meaningful only before any conversion)
inline bool isFloat(void) const throw() // nothrow
{
return (SQLITE_FLOAT == getType());
}
/// @brief Test if the column is a text type value (meaningful only before any conversion)
inline bool isText(void) const throw() // nothrow
{
return (SQLITE_TEXT == getType());
}
/// @brief Test if the column is a binary blob type value (meaningful only before any conversion)
inline bool isBlob(void) const throw() // nothrow
{
return (SQLITE_BLOB == getType());
}
/// @brief Test if the column is NULL (meaningful only before any conversion)
inline bool isNull(void) const throw() // nothrow
{
return (SQLITE_NULL == getType());
}
/**
* @brief Return the number of bytes used by the text (or blob) value of the column
*
* Return either :
* - 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)
* - size in bytes of the binary blob returned by getBlob()
* - 0 for a NULL value
*/
int getBytes(void) const throw();
/// @brief Alias returning the number of bytes used by the text (or blob) value of the column
inline int size(void) const throw()
{
return getBytes ();
}
/// @brief Inline cast operator to int
inline operator int() const
{
return getInt();
}
/// @brief Inline cast operator to 64bits integer
inline operator sqlite3_int64() const
{
return getInt64();
}
/// @brief Inline cast operator to double
inline operator double() const
{
return getDouble();
}
/**
* @brief Inline cast operator to char*
*
* @see getText
*/
inline operator const char*() const
{
return getText();
}
/**
* @brief Inline cast operator to void*
*
* @see getBlob
*/
inline operator const void*() const
{
return getBlob();
}
#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)
// 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>'
// [...] constructor overload resolution was ambiguous)
/// Inline cast operator to std::string
inline operator const std::string() const
{
return getText();
}
#endif
/// @brief Return UTF-8 encoded English language explanation of the most recent error.
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
* numbers used in [SQLITECPP_VERSION].
*/
#define SQLITECPP_VERSION "0.5.0"
#define SQLITECPP_VERSION_NUMBER 0005000
#define SQLITECPP_VERSION "0.5.1"
#define SQLITECPP_VERSION_NUMBER 0005001