WARNING: USE THIS SOFTWARE AT YOUR OWN RISK! THIS IS EXPERIMENTAL SOFTWARE NOT INTENDED FOR PRODUCTION USE! Zuble is currently an early stage prototype. As such Zuble is minimally tested and inherently unstable. It is provided for experimental, development, and demonstration purposes only. Zuble QML Types   |  Zuble C++ Classes   |  Zuble Overview
Zuble  0.1
Zuble Framework C++/QML extension API
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | Properties | Private Types | Friends | List of all members
Zbl::ZsqlDatabase Class Reference

A javascript wrapper class for QSqlDatabase objects. This object represents a database connection. (database sprocket) More...

#include <ZsqlDatabase.h>

Inheritance diagram for Zbl::ZsqlDatabase:
Inheritance graph
[legend]
Collaboration diagram for Zbl::ZsqlDatabase:
Collaboration graph
[legend]

Public Types

enum  TableType { Tables = QSql::Tables, SystemTables = QSql::SystemTables, Views = QSql::Views, AllTables = QSql::AllTables }
 
enum  NumericalPrecisionPolicy { LowPrecisionInt32 = QSql::LowPrecisionInt32, LowPrecisionInt64 = QSql::LowPrecisionInt64, LowPrecisionDouble = QSql::LowPrecisionDouble, HighPrecision = QSql::HighPrecision }
 

Public Member Functions

 ZsqlDatabase (QObject *parent=0)
 
virtual ~ZsqlDatabase ()
 
bool isValid () const
 
bool isOpen () const
 
bool isOpenError () const
 
QString getHostname () const
 
void setHostname (const QString &hostname)
 
QString getDbName () const
 
void setDbName (const QString &hostname)
 
int getPortNumber () const
 
void setPortNumber (int)
 
void setConnectOptions (const QString &options)
 
QString getConnectOptions ()
 
QObject * getLastError ()
 
QVariant getTags ()
 
Q_INVOKABLE bool open (const QString &user, const QString &password)
 Opens a database connection using the given user name and password. More...
 
Q_INVOKABLE void close ()
 Closes the database connection, freeing associated resources. ZsqlQuery objects created by this database connection are invalidated upon return from this method. More...
 
Q_INVOKABLE QStringList getTableNames (int tableType) const
 Obtain names of tables in the database. More...
 
Q_INVOKABLE bool transaction ()
 Begins a database transaction if supported by the database driver. More...
 
Q_INVOKABLE bool commit ()
 Commits a database transaction if supported by the database driver and a transaction() has been started. More...
 
Q_INVOKABLE bool rollback ()
 Rolls back a database transaction if supported by the database driver and a transaction() has been started. More...
 
Q_INVOKABLE QObject * newQuery ()
 Creates a new ZsqlQuery object for this database connection. More...
 
Q_INVOKABLE QObject * record (const QString &tablename)
 Creates a new ZsqlRecord object for the specified table. More...
 
Q_INVOKABLE void release ()
 Releases references from this object to wrapped Qt C++ objects. The C++ objects may be deleted by this function. More...
 

Static Public Member Functions

static QObject * addDatabase (const QString &driverType, const QString &connectionName)
 Adds a SQL database to the list of database connections. More...
 
static void removeDatabase (const QString &connectionName)
 Removes a SQL database from the list of database connections. More...
 
static void removeThreadDatabases ()
 Removes all SQL databases that were created by the current thread. More...
 
static QObject * database (const QString &connectionName)
 Obtains a SQL database from the list of database connections. The named connection must have previously been created by a call to addDatabase(). More...
 

Protected Member Functions

void createTags ()
 Create the m_tag object that presents a Javascript interface to ZsqlDatabase enumeration values. More...
 
void validateDb () const
 Checks that the embedded QSqlDatabase object exists and throws a ZblException if not. More...
 
void validateThread ()
 Checks to see if this object was created in the current thread and throws a ZblException if not. More...
 

Protected Attributes

QSqlDatabase * m_db
 An encapsulated SQL database object. More...
 

Static Protected Attributes

static ZsqlDbMap m_databaseObjects
 A map of SQL database connection wrapper objects. More...
 
static QReadWriteLock m_lock
 A lock for multi-threaded data access to m_databaseObjects. More...
 
static QVariant m_tags
 A QVariantMap used to pass ZsqlDatabase enumeration values to Javascript programs. More...
 

Properties

QString hostName
 Name of the host to which the database connection is made. More...
 
QString databaseName
 Name of the database to which the database connection is made. More...
 
int portNumber
 Number of the port to which the database connection is made. More...
 
QString connectOptions
 Connect options for the database driver. More...
 
bool isValid
 Determines if this database connection has a valid database driver. More...
 
bool isOpen
 Determines if this database connection is currently open. More...
 
bool isOpenError
 Determines if this database connection is currently open. More...
 
QObject lastError
 Name of the host to which the database connection is made. More...
 
QVariant tags
 A Javascript object containing ZsqlDatabase enumeration values. More...
 

Private Types

typedef QPointer< ZsqlQueryZsqlQueryPtr
 
typedef QMap< QString, ZsqlDatabase * > ZsqlDbMap
 
typedef QList< ZsqlQuery * > ZsqlQueryList
 

Friends

class Zbl::ZsqlQuery
 

Detailed Description

A javascript wrapper class for QSqlDatabase objects. This object represents a database connection. (database sprocket)

ZsqlDatabase objects are explicitly shared. Their ownership is retained by the zuble c++ plugin. They're deleted when ZsqlDatabase::removeDatabase() is called for that database object or when the ZScriptThread object that created them exits.

Call ZsqlDatabase::release() to delete the encapsulated QSqlDatabase object. Note this action will also call ZsqlQuery::release() on all ZsqlQuery objects created by this object.

ZsqlDatabase object methods are reentrant unless explicitly noted to be "thread-safe". Reentrant methods should only be called by the thread that created the ZsqlDatabase object.

See also
ZsqlQuery
ZsqlDatabase::release()
ZsqlQuery::release()
ZblApp::sqlAddDatabase()

Definition at line 71 of file ZsqlDatabase.h.

Member Typedef Documentation

◆ ZsqlDbMap

typedef QMap<QString, ZsqlDatabase*> Zbl::ZsqlDatabase::ZsqlDbMap
private

Definition at line 80 of file ZsqlDatabase.h.

◆ ZsqlQueryList

Definition at line 81 of file ZsqlDatabase.h.

◆ ZsqlQueryPtr

typedef QPointer<ZsqlQuery> Zbl::ZsqlDatabase::ZsqlQueryPtr
private

Definition at line 76 of file ZsqlDatabase.h.

Member Enumeration Documentation

◆ NumericalPrecisionPolicy

Enumerator
LowPrecisionInt32 
LowPrecisionInt64 
LowPrecisionDouble 
HighPrecision 

Definition at line 104 of file ZsqlDatabase.h.

◆ TableType

Enumerator
Tables 
SystemTables 
Views 
AllTables 

Definition at line 92 of file ZsqlDatabase.h.

Constructor & Destructor Documentation

◆ ZsqlDatabase()

Zbl::ZsqlDatabase::ZsqlDatabase ( QObject *  parent = 0)
explicit

Definition at line 44 of file ZsqlDatabase.cpp.

Referenced by addDatabase().

◆ ~ZsqlDatabase()

Zbl::ZsqlDatabase::~ZsqlDatabase ( )
virtual

Definition at line 55 of file ZsqlDatabase.cpp.

Member Function Documentation

◆ addDatabase()

QObject * Zbl::ZsqlDatabase::addDatabase ( const QString &  driverType,
const QString &  connectionName 
)
static

Adds a SQL database to the list of database connections.

If a connection of the same name exists it is replaced with a new connection. All references to the old connection and it's queries is invalidated.

Javascript programs should call ZblApp::addSqlDatabase().

Parameters
driverTypea database driver type to use for the connection
connectionNamea name for the connection
Returns
A ZsqlDatabase object that can be used to access the database, or null if an error occurred

Definition at line 344 of file ZsqlDatabase.cpp.

Referenced by Zbl::ZblDatabase::sqlAddDatabase().

◆ close()

void Zbl::ZsqlDatabase::close ( )

Closes the database connection, freeing associated resources. ZsqlQuery objects created by this database connection are invalidated upon return from this method.

Definition at line 215 of file ZsqlDatabase.cpp.

◆ commit()

bool Zbl::ZsqlDatabase::commit ( )

Commits a database transaction if supported by the database driver and a transaction() has been started.

Some databases may fail if an active query is using the database for a SELECT. Make the query inactive first in that case.

Returns
true if success, false if failed

Definition at line 485 of file ZsqlDatabase.cpp.

◆ createTags()

void Zbl::ZsqlDatabase::createTags ( )
protected

Create the m_tag object that presents a Javascript interface to ZsqlDatabase enumeration values.

Definition at line 60 of file ZsqlDatabase.cpp.

Referenced by ZsqlDatabase().

◆ database()

QObject * Zbl::ZsqlDatabase::database ( const QString &  connectionName)
static

Obtains a SQL database from the list of database connections. The named connection must have previously been created by a call to addDatabase().

Javascript programs should call ZblApp::getSqlDatabase().

Parameters
driverTypea database driver type to use for the connection
connectionNamea name for the connection
enginethe Javascript engine that will own the new ZsqlDatabase object
Returns
A ZsqlDatabase object that can be used to access the database, or NULL if no such database connection exists

Definition at line 415 of file ZsqlDatabase.cpp.

Referenced by Zbl::ZblDatabase::sqlGetDatabase().

◆ getConnectOptions()

QString Zbl::ZsqlDatabase::getConnectOptions ( )

Definition at line 168 of file ZsqlDatabase.cpp.

◆ getDbName()

QString Zbl::ZsqlDatabase::getDbName ( ) const

Definition at line 126 of file ZsqlDatabase.cpp.

◆ getHostname()

QString Zbl::ZsqlDatabase::getHostname ( ) const

Definition at line 110 of file ZsqlDatabase.cpp.

◆ getLastError()

QObject * Zbl::ZsqlDatabase::getLastError ( )

Definition at line 177 of file ZsqlDatabase.cpp.

Referenced by Zbl::ZsqlQuery::getLastError().

◆ getPortNumber()

int Zbl::ZsqlDatabase::getPortNumber ( ) const

Definition at line 142 of file ZsqlDatabase.cpp.

◆ getTableNames()

QStringList Zbl::ZsqlDatabase::getTableNames ( int  tableType) const

Obtain names of tables in the database.

Parameters
tableTypeEnumeration specifies type of tables to return. May be one of:
  • Tables
  • SystemTables
  • Views
  • AllTables
Returns
A list of table names of the specified type

Definition at line 265 of file ZsqlDatabase.cpp.

◆ getTags()

QVariant Zbl::ZsqlDatabase::getTags ( )

Definition at line 284 of file ZsqlDatabase.cpp.

◆ isOpen()

bool Zbl::ZsqlDatabase::isOpen ( ) const

Referenced by createTags().

◆ isOpenError()

bool Zbl::ZsqlDatabase::isOpenError ( ) const

Referenced by createTags().

◆ isValid()

bool Zbl::ZsqlDatabase::isValid ( ) const

Referenced by createTags().

◆ newQuery()

QObject * Zbl::ZsqlDatabase::newQuery ( )

Creates a new ZsqlQuery object for this database connection.

Returns
A ZsqlQuery object that can be used to execute SQL commands on the database.

Definition at line 289 of file ZsqlDatabase.cpp.

◆ open()

bool Zbl::ZsqlDatabase::open ( const QString &  user,
const QString &  password 
)

Opens a database connection using the given user name and password.

Parameters
userthe database user name
passwordthe password for the specified database user
Returns
True on success, false otherwise

Definition at line 207 of file ZsqlDatabase.cpp.

◆ record()

QObject * Zbl::ZsqlDatabase::record ( const QString &  tablename)

Creates a new ZsqlRecord object for the specified table.

Returns
A ZsqlRecord object describing the fields of the specified table.

Definition at line 501 of file ZsqlDatabase.cpp.

◆ release()

void Zbl::ZsqlDatabase::release ( )

Releases references from this object to wrapped Qt C++ objects. The C++ objects may be deleted by this function.

WARNING: The ZsqlDatabase object can't be used again once this function is called.

Definition at line 223 of file ZsqlDatabase.cpp.

Referenced by removeDatabase(), removeThreadDatabases(), and ~ZsqlDatabase().

◆ removeDatabase()

void Zbl::ZsqlDatabase::removeDatabase ( const QString &  connectionName)
static

Removes a SQL database from the list of database connections.

Warning: All ZsqlQuery and ZsqlRecord objects created by this database will be unusable upon return from this method.

Note: this method is thread-safe

Javascript programs should call ZblApp::removeSqlDatabase().

Parameters
connectionNamename of the connection to remove
See also
ZblApp::removeSqlDatabase

Definition at line 389 of file ZsqlDatabase.cpp.

Referenced by addDatabase(), and Zbl::ZblDatabase::sqlRemoveDatabase().

◆ removeThreadDatabases()

void Zbl::ZsqlDatabase::removeThreadDatabases ( )
static

Removes all SQL databases that were created by the current thread.

Parameters
connectionNamename of the connection to remove

Definition at line 439 of file ZsqlDatabase.cpp.

◆ rollback()

bool Zbl::ZsqlDatabase::rollback ( )

Rolls back a database transaction if supported by the database driver and a transaction() has been started.

Some databases may fail if an active query is using the database for a SELECT. Make the query inactive first in that case.

Returns
true if success, false if failed

Definition at line 493 of file ZsqlDatabase.cpp.

◆ setConnectOptions()

void Zbl::ZsqlDatabase::setConnectOptions ( const QString &  options)

Definition at line 159 of file ZsqlDatabase.cpp.

◆ setDbName()

void Zbl::ZsqlDatabase::setDbName ( const QString &  hostname)

Definition at line 133 of file ZsqlDatabase.cpp.

◆ setHostname()

void Zbl::ZsqlDatabase::setHostname ( const QString &  hostname)

Definition at line 117 of file ZsqlDatabase.cpp.

◆ setPortNumber()

void Zbl::ZsqlDatabase::setPortNumber ( int  portNumber)

Definition at line 149 of file ZsqlDatabase.cpp.

◆ transaction()

bool Zbl::ZsqlDatabase::transaction ( )

Begins a database transaction if supported by the database driver.

Returns
true if success, false if failed

Definition at line 477 of file ZsqlDatabase.cpp.

◆ validateDb()

void Zbl::ZsqlDatabase::validateDb ( ) const
inlineprotected

◆ validateThread()

void Zbl::ZsqlDatabase::validateThread ( )
inlineprotected

Checks to see if this object was created in the current thread and throws a ZblException if not.

Definition at line 416 of file ZsqlDatabase.h.

Referenced by newQuery().

Friends And Related Function Documentation

◆ Zbl::ZsqlQuery

friend class Zbl::ZsqlQuery
friend

Definition at line 73 of file ZsqlDatabase.h.

Member Data Documentation

◆ m_databaseObjects

ZsqlDatabase::ZsqlDbMap Zbl::ZsqlDatabase::m_databaseObjects
staticprotected

A map of SQL database connection wrapper objects.

A single ZsqlDatabase object is shared among all Javascript references to that named database. Closing or releasing the database invalidates all Javascript references to that ZsqlDatabase object and to any ZsqlQuery objects created by it.

Definition at line 398 of file ZsqlDatabase.h.

Referenced by addDatabase(), database(), removeDatabase(), and removeThreadDatabases().

◆ m_db

QSqlDatabase* Zbl::ZsqlDatabase::m_db
protected

◆ m_lock

QReadWriteLock Zbl::ZsqlDatabase::m_lock
staticprotected

A lock for multi-threaded data access to m_databaseObjects.

Definition at line 403 of file ZsqlDatabase.h.

Referenced by addDatabase(), database(), removeDatabase(), and removeThreadDatabases().

◆ m_tags

QVariant Zbl::ZsqlDatabase::m_tags
staticprotected

A QVariantMap used to pass ZsqlDatabase enumeration values to Javascript programs.

This object is automatically converted to a Javascript object by Qt.

Definition at line 412 of file ZsqlDatabase.h.

Referenced by createTags(), and getTags().

Property Documentation

◆ connectOptions

QString Zbl::ZsqlDatabase::connectOptions
readwrite

Connect options for the database driver.

Definition at line 145 of file ZsqlDatabase.h.

◆ databaseName

QString Zbl::ZsqlDatabase::databaseName
readwrite

Name of the database to which the database connection is made.

Returns
connection's database name

Definition at line 130 of file ZsqlDatabase.h.

◆ hostName

QString Zbl::ZsqlDatabase::hostName
readwrite

Name of the host to which the database connection is made.

Returns
connection's host name

Definition at line 122 of file ZsqlDatabase.h.

◆ isOpen

bool Zbl::ZsqlDatabase::isOpen
read

Determines if this database connection is currently open.

Returns
True if this database connection is open, false otherwise

Definition at line 161 of file ZsqlDatabase.h.

Referenced by Zbl::ZsqlQuery::newQuery().

◆ isOpenError

bool Zbl::ZsqlDatabase::isOpenError
read

Determines if this database connection is currently open.

Returns
True if this database connection is open, false otherwise

Definition at line 169 of file ZsqlDatabase.h.

◆ isValid

bool Zbl::ZsqlDatabase::isValid
read

Determines if this database connection has a valid database driver.

Returns
True if this database connection has a valid driver, false otherwise

Definition at line 153 of file ZsqlDatabase.h.

Referenced by Zbl::ZsqlQuery::newQuery().

◆ lastError

QObject Zbl::ZsqlDatabase::lastError
read

Name of the host to which the database connection is made.

Returns
connection's host name

Definition at line 177 of file ZsqlDatabase.h.

◆ portNumber

int Zbl::ZsqlDatabase::portNumber
readwrite

Number of the port to which the database connection is made.

Returns
connection's database name

Definition at line 138 of file ZsqlDatabase.h.

◆ tags

QVariant Zbl::ZsqlDatabase::tags
read

A Javascript object containing ZsqlDatabase enumeration values.

Definition at line 183 of file ZsqlDatabase.h.


The documentation for this class was generated from the following files: