Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / connectivity / source / drivers / jdbc / tools.cxx
blob4a4d5069ea18bc9cc4c6c2e5bd3fc8bf1ca9bf93
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 .
21 #include <string.h>
22 #include <java/tools.hxx>
23 #include <java/util/Property.hxx>
24 #include <com/sun/star/sdbc/DriverPropertyInfo.hpp>
25 #include <com/sun/star/sdbc/SQLException.hpp>
26 #include <com/sun/star/container/XNameAccess.hpp>
27 #include <connectivity/dbexception.hxx>
28 #include <osl/diagnose.h>
30 using namespace connectivity;
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::beans;
33 using namespace ::com::sun::star::sdbc;
34 using namespace ::com::sun::star::container;
35 using namespace ::com::sun::star::lang;
37 void java_util_Properties::setProperty(const OUString& key, const OUString& value)
39 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
42 jvalue args[2];
43 // Convert Parameter
44 args[0].l = convertwchar_tToJavaString(t.pEnv,key);
45 args[1].l = convertwchar_tToJavaString(t.pEnv,value);
46 // Initialize temporary Variables
47 static const char * const cSignature = "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;";
48 static const char * const cMethodName = "setProperty";
49 // Turn off Java-Call
50 static jmethodID mID(nullptr);
51 obtainMethodId_throwSQL(t.pEnv, cMethodName,cSignature, mID);
52 jobject out = t.pEnv->CallObjectMethod(object, mID, args[0].l,args[1].l);
53 ThrowSQLException(t.pEnv,nullptr);
54 t.pEnv->DeleteLocalRef(static_cast<jstring>(args[1].l));
55 t.pEnv->DeleteLocalRef(static_cast<jstring>(args[0].l));
56 ThrowSQLException(t.pEnv,nullptr);
57 if(out)
58 t.pEnv->DeleteLocalRef(out);
59 } //t.pEnv
60 // WARNING: The caller will be owner of the returned pointers!!!
62 jclass java_util_Properties::theClass = nullptr;
64 java_util_Properties::~java_util_Properties()
67 jclass java_util_Properties::getMyClass() const
69 // the class needs only be called once, that is why it is static
70 if( !theClass )
71 theClass = findMyClass("java/util/Properties");
72 return theClass;
76 java_util_Properties::java_util_Properties( ): java_lang_Object( nullptr, nullptr )
78 SDBThreadAttach t;
79 if( !t.pEnv )
80 return;
81 // Turn off Java-Call for the constructor
82 // Initialize temporary Variables
83 static const char * const cSignature = "()V";
84 jobject tempObj;
85 static jmethodID mID(nullptr);
86 obtainMethodId_throwSQL(t.pEnv, "<init>",cSignature, mID);
87 tempObj = t.pEnv->NewObject( getMyClass(), mID);
88 saveRef( t.pEnv, tempObj );
89 t.pEnv->DeleteLocalRef( tempObj );
93 jstring connectivity::convertwchar_tToJavaString(JNIEnv *pEnv,const OUString& _rTemp)
95 OSL_ENSURE(pEnv,"Environment is NULL!");
96 jstring pStr = pEnv->NewString(
97 reinterpret_cast<jchar const *>(_rTemp.getStr()), _rTemp.getLength());
98 pEnv->ExceptionClear();
99 OSL_ENSURE(pStr,"Could not create a jsstring object!");
100 return pStr;
104 std::unique_ptr<java_util_Properties> connectivity::createStringPropertyArray(const Sequence< PropertyValue >& info )
106 std::unique_ptr<java_util_Properties> pProps(new java_util_Properties());
107 const PropertyValue* pBegin = info.getConstArray();
108 const PropertyValue* pEnd = pBegin + info.getLength();
110 for(;pBegin != pEnd;++pBegin)
112 // these are properties used internally by LibreOffice,
113 // and should not be passed to the JDBC driver
114 // (which probably does not know anything about them anyway).
115 if ( pBegin->Name != "JavaDriverClass"
116 && pBegin->Name != "JavaDriverClassPath"
117 && pBegin->Name != "SystemProperties"
118 && pBegin->Name != "CharSet"
119 && pBegin->Name != "AppendTableAliasName"
120 && pBegin->Name != "AppendTableAliasInSelect"
121 && pBegin->Name != "DisplayVersionColumns"
122 && pBegin->Name != "GeneratedValues"
123 && pBegin->Name != "UseIndexDirectionKeyword"
124 && pBegin->Name != "UseKeywordAsBeforeAlias"
125 && pBegin->Name != "AddIndexAppendix"
126 && pBegin->Name != "FormsCheckRequiredFields"
127 && pBegin->Name != "GenerateASBeforeCorrelationName"
128 && pBegin->Name != "EscapeDateTime"
129 && pBegin->Name != "ParameterNameSubstitution"
130 && pBegin->Name != "IsPasswordRequired"
131 && pBegin->Name != "IsAutoRetrievingEnabled"
132 && pBegin->Name != "AutoRetrievingStatement"
133 && pBegin->Name != "UseCatalogInSelect"
134 && pBegin->Name != "UseSchemaInSelect"
135 && pBegin->Name != "AutoIncrementCreation"
136 && pBegin->Name != "Extension"
137 && pBegin->Name != "NoNameLengthLimit"
138 && pBegin->Name != "EnableSQL92Check"
139 && pBegin->Name != "EnableOuterJoinEscape"
140 && pBegin->Name != "BooleanComparisonMode"
141 && pBegin->Name != "IgnoreCurrency"
142 && pBegin->Name != "TypeInfoSettings"
143 && pBegin->Name != "IgnoreDriverPrivileges"
144 && pBegin->Name != "ImplicitCatalogRestriction"
145 && pBegin->Name != "ImplicitSchemaRestriction"
146 && pBegin->Name != "SupportsTableCreation"
147 && pBegin->Name != "UseJava"
148 && pBegin->Name != "Authentication"
149 && pBegin->Name != "PreferDosLikeLineEnds"
150 && pBegin->Name != "PrimaryKeySupport"
151 && pBegin->Name != "RespectDriverResultSetType"
154 OUString aStr;
155 OSL_VERIFY( pBegin->Value >>= aStr );
156 pProps->setProperty(pBegin->Name,aStr);
159 return pProps;
162 OUString connectivity::JavaString2String(JNIEnv *pEnv,jstring Str)
164 OUString aStr;
165 if(Str)
167 jboolean bCopy(true);
168 const jchar* pChar = pEnv->GetStringChars(Str,&bCopy);
169 jsize len = pEnv->GetStringLength(Str);
170 aStr = OUString(reinterpret_cast<sal_Unicode const *>(pChar), len);
172 if(bCopy)
173 pEnv->ReleaseStringChars(Str,pChar);
174 pEnv->DeleteLocalRef(Str);
176 return aStr;
179 jobject connectivity::convertTypeMapToJavaMap(const Reference< css::container::XNameAccess > & _rMap)
181 if ( _rMap.is() )
183 css::uno::Sequence< OUString > aNames = _rMap->getElementNames();
184 if ( aNames.hasElements() )
185 ::dbtools::throwFeatureNotImplementedSQLException( "Type maps", nullptr );
187 return nullptr;
190 bool connectivity::isExceptionOccurred(JNIEnv *pEnv)
192 if ( !pEnv )
193 return false;
195 jthrowable pThrowable = pEnv->ExceptionOccurred();
196 bool bRet = pThrowable != nullptr;
197 if ( pThrowable )
199 pEnv->ExceptionClear();
200 pEnv->DeleteLocalRef(pThrowable);
203 return bRet;
206 jobject connectivity::createByteInputStream(const css::uno::Reference< css::io::XInputStream >& x,sal_Int32 length)
208 SDBThreadAttach t;
209 if( !t.pEnv || !x.is() )
210 return nullptr;
211 // Turn off Java-Call for the constructor
212 // Initialize temporary variables
213 jclass clazz = java_lang_Object::findMyClass("java/io/ByteArrayInputStream");
214 static jmethodID mID(nullptr);
215 if ( !mID )
217 static const char * const cSignature = "([B)V";
218 mID = t.pEnv->GetMethodID( clazz, "<init>", cSignature );
219 OSL_ENSURE( mID, cSignature );
220 if ( !mID )
221 throw SQLException();
222 } // if ( !_inout_MethodID )
223 jbyteArray pByteArray = t.pEnv->NewByteArray(length);
224 Sequence< sal_Int8 > aData;
225 x->readBytes(aData,length);
226 jboolean p = false;
227 memcpy(t.pEnv->GetByteArrayElements(pByteArray,&p),aData.getArray(),aData.getLength());
228 jobject out = t.pEnv->NewObject( clazz, mID,pByteArray);
229 t.pEnv->DeleteLocalRef(pByteArray);
230 return out;
233 jobject connectivity::createCharArrayReader(const css::uno::Reference< css::io::XInputStream >& x,sal_Int32 length)
235 SDBThreadAttach t;
236 if( !t.pEnv || !x.is() )
237 return nullptr;
238 // Turn off Java-Call for the constructor
239 // Initialize temporary Variables
240 jclass clazz = java_lang_Object::findMyClass("java/io/CharArrayReader");
241 static jmethodID mID(nullptr);
242 if ( !mID )
244 static const char * const cSignature = "([C)V";
245 mID = t.pEnv->GetMethodID( clazz, "<init>", cSignature );
246 OSL_ENSURE( mID, cSignature );
247 if ( !mID )
248 throw SQLException();
249 } // if ( !_inout_MethodID )
250 jcharArray pCharArray = t.pEnv->NewCharArray(length);
251 Sequence< sal_Int8 > aData;
252 x->readBytes(aData,length);
253 jboolean p = false;
254 memcpy(t.pEnv->GetCharArrayElements(pCharArray,&p),aData.getArray(),aData.getLength());
255 jobject out = t.pEnv->NewObject( clazz, mID,pCharArray);
256 t.pEnv->DeleteLocalRef(pCharArray);
257 return out;
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */