Skip to content

Commit

Permalink
export cpp classes
Browse files Browse the repository at this point in the history
  • Loading branch information
UnixY2K committed Mar 19, 2022
1 parent 5acf16d commit 0207d26
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 96 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ source_group(src FILES ${SQLITECPP_SRC})

# list of header files of the library
set(SQLITECPP_INC
$(PROJECT_SOURCE_DIR)/include/private/exportAPI.h
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/SQLiteCpp.h
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Assertion.h
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Backup.h
Expand Down
2 changes: 1 addition & 1 deletion include/SQLiteCpp/Backup.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace SQLite
* See also the a reference implementation of live backup taken from the official site:
* https://www.sqlite.org/backup.html
*/
class Backup
class SQLiteCppAPI Backup
{
public:
/**
Expand Down
14 changes: 7 additions & 7 deletions include/SQLiteCpp/Column.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
namespace SQLite
{

extern const int INTEGER; ///< SQLITE_INTEGER
extern const int FLOAT; ///< SQLITE_FLOAT
extern const int TEXT; ///< SQLITE_TEXT
extern const int BLOB; ///< SQLITE_BLOB
extern const int Null; ///< SQLITE_NULL
SQLiteCppAPI extern const int INTEGER; ///< SQLITE_INTEGER
SQLiteCppAPI extern const int FLOAT; ///< SQLITE_FLOAT
SQLiteCppAPI extern const int TEXT; ///< SQLITE_TEXT
SQLiteCppAPI extern const int BLOB; ///< SQLITE_BLOB
SQLiteCppAPI extern const int Null; ///< SQLITE_NULL


/**
Expand All @@ -43,7 +43,7 @@ extern const int Null; ///< SQLITE_NULL
* because of the way it shares the underling SQLite precompiled statement
* in a custom shared pointer (See the inner class "Statement::Ptr").
*/
class Column
class SQLiteCppAPI Column
{
public:
/**
Expand Down Expand Up @@ -264,7 +264,7 @@ class Column
*
* @return Reference to the stream used
*/
std::ostream& operator<<(std::ostream& aStream, const Column& aColumn);
SQLiteCppAPI std::ostream& operator<<(std::ostream& aStream, const Column& aColumn);

#if __cplusplus >= 201402L || (defined(_MSC_VER) && _MSC_VER >= 1900) // c++14: Visual Studio 2015

Expand Down
33 changes: 17 additions & 16 deletions include/SQLiteCpp/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include <memory>
#include <string.h>
#include <SQLiteCpp/private/exportAPI.h>

// Forward declarations to avoid inclusion of <sqlite3.h> in a header
struct sqlite3;
Expand All @@ -54,37 +55,37 @@ namespace SQLite
// Those public constants enable most usages of SQLiteCpp without including <sqlite3.h> in the client application.

/// The database is opened in read-only mode. If the database does not already exist, an error is returned.
extern const int OPEN_READONLY; // SQLITE_OPEN_READONLY
SQLiteCppAPI extern const int OPEN_READONLY; // SQLITE_OPEN_READONLY
/// The database is opened for reading and writing if possible, or reading only if the file is write protected
/// by the operating system. In either case the database must already exist, otherwise an error is returned.
extern const int OPEN_READWRITE; // SQLITE_OPEN_READWRITE
SQLiteCppAPI extern const int OPEN_READWRITE; // SQLITE_OPEN_READWRITE
/// With OPEN_READWRITE: The database is opened for reading and writing, and is created if it does not already exist.
extern const int OPEN_CREATE; // SQLITE_OPEN_CREATE
SQLiteCppAPI extern const int OPEN_CREATE; // SQLITE_OPEN_CREATE
/// Enable URI filename interpretation, parsed according to RFC 3986 (ex. "file:data.db?mode=ro&cache=private")
extern const int OPEN_URI; // SQLITE_OPEN_URI
SQLiteCppAPI extern const int OPEN_URI; // SQLITE_OPEN_URI
/// Open in memory database
extern const int OPEN_MEMORY; // SQLITE_OPEN_MEMORY
SQLiteCppAPI extern const int OPEN_MEMORY; // SQLITE_OPEN_MEMORY
/// Open database in multi-thread threading mode
extern const int OPEN_NOMUTEX; // SQLITE_OPEN_NOMUTEX
SQLiteCppAPI extern const int OPEN_NOMUTEX; // SQLITE_OPEN_NOMUTEX
/// Open database with thread-safety in serialized threading mode
extern const int OPEN_FULLMUTEX; // SQLITE_OPEN_FULLMUTEX
SQLiteCppAPI extern const int OPEN_FULLMUTEX; // SQLITE_OPEN_FULLMUTEX
/// Open database with shared cache enabled
extern const int OPEN_SHAREDCACHE; // SQLITE_OPEN_SHAREDCACHE
SQLiteCppAPI extern const int OPEN_SHAREDCACHE; // SQLITE_OPEN_SHAREDCACHE
/// Open database with shared cache disabled
extern const int OPEN_PRIVATECACHE; // SQLITE_OPEN_PRIVATECACHE
SQLiteCppAPI extern const int OPEN_PRIVATECACHE; // SQLITE_OPEN_PRIVATECACHE
/// Database filename is not allowed to be a symbolic link (Note: only since SQlite 3.31.0 from 2020-01-22)
extern const int OPEN_NOFOLLOW; // SQLITE_OPEN_NOFOLLOW
SQLiteCppAPI extern const int OPEN_NOFOLLOW; // SQLITE_OPEN_NOFOLLOW


extern const int OK; ///< SQLITE_OK (used by check() bellow)
SQLiteCppAPI extern const int OK; ///< SQLITE_OK (used by check() bellow)

extern const char* VERSION; ///< SQLITE_VERSION string from the sqlite3.h used at compile time
extern const int VERSION_NUMBER; ///< SQLITE_VERSION_NUMBER from the sqlite3.h used at compile time
SQLiteCppAPI extern const char* VERSION; ///< SQLITE_VERSION string from the sqlite3.h used at compile time
SQLiteCppAPI extern const int VERSION_NUMBER; ///< SQLITE_VERSION_NUMBER from the sqlite3.h used at compile time

/// Return SQLite version string using runtime call to the compiled library
const char* getLibVersion() noexcept;
SQLiteCppAPI const char* getLibVersion() noexcept;
/// Return SQLite version number using runtime call to the compiled library
int getLibVersionNumber() noexcept;
SQLiteCppAPI int getLibVersionNumber() noexcept;

// Public structure for representing all fields contained within the SQLite header.
// Official documentation for fields: https://www.sqlite.org/fileformat.html#the_database_header
Expand Down Expand Up @@ -130,7 +131,7 @@ struct Header {
* because of the way it shares the underling SQLite precompiled statement
* in a custom shared pointer (See the inner class "Statement::Ptr").
*/
class Database
class SQLiteCppAPI Database
{
friend class Statement; // Give Statement constructor access to the mSQLitePtr Connection Handle

Expand Down
3 changes: 2 additions & 1 deletion include/SQLiteCpp/Exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* or copy at http://opensource.org/licenses/MIT)
*/
#pragma once
#include <SQLiteCpp/private/exportAPI.h>

#include <stdexcept>
#include <string>
Expand All @@ -23,7 +24,7 @@ namespace SQLite
/**
* @brief Encapsulation of the error message from SQLite3, based on std::runtime_error.
*/
class Exception : public std::runtime_error
class SQLiteCppAPI Exception : public std::runtime_error
{
public:
/**
Expand Down
5 changes: 3 additions & 2 deletions include/SQLiteCpp/Statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
#pragma once

#include <SQLiteCpp/private/exportAPI.h>
#include <SQLiteCpp/Exception.h>
#include <SQLiteCpp/Utils.h> // SQLITECPP_PURE_FUNC

Expand All @@ -30,7 +31,7 @@ namespace SQLite
class Database;
class Column;

extern const int OK; ///< SQLITE_OK
SQLiteCppAPI extern const int OK; ///< SQLITE_OK

/**
* @brief RAII encapsulation of a prepared SQLite Statement.
Expand All @@ -49,7 +50,7 @@ extern const int OK; ///< SQLITE_OK
* because of the way it shares the underling SQLite precompiled statement
* in a custom shared pointer (See the inner class "Statement::Ptr").
*/
class Statement
class SQLiteCppAPI Statement
{
friend class Column; // For access to Statement::Ptr inner class

Expand Down
5 changes: 3 additions & 2 deletions include/SQLiteCpp/Transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* or copy at http://opensource.org/licenses/MIT)
*/
#pragma once
#include <SQLiteCpp/private/exportAPI.h>

#include <SQLiteCpp/Exception.h>

Expand All @@ -18,7 +19,7 @@ namespace SQLite


// Forward declaration
class Database;
class SQLiteCppAPI Database;

/**
* @brief Transaction behaviors when opening an SQLite transaction.
Expand Down Expand Up @@ -50,7 +51,7 @@ enum class TransactionBehavior {
* because of the way it shares the underling SQLite precompiled statement
* in a custom shared pointer (See the inner class "Statement::Ptr").
*/
class Transaction
class SQLiteCppAPI Transaction
{
public:
/**
Expand Down
24 changes: 24 additions & 0 deletions include/SQLiteCpp/private/exportAPI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once
// Utility header for SQLiteCpp
// it allows exporting/importing c++ code to an dynamic library

#if defined(SQLiteCppLIBRARY_EXPORT)
#define SQLiteCppAPI EXPORT
#else
#define SQLiteCppAPI IMPORT
#endif

#if defined(_MSC_VER)
// Microsoft
#define EXPORT __declspec(dllexport)
#define IMPORT __declspec(dllimport)
#elif defined(__GNUC__)
// GCC
#define EXPORT __attribute__((visibility("default")))
#define IMPORT
#else
// do nothing
#define EXPORT
#define IMPORT
#pragma warning Unknown dynamic link import/export semantics.
#endif
37 changes: 20 additions & 17 deletions src/Column.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
* or copy at http://opensource.org/licenses/MIT)
*/
#define SQLiteCppLIBRARY_EXPORT
#include <SQLiteCpp/private/exportAPI.h>

#include <SQLiteCpp/Column.h>

#include <sqlite3.h>
Expand All @@ -18,22 +21,22 @@
namespace SQLite
{

const int INTEGER = SQLITE_INTEGER;
const int FLOAT = SQLITE_FLOAT;
const int TEXT = SQLITE_TEXT;
const int BLOB = SQLITE_BLOB;
const int Null = SQLITE_NULL;
SQLiteCppAPI const int INTEGER = SQLITE_INTEGER;
SQLiteCppAPI const int FLOAT = SQLITE_FLOAT;
SQLiteCppAPI const int TEXT = SQLITE_TEXT;
SQLiteCppAPI const int BLOB = SQLITE_BLOB;
SQLiteCppAPI const int Null = SQLITE_NULL;


// Encapsulation of a Column in a row of the result pointed by the prepared Statement.
Column::Column(Statement::Ptr& aStmtPtr, int aIndex) noexcept :
SQLiteCppAPI Column::Column(Statement::Ptr& aStmtPtr, int aIndex) noexcept :
mStmtPtr(aStmtPtr),
mIndex(aIndex)
{
}

// Return the named assigned to this result column (potentially aliased)
const char* Column::getName() const noexcept
SQLiteCppAPI const char* Column::getName() const noexcept
{
return sqlite3_column_name(mStmtPtr, mIndex);
}
Expand All @@ -47,44 +50,44 @@ const char* Column::getOriginName() const noexcept
#endif

// Return the integer value of the column specified by its index starting at 0
int Column::getInt() const noexcept
SQLiteCppAPI int Column::getInt() const noexcept
{
return sqlite3_column_int(mStmtPtr, mIndex);
}

// Return the unsigned integer value of the column specified by its index starting at 0
unsigned Column::getUInt() const noexcept
SQLiteCppAPI unsigned Column::getUInt() const noexcept
{
return static_cast<unsigned>(getInt64());
}

// Return the 64bits integer value of the column specified by its index starting at 0
long long Column::getInt64() const noexcept
SQLiteCppAPI long long Column::getInt64() const noexcept
{
return sqlite3_column_int64(mStmtPtr, mIndex);
}

// Return the double value of the column specified by its index starting at 0
double Column::getDouble() const noexcept
SQLiteCppAPI double Column::getDouble() const noexcept
{
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(const char* apDefaultValue /* = "" */) const noexcept
SQLiteCppAPI const char* Column::getText(const char* apDefaultValue /* = "" */) const noexcept
{
const char* pText = reinterpret_cast<const char*>(sqlite3_column_text(mStmtPtr, mIndex));
return (pText?pText:apDefaultValue);
}

// Return a pointer to the blob value (*not* NULL terminated) of the column specified by its index starting at 0
const void* Column::getBlob() const noexcept
SQLiteCppAPI const void* Column::getBlob() const noexcept
{
return sqlite3_column_blob(mStmtPtr, mIndex);
}

// Return a std::string to a TEXT or BLOB column
std::string Column::getString() const
SQLiteCppAPI std::string Column::getString() const
{
// Note: using sqlite3_column_blob and not sqlite3_column_text
// - no need for sqlite3_column_text to add a \0 on the end, as we're getting the bytes length directly
Expand All @@ -96,19 +99,19 @@ std::string Column::getString() const
}

// Return the type of the value of the column
int Column::getType() const noexcept
SQLiteCppAPI int Column::getType() const noexcept
{
return sqlite3_column_type(mStmtPtr, mIndex);
}

// Return the number of bytes used by the text value of the column
int Column::getBytes() const noexcept
SQLiteCppAPI int Column::getBytes() const noexcept
{
return sqlite3_column_bytes(mStmtPtr, mIndex);
}

// Standard std::ostream inserter
std::ostream& operator<<(std::ostream& aStream, const Column& aColumn)
SQLiteCppAPI std::ostream& operator<<(std::ostream& aStream, const Column& aColumn)
{
aStream.write(aColumn.getText(), aColumn.getBytes());
return aStream;
Expand Down
34 changes: 18 additions & 16 deletions src/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
* or copy at http://opensource.org/licenses/MIT)
*/
#define SQLiteCppLIBRARY_EXPORT
#include <SQLiteCpp/private/exportAPI.h>
#include <SQLiteCpp/Database.h>

#include <SQLiteCpp/Assertion.h>
Expand All @@ -27,34 +29,34 @@
namespace SQLite
{

const int OPEN_READONLY = SQLITE_OPEN_READONLY;
const int OPEN_READWRITE = SQLITE_OPEN_READWRITE;
const int OPEN_CREATE = SQLITE_OPEN_CREATE;
const int OPEN_URI = SQLITE_OPEN_URI;
const int OPEN_MEMORY = SQLITE_OPEN_MEMORY;
const int OPEN_NOMUTEX = SQLITE_OPEN_NOMUTEX;
const int OPEN_FULLMUTEX = SQLITE_OPEN_FULLMUTEX;
const int OPEN_SHAREDCACHE = SQLITE_OPEN_SHAREDCACHE;
const int OPEN_PRIVATECACHE = SQLITE_OPEN_PRIVATECACHE;
SQLiteCppAPI const int OPEN_READONLY = SQLITE_OPEN_READONLY;
SQLiteCppAPI const int OPEN_READWRITE = SQLITE_OPEN_READWRITE;
SQLiteCppAPI const int OPEN_CREATE = SQLITE_OPEN_CREATE;
SQLiteCppAPI const int OPEN_URI = SQLITE_OPEN_URI;
SQLiteCppAPI const int OPEN_MEMORY = SQLITE_OPEN_MEMORY;
SQLiteCppAPI const int OPEN_NOMUTEX = SQLITE_OPEN_NOMUTEX;
SQLiteCppAPI const int OPEN_FULLMUTEX = SQLITE_OPEN_FULLMUTEX;
SQLiteCppAPI const int OPEN_SHAREDCACHE = SQLITE_OPEN_SHAREDCACHE;
SQLiteCppAPI const int OPEN_PRIVATECACHE = SQLITE_OPEN_PRIVATECACHE;
#if SQLITE_VERSION_NUMBER >= 3031000
const int OPEN_NOFOLLOW = SQLITE_OPEN_NOFOLLOW;
SQLiteCppAPI const int OPEN_NOFOLLOW = SQLITE_OPEN_NOFOLLOW;
#else
const int OPEN_NOFOLLOW = 0;
SQLiteCppAPI const int OPEN_NOFOLLOW = 0;
#endif

const int OK = SQLITE_OK;
SQLiteCppAPI const int OK = SQLITE_OK;

const char* VERSION = SQLITE_VERSION;
const int VERSION_NUMBER = SQLITE_VERSION_NUMBER;
SQLiteCppAPI const char* VERSION = SQLITE_VERSION;
SQLiteCppAPI const int VERSION_NUMBER = SQLITE_VERSION_NUMBER;

// Return SQLite version string using runtime call to the compiled library
const char* getLibVersion() noexcept
SQLiteCppAPI const char* getLibVersion() noexcept
{
return sqlite3_libversion();
}

// Return SQLite version number using runtime call to the compiled library
int getLibVersionNumber() noexcept
SQLiteCppAPI int getLibVersionNumber() noexcept
{
return sqlite3_libversion_number();
}
Expand Down
Loading

0 comments on commit 0207d26

Please sign in to comment.