File: classDataSource.cpp

package info (click to toggle)
unixodbc 2.2.14p2-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 14,620 kB
  • sloc: ansic: 104,255; cpp: 38,573; sh: 15,958; makefile: 2,728; sql: 1
file content (168 lines) | stat: -rw-r--r-- 5,013 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**************************************************
 *
 *
 **************************************************
 * This code was created by Peter Harvey @ CodeByDesign.
 * Released under GPL 18.FEB.99
 *
 * Contributions from...
 * -----------------------------------------------
 * Peter Harvey         - pharvey@codebydesign.com
 **************************************************/

#include "classDataSource.h"
#include "classLogin.h"
#ifdef QT_V4LAYOUT
#include "Qt/qpixmap.h"
#else
#include "qpixmap.h"
#endif
#include <ini.h>
#include <odbcinst.h>
#include "computergreen.xpm"
#include "computerred.xpm"

#ifdef QT_V4LAYOUT
classDataSource::classDataSource( Q3ListViewItem *pParent, Q3ListViewItem *pAfter, classCanvas *pCanvas, classODBC::DSType dataSourceType, const char *pszDataSourceName )
#else
classDataSource::classDataSource( QListViewItem *pParent, QListViewItem *pAfter, classCanvas *pCanvas, classODBC::DSType dataSourceType, const char *pszDataSourceName )
#endif
    : classNode( pParent, pAfter, pCanvas ), env(), dbc( env(), "classDataSource" ), pConnectionFrame( NULL )
{
  listLibraries.setAutoDelete( TRUE );
  char szResults[9600];
  char szDescription[INI_MAX_PROPERTY_VALUE+1];
  char szDriver[INI_MAX_PROPERTY_VALUE+1];
  QString qsDescription;

  if ( pszDataSourceName != 0 )
    qsDataSourceName = pszDataSourceName;

  setPixmap( 0, QPixmap( computerred_xpm ) );
  setExpandable( TRUE );

  szResults[0]     = '\0';
  szDescription[0] = '\0';
  szDriver[0]      = '\0';
  if ( dataSourceType == classODBC::User )
    SQLSetConfigMode( ODBC_USER_DSN );
  else
    SQLSetConfigMode( ODBC_SYSTEM_DSN );

  if ( SQLGetPrivateProfileString((char*) qsDataSourceName.ascii(), "Description", "", szResults, sizeof(szResults), 0 ) > 0 )
    iniElement( szResults, '\0', '\0', 0, szDescription, INI_MAX_PROPERTY_VALUE );
  if ( SQLGetPrivateProfileString((char*) qsDataSourceName.ascii(), "Driver", "", szResults, sizeof(szResults), 0 ) > 0 )
    iniElement( szResults, '\0', '\0', 0, szDriver, INI_MAX_PROPERTY_VALUE );

  SQLSetConfigMode( ODBC_BOTH_DSN );

  qsDescription.sprintf( "%s [%s]", szDescription, szDriver );
  setText( 0, qsDataSourceName );
  setText( 1, "DATASOURCE  " );
  setText( 2, qsDescription );
}

classDataSource::~classDataSource()
{
  delete pConnectionFrame ;
  pConnectionFrame = NULL ;
}

void classDataSource::setOpen( bool bOpen )
{
  if ( bOpen && !dbc.active() ) // Always refresh
  {
    classLogin login( pCanvas, dbc, qsDataSourceName );
    if ( login.exec() )
    {
      setPixmap( 0, QPixmap( computergreen_xpm ) );
      LoadLibraries();
      if ( !pConnectionFrame )
        pConnectionFrame = new classConnectionFrame( dbc(), qsDataSourceName, pCanvas );
      pConnectionFrame->show();
    }
  }
  else if ( !bOpen && dbc.active() )
  {
    listLibraries.clear();
    delete pConnectionFrame;
    pConnectionFrame = NULL;
    // disconnect
    dbc.disconnect() ;
    setPixmap( 0, QPixmap( computerred_xpm ) );
  }
#ifdef QT_V4LAYOUT
  Q3ListViewItem::setOpen( bOpen );
#else
  QListViewItem::setOpen( bOpen );
#endif
}

void classDataSource::LoadLibraries()
{
  CursorScoper s(listView()) ;
  SQLRETURN    nReturn ;
  SQLCHAR      szLibraryName[MAX_COLUMN_WIDTH];
  SQLLEN       nIndicatorName;
  classTables  *pLibrary = NULL;

  // CREATE A STATEMENT
  StatementScoper stmt( dbc() ) ; if ( !stmt() ) return ;

  // EXECUTE OUR SQL/CALL
  if (!SQL_SUCCEEDED(nReturn=SQLTables( stmt(), (SQLCHAR*)"", SQL_NTS, (SQLCHAR*)"%", SQL_NTS, (SQLCHAR*)"", SQL_NTS, 0, 0 ) ) )
    return my_msgBox( "classDataSource", "SQLTables", nReturn, NULL, NULL, stmt() ) ;

  SQLBindCol( stmt(), SQLTables_TABLE_SCHEM, SQL_C_CHAR, szLibraryName, sizeof(szLibraryName), &nIndicatorName );

  // GET RESULTS
  
  int first = 1;
  SQLCHAR last_name[ MAX_COLUMN_WIDTH ];
  
  while ( SQL_SUCCEEDED(SQLFetch( stmt() ) ) )
  {
    if ( nIndicatorName != SQL_NULL_DATA )
    {
        // remove duplicate rows with driver such as Postgres
        if ( first )
        {
            first = 0;
            strcpy((char *) last_name,(char *) szLibraryName );
        }
        else
        {
            if ( strcmp((char *) last_name,(char *) szLibraryName ) == 0 )
            {
                continue;
            }
            else
            {
                strcpy((char *) last_name,(char *) szLibraryName );
            }
        }

        listLibraries.append( pLibrary = new classTables( this, pLibrary, pCanvas, dbc(), QString((char*)szLibraryName).stripWhiteSpace() ) );
    }
  }
}

#ifdef QT_V4LAYOUT
void classDataSource::selectionChanged( Q3ListViewItem *p )
#else
void classDataSource::selectionChanged( QListViewItem *p )
#endif
{

  for ( classTables *pLibrary = listLibraries.first(); pLibrary != 0; pLibrary = listLibraries.next() )
    pLibrary->selectionChanged( p );

  if ( dbc.active() )
  {
    if ( p == this || p->parent() == this ) // Allow any library also
      pConnectionFrame->show();
    else
      pConnectionFrame->hide();
  }
}