1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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
;
36 void java_util_Properties::setProperty(const OUString
& key
, const OUString
& value
)
38 SDBThreadAttach t
; OSL_ENSURE(t
.pEnv
,"Java environment has been deleted!");
43 args
[0].l
= convertwchar_tToJavaString(t
.pEnv
,key
);
44 args
[1].l
= convertwchar_tToJavaString(t
.pEnv
,value
);
45 // Initialize temporary Variables
46 static const char * const cSignature
= "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;";
47 static const char * const cMethodName
= "setProperty";
49 static jmethodID
mID(nullptr);
50 obtainMethodId_throwSQL(t
.pEnv
, cMethodName
,cSignature
, mID
);
51 jobject out
= t
.pEnv
->CallObjectMethod(object
, mID
, args
[0].l
,args
[1].l
);
52 ThrowSQLException(t
.pEnv
,nullptr);
53 t
.pEnv
->DeleteLocalRef(static_cast<jstring
>(args
[1].l
));
54 t
.pEnv
->DeleteLocalRef(static_cast<jstring
>(args
[0].l
));
55 ThrowSQLException(t
.pEnv
,nullptr);
57 t
.pEnv
->DeleteLocalRef(out
);
59 // WARNING: The caller will be owner of the returned pointers!!!
61 jclass
java_util_Properties::theClass
= nullptr;
63 java_util_Properties::~java_util_Properties()
66 jclass
java_util_Properties::getMyClass() const
68 // the class needs only be called once, that is why it is static
70 theClass
= findMyClass("java/util/Properties");
75 java_util_Properties::java_util_Properties( ): java_lang_Object( nullptr, nullptr )
80 // Turn off Java-Call for the constructor
81 // Initialize temporary Variables
82 static const char * const cSignature
= "()V";
84 static jmethodID
mID(nullptr);
85 obtainMethodId_throwSQL(t
.pEnv
, "<init>",cSignature
, mID
);
86 tempObj
= t
.pEnv
->NewObject( getMyClass(), mID
);
87 saveRef( t
.pEnv
, tempObj
);
88 t
.pEnv
->DeleteLocalRef( tempObj
);
92 jstring
connectivity::convertwchar_tToJavaString(JNIEnv
*pEnv
,const OUString
& _rTemp
)
94 OSL_ENSURE(pEnv
,"Environment is NULL!");
95 jstring pStr
= pEnv
->NewString(
96 reinterpret_cast<jchar
const *>(_rTemp
.getStr()), _rTemp
.getLength());
97 pEnv
->ExceptionClear();
98 OSL_ENSURE(pStr
,"Could not create a jsstring object!");
103 std::unique_ptr
<java_util_Properties
> connectivity::createStringPropertyArray(const Sequence
< PropertyValue
>& info
)
105 std::unique_ptr
<java_util_Properties
> pProps(new java_util_Properties());
106 const PropertyValue
* pBegin
= info
.getConstArray();
107 const PropertyValue
* pEnd
= pBegin
+ info
.getLength();
109 for(;pBegin
!= pEnd
;++pBegin
)
111 // these are properties used internally by LibreOffice,
112 // and should not be passed to the JDBC driver
113 // (which probably does not know anything about them anyway).
114 if ( pBegin
->Name
!= "JavaDriverClass"
115 && pBegin
->Name
!= "JavaDriverClassPath"
116 && pBegin
->Name
!= "SystemProperties"
117 && pBegin
->Name
!= "CharSet"
118 && pBegin
->Name
!= "AppendTableAliasName"
119 && pBegin
->Name
!= "AppendTableAliasInSelect"
120 && pBegin
->Name
!= "DisplayVersionColumns"
121 && pBegin
->Name
!= "GeneratedValues"
122 && pBegin
->Name
!= "UseIndexDirectionKeyword"
123 && pBegin
->Name
!= "UseKeywordAsBeforeAlias"
124 && pBegin
->Name
!= "AddIndexAppendix"
125 && pBegin
->Name
!= "FormsCheckRequiredFields"
126 && pBegin
->Name
!= "GenerateASBeforeCorrelationName"
127 && pBegin
->Name
!= "EscapeDateTime"
128 && pBegin
->Name
!= "ParameterNameSubstitution"
129 && pBegin
->Name
!= "IsPasswordRequired"
130 && pBegin
->Name
!= "IsAutoRetrievingEnabled"
131 && pBegin
->Name
!= "AutoRetrievingStatement"
132 && pBegin
->Name
!= "UseCatalogInSelect"
133 && pBegin
->Name
!= "UseSchemaInSelect"
134 && pBegin
->Name
!= "AutoIncrementCreation"
135 && pBegin
->Name
!= "Extension"
136 && pBegin
->Name
!= "NoNameLengthLimit"
137 && pBegin
->Name
!= "EnableSQL92Check"
138 && pBegin
->Name
!= "EnableOuterJoinEscape"
139 && pBegin
->Name
!= "BooleanComparisonMode"
140 && pBegin
->Name
!= "IgnoreCurrency"
141 && pBegin
->Name
!= "TypeInfoSettings"
142 && pBegin
->Name
!= "IgnoreDriverPrivileges"
143 && pBegin
->Name
!= "ImplicitCatalogRestriction"
144 && pBegin
->Name
!= "ImplicitSchemaRestriction"
145 && pBegin
->Name
!= "SupportsTableCreation"
146 && pBegin
->Name
!= "UseJava"
147 && pBegin
->Name
!= "Authentication"
148 && pBegin
->Name
!= "PreferDosLikeLineEnds"
149 && pBegin
->Name
!= "PrimaryKeySupport"
150 && pBegin
->Name
!= "RespectDriverResultSetType"
154 OSL_VERIFY( pBegin
->Value
>>= aStr
);
155 pProps
->setProperty(pBegin
->Name
,aStr
);
161 OUString
connectivity::JavaString2String(JNIEnv
*pEnv
,jstring Str
)
166 jboolean
bCopy(true);
167 const jchar
* pChar
= pEnv
->GetStringChars(Str
,&bCopy
);
168 jsize len
= pEnv
->GetStringLength(Str
);
169 aStr
= OUString(reinterpret_cast<sal_Unicode
const *>(pChar
), len
);
172 pEnv
->ReleaseStringChars(Str
,pChar
);
173 pEnv
->DeleteLocalRef(Str
);
178 jobject
connectivity::convertTypeMapToJavaMap(const Reference
< css::container::XNameAccess
> & _rMap
)
182 css::uno::Sequence
< OUString
> aNames
= _rMap
->getElementNames();
183 if ( aNames
.hasElements() )
184 ::dbtools::throwFeatureNotImplementedSQLException( u
"Type maps"_ustr
, nullptr );
189 bool connectivity::isExceptionOccurred(JNIEnv
*pEnv
)
194 jthrowable pThrowable
= pEnv
->ExceptionOccurred();
195 bool bRet
= pThrowable
!= nullptr;
198 pEnv
->ExceptionClear();
199 pEnv
->DeleteLocalRef(pThrowable
);
205 jobject
connectivity::createByteInputStream(const css::uno::Reference
< css::io::XInputStream
>& x
,sal_Int32 length
)
208 if( !t
.pEnv
|| !x
.is() )
210 // Turn off Java-Call for the constructor
211 // Initialize temporary variables
212 jclass clazz
= java_lang_Object::findMyClass("java/io/ByteArrayInputStream");
213 static jmethodID
mID(nullptr);
216 static const char * const cSignature
= "([B)V";
217 mID
= t
.pEnv
->GetMethodID( clazz
, "<init>", cSignature
);
218 OSL_ENSURE( mID
, cSignature
);
220 throw SQLException();
221 } // if ( !_inout_MethodID )
222 jbyteArray pByteArray
= t
.pEnv
->NewByteArray(length
);
223 Sequence
< sal_Int8
> aData
;
224 x
->readBytes(aData
,length
);
226 memcpy(t
.pEnv
->GetByteArrayElements(pByteArray
,&p
),aData
.getArray(),aData
.getLength());
227 jobject out
= t
.pEnv
->NewObject( clazz
, mID
,pByteArray
);
228 t
.pEnv
->DeleteLocalRef(pByteArray
);
232 jobject
connectivity::createCharArrayReader(const css::uno::Reference
< css::io::XInputStream
>& x
,sal_Int32 length
)
235 if( !t
.pEnv
|| !x
.is() )
237 // Turn off Java-Call for the constructor
238 // Initialize temporary Variables
239 jclass clazz
= java_lang_Object::findMyClass("java/io/CharArrayReader");
240 static jmethodID
mID(nullptr);
243 static const char * const cSignature
= "([C)V";
244 mID
= t
.pEnv
->GetMethodID( clazz
, "<init>", cSignature
);
245 OSL_ENSURE( mID
, cSignature
);
247 throw SQLException();
248 } // if ( !_inout_MethodID )
249 jcharArray pCharArray
= t
.pEnv
->NewCharArray(length
);
250 Sequence
< sal_Int8
> aData
;
251 x
->readBytes(aData
,length
);
253 memcpy(t
.pEnv
->GetCharArrayElements(pCharArray
,&p
),aData
.getArray(),aData
.getLength());
254 jobject out
= t
.pEnv
->NewObject( clazz
, mID
,pCharArray
);
255 t
.pEnv
->DeleteLocalRef(pCharArray
);
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */