1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3*cdf0e10cSrcweir *
4*cdf0e10cSrcweir * Copyright 2008 by Sun Microsystems, Inc.
5*cdf0e10cSrcweir *
6*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite
7*cdf0e10cSrcweir *
8*cdf0e10cSrcweir * $RCSfile: mysqlc_general.cxx,v $
9*cdf0e10cSrcweir *
10*cdf0e10cSrcweir * $Revision: 1.1.2.3 $
11*cdf0e10cSrcweir *
12*cdf0e10cSrcweir * This file is part of OpenOffice.org.
13*cdf0e10cSrcweir *
14*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify
15*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3
16*cdf0e10cSrcweir * only, as published by the Free Software Foundation.
17*cdf0e10cSrcweir *
18*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful,
19*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of
20*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details
22*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code).
23*cdf0e10cSrcweir *
24*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License
25*cdf0e10cSrcweir * version 3 along with OpenOffice.org.  If not, see
26*cdf0e10cSrcweir * <http://www.openoffice.org/license.html>
27*cdf0e10cSrcweir * for a copy of the LGPLv3 License.
28*cdf0e10cSrcweir ************************************************************************/
29*cdf0e10cSrcweir #include "mysqlc_general.hxx"
30*cdf0e10cSrcweir #include "mysqlc_resultsetmetadata.hxx"
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <cppconn/exception.h>
33*cdf0e10cSrcweir #include <cppconn/datatype.h>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir using com::sun::star::sdbc::SQLException;
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY;
38*cdf0e10cSrcweir using com::sun::star::uno::Reference;
39*cdf0e10cSrcweir using com::sun::star::uno::XInterface;
40*cdf0e10cSrcweir using com::sun::star::uno::Any;
41*cdf0e10cSrcweir using ::rtl::OUString;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir namespace mysqlc_sdbc_driver
44*cdf0e10cSrcweir {
45*cdf0e10cSrcweir // -----------------------------------------------------------------------------
46*cdf0e10cSrcweir void throwFeatureNotImplementedException( const sal_Char* _pAsciiFeatureName, const Reference< XInterface >& _rxContext, const Any* _pNextException )
47*cdf0e10cSrcweir     throw (SQLException)
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir     const ::rtl::OUString sMessage = ::rtl::OUString::createFromAscii( _pAsciiFeatureName ) + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ": feature not implemented." ) );
50*cdf0e10cSrcweir     throw SQLException(
51*cdf0e10cSrcweir         sMessage,
52*cdf0e10cSrcweir         _rxContext,
53*cdf0e10cSrcweir         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("HYC00")),
54*cdf0e10cSrcweir         0,
55*cdf0e10cSrcweir         _pNextException ? *_pNextException : Any()
56*cdf0e10cSrcweir     );
57*cdf0e10cSrcweir }
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir void throwInvalidArgumentException( const sal_Char* _pAsciiFeatureName, const Reference< XInterface >& _rxContext, const Any* _pNextException )
61*cdf0e10cSrcweir     throw (SQLException)
62*cdf0e10cSrcweir {
63*cdf0e10cSrcweir     const ::rtl::OUString sMessage = ::rtl::OUString::createFromAscii( _pAsciiFeatureName ) + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ": invalid arguments." ) );
64*cdf0e10cSrcweir     throw SQLException(
65*cdf0e10cSrcweir         sMessage,
66*cdf0e10cSrcweir         _rxContext,
67*cdf0e10cSrcweir         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("HYC00")),
68*cdf0e10cSrcweir         0,
69*cdf0e10cSrcweir         _pNextException ? *_pNextException : Any()
70*cdf0e10cSrcweir     );
71*cdf0e10cSrcweir }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir void translateAndThrow(const ::sql::SQLException& _error, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _context, const rtl_TextEncoding encoding)
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir 	throw SQLException(
76*cdf0e10cSrcweir 			convert(_error.what(), encoding),
77*cdf0e10cSrcweir 			_context,
78*cdf0e10cSrcweir 			convert(_error.getSQLState(), encoding),
79*cdf0e10cSrcweir 			_error.getErrorCode(),
80*cdf0e10cSrcweir 			Any()
81*cdf0e10cSrcweir 		);
82*cdf0e10cSrcweir }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir OUString getStringFromAny(const Any& _rAny)
86*cdf0e10cSrcweir {
87*cdf0e10cSrcweir 	OUString nReturn;
88*cdf0e10cSrcweir 	OSL_VERIFY( _rAny >>= nReturn );
89*cdf0e10cSrcweir 	return nReturn;
90*cdf0e10cSrcweir }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir int mysqlToOOOType(int cppConnType)
94*cdf0e10cSrcweir 	throw ()
95*cdf0e10cSrcweir {
96*cdf0e10cSrcweir 	switch (cppConnType) {
97*cdf0e10cSrcweir 		case sql::DataType::BIT:
98*cdf0e10cSrcweir 			return com::sun::star::sdbc::DataType::VARCHAR;
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 		case sql::DataType::TINYINT:
101*cdf0e10cSrcweir 			return com::sun::star::sdbc::DataType::TINYINT;
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 		case sql::DataType::SMALLINT:
104*cdf0e10cSrcweir 			return com::sun::star::sdbc::DataType::SMALLINT;
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 		case sql::DataType::INTEGER:
107*cdf0e10cSrcweir 			return com::sun::star::sdbc::DataType::INTEGER;
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 		case sql::DataType::BIGINT:
110*cdf0e10cSrcweir 			return com::sun::star::sdbc::DataType::BIGINT;
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 		case sql::DataType::REAL:
113*cdf0e10cSrcweir 			return com::sun::star::sdbc::DataType::REAL;
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 		case sql::DataType::DOUBLE:
116*cdf0e10cSrcweir 			return com::sun::star::sdbc::DataType::DOUBLE;
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 		case sql::DataType::DECIMAL:
119*cdf0e10cSrcweir 			return com::sun::star::sdbc::DataType::DECIMAL;
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 		case sql::DataType::CHAR:
122*cdf0e10cSrcweir 			return com::sun::star::sdbc::DataType::CHAR;
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 		case sql::DataType::BINARY:
125*cdf0e10cSrcweir 			return com::sun::star::sdbc::DataType::BINARY;
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 		case sql::DataType::ENUM:
128*cdf0e10cSrcweir 		case sql::DataType::SET:
129*cdf0e10cSrcweir 		case sql::DataType::VARCHAR:
130*cdf0e10cSrcweir 			return com::sun::star::sdbc::DataType::VARCHAR;
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 		case sql::DataType::VARBINARY:
133*cdf0e10cSrcweir 			return com::sun::star::sdbc::DataType::VARBINARY;
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir 		case sql::DataType::LONGVARCHAR:
136*cdf0e10cSrcweir 			return com::sun::star::sdbc::DataType::LONGVARCHAR;
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir 		case sql::DataType::LONGVARBINARY:
139*cdf0e10cSrcweir 			return com::sun::star::sdbc::DataType::LONGVARBINARY;
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 		case sql::DataType::TIMESTAMP:
142*cdf0e10cSrcweir 			return com::sun::star::sdbc::DataType::TIMESTAMP;
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir 		case sql::DataType::DATE:
145*cdf0e10cSrcweir 			return com::sun::star::sdbc::DataType::DATE;
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 		case sql::DataType::TIME:
148*cdf0e10cSrcweir 			return com::sun::star::sdbc::DataType::TIME;
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 		case sql::DataType::GEOMETRY:
151*cdf0e10cSrcweir 			return com::sun::star::sdbc::DataType::VARCHAR;
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 		case sql::DataType::SQLNULL:
154*cdf0e10cSrcweir 			return com::sun::star::sdbc::DataType::SQLNULL;
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir 		case sql::DataType::UNKNOWN:
157*cdf0e10cSrcweir 			return com::sun::star::sdbc::DataType::VARCHAR;
158*cdf0e10cSrcweir 	}
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir     OSL_ENSURE( false, "mysqlToOOOType: unhandled case, falling back to VARCHAR" );
161*cdf0e10cSrcweir     return com::sun::star::sdbc::DataType::VARCHAR;
162*cdf0e10cSrcweir }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir ::rtl::OUString convert(const ::ext_std::string& _string, const rtl_TextEncoding encoding)
166*cdf0e10cSrcweir {
167*cdf0e10cSrcweir 	return ::rtl::OUString( _string.c_str(), _string.size(), encoding );
168*cdf0e10cSrcweir }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir ::ext_std::string convert(const ::rtl::OUString& _string, const rtl_TextEncoding encoding)
171*cdf0e10cSrcweir {
172*cdf0e10cSrcweir 	return ::ext_std::string( ::rtl::OUStringToOString( _string, encoding ).getStr() );
173*cdf0e10cSrcweir }
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir } /* namespace */
177