SOL9 2.0 Class: ADOApplet

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * ADOApplet.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9
// 2009/05/20

// SQL Client Application base class on ADO with the SQL Native Client.
// Each ADO based application can be dervied from this base class
// by implementing a virtual run() method in each subclass.

// Assumes that environment of SQL Server 2008 and SQLClient1.0 

// 2009/05/25
// Modified to allow argc >=2 in contructor ADOApplet(int argc, const char** argv), 
// but anyway argv[1] should be a file name of Connection.properties

#include <sol/sql/ADOConnection.h>
#include <sol/sql/ADOField.h>
#include <sol/sql/ADORecord.h>
#include <sol/sql/ADOParameter.h>
#include <sol/sql/ADOParameters.h>
#include <sol/sql/ADOField.h>
#include <sol/sql/ADOFields.h>
#include <sol/sql/ADORecordset.h>
#include <sol/sql/ADOCommand.h>
#include <sol/sql/ADOStream.h>

// This should be included here to show CredentialsDialog
#include <sol/CredentialsDialog.h>

namespace SOL {

class ADOApplet: public Object{

private:
    String propertiesFile;

private:
    ADOConnectionProperties properties;

private:
    ADOConnection connection;

public:
    /**
     * Constructor
     * 1 Call OleInitialize()
     * 2 Parser argc and argv parameters and extract a filePath.
     * 3 Create an instance of ADOConenciton.
     * 4 Open a connection to a db using a file name of filePath(connection,properites file.
     *
     * @param argc A count of arguments from a commandline .
     * @param argv An Array of strings of parameters from a commandline
     *
     */
    ADOApplet(int argc, const TCHAR** argv)
    {
        HRESULT hr = OleInitialize(NULL);
        if (FAILED(hr)) {
            throw Exception(hr, "%s: %s\n", "OleInitialize", __FUNCTION__);
        }
        connection.createInstance();

        const TCHAR* filePath = NULL;
        //if (argc == 2) {
        //2009/05/25
        if (argc >= 2) {
            filePath = argv[1];
        }
        propertiesFile = filePath;
    }

public:
    /**
     * Construction
     * Create an instance of ADOConenciton.
     * Open a connection to a db using a file name of filePath(connection,properites file.
     *
     * @param filePath    A file path to a connection.properties file.
     *
     */
    ADOApplet(const TCHAR* filePath)
    {
        HRESULT hr = OleInitialize(NULL);
        if (FAILED(hr)) {
            throw Exception(hr, "%s: %s\n", "OleInitialize", __FUNCTION__);
        }
        connection.createInstance();

        propertiesFile = filePath;
    }

public:
    ADOApplet()
    {
        HRESULT hr = OleInitialize(NULL);
        if (FAILED(hr)) {
            throw Exception(hr, "%s: %s\n", "OleInitialize", __FUNCTION__);
        }
        connection.createInstance();
    }

public:
    ~ADOApplet()
    {
        connection.close();

        OleUninitialize();
    }

public:
    virtual void start() 
    {
        try {
            openConnection();
            
            //Call virtual main procedure.
            run();
            
            closeConnection();

        } catch(HRESULT hr){
            printf("Exception hr=%x\n", hr);
        } catch( _com_error &e)  {
            COMError error(e);
            error.dump();
        } catch(Exception& ex){
            ex.dump();
        } catch(...) {
            printf("Exception:Unknown\n");
        }
    }

private:
    bool loadPropertiesFile(const TCHAR* propertiesFile)
    {
        bool rc = false;
        TCHAR filePath[1024];
        memset(filePath, (TCHAR)0, SizeOf(filePath));

        const TCHAR* DEFAULT_PROPERTIES_FILE_NAME = _T("Connection.properties"); 
        if (propertiesFile == NULL) {
            TCHAR module[_MAX_PATH];
            memset(module, (TCHAR)0, SizeOf(module));
            GetModuleFileName(NULL, module, _MAX_PATH);
            TCHAR* dot = strrchr(module, '.');
            if (dot) {
                *dot = '\0';    //NULL terminated hered
            }
            _stprintf(filePath, _T("%s.%s"), module, DEFAULT_PROPERTIES_FILE_NAME);
            if (GetFileAttributes(filePath) == 0xffffffff) {
                throw Exception(E_INVALIDARG, "%s: %s: %s",  filePath, "File not found", __FUNCTION__); 
            }
        } else {
            _stprintf(filePath, _T("%s"), propertiesFile);
        }
        if (GetFileAttributes(filePath) == 0xffffffff) {
            throw Exception(E_INVALIDARG, "%s:%s: %s",  filePath, "File not found", __FUNCTION__); 
        } else {
            rc = properties.load(filePath);
        }
        return rc;
    }

public:
    HRESULT openConnection()
    {
        const TCHAR* filePath = (const TCHAR*)propertiesFile;
        //printf("openConnection Connection.properties file=%s\n", filePath);

        loadPropertiesFile(filePath);
    
        String constring;
        properties.getString(constring);    
        _bstr_t connectionString = (const TCHAR*)constring;
        connection.putConnectionString(connectionString);
    
        String isecurity = "";
        String userID   = "";
        String password = "";

        if (properties.getIntegratedSecurity(isecurity) == false) {
            properties.getUserID(userID);
            properties.getPassword(password);
            CredentialsDialog credentialsDialog;
            if (credentialsDialog.show(userID, password) != NO_ERROR) {
                throw Exception(E_FAIL, "Login cancelled; %s", __FUNCTION__);    
            }
        } 
        //printf("Try to open a db with a connectionString:%s\n",
        //    (const TCHAR*)connection.getConnectionString());
        HRESULT hr = connection.open("", (const TCHAR*)userID, (const TCHAR*)password);
        //printf("OK opened a db\n");
        return hr;
    }


public:
    // This is a virtual fuction. 
    //You have to re-define this method on your own subclass.
    virtual void run()
    {
        // Implement on each subclass
    }

public:
    void closeConnection()
    {
        connection.close();
    }


public:
    ADOConnection& getConnection() 
    {
        return connection;
    }

public:
    bool getErrors(ADOErrors& errors)
    {
        return connection.getErrors(errors);
    }
};

}

Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.