1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 #include "mysqlc_general.hxx"
22 #include "mysqlc_resultsetmetadata.hxx"
23 
24 #include <cppconn/exception.h>
25 #include <cppconn/datatype.h>
26 
27 using com::sun::star::sdbc::SQLException;
28 
29 using com::sun::star::uno::UNO_QUERY;
30 using com::sun::star::uno::Reference;
31 using com::sun::star::uno::XInterface;
32 using com::sun::star::uno::Any;
33 using ::rtl::OUString;
34 
35 namespace mysqlc_sdbc_driver
36 {
37 // -----------------------------------------------------------------------------
throwFeatureNotImplementedException(const sal_Char * _pAsciiFeatureName,const Reference<XInterface> & _rxContext,const Any * _pNextException)38 void throwFeatureNotImplementedException( const sal_Char* _pAsciiFeatureName, const Reference< XInterface >& _rxContext, const Any* _pNextException )
39     throw (SQLException)
40 {
41     const ::rtl::OUString sMessage = ::rtl::OUString::createFromAscii( _pAsciiFeatureName ) + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ": feature not implemented." ) );
42     throw SQLException(
43         sMessage,
44         _rxContext,
45         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("HYC00")),
46         0,
47         _pNextException ? *_pNextException : Any()
48     );
49 }
50 
51 
throwInvalidArgumentException(const sal_Char * _pAsciiFeatureName,const Reference<XInterface> & _rxContext,const Any * _pNextException)52 void throwInvalidArgumentException( const sal_Char* _pAsciiFeatureName, const Reference< XInterface >& _rxContext, const Any* _pNextException )
53     throw (SQLException)
54 {
55     const ::rtl::OUString sMessage = ::rtl::OUString::createFromAscii( _pAsciiFeatureName ) + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ": invalid arguments." ) );
56     throw SQLException(
57         sMessage,
58         _rxContext,
59         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("HYC00")),
60         0,
61         _pNextException ? *_pNextException : Any()
62     );
63 }
64 
translateAndThrow(const::sql::SQLException & _error,const::com::sun::star::uno::Reference<::com::sun::star::uno::XInterface> & _context,const rtl_TextEncoding encoding)65 void translateAndThrow(const ::sql::SQLException& _error, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _context, const rtl_TextEncoding encoding)
66 {
67 	throw SQLException(
68 			convert(_error.what(), encoding),
69 			_context,
70 			convert(_error.getSQLState(), encoding),
71 			_error.getErrorCode(),
72 			Any()
73 		);
74 }
75 
76 
getStringFromAny(const Any & _rAny)77 OUString getStringFromAny(const Any& _rAny)
78 {
79 	OUString nReturn;
80 	OSL_VERIFY( _rAny >>= nReturn );
81 	return nReturn;
82 }
83 
84 
mysqlToOOOType(int cppConnType)85 int mysqlToOOOType(int cppConnType)
86 	throw ()
87 {
88 	switch (cppConnType) {
89 		case sql::DataType::BIT:
90 			return com::sun::star::sdbc::DataType::VARCHAR;
91 
92 		case sql::DataType::TINYINT:
93 			return com::sun::star::sdbc::DataType::TINYINT;
94 
95 		case sql::DataType::SMALLINT:
96 			return com::sun::star::sdbc::DataType::SMALLINT;
97 
98 		case sql::DataType::INTEGER:
99 			return com::sun::star::sdbc::DataType::INTEGER;
100 
101 		case sql::DataType::BIGINT:
102 			return com::sun::star::sdbc::DataType::BIGINT;
103 
104 		case sql::DataType::REAL:
105 			return com::sun::star::sdbc::DataType::REAL;
106 
107 		case sql::DataType::DOUBLE:
108 			return com::sun::star::sdbc::DataType::DOUBLE;
109 
110 		case sql::DataType::DECIMAL:
111 			return com::sun::star::sdbc::DataType::DECIMAL;
112 
113 		case sql::DataType::CHAR:
114 			return com::sun::star::sdbc::DataType::CHAR;
115 
116 		case sql::DataType::BINARY:
117 			return com::sun::star::sdbc::DataType::BINARY;
118 
119 		case sql::DataType::ENUM:
120 		case sql::DataType::SET:
121 		case sql::DataType::VARCHAR:
122 			return com::sun::star::sdbc::DataType::VARCHAR;
123 
124 		case sql::DataType::VARBINARY:
125 			return com::sun::star::sdbc::DataType::VARBINARY;
126 
127 		case sql::DataType::LONGVARCHAR:
128 			return com::sun::star::sdbc::DataType::LONGVARCHAR;
129 
130 		case sql::DataType::LONGVARBINARY:
131 			return com::sun::star::sdbc::DataType::LONGVARBINARY;
132 
133 		case sql::DataType::TIMESTAMP:
134 			return com::sun::star::sdbc::DataType::TIMESTAMP;
135 
136 		case sql::DataType::DATE:
137 			return com::sun::star::sdbc::DataType::DATE;
138 
139 		case sql::DataType::TIME:
140 			return com::sun::star::sdbc::DataType::TIME;
141 
142 		case sql::DataType::GEOMETRY:
143 			return com::sun::star::sdbc::DataType::VARCHAR;
144 
145 		case sql::DataType::SQLNULL:
146 			return com::sun::star::sdbc::DataType::SQLNULL;
147 
148 		case sql::DataType::UNKNOWN:
149 			return com::sun::star::sdbc::DataType::VARCHAR;
150 	}
151 
152     OSL_ENSURE( false, "mysqlToOOOType: unhandled case, falling back to VARCHAR" );
153     return com::sun::star::sdbc::DataType::VARCHAR;
154 }
155 
156 
convert(const::ext_std::string & _string,const rtl_TextEncoding encoding)157 ::rtl::OUString convert(const ::ext_std::string& _string, const rtl_TextEncoding encoding)
158 {
159 	return ::rtl::OUString( _string.c_str(), _string.size(), encoding );
160 }
161 
convert(const::rtl::OUString & _string,const rtl_TextEncoding encoding)162 ::ext_std::string convert(const ::rtl::OUString& _string, const rtl_TextEncoding encoding)
163 {
164 	return ::ext_std::string( ::rtl::OUStringToOString( _string, encoding ).getStr() );
165 }
166 
167 
168 } /* namespace */
169