cplusDBAction 4.2                                                                                        Copyright addoit GmbH 2009

 

 

5.5 Data Organization and Memory Management

 

The cplusDBAction always needs an instance of a User DBTableSet. Picture 5: Minimal required environment for cplusDBAction contains all needed types and objects. Furthermore, in order to work with UNCODE, a character set has to be defined.

01: #include "heaDBSettings.h"

02: #include "heaDBGenType.h"

03:

04: using namespace TableSetDBGenType;

 

... ... ... ... ...

 

08:    //////////////////////////////////////////////////////////////////////////

09:    // Variable declaration

10:    //////////////////////////////////////////////////////////////////////////

11:

12:    // The main reference which will be created by the Open method and

13:    // destroyed by the Close method

14:    TableSetDBGenType::clsDBGenType* pobjDBGenType = NULL;

15:

16:    // Stores all the settings required to make a connection to the database

17:    // as well as to initialize the DBAction framework

18:    DBTableSet::clsDBSettings objDBSettings;

19:

20:    // Holds the current environment

21:    DBEnvironment::clsDBEnvironment objDBEnvironment;

22:

23:    // Will store the return value for the DBAction operations

24:    int iRetVal = DBBaseObject::eRetCodeUnknownError;

25:

26:    // Will contain detailed error information

27:    DBString::clsDBString strDBErrorInfo;

 

Picture 5: Minimal required environment for cplusDBAction

For the cplusDBAction environment to get a new instance out of a User DBTableSet, the following details are required: the name of the library (in our example DBGenType), the version (in our case 1), and the connection details to the database server (see Picture 6:The Initialization of the cplusDBAction Framework).

29:    //////////////////////////////////////////////////////////////////////////

30:    // Initialization

31:    //////////////////////////////////////////////////////////////////////////

32:

33:    // Codec initialization (required for proper UNCODE handling)

34: #ifdef OS_WIN_32

35:    DBEnvironment::clsDBEnvironment::SetSTDWorkingCodec(DBEnvironment::eCodecType_Windows_1252);

36: #else

37:    DBEnvironment::clsDBEnvironment::SetSTDWorkingCodec(DBEnvironment::eCodecType_UTF_8);

38: #endif // OS_WIN_32

39:

40:    // Initializing the connection settings

41:    objDBSettings.SetHostName("localhost");

42:    objDBSettings.SetPort(1433);

43:    objDBSettings.SetUserName("sa");

44:    objDBSettings.SetPassword("password");

45:    objDBSettings.SetSchemaName("dbo");

46:    objDBSettings.SetDatabaseName("GetType");

47:

48:    // Configure the database type

49:    objDBSettings.SetDriverType(DBEnvironment::eDBODBC_MSSQL);

50:

51:    // configure the DBTableSet which will be used

52:    // WARNING: On LINUX, this name is case sensitive!

53:    objDBSettings.SetTableSetName("DBGenType");

54:

55:    // configure the version of the table set which should be used (in our case 1)

56:    objDBSettings.SetTableSetVersion(1);

Picture 6:The Initialization of the cplusDBAction Framework

Using the TableSetName and TableSetVersion, the ClassFactory will first locate the library DBGenType_v1.dll (or libDBGenType_v1.so under Linux). Once found, a new instance of the clsDBGenType class will be created. All these operations happen as a result of the Open() method call shown below.

WARNING: When a Debug Version is launched, the framework searches for a library named “DBGenType_v1d.dll” (or “libDBGenType_v1d.so” under Linux).

 

 

060:    //now we are ready to start the session

061:    DBString::clsDBString strDBErrorInfo;

062:    int iRetVal = clsDBGenType::Open(&objDBSettings,

063:                                     &objDBEnvironment,

064:                                     &m_pDBGenType,

065:                                     strDBErrorInfo);

066:

067:    bool succeeded = (iRetVal >= DBBaseObject::eRetCodeSuccess);

026:    //here we close the session

027:    //while closing every object will be automatically destroyed

028:    //at the end pobjDBGenType should be NULL

029:    DBString::clsDBString strDBErrorInfo;

030:    int iRetVal = clsDBGenType::Close(  &objDBEnvironment,

031:                                        &m_pDBGenType,

032:                                        strDBErrorInfo);

033:

034:    bool succeeded = (iRetVal >= DBBaseObject::eRetCodeSuccess);

Picture 7: Opening and Closing of a cplusDBAction Session

As soon as the code from Picture 7: Opening and Closing of a cplusDBAction Session successfully opens a session (line 67), we can use the User DBTableSet through the „pobjDBGenType” pointer of type clsDBGenType. This instance (pobjDBGenType) contains all the methods and properties needed for the creation and the control of the DDL, DML und DQL Statements.

 

068: void clsSampleGroupSelect::Sample_Iterate_on_DeclarationType()

069: {

070:    std::stringstream text;

071:

072:    // Perform a simple select on the "DeclarationType" table belonging to

073:    // the "m_pDBGenType" Table Set

074:    clsCollDeclarationType* pCollDeclType = clsDeclarationType::Select(m_pDBGenType);

075:

076:    if (pCollDeclType != NULL)

077:    {

078:       // iterate on the items collection and get the DeclarationType field

079:       while (pCollDeclType->MoveNext())

080:       {

081:          clsDeclarationType* pDeclType = pCollDeclType->Current();

082:          text << "Item: " << pDeclType->DB_DeclarationType() << std::endl;

083:       }

084:    }

085:

086:    DisplayResults(pCollDeclType, &text);

087: }

Picture 8: A Simple Select Query on a Table

As shown in  Picture 8: A Simple Select Query on a Table, the source code on row 74 reads out the data from the DeclarationType table. Please note that no SQL statement was needed to achieve this. After the evaluation of the returned error code (must always be >= 0), you can iterate through the returned container (objCollDeclarationType) using its internal cursor.

Related Topics

5 cplusDBAction Functionality
5.5.1 Address Space in a User DBTableSet
5.5.2 Rules for Working with TableRefPtr and Internal Cache


Copyright (c) 1998-2009 addoit GmbH, All Rights Reserved.

www.addoit.com                                                                                                          Page 1 of 52