2010-10-12 15:55:21 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-06-25 16:56:16 +01:00
|
|
|
/*
|
|
|
|
* 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 .
|
|
|
|
*/
|
2009-12-11 09:51:25 +01:00
|
|
|
|
|
|
|
#include "mysqlc_resultsetmetadata.hxx"
|
2018-06-17 18:09:43 +02:00
|
|
|
#include "mysqlc_resultset.hxx"
|
2009-12-11 09:51:25 +01:00
|
|
|
#include "mysqlc_general.hxx"
|
2017-10-23 22:37:22 +02:00
|
|
|
#include <cppconn/exception.h>
|
2009-12-11 09:51:25 +01:00
|
|
|
|
2018-06-17 18:09:43 +02:00
|
|
|
#include <com/sun/star/sdbc/XRow.hpp>
|
2009-12-11 09:51:25 +01:00
|
|
|
#include <rtl/ustrbuf.hxx>
|
|
|
|
|
|
|
|
using namespace connectivity::mysqlc;
|
|
|
|
using namespace com::sun::star::uno;
|
|
|
|
using namespace com::sun::star::lang;
|
|
|
|
using namespace com::sun::star::sdbc;
|
|
|
|
|
2018-06-17 18:09:43 +02:00
|
|
|
MYSQL_FIELD* OResultSetMetaData::getField(sal_Int32 column) const
|
2009-12-11 09:51:25 +01:00
|
|
|
{
|
2018-06-17 18:09:43 +02:00
|
|
|
return mysql_fetch_field_direct(m_pRes, column - 1);
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sal_Int32 SAL_CALL OResultSetMetaData::getColumnDisplaySize(sal_Int32 column)
|
|
|
|
{
|
2018-06-17 18:09:43 +02:00
|
|
|
MYSQL_FIELD* pField = getField(column);
|
|
|
|
return pField->length;
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sal_Int32 SAL_CALL OResultSetMetaData::getColumnType(sal_Int32 column)
|
|
|
|
{
|
|
|
|
checkColumnIndex(column);
|
2018-06-17 18:09:43 +02:00
|
|
|
MYSQL_FIELD* pField = getField(column);
|
2009-12-11 09:51:25 +01:00
|
|
|
|
2018-06-17 18:09:43 +02:00
|
|
|
return mysqlc_sdbc_driver::mysqlToOOOType(pField->type, pField->charsetnr);
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sal_Int32 SAL_CALL OResultSetMetaData::getColumnCount()
|
|
|
|
{
|
2018-06-17 18:09:43 +02:00
|
|
|
return mysql_num_fields(m_pRes);
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sal_Bool SAL_CALL OResultSetMetaData::isCaseSensitive(sal_Int32 column)
|
|
|
|
{
|
|
|
|
checkColumnIndex(column);
|
2018-06-17 18:09:43 +02:00
|
|
|
// MYSQL_FIELD::charsetnr is the collation identifier
|
|
|
|
// _ci postfix means it's insensitive
|
|
|
|
MYSQL_FIELD* pField = getField(column);
|
|
|
|
rtl::OUStringBuffer sql{"SHOW COLLATION WHERE Id ="};
|
|
|
|
sql.append(rtl::OUString::number(pField->charsetnr));
|
2009-12-11 09:51:25 +01:00
|
|
|
|
2018-06-17 18:09:43 +02:00
|
|
|
Reference< XStatement > stmt = m_rConnection.createStatement();
|
|
|
|
Reference< XResultSet > rs = stmt->executeQuery(sql.makeStringAndClear());
|
|
|
|
Reference< XRow > xRow( rs, UNO_QUERY_THROW );
|
|
|
|
|
|
|
|
rs->next(); // fetch first and only row
|
|
|
|
rtl::OUString sColName = xRow->getString(1); // first column is Collation name
|
|
|
|
|
|
|
|
return !sColName.isEmpty() && !sColName.endsWith("_ci");
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 11:43:51 +01:00
|
|
|
rtl::OUString SAL_CALL OResultSetMetaData::getSchemaName(sal_Int32 column)
|
2009-12-11 09:51:25 +01:00
|
|
|
{
|
|
|
|
checkColumnIndex(column);
|
2018-06-17 18:09:43 +02:00
|
|
|
MYSQL_FIELD* pField = getField(column);
|
2009-12-11 09:51:25 +01:00
|
|
|
|
2018-06-17 18:09:43 +02:00
|
|
|
return rtl::OStringToOUString(pField->db, m_rConnection.getConnectionEncoding());
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 11:43:51 +01:00
|
|
|
rtl::OUString SAL_CALL OResultSetMetaData::getColumnName(sal_Int32 column)
|
2009-12-11 09:51:25 +01:00
|
|
|
{
|
|
|
|
checkColumnIndex(column);
|
|
|
|
|
2018-06-17 18:09:43 +02:00
|
|
|
MYSQL_FIELD* pField = getField(column);
|
|
|
|
return rtl::OStringToOUString(pField->name, m_rConnection.getConnectionEncoding());
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 11:43:51 +01:00
|
|
|
rtl::OUString SAL_CALL OResultSetMetaData::getTableName(sal_Int32 column)
|
2009-12-11 09:51:25 +01:00
|
|
|
{
|
|
|
|
checkColumnIndex(column);
|
2018-06-17 18:09:43 +02:00
|
|
|
MYSQL_FIELD* pField = getField(column);
|
|
|
|
return rtl::OStringToOUString(pField->table, m_rConnection.getConnectionEncoding());
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 11:43:51 +01:00
|
|
|
rtl::OUString SAL_CALL OResultSetMetaData::getCatalogName(sal_Int32 column)
|
2009-12-11 09:51:25 +01:00
|
|
|
{
|
|
|
|
checkColumnIndex(column);
|
2018-06-17 18:09:43 +02:00
|
|
|
MYSQL_FIELD* pField = getField(column);
|
|
|
|
return rtl::OStringToOUString(pField->catalog, m_rConnection.getConnectionEncoding());
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 11:43:51 +01:00
|
|
|
rtl::OUString SAL_CALL OResultSetMetaData::getColumnTypeName(sal_Int32 column)
|
2009-12-11 09:51:25 +01:00
|
|
|
{
|
|
|
|
checkColumnIndex(column);
|
2018-06-17 18:09:43 +02:00
|
|
|
MYSQL_FIELD* pField = getField(column);
|
2009-12-11 09:51:25 +01:00
|
|
|
|
2018-06-17 18:09:43 +02:00
|
|
|
return mysqlc_sdbc_driver::mysqlTypeToStr(pField);
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 11:43:51 +01:00
|
|
|
rtl::OUString SAL_CALL OResultSetMetaData::getColumnLabel(sal_Int32 column)
|
2009-12-11 09:51:25 +01:00
|
|
|
{
|
|
|
|
checkColumnIndex(column);
|
2018-06-17 18:09:43 +02:00
|
|
|
MYSQL_FIELD* pField = getField(column);
|
|
|
|
return rtl::OStringToOUString(pField->name, m_rConnection.getConnectionEncoding());
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 11:43:51 +01:00
|
|
|
rtl::OUString SAL_CALL OResultSetMetaData::getColumnServiceName(sal_Int32 column)
|
2009-12-11 09:51:25 +01:00
|
|
|
{
|
|
|
|
checkColumnIndex(column);
|
|
|
|
|
2014-12-19 11:43:51 +01:00
|
|
|
rtl::OUString aRet = rtl::OUString();
|
2009-12-11 09:51:25 +01:00
|
|
|
return aRet;
|
|
|
|
}
|
|
|
|
|
2018-06-17 18:09:43 +02:00
|
|
|
sal_Bool SAL_CALL OResultSetMetaData::isCurrency(sal_Int32 /*column*/)
|
2009-12-11 09:51:25 +01:00
|
|
|
{
|
2018-06-17 18:09:43 +02:00
|
|
|
return false; // TODO
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sal_Bool SAL_CALL OResultSetMetaData::isAutoIncrement(sal_Int32 column)
|
|
|
|
{
|
|
|
|
checkColumnIndex(column);
|
|
|
|
|
2018-06-17 18:09:43 +02:00
|
|
|
MYSQL_FIELD* pField = getField(column);
|
2018-08-13 09:09:18 +02:00
|
|
|
return (pField->flags & AUTO_INCREMENT_FLAG) != 0;
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sal_Bool SAL_CALL OResultSetMetaData::isSigned(sal_Int32 column)
|
|
|
|
{
|
|
|
|
checkColumnIndex(column);
|
|
|
|
|
2018-06-17 18:09:43 +02:00
|
|
|
MYSQL_FIELD* pField = getField(column);
|
|
|
|
return !(pField->flags & UNSIGNED_FLAG);
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sal_Int32 SAL_CALL OResultSetMetaData::getPrecision(sal_Int32 column)
|
|
|
|
{
|
|
|
|
checkColumnIndex(column);
|
2018-06-17 18:09:43 +02:00
|
|
|
MYSQL_FIELD* pField = getField(column);
|
|
|
|
return pField->max_length - pField->decimals;
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sal_Int32 SAL_CALL OResultSetMetaData::getScale(sal_Int32 column)
|
|
|
|
{
|
|
|
|
checkColumnIndex(column);
|
2018-06-17 18:09:43 +02:00
|
|
|
MYSQL_FIELD* pField = getField(column);
|
|
|
|
return pField->decimals;
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sal_Int32 SAL_CALL OResultSetMetaData::isNullable(sal_Int32 column)
|
|
|
|
{
|
|
|
|
checkColumnIndex(column);
|
2018-06-17 18:09:43 +02:00
|
|
|
MYSQL_FIELD* pField = getField(column);
|
2018-08-13 09:05:19 +02:00
|
|
|
return (pField->flags & NOT_NULL_FLAG) ? 0 : 1;
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
2018-06-17 18:09:43 +02:00
|
|
|
sal_Bool SAL_CALL OResultSetMetaData::isSearchable(sal_Int32 /*column*/)
|
2009-12-11 09:51:25 +01:00
|
|
|
{
|
2018-06-17 18:09:43 +02:00
|
|
|
return true;
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sal_Bool SAL_CALL OResultSetMetaData::isReadOnly(sal_Int32 column)
|
|
|
|
{
|
|
|
|
checkColumnIndex(column);
|
2018-06-17 18:09:43 +02:00
|
|
|
MYSQL_FIELD* pField = getField(column);
|
|
|
|
return !(pField->db && strlen(pField->db));
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sal_Bool SAL_CALL OResultSetMetaData::isDefinitelyWritable(sal_Int32 column)
|
|
|
|
{
|
|
|
|
checkColumnIndex(column);
|
2018-06-17 18:09:43 +02:00
|
|
|
return !isReadOnly(column);
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sal_Bool SAL_CALL OResultSetMetaData::isWritable(sal_Int32 column)
|
|
|
|
{
|
|
|
|
checkColumnIndex(column);
|
2018-06-17 18:09:43 +02:00
|
|
|
return !isReadOnly(column);
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void OResultSetMetaData::checkColumnIndex(sal_Int32 columnIndex)
|
|
|
|
{
|
2018-06-17 18:09:43 +02:00
|
|
|
unsigned nColCount = mysql_num_fields(m_pRes);
|
|
|
|
if (columnIndex < 1 || columnIndex > static_cast<sal_Int32>(nColCount)) {
|
2009-12-11 09:51:25 +01:00
|
|
|
|
2014-12-19 11:43:51 +01:00
|
|
|
rtl::OUStringBuffer buf;
|
2009-12-11 09:51:25 +01:00
|
|
|
buf.appendAscii( "Column index out of range (expected 1 to " );
|
2018-06-17 18:09:43 +02:00
|
|
|
buf.append( sal_Int32( nColCount ) );
|
2009-12-11 09:51:25 +01:00
|
|
|
buf.appendAscii( ", got " );
|
2017-05-22 15:55:38 +02:00
|
|
|
buf.append( columnIndex );
|
2013-11-14 08:16:35 +02:00
|
|
|
buf.append( '.' );
|
2014-12-19 11:43:51 +01:00
|
|
|
throw SQLException( buf.makeStringAndClear(), *this, rtl::OUString(), 1, Any() );
|
2009-12-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-12 15:55:21 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|