Click here to Skip to main content
15,893,161 members
Articles / Programming Languages / C++

Get Started with QT and Embedded FireBird

,
Rate me:
Please Sign up or sign in to vote.
4.97/5 (24 votes)
14 Dec 2009CPOL6 min read 73.3K   1.3K   57  
This article is the description of the first steps in setting QT, Visual Studio and FireBird.
#include "stdafx.h"
#include "FireBirdDatabase.h"

#include "ibase.h"
#include "iberror.h"

using namespace Model;

QString FireBirdDatabase::qtIBaseDriverName_ = "QIBASE";
QString FireBirdDatabase::connectionString_ = "server type=Embedded; auto_commit=True; auto_commit_level=4096; connection lifetime=1; DataBase=\"%1\"";

#ifdef _DEBUG
QString FireBirdDatabase::qtIBasePluginName_ = "qsqlibased4.dll";
#else
QString FireBirdDatabase::qtIBasePluginName_ = "qsqlibase4.dll";
#endif

FireBirdDatabase::FireBirdDatabase()
    :isInitialized_(false), databasePath_(""), connectionName_(""), instances_(0), lastError_(0)
{
   
}

FireBirdDatabase::~FireBirdDatabase()
{
    

    Close();
}
bool FireBirdDatabase::Initialize()
{
    

    lastError_ = 0;

    if (isInitialized_)
    {
        return true;
    }

    if(!pluginLoader_.isLoaded())
    {
        pluginLoader_.setFileName(QApplication::instance()->applicationDirPath() + QDir::separator() + qtIBasePluginName_);
        
        if (!pluginLoader_.load())
        {            
            //// "Loading SQL Driver failed.";
            isInitialized_ = false;
            return false;
        }
    }

    QObject* object = pluginLoader_.instance();

    if (object == NULL)
    {            
        // "Loading SQL Driver Instance failed.";

        pluginLoader_.unload();
        return false;
    }

    QSqlDriverPlugin* plugin = qobject_cast<QSqlDriverPlugin*>(object);

    if (plugin == NULL)
    {            
        // "QSqlDriverPlugin == NULL");
        pluginLoader_.unload();
        return false;
    }

    driver_ = plugin->create("QIBASE");

    if (driver_ == NULL)
    {            
        // "Loading QIBASE Driver Instance failed.";
        pluginLoader_.unload();
        return false;
    }

    isInitialized_ = true;

    return isInitialized_;
}
bool FireBirdDatabase::Create(const QString& filePath, const QString& userName, const QString& password)
{

    if (!isInitialized_)
    {
        Initialize();
    }

    if (QFile::exists(filePath))
    {
        return false;
    }

    databasePath_ = filePath;

    QString queryString;
    queryString += "CREATE DATABASE";
    queryString += " \'" + filePath + "\'";
    queryString += " USER \'" + userName + "\'";
    queryString += " PASSWORD \'" + password + "\'";
    queryString += " DEFAULT CHARACTER SET UNICODE_FSS";

    ISC_STATUS_ARRAY status;
    isc_db_handle   databaseHandle = NULL;
    isc_tr_handle   transactionHandle = NULL;

    unsigned short g_nFbDialect = SQL_DIALECT_V6;

    if (isc_dsql_execute_immediate(status, &databaseHandle, &transactionHandle, 0, queryString.toStdString().c_str (), g_nFbDialect, NULL))
    {
        long SQLCODE=isc_sqlcode(status);
        return false;
    }



    isc_commit_transaction( status, &transactionHandle );

    if (databaseHandle != NULL)
    {
        ISC_STATUS_ARRAY status;
        isc_detach_database(status, &databaseHandle);
    }

    return true;
}
bool FireBirdDatabase::Open(const QString& filePath, const QString& userName, const QString& password)
{
    

    isInitialized_ = false;
    lastError_ = 0;

    if (!Initialize())
    {
        // "Firebird database is not initialized.";
        return false;
    }

    instances_++;

    connectionName_ = "Connection_1";
            
    QSqlDatabase database; 

    // "Adding database (DRIVER) ";
    database = QSqlDatabase::addDatabase(driver_, connectionName_);

    // "Check Valid database. ";
    if (!database.isValid())
    {
        QString lastError = database.lastError().text();
        // "Database is not valid. LastError = %S", lastError.toStdWString();
        return false;
    }

    // "Set database configurations.";

    database.setDatabaseName(filePath);
    database.setUserName(userName);
    database.setPassword(password);

    QString connectionString = QString(connectionString_).arg(filePath);
    database.setConnectOptions(connectionString);

    bool result = false;
    
    result = database.open();

    if(!result)
    {
        QString lastError = database.lastError().text();        
        lastError_ = (uint)database.lastError().number();        
    }
    return result;
}

bool FireBirdDatabase::Close()
{
    
    try
    {
        lastError_ = 0;

        QSqlDatabase database = QSqlDatabase::database(connectionName_);
        if (database.isValid() && database.isOpen())
        {
            database.close();
        }        
        QSqlDatabase::removeDatabase(connectionName_);
        driver_ = NULL;
    }
    catch (...)
    {
        return false;
    }

    return true;
}
bool FireBirdDatabase::IsOpen()
{
    

    QSqlDatabase database = QSqlDatabase::database(connectionName_);

    if (database.isValid() && database.isOpen())
    {
        return true;
    }

    return false;
}
QSqlQuery Model::FireBirdDatabase::CreateQuery()
{
    return QSqlQuery(QSqlDatabase::database(connectionName_));
}

int Model::FireBirdDatabase::LastError()
{
    return lastError_;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Apriorit Inc.
United States United States
ApriorIT is a software research and development company specializing in cybersecurity and data management technology engineering. We work for a broad range of clients from Fortune 500 technology leaders to small innovative startups building unique solutions.

As Apriorit offers integrated research&development services for the software projects in such areas as endpoint security, network security, data security, embedded Systems, and virtualization, we have strong kernel and driver development skills, huge system programming expertise, and are reals fans of research projects.

Our specialty is reverse engineering, we apply it for security testing and security-related projects.

A separate department of Apriorit works on large-scale business SaaS solutions, handling tasks from business analysis, data architecture design, and web development to performance optimization and DevOps.

Official site: https://www.apriorit.com
Clutch profile: https://clutch.co/profile/apriorit
This is a Organisation

33 members

Written By
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions