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