Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / mysqlc / source / mysqlc_general.cxx
blob1ee31493a1547f97ffc546fddfd8cfd5294838ad
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
19 #include "mysqlc_general.hxx"
20 #include "mysqlc_resultsetmetadata.hxx"
22 #include <cppconn/exception.h>
23 #include <cppconn/datatype.h>
25 using com::sun::star::sdbc::SQLException;
27 using com::sun::star::uno::UNO_QUERY;
28 using com::sun::star::uno::Reference;
29 using com::sun::star::uno::XInterface;
30 using com::sun::star::uno::Any;
31 using ::rtl::OUString;
33 namespace mysqlc_sdbc_driver
35 // -----------------------------------------------------------------------------
36 void throwFeatureNotImplementedException( const sal_Char* _pAsciiFeatureName, const Reference< XInterface >& _rxContext, const Any* _pNextException )
37 throw (SQLException)
39 const ::rtl::OUString sMessage = ::rtl::OUString::createFromAscii( _pAsciiFeatureName ) + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ": feature not implemented." ) );
40 throw SQLException(
41 sMessage,
42 _rxContext,
43 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("HYC00")),
45 _pNextException ? *_pNextException : Any()
50 void throwInvalidArgumentException( const sal_Char* _pAsciiFeatureName, const Reference< XInterface >& _rxContext, const Any* _pNextException )
51 throw (SQLException)
53 const ::rtl::OUString sMessage = ::rtl::OUString::createFromAscii( _pAsciiFeatureName ) + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ": invalid arguments." ) );
54 throw SQLException(
55 sMessage,
56 _rxContext,
57 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("HYC00")),
59 _pNextException ? *_pNextException : Any()
63 void translateAndThrow(const ::sql::SQLException& _error, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _context, const rtl_TextEncoding encoding)
65 throw SQLException(
66 convert(_error.what(), encoding),
67 _context,
68 convert(_error.getSQLState(), encoding),
69 _error.getErrorCode(),
70 Any()
75 OUString getStringFromAny(const Any& _rAny)
77 OUString nReturn;
78 OSL_VERIFY( _rAny >>= nReturn );
79 return nReturn;
83 int mysqlToOOOType(int cppConnType)
84 throw ()
86 switch (cppConnType) {
87 case sql::DataType::BIT:
88 return com::sun::star::sdbc::DataType::VARCHAR;
90 case sql::DataType::TINYINT:
91 return com::sun::star::sdbc::DataType::TINYINT;
93 case sql::DataType::SMALLINT:
94 return com::sun::star::sdbc::DataType::SMALLINT;
96 case sql::DataType::INTEGER:
97 return com::sun::star::sdbc::DataType::INTEGER;
99 case sql::DataType::BIGINT:
100 return com::sun::star::sdbc::DataType::BIGINT;
102 case sql::DataType::REAL:
103 return com::sun::star::sdbc::DataType::REAL;
105 case sql::DataType::DOUBLE:
106 return com::sun::star::sdbc::DataType::DOUBLE;
108 case sql::DataType::DECIMAL:
109 return com::sun::star::sdbc::DataType::DECIMAL;
111 case sql::DataType::CHAR:
112 return com::sun::star::sdbc::DataType::CHAR;
114 case sql::DataType::BINARY:
115 return com::sun::star::sdbc::DataType::BINARY;
117 case sql::DataType::ENUM:
118 case sql::DataType::SET:
119 case sql::DataType::VARCHAR:
120 return com::sun::star::sdbc::DataType::VARCHAR;
122 case sql::DataType::VARBINARY:
123 return com::sun::star::sdbc::DataType::VARBINARY;
125 case sql::DataType::LONGVARCHAR:
126 return com::sun::star::sdbc::DataType::LONGVARCHAR;
128 case sql::DataType::LONGVARBINARY:
129 return com::sun::star::sdbc::DataType::LONGVARBINARY;
131 case sql::DataType::TIMESTAMP:
132 return com::sun::star::sdbc::DataType::TIMESTAMP;
134 case sql::DataType::DATE:
135 return com::sun::star::sdbc::DataType::DATE;
137 case sql::DataType::TIME:
138 return com::sun::star::sdbc::DataType::TIME;
140 case sql::DataType::GEOMETRY:
141 return com::sun::star::sdbc::DataType::VARCHAR;
143 case sql::DataType::SQLNULL:
144 return com::sun::star::sdbc::DataType::SQLNULL;
146 case sql::DataType::UNKNOWN:
147 return com::sun::star::sdbc::DataType::VARCHAR;
150 OSL_FAIL( "mysqlToOOOType: unhandled case, falling back to VARCHAR" );
151 return com::sun::star::sdbc::DataType::VARCHAR;
155 ::rtl::OUString convert(const ::std::string& _string, const rtl_TextEncoding encoding)
157 return ::rtl::OUString( _string.c_str(), _string.size(), encoding );
160 ::std::string convert(const ::rtl::OUString& _string, const rtl_TextEncoding encoding)
162 return ::std::string( ::rtl::OUStringToOString( _string, encoding ).getStr() );
166 } /* namespace */
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */