summaryrefslogtreecommitdiffstats
path: root/connectivity/source/drivers/odbc/OResultSetMetaData.cxx
blob: 21b95c6a7b298359a0afc69f1853898027b61f3f (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <odbc/OResultSetMetaData.hxx>
#include <odbc/OTools.hxx>

using namespace connectivity::odbc;
using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::sdbc;


OResultSetMetaData::~OResultSetMetaData()
{
}

OUString OResultSetMetaData::getCharColAttrib(sal_Int32 _column,sal_Int32 ident)
{
    sal_Int32 column = _column;
    if(_column <static_cast<sal_Int32>(m_vMapping.size())) // use mapping
        column = m_vMapping[_column];

    SQLSMALLINT BUFFER_LEN = 128;
    std::unique_ptr<char[]> pName(new char[BUFFER_LEN+1]);
    SQLSMALLINT nRealLen=0;
    SQLRETURN nRet = N3SQLColAttribute(m_aStatementHandle,
                                    static_cast<SQLUSMALLINT>(column),
                                    static_cast<SQLUSMALLINT>(ident),
                                    static_cast<SQLPOINTER>(pName.get()),
                                    BUFFER_LEN,
                                    &nRealLen,
                                    nullptr
                                    );
    OUString sValue;
    if ( nRet == SQL_SUCCESS )
    {
        if ( nRealLen < 0 )
            nRealLen = BUFFER_LEN;
        sValue = OUString(pName.get(),nRealLen,m_pConnection->getTextEncoding());
    }
    pName.reset();
    OTools::ThrowException(m_pConnection,nRet,m_aStatementHandle,SQL_HANDLE_STMT,*this);
    if(nRealLen > BUFFER_LEN)
    {
        pName.reset(new char[nRealLen+1]);
        nRet = N3SQLColAttribute(m_aStatementHandle,
                                    static_cast<SQLUSMALLINT>(column),
                                    static_cast<SQLUSMALLINT>(ident),
                                    static_cast<SQLPOINTER>(pName.get()),
                                    nRealLen,
                                    &nRealLen,
                                    nullptr
                                    );
        if ( nRet == SQL_SUCCESS && nRealLen > 0)
            sValue = OUString(pName.get(),nRealLen,m_pConnection->getTextEncoding());
        OTools::ThrowException(m_pConnection,nRet,m_aStatementHandle,SQL_HANDLE_STMT,*this);
    }

    return  sValue;
}

SQLLEN OResultSetMetaData::getNumColAttrib(OConnection const * _pConnection
                                              ,SQLHANDLE _aStatementHandle
                                              ,const css::uno::Reference< css::uno::XInterface >& _xInterface
                                              ,sal_Int32 _column
                                              ,sal_Int32 _ident)
{
    SQLLEN nValue=0;
    OTools::ThrowException(_pConnection,(*reinterpret_cast<T3SQLColAttribute>(_pConnection->getOdbcFunction(ODBC3SQLFunctionId::ColAttribute)))(_aStatementHandle,
                                         static_cast<SQLUSMALLINT>(_column),
                                         static_cast<SQLUSMALLINT>(_ident),
                                         nullptr,
                                         0,
                                         nullptr,
                                         &nValue),_aStatementHandle,SQL_HANDLE_STMT,_xInterface);
    return nValue;
}

sal_Int32 OResultSetMetaData::getNumColAttrib(sal_Int32 _column,sal_Int32 ident)
{
    sal_Int32 column = _column;
    if(_column < static_cast<sal_Int32>(m_vMapping.size())) // use mapping
        column = m_vMapping[_column];

    return getNumColAttrib(m_pConnection,m_aStatementHandle,*this,column,ident);
}

sal_Int32 SAL_CALL OResultSetMetaData::getColumnDisplaySize( sal_Int32 column )
{
    return getNumColAttrib(column,SQL_DESC_DISPLAY_SIZE);
}

SQLSMALLINT OResultSetMetaData::getColumnODBCType(OConnection const * _pConnection
                                              ,SQLHANDLE _aStatementHandle
                                              ,const css::uno::Reference< css::uno::XInterface >& _xInterface
                                              ,sal_Int32 column)
{
    SQLSMALLINT nType = 0;
    try
    {
        nType = static_cast<SQLSMALLINT>(getNumColAttrib(_pConnection,_aStatementHandle,_xInterface,column,SQL_DESC_CONCISE_TYPE));
        if(nType == SQL_UNKNOWN_TYPE)
            nType = static_cast<SQLSMALLINT>(getNumColAttrib(_pConnection,_aStatementHandle,_xInterface,column, SQL_DESC_TYPE));
    }
    catch(SQLException& ) // in this case we have an odbc 2.0 driver
    {
        nType = static_cast<SQLSMALLINT>(getNumColAttrib(_pConnection,_aStatementHandle,_xInterface,column,SQL_DESC_CONCISE_TYPE ));
    }

    return nType;
}

sal_Int32 SAL_CALL OResultSetMetaData::getColumnType( sal_Int32 column )
{
    std::map<sal_Int32,sal_Int32>::iterator aFind = m_aColumnTypes.find(column);
    if ( aFind == m_aColumnTypes.end() )
    {
        sal_Int32 nType = 0;
        if(!m_bUseODBC2Types)
        {
            try
            {
                nType = getNumColAttrib(column,SQL_DESC_CONCISE_TYPE);
                if(nType == SQL_UNKNOWN_TYPE)
                    nType = getNumColAttrib(column, SQL_DESC_TYPE);
                nType = OTools::MapOdbcType2Jdbc(nType);
            }
            catch(SQLException& ) // in this case we have an odbc 2.0 driver
            {
                m_bUseODBC2Types = true;
                nType = OTools::MapOdbcType2Jdbc(getNumColAttrib(column,SQL_DESC_CONCISE_TYPE ));
            }
        }
        else
            nType = OTools::MapOdbcType2Jdbc(getNumColAttrib(column,SQL_DESC_CONCISE_TYPE ));
        aFind = m_aColumnTypes.emplace(column,nType).first;
    }


    return aFind->second;
}


sal_Int32 SAL_CALL OResultSetMetaData::getColumnCount(  )
{
    if(m_nColCount != -1)
        return m_nColCount;
    sal_Int16 nNumResultCols=0;
    OTools::ThrowException(m_pConnection,N3SQLNumResultCols(m_aStatementHandle,&nNumResultCols),m_aStatementHandle,SQL_HANDLE_STMT,*this);
    m_nColCount = nNumResultCols;
    return m_nColCount;
}


sal_Bool SAL_CALL OResultSetMetaData::isCaseSensitive( sal_Int32 column )
{
    return getNumColAttrib(column,SQL_DESC_CASE_SENSITIVE) == SQL_TRUE;
}


OUString SAL_CALL OResultSetMetaData::getSchemaName( sal_Int32 column )
{
    return getCharColAttrib(column,SQL_DESC_SCHEMA_NAME);
}


OUString SAL_CALL OResultSetMetaData::getColumnName( sal_Int32 column )
{
    return getCharColAttrib(column,SQL_DESC_NAME);
}

OUString SAL_CALL OResultSetMetaData::getTableName( sal_Int32 column )
{
    return getCharColAttrib(column,SQL_DESC_TABLE_NAME);
}

OUString SAL_CALL OResultSetMetaData::getCatalogName( sal_Int32 column )
{
    return getCharColAttrib(column,SQL_DESC_CATALOG_NAME);
}

OUString SAL_CALL OResultSetMetaData::getColumnTypeName( sal_Int32 column )
{
    return getCharColAttrib(column,SQL_DESC_TYPE_NAME);
}

OUString SAL_CALL OResultSetMetaData::getColumnLabel( sal_Int32 column )
{
    return getCharColAttrib(column,SQL_DESC_LABEL);
}

OUString SAL_CALL OResultSetMetaData::getColumnServiceName( sal_Int32 /*column*/ )
{
    return OUString();
}


sal_Bool SAL_CALL OResultSetMetaData::isCurrency( sal_Int32 column )
{
    return getNumColAttrib(column,SQL_DESC_FIXED_PREC_SCALE) == SQL_TRUE;
}


sal_Bool SAL_CALL OResultSetMetaData::isAutoIncrement( sal_Int32 column )
{
    return getNumColAttrib(column,SQL_DESC_AUTO_UNIQUE_VALUE) == SQL_TRUE;
}


sal_Bool SAL_CALL OResultSetMetaData::isSigned( sal_Int32 column )
{
    return getNumColAttrib(column,SQL_DESC_UNSIGNED) == SQL_FALSE;
}

sal_Int32 SAL_CALL OResultSetMetaData::getPrecision( sal_Int32 column )
{
    sal_Int32 nType = 0;
    try
    {
        nType = getNumColAttrib(column,SQL_DESC_PRECISION);
    }
    catch(const SQLException& ) // in this case we have an odbc 2.0 driver
    {
        m_bUseODBC2Types = true;
        nType = getNumColAttrib(column,SQL_COLUMN_PRECISION );
    }
    return nType;
}

sal_Int32 SAL_CALL OResultSetMetaData::getScale( sal_Int32 column )
{
    sal_Int32 nType = 0;
    try
    {
        nType = getNumColAttrib(column,SQL_DESC_SCALE);
    }
    catch(const SQLException& ) // in this case we have an odbc 2.0 driver
    {
        m_bUseODBC2Types = true;
        nType = getNumColAttrib(column,SQL_COLUMN_SCALE );
    }
    return nType;
}


sal_Int32 SAL_CALL OResultSetMetaData::isNullable( sal_Int32 column )
{
    return getNumColAttrib(column,SQL_DESC_NULLABLE);
}


sal_Bool SAL_CALL OResultSetMetaData::isSearchable( sal_Int32 column )
{
    return getNumColAttrib(column,SQL_DESC_SEARCHABLE) != SQL_PRED_NONE;
}


sal_Bool SAL_CALL OResultSetMetaData::isReadOnly( sal_Int32 column )
{
    return getNumColAttrib(column,SQL_DESC_UPDATABLE) == SQL_ATTR_READONLY;
}


sal_Bool SAL_CALL OResultSetMetaData::isDefinitelyWritable( sal_Int32 column )
{
    return getNumColAttrib(column,SQL_DESC_UPDATABLE) == SQL_ATTR_WRITE;
}

sal_Bool SAL_CALL OResultSetMetaData::isWritable( sal_Int32 column )
{
    return getNumColAttrib(column,SQL_DESC_UPDATABLE) == SQL_ATTR_WRITE;
}


/* vim:set shiftwidth=4 softtabstop=4 expandtab: */