File: SQLSpecialColumns.c

package info (click to toggle)
unixodbc 2.2.11-16
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 17,332 kB
  • ctags: 12,399
  • sloc: ansic: 116,624; cpp: 29,333; sh: 25,024; makefile: 3,002; lex: 241; yacc: 182; perl: 142; sed: 16; sql: 1
file content (168 lines) | stat: -rwxr-xr-x 5,178 bytes parent folder | download | duplicates (6)
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
/********************************************************************
 * SQLSpecialColumns
 *
 **********************************************************************
 *
 * This code was created by Peter Harvey (mostly during Christmas 98/99).
 * This code is LGPL. Please ensure that this message remains in future
 * distributions and uses of this code (thats about all I get out of it).
 * - Peter Harvey pharvey@codebydesign.com
 *
 ********************************************************************/

#include "driver.h"

enum nSQLSpecialColumns
{
	COLUMN_NAME		= 1,
	COL_MAX
};

m_field aSQLSpecialColumns[] =
{
	"",					"",		0,			0,	0,
	"COLUMN_NAME",		"sys", CHAR_TYPE,	50,	0
};
	

/* THIS IS CORRECT...
SQLRETURN SQLSpecialColumns(  SQLHSTMT        hDrvStmt,
									SQLSMALLINT     nColumnType,
									SQLCHAR         *szCatalogName,
									SQLSMALLINT     nCatalogNameLength,
									SQLCHAR         *szSchemaName,
									SQLSMALLINT     nSchemaNameLength,
									SQLCHAR         *szTableName,
									SQLSMALLINT     nTableNameLength,
									SQLSMALLINT     nScope,
									SQLSMALLINT     nNullable )
THIS WORKS... */
SQLRETURN SQLSpecialColumns(  SQLHSTMT        hDrvStmt,
									UWORD     nColumnType,
									UCHAR         *szCatalogName,
									SWORD     nCatalogNameLength,
									UCHAR         *szSchemaName,
									SWORD     nSchemaNameLength,
									UCHAR         *szTableName,
									SWORD     nTableNameLength,
									UWORD     nScope,
									UWORD     nNullable )
{
    HDRVSTMT hStmt	= (HDRVSTMT)hDrvStmt;
	COLUMNHDR	*pColumnHeader;			
	int			nColumn;
	long		nCols;
	long		nRow;
	long		nCurRow;

	/* SANITY CHECKS */
    if( hStmt == SQL_NULL_HSTMT )
        return SQL_INVALID_HANDLE;

    if ( !szTableName )
        return SQL_INVALID_HANDLE;

	sprintf( hStmt->szSqlMsg, "hStmt = $%08lX", hStmt );
    logPushMsg( hStmt->hLog, __FILE__, __FILE__, __LINE__, LOG_WARNING, LOG_WARNING, hStmt->szSqlMsg );

	/* validate nColumnType */
	switch ( nColumnType )
	{
	case SQL_BEST_ROWID:
		break;
	case SQL_ROWVER:
	default:
		logPushMsg( hStmt->hLog, __FILE__, __FILE__, __LINE__, LOG_WARNING, LOG_WARNING, "SQL_ERROR This function not supported" );
		return SQL_ERROR;
	}
	
	/* validate nScope */
	switch ( nScope )
	{
	case SQL_SCOPE_CURROW:
	case SQL_SCOPE_TRANSACTION:
	case SQL_SCOPE_SESSION:
		break;
	default:
		logPushMsg( hStmt->hLog, __FILE__, __FILE__, __LINE__, LOG_WARNING, LOG_WARNING, "SQL_ERROR This function not supported" );
		return SQL_ERROR;
	}

	/* validate nNullable */
	switch ( nNullable )
	{
	case SQL_NO_NULLS:
	case SQL_NULLABLE:
		break;
	default:
		logPushMsg( hStmt->hLog, __FILE__, __FILE__, __LINE__, LOG_WARNING, LOG_WARNING, "SQL_ERROR This function not supported" );
		return SQL_ERROR;
	}

	/* only one scenario supported: nColumnType=SQL_BEST_ROWID and nScope=ignored nNullable=ignored */

    /**************************
	 * close any existing result
     **************************/
	if ( hStmt->hStmtExtras->aResults )
		_FreeResults( hStmt->hStmtExtras );

	if ( hStmt->pszQuery != NULL )
		free( hStmt->pszQuery );

	hStmt->pszQuery							= NULL;

    /**************************
	 * allocate memory for columns headers and result data (row 0 is column header while col 0 is reserved for bookmarks)
     **************************/
	hStmt->hStmtExtras->nCols 		= COL_MAX-1;
	hStmt->hStmtExtras->nRows 		= 1;
    hStmt->hStmtExtras->nRow		= 0;
	hStmt->hStmtExtras->aResults 	=  malloc( sizeof(char*) * (hStmt->hStmtExtras->nRows+1) * (hStmt->hStmtExtras->nCols+1) );
	memset( hStmt->hStmtExtras->aResults, 0, sizeof(char*) * (hStmt->hStmtExtras->nRows+1) * (hStmt->hStmtExtras->nCols+1) );
	if ( hStmt->hStmtExtras->aResults == NULL )
	{
		logPushMsg( hStmt->hLog, __FILE__, __FILE__, __LINE__, LOG_WARNING, LOG_WARNING, "Not enough memory. (malloc failed)" );
        hStmt->hStmtExtras->nRows = 0;
        hStmt->hStmtExtras->nCols = 0;	
        return SQL_ERROR;
	}

    /**************************
	 * gather column header information (save col 0 for bookmarks)
     **************************/
	for ( nColumn = 1; nColumn < COL_MAX; nColumn++ )
	{
		(hStmt->hStmtExtras->aResults)[nColumn] = malloc( sizeof(COLUMNHDR) );
		pColumnHeader = (COLUMNHDR*)(hStmt->hStmtExtras->aResults)[nColumn];
		memset( pColumnHeader, 0, sizeof(COLUMNHDR) );
		_NativeToSQLColumnHeader( pColumnHeader, &(aSQLSpecialColumns[nColumn]) );
	}

	/************************
	 * gather data (save col 0 for bookmarks and factor out index columns)
	 ************************/
	nCols 						= hStmt->hStmtExtras->nCols;
	hStmt->hStmtExtras->nRow	= 0;
    nCurRow 					= 1;
	hStmt->hStmtExtras->nRow++;
	nRow						= hStmt->hStmtExtras->nRow;
	for ( nColumn = 1; nColumn < COL_MAX; nColumn++ )
	{
		switch ( nColumn )
		{
		case COLUMN_NAME:
			(hStmt->hStmtExtras->aResults)[nRow*nCols+nColumn] = (char*)strdup( "_rowid" );
			break;
		default:
			(hStmt->hStmtExtras->aResults)[nRow*nCols+nColumn] = NULL;
		} /* switch nColumn */
	} /* for nColumn */
	hStmt->hStmtExtras->nRow  = 0;

    logPushMsg( hStmt->hLog, __FILE__, __FILE__, __LINE__, LOG_INFO, LOG_INFO, "SQL_SUCCESS" );
	return SQL_SUCCESS;
}