SOL9 2.0 Class: ADOXApplet

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

Source code

/*
 * ADOXApplet.h 
 * Copyright (c) 2009 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9
// 2009/06/05

// SQL Client Application base class on ADOX with the SQL Native Client, OLEDB and so on.
// Each ADOX 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 or Access2007 

#include <sol/sql/ADOApplet.h>
#include <sol/sql/ADOXCatalog.h>

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

namespace SOL {

class ADOXApplet: public Object{

private:
    String propertiesFile;

private:
    ADOConnectionProperties properties;

private:
    ADOXCatalog catalog;

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

        const TCHAR* filePath = NULL;

        if (argc >= 2) {
            filePath = argv[1];
        }
        propertiesFile = filePath;
    }

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

        propertiesFile = filePath;
    }

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

public:
    ~ADOXApplet()
    {
        catalog.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:
    /*
     * Connect or create a connection to a catalog using a file name of 
     * filePath(connection,properites file.
     */
    HRESULT openConnection()
    {
        static const char* JET_OLEDB_PASSWORD = "Jet OLEDB:Database Password";

        const TCHAR* filePath = (const TCHAR*)propertiesFile;
        //_tprintf(_T("openConnection Connection.properties file=%s\n"), filePath);

        loadPropertiesFile(filePath);
        //properties.dump();

        StringT<char> constring;
        properties.getString(constring);    
        _bstr_t connectionString = (const char*)constring;
        //printf("ConnectionString =%S\n", (const wchar_t*)connectionString);
        String provider = "";
        bool nativeClient = properties.isProviderSQLNativeClient();
        bool jetOleDB     = properties.isProviderJetOleDB();
        bool aceOleDB     = properties.isProviderAceOleDB();

        String jetOleDBPassword = "";
        properties.getJetOleDBPassword(jetOleDBPassword);
        int jetOleDBPasswordLen = properties.getJetOleDBPasswordLength();
        String isecurity = "";
        String userID   = "";
        String password = "";
            
        properties.getUserID(userID);
        properties.getPassword(password);

        //2009/06/05
        if (properties.getIntegratedSecurity(isecurity) == false) {
            //Need password
            
            if ((jetOleDB || aceOleDB) ) {
                if (jetOleDBPasswordLen>0) {
                    connectionString = connectionString + _bstr_t(JET_OLEDB_PASSWORD)+ _bstr_t("=") + 
                        _bstr_t((const TCHAR*)jetOleDBPassword);
                } 
                if (jetOleDBPasswordLen==0){
                    //Prompt password
                    CredentialsDialog credentialsDialog;
                    if (credentialsDialog.show(userID, password) != NO_ERROR) {
                        throw Exception(E_FAIL, "Login cancelled; %s", __FUNCTION__);    
                    }
                    properties.putProperty(JET_OLEDB_PASSWORD, password);
                    String nconstring;
                    properties.getString(nconstring);    
                    connectionString = (const TCHAR*)nconstring;
                    //printf("Connstring=%s\n", (const char*)connectionString);
                }
                else {
                    ;//printf("Do not do nothing\n");
                }
            } else {
                //In case of SQLNCLI1.0                
                CredentialsDialog credentialsDialog;
                if (credentialsDialog.show(userID, password) != NO_ERROR) {
                    throw Exception(E_FAIL, "Login cancelled; %s", __FUNCTION__);    
                }
                if (nativeClient) {
                    connectionString = connectionString + _bstr_t("User ID=") + _bstr_t((const TCHAR*)userID) + _bstr_t(";");
                    connectionString = connectionString + _bstr_t("Password=") + _bstr_t((const TCHAR*)password);
                }
            }
        }
        
    //    printf("Try to open(connect or create) a catalog with a connectionString:%s\n",
    //        (const char*)connectionString);
            
        HRESULT hr = catalog.open(connectionString);

    //    printf("OK opened a catalog\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()
    {
        catalog.close();
    }


public:
    ADOXCatalog& getCatalog() 
    {
        return catalog;
    }

public:
    ADOConnectionProperties& getConnectionProperties()
    {
        return properties;
    }

public:
    bool getConnectionProerty(const char* name, String& value)
    {
        return properties.getValue(name, value);
    }

public:
    bool getXMLEncoding(String& value)
    {
        //default encoding
        value = "UTF-8";
        return properties.getValue("X_XML_ENCODING", value);
    }

public:
    bool getInitialCatalog(String& initialCatalog)
    {
        return properties.getInitialCatalog(initialCatalog);
    }

public:
    // dump some importantant connection properties
    void showConnectionProperties()
    {
        String provider = "";
        properties.getProvider(provider);
        _tprintf(_T("Provider =\"%s\"\n"), (const TCHAR*)provider);

        String integratedSecurity = "";
        properties.getIntegratedSecurity(integratedSecurity);
        _tprintf(_T("Integrated Security =\"%s\"\n"), (const TCHAR*)integratedSecurity);

        String initialCatalog = "";
        properties.getInitialCatalog(initialCatalog);
        _tprintf(_T("Initial catalog =\"%s\"\n"), (const TCHAR*)initialCatalog);

        String dataSource = "";
        properties.getDataSource(dataSource);
        _tprintf(_T("Data Source =\"%s\"\n"), (const TCHAR*)dataSource);
    }
};

}

Last modified: 19 Dec 2009

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