summaryrefslogtreecommitdiff
path: root/storage/ndb/src/old_files/client/odbc/handles/HandleDesc.cpp
blob: 4cff1bb88927b125aa5c8400ff535d5be3965cd1 (plain)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include <common/OdbcData.hpp>
#include <common/DiagArea.hpp>
#include <common/DataType.hpp>
#include "HandleRoot.hpp"
#include "HandleDbc.hpp"
#include "HandleDesc.hpp"

HandleDesc::HandleDesc(HandleDbc* pDbc) :
    m_dbc(pDbc),
    m_descArea(this, m_descSpec)
{
}

HandleDesc::~HandleDesc()
{
}

void
HandleDesc::ctor(Ctx& ctx)
{
}

void
HandleDesc::dtor(Ctx& ctx)
{
}

// allocate and free handles (no valid case)

void
HandleDesc::sqlAllocHandle(Ctx& ctx, SQLSMALLINT childType, HandleBase** ppChild)
{
    ctx.pushStatus(Sqlstate::_HY092, Error::Gen, "inappropriate handle type");
}

void
HandleDesc::sqlFreeHandle(Ctx& ctx, SQLSMALLINT childType, HandleBase* ppChild)
{
    ctx.pushStatus(Sqlstate::_HY092, Error::Gen, "inappropriate handle type");
}

// set and get descriptor values

void
HandleDesc::sqlSetDescField(Ctx& ctx, SQLSMALLINT recNumber, SQLSMALLINT fieldIdentifier, SQLPOINTER value, SQLINTEGER bufferLength)
{
    const DescSpec& spec = m_descArea.findSpec(fieldIdentifier);
    if (spec.m_pos == Desc_pos_end) {
	ctx.pushStatus(Sqlstate::_HY091, Error::Gen, "invalid descriptor id %d", (int)fieldIdentifier);
	return;
    }
    OdbcData data;
    data.copyin(ctx, spec.m_type, value, bufferLength);
    if (! ctx.ok())
	return;
    const bool header = (spec.m_pos == Desc_pos_header);
    const bool record = (spec.m_pos == Desc_pos_record);
    ctx_assert(header || record);
    DescArea& area = m_descArea;
    if (header) {
	area.getHeader().setField(ctx, fieldIdentifier, data);
    }
    if (record) {
	if (recNumber < 0) {
	    ctx.pushStatus(Sqlstate::_07009, Error::Gen, "invalid record number %d", (int)recNumber);
	    return;
	}
	if (recNumber == 0) {	// bookmark record
	    if (area.getUsage() == Desc_usage_IPD) {
		ctx.pushStatus(Sqlstate::_07009, Error::Gen, "cannot set bookmark IPD");
		return;
	    }
	    if (area.getUsage() == Desc_usage_APD) {
		ctx.pushStatus(Sqlstate::_07009, Error::Gen, "cannot set bookmark APD");
		return;
	    }
	}
	area.getRecord(recNumber).setField(ctx, fieldIdentifier, data);
    }
}

void
HandleDesc::sqlGetDescField(Ctx& ctx, SQLSMALLINT recNumber, SQLSMALLINT fieldIdentifier, SQLPOINTER value, SQLINTEGER bufferLength, SQLINTEGER* stringLength, SQLSMALLINT* stringLength2)
{
    const DescSpec& spec = m_descArea.findSpec(fieldIdentifier);
    if (spec.m_pos == Desc_pos_end) {
	ctx.pushStatus(Sqlstate::_HY091, Error::Gen, "invalid descriptor id %d", (int)fieldIdentifier);
	return;
    }
    const bool header = (spec.m_pos == Desc_pos_header);
    const bool record = (spec.m_pos == Desc_pos_record);
    ctx_assert(header || record);
    DescArea& area = m_descArea;
    OdbcData data;
    if (header) {
	area.getHeader().getField(ctx, fieldIdentifier, data);
	if (! ctx.ok())
	    return;
    }
    if (record) {
	if (recNumber < 0) {
	    ctx.pushStatus(Sqlstate::_07009, Error::Gen, "invalid record number %d", (int)recNumber);
	    return;
	}
	if (recNumber == 0) {	// bookmark record
	    if (area.getUsage() == Desc_usage_IPD) {
		ctx.pushStatus(Sqlstate::_07009, Error::Gen, "cannot get bookmark IPD");
		return;
	    }
	    if (area.getUsage() == Desc_usage_IRD) {
		// XXX check SQL_ATTR_USE_BOOKMARK != SQL_UB_OFF
	    }
	}
	if ((unsigned)recNumber > area.getCount()) {
	    ctx.setCode(SQL_NO_DATA);
	    return;
	}
	area.getRecord(recNumber).getField(ctx, fieldIdentifier, data);
	if (! ctx.ok())
	    return;
    }
    // if no data, return success and undefined value
    if (data.type() == OdbcData::Undef)
	return;
    data.copyout(ctx, value, bufferLength, stringLength, stringLength2);
}

void
HandleDesc::sqlColAttribute(Ctx& ctx, SQLUSMALLINT columnNumber, SQLUSMALLINT fieldIdentifier, SQLPOINTER characterAttribute, SQLSMALLINT bufferLength, SQLSMALLINT* stringLength, SQLPOINTER numericAttribute)
{
    ctx_log3(("sqlColAttribute col=%d id=%d", columnNumber, fieldIdentifier));
    if (fieldIdentifier == SQL_COLUMN_LENGTH) {		// XXX iODBC workaround
	fieldIdentifier = SQL_DESC_LENGTH;
    }
    if (fieldIdentifier == 1205 || fieldIdentifier == 1206) {
	ctx_log2(("ignore unknown OSQL fieldIdentifier %d", (int)fieldIdentifier));
	if (characterAttribute != 0)
	    *(char*)characterAttribute = 0;
	if (stringLength != 0)
	    *stringLength = 0;
	return;
    }
    const DescSpec& spec = m_descArea.findSpec(fieldIdentifier);
    if (spec.m_pos == Desc_pos_end) {
	ctx.pushStatus(Sqlstate::_HY091, Error::Gen, "invalid descriptor id %d", (int)fieldIdentifier);
	return;
    }
    if (spec.m_type == OdbcData::Sqlchar || spec.m_type == OdbcData::Sqlstate)
	sqlGetDescField(ctx, columnNumber, fieldIdentifier, characterAttribute, bufferLength, 0, stringLength);
    else {
	sqlGetDescField(ctx, columnNumber, fieldIdentifier, numericAttribute, -1, 0);
    }
    if (ctx.getCode() == SQL_NO_DATA) {
	ctx.setCode(SQL_SUCCESS);
	ctx.pushStatus(Sqlstate::_07009, Error::Gen, "invalid column number %d", (int)columnNumber);
    }
}

void
HandleDesc::sqlColAttributes(Ctx& ctx, SQLUSMALLINT icol, SQLUSMALLINT fdescType, SQLPOINTER rgbDesc, SQLSMALLINT cbDescMax, SQLSMALLINT* pcbDesc, SQLINTEGER* pfDesc)
{
    ctx_log3(("sqlColAttributes col=%hu id=%hu", icol, fdescType));
    SQLUSMALLINT columnNumber = icol;
    SQLUSMALLINT fieldIdentifier;
    // XXX incomplete
    if (fdescType == SQL_COLUMN_TYPE) {
	fieldIdentifier = SQL_DESC_TYPE;
    } else if (fdescType == SQL_COLUMN_PRECISION) {
	SQLSMALLINT type;
	sqlGetDescField(ctx, columnNumber, SQL_DESC_TYPE, static_cast<SQLPOINTER>(&type), -1, 0);
	if (! ctx.ok())
	    return;
	switch (type) {
	case SQL_CHAR:
	case SQL_VARCHAR:
	case SQL_BINARY:
	case SQL_VARBINARY:
	case SQL_LONGVARCHAR:
	case SQL_LONGVARBINARY:
	case SQL_DATE:
	    fieldIdentifier = SQL_DESC_LENGTH;
	    break;
	default:
	    fieldIdentifier = SQL_DESC_PRECISION;
	    break;
	}
    } else if (fdescType == SQL_COLUMN_SCALE) {
	SQLSMALLINT type;
	sqlGetDescField(ctx, columnNumber, SQL_DESC_TYPE, static_cast<SQLPOINTER>(&type), -1, 0);
	if (! ctx.ok())
	    return;
	switch (type) {
	default:
	    fieldIdentifier = SQL_DESC_SCALE;
	    break;
	}
    } else if (fdescType == SQL_COLUMN_LENGTH) {
	SQLSMALLINT type;
	sqlGetDescField(ctx, columnNumber, SQL_DESC_TYPE, static_cast<SQLPOINTER>(&type), -1, 0);
	if (! ctx.ok())
	    return;
	switch (type) {
	default:
	    fieldIdentifier = SQL_DESC_LENGTH;
	    break;
	}
    } else {
	fieldIdentifier = fdescType;
    }
    sqlColAttribute(ctx, columnNumber, fieldIdentifier, rgbDesc, cbDescMax, pcbDesc, pfDesc);
}

// set and get several common descriptor values

void
HandleDesc::sqlSetDescRec(Ctx& ctx, SQLSMALLINT recNumber, SQLSMALLINT type, SQLSMALLINT subType, SQLINTEGER length, SQLSMALLINT precision, SQLSMALLINT scale, SQLPOINTER data, SQLINTEGER* stringLength, SQLINTEGER* indicator)
{
    sqlSetDescField(ctx, recNumber, SQL_DESC_TYPE, reinterpret_cast<SQLPOINTER>(type), -1);
    sqlSetDescField(ctx, recNumber, SQL_DESC_DATETIME_INTERVAL_CODE, reinterpret_cast<SQLPOINTER>(subType), -1);
    sqlSetDescField(ctx, recNumber, SQL_DESC_OCTET_LENGTH, reinterpret_cast<SQLPOINTER>(length), -1);
    sqlSetDescField(ctx, recNumber, SQL_DESC_PRECISION, reinterpret_cast<SQLPOINTER>(precision), -1);
    sqlSetDescField(ctx, recNumber, SQL_DESC_SCALE, reinterpret_cast<SQLPOINTER>(scale), -1);
    sqlSetDescField(ctx, recNumber, SQL_DESC_DATA_PTR, data, -1);
    sqlSetDescField(ctx, recNumber, SQL_DESC_OCTET_LENGTH_PTR, reinterpret_cast<SQLPOINTER>(stringLength), -1);
    sqlSetDescField(ctx, recNumber, SQL_DESC_INDICATOR_PTR, reinterpret_cast<SQLPOINTER>(indicator), -1);
}

void
HandleDesc::sqlGetDescRec(Ctx& ctx, SQLSMALLINT recNumber, SQLCHAR* name, SQLSMALLINT bufferLength, SQLSMALLINT* stringLength, SQLSMALLINT* type, SQLSMALLINT* subType, SQLINTEGER* length, SQLSMALLINT* precision, SQLSMALLINT* scale, SQLSMALLINT* nullable)
{
    sqlGetDescField(ctx, recNumber, SQL_DESC_NAME, reinterpret_cast<SQLPOINTER>(name), bufferLength, 0, stringLength);
    sqlGetDescField(ctx, recNumber, SQL_DESC_TYPE, reinterpret_cast<SQLPOINTER>(type), -1, 0);
    sqlGetDescField(ctx, recNumber, SQL_DESC_DATETIME_INTERVAL_CODE, reinterpret_cast<SQLPOINTER>(subType), -1, 0);
    sqlGetDescField(ctx, recNumber, SQL_DESC_OCTET_LENGTH, reinterpret_cast<SQLPOINTER>(length), -1, 0);
    sqlGetDescField(ctx, recNumber, SQL_DESC_PRECISION, reinterpret_cast<SQLPOINTER>(precision), -1, 0);
    sqlGetDescField(ctx, recNumber, SQL_DESC_SCALE, reinterpret_cast<SQLPOINTER>(scale), -1, 0);
    sqlGetDescField(ctx, recNumber, SQL_DESC_NULLABLE, reinterpret_cast<SQLPOINTER>(nullable), -1, 0);
}