1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "mysqlc_resultsetmetadata.hxx"
21 #include "mysqlc_general.hxx"
23 #include <com/sun/star/sdbc/XRow.hpp>
24 #include <rtl/ustrbuf.hxx>
26 using namespace connectivity::mysqlc
;
27 using namespace com::sun::star::uno
;
28 using namespace com::sun::star::lang
;
29 using namespace com::sun::star::sdbc
;
31 OResultSetMetaData::OResultSetMetaData(OConnection
& rConn
, MYSQL_RES
* pResult
)
32 : m_rConnection(rConn
)
34 MYSQL_FIELD
* fields
= mysql_fetch_field(pResult
);
35 unsigned nFieldCount
= mysql_num_fields(pResult
);
36 for (unsigned i
= 0; i
< nFieldCount
; ++i
)
38 MySqlFieldInfo fieldInfo
;
41 = OUString
{ fields
[i
].name
, static_cast<sal_Int32
>(fields
[i
].name_length
),
42 m_rConnection
.getConnectionEncoding() };
43 fieldInfo
.length
= static_cast<sal_Int32
>(fields
[i
].length
);
45 = mysqlc_sdbc_driver::mysqlToOOOType(fields
[i
].type
, fields
[i
].charsetnr
);
46 fieldInfo
.mysql_type
= fields
[i
].type
;
47 fieldInfo
.charsetNumber
= fields
[i
].charsetnr
;
48 fieldInfo
.flags
= fields
[i
].flags
;
50 = OUString
{ fields
[i
].db
, static_cast<sal_Int32
>(fields
[i
].db_length
),
51 m_rConnection
.getConnectionEncoding() };
53 = OUString
{ fields
[i
].table
, static_cast<sal_Int32
>(fields
[i
].table_length
),
54 m_rConnection
.getConnectionEncoding() };
56 = OUString
{ fields
[i
].catalog
, static_cast<sal_Int32
>(fields
[i
].catalog_length
),
57 m_rConnection
.getConnectionEncoding() };
58 fieldInfo
.decimals
= static_cast<sal_Int32
>(fields
[i
].decimals
);
59 fieldInfo
.max_length
= static_cast<sal_Int32
>(fields
[i
].max_length
);
61 m_fields
.push_back(std::move(fieldInfo
));
65 sal_Int32 SAL_CALL
OResultSetMetaData::getColumnDisplaySize(sal_Int32 column
)
67 checkColumnIndex(column
);
68 return m_fields
.at(column
- 1).length
;
71 sal_Int32 SAL_CALL
OResultSetMetaData::getColumnType(sal_Int32 column
)
73 checkColumnIndex(column
);
74 return m_fields
.at(column
- 1).type
;
77 sal_Int32 SAL_CALL
OResultSetMetaData::getColumnCount() { return m_fields
.size(); }
79 sal_Bool SAL_CALL
OResultSetMetaData::isCaseSensitive(sal_Int32 column
)
81 // MYSQL_FIELD::charsetnr is the collation identifier
82 // _ci postfix means it's insensitive
83 OUStringBuffer sql
{ "SHOW COLLATION WHERE Id =" };
84 sql
.append(OUString::number(m_fields
.at(column
- 1).charsetNumber
));
86 Reference
<XStatement
> stmt
= m_rConnection
.createStatement();
87 Reference
<XResultSet
> rs
= stmt
->executeQuery(sql
.makeStringAndClear());
88 Reference
<XRow
> xRow(rs
, UNO_QUERY_THROW
);
90 if (!rs
->next()) // fetch first and only row
93 OUString sColName
= xRow
->getString(1); // first column is Collation name
95 return !sColName
.isEmpty() && !sColName
.endsWith("_ci");
98 OUString SAL_CALL
OResultSetMetaData::getSchemaName(sal_Int32 column
)
100 checkColumnIndex(column
);
101 return m_fields
.at(column
- 1).schemaName
;
104 OUString SAL_CALL
OResultSetMetaData::getColumnName(sal_Int32 column
)
106 checkColumnIndex(column
);
107 return m_fields
.at(column
- 1).columnName
;
110 OUString SAL_CALL
OResultSetMetaData::getTableName(sal_Int32 column
)
112 checkColumnIndex(column
);
113 return m_fields
.at(column
- 1).tableName
;
116 OUString SAL_CALL
OResultSetMetaData::getCatalogName(sal_Int32 column
)
118 checkColumnIndex(column
);
119 return m_fields
.at(column
- 1).catalogName
;
122 OUString SAL_CALL
OResultSetMetaData::getColumnTypeName(sal_Int32 column
)
124 checkColumnIndex(column
);
125 return mysqlc_sdbc_driver::mysqlTypeToStr(m_fields
.at(column
- 1).mysql_type
,
126 m_fields
.at(column
- 1).flags
);
129 OUString SAL_CALL
OResultSetMetaData::getColumnLabel(sal_Int32 column
)
131 checkColumnIndex(column
);
132 return getColumnName(column
);
135 OUString SAL_CALL
OResultSetMetaData::getColumnServiceName(sal_Int32
/*column*/)
140 sal_Bool SAL_CALL
OResultSetMetaData::isCurrency(sal_Int32
/*column*/)
142 return false; // TODO
145 sal_Bool SAL_CALL
OResultSetMetaData::isAutoIncrement(sal_Int32 column
)
147 checkColumnIndex(column
);
148 return (m_fields
.at(column
- 1).flags
& AUTO_INCREMENT_FLAG
) != 0;
151 sal_Bool SAL_CALL
OResultSetMetaData::isSigned(sal_Int32 column
)
153 checkColumnIndex(column
);
154 return !(m_fields
.at(column
- 1).flags
& UNSIGNED_FLAG
);
157 sal_Int32 SAL_CALL
OResultSetMetaData::getPrecision(sal_Int32 column
)
159 checkColumnIndex(column
);
160 return m_fields
.at(column
- 1).max_length
- m_fields
.at(column
- 1).decimals
;
163 sal_Int32 SAL_CALL
OResultSetMetaData::getScale(sal_Int32 column
)
165 checkColumnIndex(column
);
166 return m_fields
.at(column
- 1).decimals
;
169 sal_Int32 SAL_CALL
OResultSetMetaData::isNullable(sal_Int32 column
)
171 checkColumnIndex(column
);
172 return (m_fields
.at(column
- 1).flags
& NOT_NULL_FLAG
) ? 0 : 1;
175 sal_Bool SAL_CALL
OResultSetMetaData::isSearchable(sal_Int32 column
)
177 checkColumnIndex(column
);
181 sal_Bool SAL_CALL
OResultSetMetaData::isReadOnly(sal_Int32 column
)
183 checkColumnIndex(column
);
184 return m_fields
.at(column
- 1).schemaName
.isEmpty();
187 sal_Bool SAL_CALL
OResultSetMetaData::isDefinitelyWritable(sal_Int32 column
)
189 checkColumnIndex(column
);
190 return !isReadOnly(column
);
193 sal_Bool SAL_CALL
OResultSetMetaData::isWritable(sal_Int32 column
)
195 checkColumnIndex(column
);
196 return !isReadOnly(column
);
199 void OResultSetMetaData::checkColumnIndex(sal_Int32 columnIndex
)
201 auto nColCount
= m_fields
.size();
202 if (columnIndex
< 1 || columnIndex
> static_cast<sal_Int32
>(nColCount
))
204 OUString str
= "Column index out of range (expected 1 to "
205 + OUString::number(sal_Int32(nColCount
)) + ", got "
206 + OUString::number(columnIndex
) + ".";
207 throw SQLException(str
, *this, OUString(), 1, Any());
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */