1 <?xml version=
"1.0" encoding=
"UTF-8"?>
2 <!DOCTYPE script:module PUBLIC
"-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3 <script:module xmlns:
script=
"http://openoffice.org/2000/script" script:
name=
"Utils" script:
language=
"StarBasic">REM =======================================================================================================================
4 REM === The Access2Base library is a part of the LibreOffice project. ===
5 REM === Full documentation is available on http://www.access2base.com ===
6 REM =======================================================================================================================
10 Global _A2B_ As Variant
12 REM -----------------------------------------------------------------------------------------------------------------------
13 REM --- PRIVATE FUNCTIONS ---
14 REM -----------------------------------------------------------------------------------------------------------------------
16 Public Function _AddArray(ByVal pvArray As Variant, pvItem As Variant) As Variant
17 'Add the item at the end of the array
19 Dim vArray() As Variant
20 If IsArray(pvArray) Then vArray = pvArray Else vArray = Array()
21 ReDim Preserve vArray(LBound(vArray) To UBound(vArray) +
1)
22 vArray(UBound(vArray)) = pvItem
23 _AddArray() = vArray()
27 REM -----------------------------------------------------------------------------------------------------------------------
28 Public Function _AddNumeric(ByVal Optional pvTypes As Variant) As Variant
29 'Return on top of argument the list of all numeric types
30 'Facilitates the entry of the list of allowed types in _CheckArgument calls
32 Dim i As Integer, vNewList() As Variant, vNumeric() As Variant, iSize As Integer
33 If IsMissing(pvTypes) Then
35 ElseIf IsArray(pvTypes) Then
38 vNewList = Array(pvTypes)
41 vNumeric = Array(vbInteger, vbLong, vbSingle, vbDouble, vbCurrency, vbBigint, vbDecimal, vbBoolean)
43 iSize = UBound(vNewlist)
44 ReDim Preserve vNewList(iSize + UBound(vNumeric) +
1)
45 For i =
0 To UBound(vNumeric)
46 vNewList(iSize + i +
1) = vNumeric(i)
49 _AddNumeric = vNewList
51 End Function
' _AddNumeric V0.8
.0
53 REM -----------------------------------------------------------------------------------------------------------------------
55 Public Function _BitShift(piValue As Integer, piConstant As Integer) As Boolean
58 If piValue =
0 Then Exit Function
59 Select Case piConstant
62 Case
1,
3,
5,
7,
9,
11,
13,
15: _BitShift = True
67 Case
2,
3,
6,
7,
10,
11,
14,
15: _BitShift = True
72 Case
4,
5,
6,
7,
12,
13,
14,
15: _BitShift = True
77 Case
8,
9,
10,
11,
12,
13,
14,
15: _BitShift = True
82 End Function
' BitShift
84 REM -----------------------------------------------------------------------------------------------------------------------
85 Public Function _CalledSub() As String
86 _CalledSub = Iif(_A2B_.CalledSub =
"",
"", _GetLabel(
"CALLTO
")
& " '" & _A2B_.CalledSub
& "'")
87 End Function
' CalledSub V0.8
.9
90 REM -----------------------------------------------------------------------------------------------------------------------
91 Public Function _CheckArgument(pvItem As Variant _
92 , ByVal piArgNr As Integer _
93 , Byval pvType As Variant _
94 , ByVal Optional pvValid As Variant _
95 , ByVal Optional pvError As Boolean _
97 ' Called by public functions to check the validity of their arguments
98 ' pvItem Argument to be checked
99 ' piArgNr Argument sequence number
100 ' pvType Single value or array of allowed variable types
101 ' If of string type must contain one or more valid pseudo-object types
102 ' pvValid Single value or array of allowed values - comparison for strings is case-insensitive
103 ' pvError If True (default), error handling in this routine. False in _setProperty methods in class modules.
105 _CheckArgument = False
107 Dim iVarType As Integer
108 If IsArray(pvType) Then iVarType = VarType(pvType(LBound(pvType))) Else iVarType = VarType(pvType)
109 If iVarType = vbString Then
' pvType is a pseudo-type string
110 _CheckArgument = Utils._IsPseudo(pvItem, pvType)
112 If IsMissing(pvValid) Then _CheckArgument = Utils._IsScalar(pvItem, pvType) Else _CheckArgument = Utils._IsScalar(pvItem, pvType, pvValid)
115 If VarType(pvItem) = vbCurrency Or VarType(pvItem) = vbDecimal Or VarType(pvItem) = vbBigint Then pvItem = CDbl(pvItem)
118 If Not _CheckArgument Then
119 If IsMissing(pvError) Then pvError = True
121 TraceError(TRACEFATAL, ERRWRONGARGUMENT, Utils._CalledSub(),
0,
1, Array(piArgNr, pvItem))
125 End Function
' CheckArgument V0.9
.0
127 REM -----------------------------------------------------------------------------------------------------------------------
128 Public Function _CStr(ByVal pvArg As Variant, ByVal Optional pbShort As Boolean) As String
129 ' Convert pvArg into a readable string (truncated if too long and pbShort = True or missing)
130 ' pvArg may be a byte-array. Other arrays are rejected
132 Dim sArg As String, sObject As String, oArg As Object, sLength As String, i As Long, iMax As Long
134 Const cstByteLength =
25
135 If IsArray(pvArg) Then
136 If VarType(pvArg) = vbByte Or VarType(pvArg) -
8192 = vbByte Then
138 If pbShort And UBound(pvArg)
> cstByteLength Then iMax = cstByteLength Else iMax = UBound(pvArg)
140 sArg = sArg
& Right(
"00" & Hex(pvArg(i)),
2)
143 sArg =
"[ARRAY]
"
146 Select Case VarType(pvArg)
147 Case vbEmpty : sArg =
"[EMPTY]
"
148 Case vbNull : sArg =
"[NULL]
"
150 If IsNull(pvArg) Then
151 sArg =
"[NULL]
"
153 sObject = Utils._ImplementationName(pvArg)
154 If Utils._IsPseudo(pvArg, Array(OBJDATABASE, OBJCOLLECTION, OBJPROPERTY, OBJFORM, OBJSUBFORM, OBJCONTROL, OBJOPTIONGROUP _
155 , OBJEVENT, OBJFIELD, OBJTABLEDEF, OBJQUERYDEF, OBJRECORDSET, OBJTEMPVAR, OBJCOMMANDBAR, OBJCOMMANDBARCONTROL _
158 Set oArg = pvArg
' To avoid
"Object variable not set
" error message
159 sArg =
"[
" & oArg._Type
& "]
" & oArg._Name
160 ElseIf sObject
<> "" Then
161 sArg =
"[
" & sObject
& "]
"
163 sArg =
"[OBJECT]
"
166 Case vbVariant : sArg =
"[VARIANT]
"
167 Case vbString : sArg = pvArg
168 Case vbBoolean : sArg = Iif(pvArg,
"TRUE
",
"FALSE
")
169 Case vbByte : sArg = Right(
"00" & Hex(pvArg),
2)
170 Case Else : sArg = CStr(pvArg)
173 If IsMissing(pbShort) Then pbShort = True
174 If pbShort And Len(sArg)
> cstLength Then
175 sLength =
"(
" & Len(sArg)
& ")
"
176 sArg = Left(sArg, cstLength -
5 - Len(slength))
& " ...
" & sLength
180 End Function
' CStr V0.9
.5
182 REM -----------------------------------------------------------------------------------------------------------------------
183 Public Function _DecimalPoint() As String
184 'Return locale decimal point
185 _DecimalPoint = Mid(Format(
0,
"0.0"),
2,
1)
188 REM -----------------------------------------------------------------------------------------------------------------------
189 Private Function _ExtensionLocation() As String
190 ' Return the URL pointing to the location where OO installed the Access2Base extension
191 ' Adapted from http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Extensions/Location_of_Installed_Extensions
193 Dim oPip As Object, sLocation As String
194 Set oPip = GetDefaultContext.getByName(
"/singletons/com.sun.star.deployment.PackageInformationProvider
")
195 _ExtensionLocation = oPip.getPackageLocation(
"Access2Base
")
197 End Function
' ExtensionLocation
199 REM -----------------------------------------------------------------------------------------------------------------------
200 Private Function _GetDialogLib() As Object
201 ' Return actual Access2Base dialogs library
203 Dim oDialogLib As Object
205 Set oDialogLib = DialogLibraries
206 If oDialogLib.hasByName(
"Access2BaseDev
") Then
207 If Not oDialogLib.IsLibraryLoaded(
"Access2BaseDev
") Then oDialogLib.loadLibrary(
"Access2BaseDev
")
208 Set _GetDialogLib = DialogLibraries.Access2BaseDev
209 ElseIf oDialogLib.hasByName(
"Access2Base
") Then
210 If Not oDialogLib.IsLibraryLoaded(
"Access2Base
") Then oDialogLib.loadLibrary(
"Access2Base
")
211 Set _GetDialogLib = DialogLibraries.Access2Base
213 Set _GetDialogLib = Nothing
218 REM -----------------------------------------------------------------------------------------------------------------------
219 Private Function _GetResultSetColumnValue(poResultSet As Object _
220 , ByVal piColIndex As Integer _
221 , Optional ByVal pbReturnBinary As Boolean _
223 REM Modified from Roberto Benitez
's BaseTools
224 REM get the data for the column specified by ColIndex
225 REM If pbReturnBinary = False (default) then return length of binary field
226 REM get type name from metadata
228 Dim vValue As Variant, iType As Integer, vDateTime As Variant, oValue As Object
229 Dim bNullable As Boolean, lSize As Long
230 Const cstMaxTextLength =
65535
231 Const cstMaxBinlength =
2 *
65535
233 On Local Error Goto
0 ' Disable error handler
234 vValue = Null
' Default value if error
235 If IsMissing(pbReturnBinary) Then pbReturnBinary = False
236 With com.sun.star.sdbc.DataType
237 iType = poResultSet.MetaData.getColumnType(piColIndex)
238 bNullable = ( poResultSet.MetaData.IsNullable(piColIndex) = com.sun.star.sdbc.ColumnValue.NULLABLE )
240 Case .ARRAY : vValue = poResultSet.getArray(piColIndex)
241 Case .BINARY, .VARBINARY, .LONGVARBINARY, .BLOB
242 Set oValue = poResultSet.getBinaryStream(piColIndex)
244 If Not poResultSet.wasNull() Then
245 If Not _hasUNOMethod(oValue,
"getLength
") Then
' When no recordset
246 lSize = cstMaxBinLength
248 lSize = CLng(oValue.getLength())
250 If lSize
<= cstMaxBinLength And pbReturnBinary Then
252 oValue.readBytes(vValue, lSize)
253 Else
' Return length of field, not content
259 Case .BIT, .BOOLEAN : vValue = poResultSet.getBoolean(piColIndex)
260 Case .DATE : vDateTime = poResultSet.getDate(piColIndex)
261 If Not poResultSet.wasNull() Then vValue = DateSerial(CInt(vDateTime.Year), CInt(vDateTime.Month), CInt(vDateTime.Day))
262 Case .DISTINCT, .OBJECT, .OTHER, .STRUCT
264 Case .DOUBLE, .REAL : vValue = poResultSet.getDouble(piColIndex)
265 Case .FLOAT : vValue = poResultSet.getFloat(piColIndex)
266 Case .INTEGER, .SMALLINT : vValue = poResultSet.getInt(piColIndex)
267 Case .BIGINT : vValue = poResultSet.getLong(piColIndex)
268 Case .DECIMAL, .NUMERIC : vValue = poResultSet.getDouble(piColIndex)
269 Case .SQLNULL : vValue = poResultSet.getNull(piColIndex)
270 Case .OBJECT, .OTHER, .STRUCT : vValue = Null
271 Case .REF : vValue = poResultSet.getRef(piColIndex)
272 Case .TINYINT : vValue = poResultSet.getShort(piColIndex)
273 Case .CHAR, .VARCHAR : vValue = poResultSet.getString(piColIndex)
274 Case .LONGVARCHAR, .CLOB
275 Set oValue = poResultSet.getCharacterStream(piColIndex)
277 If Not poResultSet.wasNull() Then
278 If Not _hasUNOMethod(oValue,
"getLength
") Then
' When no recordset
279 lSize = cstMaxTextLength
281 lSize = CLng(oValue.getLength())
284 If lSize
<= cstMaxBinLength Then vValue = poResultSet.getString(piColIndex) Else vValue =
""
289 Case .TIME : vDateTime = poResultSet.getTime(piColIndex)
290 If Not poResultSet.wasNull() Then vValue = TimeSerial(vDateTime.Hours, vDateTime.Minutes, vDateTime.Seconds)
', vDateTime.HundredthSeconds)
291 Case .TIMESTAMP : vDateTime = poResultSet.getTimeStamp(piColIndex)
292 If Not poResultSet.wasNull() Then vValue = DateSerial(CInt(vDateTime.Year), CInt(vDateTime.Month), CInt(vDateTime.Day)) _
293 + TimeSerial(vDateTime.Hours, vDateTime.Minutes, vDateTime.Seconds)
', vDateTime.HundredthSeconds)
295 vValue = poResultSet.getString(piColIndex)
'GIVE STRING A TRY
296 If IsNumeric(vValue) Then vValue = Val(vValue)
'Required when type =
"", sometimes numeric fields are returned as strings (query/MSAccess)
299 If poResultSet.wasNull() Then vValue = Null
303 _GetResultSetColumnValue = vValue
305 End Function
' GetResultSetColumnValue V
1.5.0
307 REM -----------------------------------------------------------------------------------------------------------------------
308 Public Function _FinalProperty(psShortcut As String) As String
309 ' Return the final property of a shortcut
311 Const cstEXCLAMATION =
"!
"
312 Const cstDOT =
".
"
314 Dim iCurrentIndex As Integer, vCurrentObject As Variant, sCurrentProperty As String
315 Dim sComponents() As String, sSubComponents() As String
316 _FinalProperty =
""
317 sComponents = Split(Trim(psShortcut), cstEXCLAMATION)
318 If UBound(sComponents) =
0 Then Exit Function
319 sSubComponents = Split(sComponents(UBound(sComponents)), cstDOT)
320 Select Case UBound(sSubComponents)
322 _FinalProperty = sSubComponents(
1)
327 End Function
' FinalProperty
329 REM -----------------------------------------------------------------------------------------------------------------------
330 Public Function _GetProductName(ByVal Optional psFlag As String) as String
331 'Return OO product (
"PRODUCT
") and version numbers (
"VERSION
")
332 'Derived from Tools library
334 Dim oProdNameAccess as Object
335 Dim sVersion as String
336 Dim sProdName as String
337 If IsMissing(psFlag) Then psFlag =
"ALL
"
338 oProdNameAccess = _GetRegistryKeyContent(
"org.openoffice.Setup/Product
")
339 sProdName = oProdNameAccess.getByName(
"ooName
")
340 sVersion = oProdNameAccess.getByName(
"ooSetupVersionAboutBox
")
342 Case
"ALL
" : _GetProductName = sProdName
& " " & sVersion
343 Case
"PRODUCT
" : _GetProductName = sProdName
344 Case
"VERSION
" : _GetProductName = sVersion
346 End Function
' GetProductName V1.0
.0
348 REM -----------------------------------------------------------------------------------------------------------------------
349 Public Function _GetRandomFileName(ByVal psName As String) As String
350 ' Return the full name of a random temporary file suffixed by psName
352 Dim sRandom As String
353 sRandom = Right(
"000000" & Int(
999999 * Rnd),
6)
354 _GetRandomFileName = Utils._getTempDirectoryURL()
& "/
" & "A2B_TEMP_
" & psName
& "_
" & sRandom
356 End Function
' GetRandomFileName
358 REM -----------------------------------------------------------------------------------------------------------------------
359 Public Function _GetRegistryKeyContent(sKeyName as string, Optional bforUpdate as Boolean) As Variant
360 'Implement ConfigurationProvider service
361 'Derived from Tools library
363 Dim oConfigProvider as Object
364 Dim aNodePath(
0) as new com.sun.star.beans.PropertyValue
365 oConfigProvider = createUnoService(
"com.sun.star.configuration.ConfigurationProvider
")
366 aNodePath(
0).Name =
"nodepath
"
367 aNodePath(
0).Value = sKeyName
368 If IsMissing(bForUpdate) Then bForUpdate = False
370 _GetRegistryKeyContent() = oConfigProvider.createInstanceWithArguments(
"com.sun.star.configuration.ConfigurationUpdateAccess
", aNodePath())
372 _GetRegistryKeyContent() = oConfigProvider.createInstanceWithArguments(
"com.sun.star.configuration.ConfigurationAccess
", aNodePath())
374 End Function
' GetRegistryKeyContent V0.8
.5
376 REM -----------------------------------------------------------------------------------------------------------------------
377 Public Function _getTempDirectoryURL() As String
378 ' Return the temporary directory defined in the OO Options (Paths)
379 Dim sDirectory As String, oSettings As Object, oPathSettings As Object
381 If _ErrorHandler() Then On Local Error Goto Error_Function
383 _getTempDirectoryURL =
""
384 oPathSettings = createUnoService(
"com.sun.star.util.PathSettings
" )
385 sDirectory = oPathSettings.GetPropertyValue(
"Temp
" )
387 _getTempDirectoryURL = sDirectory
392 TraceError(
"ERROR
", Err,
"_getTempDirectoryURL
", Erl)
393 _getTempDirectoryURL =
""
395 End Function
' _getTempDirectoryURL V0.8
.5
397 REM -----------------------------------------------------------------------------------------------------------------------
398 Public Function _getUNOTypeName(pvObject As Variant) As String
399 ' Return the symbolic name of the pvObject (UNO-object) type
400 ' Code-snippet from XRAY
402 Dim oService As Object, vClass as Variant
403 _getUNOTypeName =
""
404 On Local Error Resume Next
405 oService = CreateUnoService(
"com.sun.star.reflection.CoreReflection
")
406 vClass = oService.getType(pvObject)
407 If vClass.TypeClass = com.sun.star.uno.TypeClass.STRUCT Then
408 _getUNOTypeName = vClass.Name
412 End Function
' getUNOTypeName
414 REM -----------------------------------------------------------------------------------------------------------------------
415 Public Function _hasUNOMethod(pvObject As Variant, psMethod As String) As Boolean
416 ' Return true if pvObject has the (UNO) method psMethod
417 ' Code-snippet found in Bernard Marcelly
's XRAY
419 Dim vInspect as Variant
420 _hasUNOMethod = False
421 On Local Error Resume Next
422 vInspect = _A2B_.Introspection.Inspect(pvObject)
423 _hasUNOMethod = vInspect.hasMethod(psMethod, com.sun.star.beans.MethodConcept.ALL)
425 End Function
' hasUNOMethod V0.8
.0
427 REM -----------------------------------------------------------------------------------------------------------------------
428 Public Function _hasUNOProperty(pvObject As Variant, psProperty As String) As Boolean
429 ' Return true if pvObject has the (UNO) property psProperty
430 ' Code-snippet found in Bernard Marcelly
's XRAY
432 Dim vInspect as Variant
433 _hasUNOProperty = False
434 On Local Error Resume Next
435 vInspect = _A2B_.Introspection.Inspect(pvObject)
436 _hasUNOProperty = vInspect.hasProperty(psProperty, com.sun.star.beans.PropertyConcept.ALL)
438 End Function
' hasUNOProperty V0.8
.0
440 REM -----------------------------------------------------------------------------------------------------------------------
441 Public Function _ImplementationName(pvObject As Variant) As String
442 ' Use getImplementationName method or _getUNOTypeName function
444 Dim sObjectType As String
445 On Local Error Resume Next
446 sObjectType = pvObject.getImplementationName()
447 If sObjectType =
"" Then sObjectType = _getUNOTypeName(pvObject)
449 _ImplementationName = sObjectType
451 End Function
' ImplementationName
453 REM -----------------------------------------------------------------------------------------------------------------------
454 Public Function _InList(ByVal pvItem As Variant, pvList As Variant, ByVal Optional pvReturnValue As Variant, Optional ByVal pbBinarySearch As Boolean) As Variant
455 ' Return True if pvItem is present in the pvList array (case insensitive comparison)
456 ' Return the value in pvList if pvReturnValue = True
458 Dim i As Integer, bFound As Boolean, iListVarType As Integer, iItemVarType As Integer
459 Dim iTop As Integer, iBottom As Integer, iFound As Integer
460 iItemVarType = VarType(pvItem)
461 If IsMissing(pvReturnValue) Then pvReturnValue = False
462 If iItemVarType = vbNull Or IsNull(pvList) Then
464 ElseIf Not IsArray(pvList) Then
465 If iItemVarType = vbString Then bFound = ( UCase(pvItem) = UCase(pvList) ) Else bFound = ( pvItem = pvList )
466 If Not pvReturnValue Then
469 If bFound Then _InList = pvList Else _InList = False
471 ElseIf UBound(pvList)
< LBound(pvList) Then
' Array not initialized
476 iListVarType = VarType(pvList(LBound(pvList)))
477 If iListVarType = iItemVarType _
478 Or ( (iListVarType = vbInteger Or iListVarType = vbLong Or iListVarType = vbSingle Or iListVarType = vbDouble _
479 Or iListVarType = vbCurrency Or iListVarType = vbBigint Or iListVarType = vbDecimal) _
480 And (iItemVarType = vbInteger Or iItemVarType = vbLong Or iItemVarType = vbSingle Or iItemVarType = vbDouble _
481 Or iItemVarType = vbCurrency Or iItemVarType = vbBigint Or iItemVarType = vbDecimal) _
483 If IsMissing(pbBinarySearch) Then pbBinarySearch = False
484 If Not pbBinarySearch Then
' Linear search
485 For i = LBound(pvList) To UBound(pvList)
486 If iItemVarType = vbString Then bFound = ( UCase(pvItem) = UCase(pvList(i)) ) Else bFound = ( pvItem = pvList(i) )
492 Else
' Binary search =
> array must be sorted
493 iTop = UBound(pvList)
494 iBottom = lBound(pvList)
496 iFound = (iTop + iBottom) /
2
497 If ( iItemVarType = vbString And UCase(pvItem)
> UCase(pvList(iFound)) ) Or ( iItemVarType
<> vbString And pvItem
> pvList(iFound) ) Then
502 If iItemVarType = vbString Then bFound = ( UCase(pvItem) = UCase(pvList(iFound)) ) Else bFound = ( pvItem = pvList(iFound) )
503 Loop Until ( bFound ) Or ( iBottom
> iTop )
506 If Not pvReturnValue Then _InList = True Else _InList = pvList(iFound)
513 End Function
' InList V1.1
.0
515 REM -----------------------------------------------------------------------------------------------------------------------
516 Public Function _InspectPropertyType(poObject As Object, psProperty As String) As String
517 'Return type of property EVEN WHEN EMPTY ! (Used in date and time controls)
519 Dim oInspect1 As Object, oInspect2 As Object, oInspect3 As Object
520 ' On Local Error Resume Next
521 _InspectPropertyType =
""
522 Set oInspect1 = CreateUnoService(
"com.sun.star.script.Invocation
")
523 Set oInspect2 = oInspect1.createInstanceWithArguments(Array(poObject)).IntroSpection
524 If Not IsNull(oInspect2) Then
525 Set oInspect3 = oInspect2.getProperty(psProperty, com.sun.star.beans.PropertyConcept.ALL)
526 If Not IsNull(oInspect3) Then _InspectPropertyType = oInspect3.Type.Name
528 Set oInspect1 = Nothing : Set oInspect2 = Nothing : Set oInspect3 = Nothing
530 End Function
' InspectPropertyType V1.0
.0
532 REM -----------------------------------------------------------------------------------------------------------------------
533 Public Function _IsLeft(psString As String, psLeft As String) As Boolean
534 ' Return True if left part of psString = psLeft
536 Dim iLength As Integer
537 iLength = Len(psLeft)
539 If Len(psString)
>= iLength Then
540 If Left(psString, iLength) = psLeft Then _IsLeft = True
545 REM -----------------------------------------------------------------------------------------------------------------------
546 Public Function _IsBinaryType(ByVal lType As Long) As Boolean
548 With com.sun.star.sdbc.DataType
550 Case .BINARY, .VARBINARY, .LONGVARBINARY, .BLOB
553 _IsBinaryType = False
557 End Function
' IsBinaryType V1.6
.0
559 REM -----------------------------------------------------------------------------------------------------------------------
560 Public Function _IsPseudo(pvObject As Variant, ByVal pvType As Variant) As Boolean
561 ' Test pvObject: does it exist ?
562 ' is the _Type item = one of the proposed pvTypes ?
563 ' does the pseudo-object refer to an existing object (e.g. does the form really exist in the db) ?
565 Dim bIsPseudo As Boolean, bPseudoExists As Boolean, vObject As Variant
567 If _ErrorHandler() Then On Local Error Goto Exit_False
571 vObject = pvObject
' To avoid
"Object variable not set
" error message
573 Case IsEmpty(vObject)
575 Case VarType(vObject)
<> vbObject
581 Case ._Type =
""
583 bIsPseudo = _InList(._Type, pvType)
584 If Not bIsPseudo Then
' If primary type did not succeed, give the subtype a chance
585 If ._Type = OBJCONTROL Then bIsPseudo = _InList(._SubType, pvType)
591 If Not bIsPseudo Then Goto Exit_Function
593 Dim oDoc As Object, oForms As Variant
595 bPseudoExists = False
599 If ._Name
<> "" Then
' Check validity of form name
600 Set oDoc = _A2B_.CurrentDocument()
601 If oDoc.DbConnect = DBCONNECTFORM Then
604 Set oForms = oDoc.Document.getFormDocuments()
605 bPseudoExists = ( oForms.HasByName(._Name) )
609 If ._DbConnect = DBCONNECTFORM Then bPseudoExists = True Else bPseudoExists = Not IsNull(.Connection)
611 If ._Name
<> "" Then
' Check validity of dialog name
612 bPseudoExists = ( _A2B_.hasItem(COLLALLDIALOGS, ._Name) )
617 If Not IsNull(.ControlModel) And ._Name
<> "" Then
' Check validity of control
618 Set oForms = .ControlModel.Parent
619 bPseudoExists = ( oForms.hasByName(._Name) )
622 If Not IsNull(.DatabaseForm) And ._Name
<> "" Then
' Check validity of subform
623 If .DatabaseForm.ImplementationName =
"com.sun.star.comp.forms.ODatabaseForm
" Then
624 Set oForms = .DatabaseForm.Parent
625 bPseudoExists = ( oForms.hasByName(._Name) )
629 bPseudoExists = ( .Count
> 0 )
631 bPseudoExists = ( Not IsNull(._Window) )
632 Case OBJCOMMANDBARCONTROL
633 bPseudoExists = ( Not IsNull(._ParentCommandBar) )
635 bPseudoExists = ( Not IsNull(._EventSource) )
637 bPseudoExists = ( ._Name
<> "" )
639 bPseudoExists = ( ._Name
<> "" And Not IsNull(.Table) )
641 bPseudoExists = ( ._Name
<> "" And Not IsNull(.Query) )
643 bPseudoExists = ( Not IsNull(.RowSet) )
645 bPseudoExists = ( ._Name
<> "" And Not IsNull(.Column) )
647 If ._Name
<> "" Then
' Check validity of tempvar name
648 bPseudoExists = ( _A2B_.hasItem(COLLTEMPVARS, ._Name) )
654 _IsPseudo = ( bIsPseudo And bPseudoExists )
661 End Function
' IsPseudo V1.1
.0
663 REM -----------------------------------------------------------------------------------------------------------------------
664 Private Function _IsScalar(ByVal pvArg As Variant, Byval pvType As Variant, ByVal Optional pvValid As Variant) As Boolean
665 ' Check type of pvArg and value in allowed pvValid list
669 If IsArray(pvType) Then
670 If Not _InList(VarType(pvArg), pvType) Then Exit Function
671 ElseIf VarType(pvArg)
<> pvType Then
672 If pvType = vbBoolean And VarType(pvArg) = vbLong Then
673 If pvArg
< -
1 And pvArg
> 0 Then Exit Function
' Special boolean processing because the Not function returns a Long
678 If Not IsMissing(pvValid) Then
679 If Not _InList(pvArg, pvValid) Then Exit Function
686 End Function
' IsScalar V0.7
.5
688 REM -----------------------------------------------------------------------------------------------------------------------
689 Public Function _PCase(ByVal psString As String) As String
690 ' Return the proper case representation of argument
692 Dim vSubStrings() As Variant, i As Integer, iLen As Integer
693 vSubStrings = Split(psString,
" ")
694 For i =
0 To UBound(vSubStrings)
695 iLen = Len(vSubStrings(i))
697 vSubStrings(i) = UCase(Left(vSubStrings(i),
1))
& LCase(Right(vSubStrings(i), iLen -
1))
699 vSubStrings(i) = UCase(vSubStrings(i))
702 _PCase = Join(vSubStrings,
" ")
704 End Function
' PCase V0.9
.0
706 REM -----------------------------------------------------------------------------------------------------------------------
707 Private Function _PercentEncode(ByVal psChar As String) As String
708 ' Percent encoding of single psChar character
709 ' https://en.wikipedia.org/wiki/UTF-
8
711 Dim lChar As Long, sByte1 As String, sByte2 As String, sByte3 As String
715 Case
48 To
57,
65 To
90,
97 To
122 ' 0-
9, A-Z, a-z
716 _PercentEncode = psChar
717 Case Asc(
"-
"), Asc(
".
"), Asc(
"_
"), Asc(
"~
")
718 _PercentEncode = psChar
719 Case Asc(
"!
"), Asc(
"$
"), Asc(
"&"), Asc(
"'"), Asc(
"(
"), Asc(
")
"), Asc(
"*
"), Asc(
"+
"), Asc(
",
"), Asc(
";
"), Asc(
"=
")
' Reserved characters used as delimitors in query strings
720 _PercentEncode = psChar
721 Case Asc(
" "), Asc(
"%
")
722 _PercentEncode =
"%
" & Right(
"00" & Hex(lChar),
2)
724 _PercentEncode = psChar
726 sByte1 =
"%
" & Right(
"00" & Hex(Int(lChar /
64) +
192),
2)
727 sByte2 =
"%
" & Right(
"00" & Hex((lChar Mod
64) +
128),
2)
728 _PercentEncode = sByte1
& sByte2
730 sByte1 =
"%
" & Right(
"00" & Hex(Int(lChar /
4096) +
224),
2)
731 sByte2 =
"%
" & Right(
"00" & Hex(Int(lChar - (
4096 * Int(lChar /
4096))) /
64 +
128),
2)
732 sByte3 =
"%
" & Right(
"00" & Hex((lChar Mod
64) +
128),
2)
733 _PercentEncode = sByte1
& sByte2
& sByte3
734 Case Else
' Not supported
735 _PercentEncode = psChar
740 End Function
' _PercentEncode V1.4
.0
742 REM -----------------------------------------------------------------------------------------------------------------------
743 Public Function _ReadFileIntoArray(ByVal psFileName) As Variant
744 ' Loads all lines of a text file into a variant array
745 ' Any error reduces output to an empty array
746 ' Input file name presumed in URL form
748 Dim vLines() As Variant, iFile As Integer, sLine As String, iCount1 As Integer, iCount2 As Integer
749 Const cstMaxLines =
16000 ' +/- the limit of array sizes in Basic
750 On Local Error GoTo Error_Function
752 _ReadFileIntoArray = Array()
753 If psFileName =
"" Then Exit Function
756 Open psFileName For Input Access Read Shared As #iFile
758 Do While Not Eof(iFile) And iCount1
< cstMaxLines
759 Line Input #iFile, sLine
760 iCount1 = iCount1 +
1
764 ReDim vLines(
0 To iCount1 -
1)
' Reading file twice preferred to ReDim Preserve for performance reasons
766 Open psFileName For Input Access Read Shared As #iFile
768 Do While Not Eof(iFile) And iCount2
< iCount1
769 Line Input #iFile, vLines(iCount2)
770 iCount2 = iCount2 +
1
775 _ReadFileIntoArray() = vLines()
780 End Function
' _ReadFileIntoArray V1.4
.0
782 REM -----------------------------------------------------------------------------------------------------------------------
783 Public Sub _ResetCalledSub(ByVal psSub As String)
784 ' Called in bottom of each public function. _A2B_.CalledSub variable is used for error handling
785 ' Used to trace routine in/outs and to clarify error messages
786 If IsEmpty(_A2B_) Then Call Application._RootInit()
' Only is Utils module recompiled
787 If _A2B_.CalledSub = psSub Then _A2B_.CalledSub =
""
788 If _A2B_.MinimalTraceLevel =
1 Then TraceLog(TRACEDEBUG, _GetLabel(
"Exiting
")
& " " & psSub
& " ...
", False)
789 End Sub
' ResetCalledSub
791 REM -----------------------------------------------------------------------------------------------------------------------
792 Public Function _RunScript(ByVal psScript As String, Optional pvArgs() As Variant) As Boolean
793 ' Execute a given script with pvArgs() array of arguments
795 On Local Error Goto Error_Function
797 If IsNull(ThisComponent) Then Goto Exit_Function
799 Dim oSCriptProvider As Object, oScript As Object, vResult As Variant
801 Set oScriptProvider = ThisComponent.ScriptProvider()
802 Set oScript = oScriptProvider.getScript(psScript)
803 If IsMissing(pvArgs()) Then pvArgs() = Array()
804 vResult = oScript.Invoke(pvArgs(), Array(), Array())
814 REM -----------------------------------------------------------------------------------------------------------------------
815 Public Sub _SetCalledSub(ByVal psSub As String)
816 ' Called in top of each public function.
817 ' Used to trace routine in/outs and to clarify error messages
818 If IsEmpty(_A2B_) Then Call Application._RootInit()
' First use of Access2Base in current LibO/AOO session
819 If _A2B_.CalledSub =
"" Then _A2B_.CalledSub = psSub
820 If _A2B_.MinimalTraceLevel =
1 Then TraceLog(TRACEDEBUG, _GetLabel(
"Entering
")
& " " & psSub
& " ...
", False)
821 End Sub
' SetCalledSub
823 REM -----------------------------------------------------------------------------------------------------------------------
824 Public Function _Surround(ByVal psName As String) As String
825 ' Return [Name] if Name contains spaces
826 ' Return [Name1].[Name2].[Name3] if Name1.Name2.Name3 contains dots
828 Const cstSquareOpen =
"[
"
829 Const cstSquareClose =
"]
"
830 Const cstDot =
".
"
833 If InStr(psName,
".
")
> 0 Then
834 sName = Join(Split(psName, cstDot), cstSquareClose
& cstDot
& cstSquareOpen)
835 _Surround = cstSquareOpen
& sName
& cstSquareClose
836 ElseIf InStr(psName,
" ")
> 0 Then
837 _Surround = cstSquareOpen
& psName
& cstSquareClose
842 End Function
' Surround
844 REM -----------------------------------------------------------------------------------------------------------------------
845 Public Function _Trim(ByVal psString As String) As String
846 ' Remove leading and trailing spaces, remove surrounding square brackets
847 Const cstSquareOpen =
"[
"
848 Const cstSquareClose =
"]
"
851 sTrim = Trim(psString)
853 If Len(sTrim)
<=
2 Then Exit Function
855 If Left(sTrim,
1) = cstSquareOpen Then
856 If Right(sTrim,
1) = cstSquareClose Then
857 _Trim = Mid(sTrim,
2, Len(sTrim) -
2)
860 End Function
' Trim V0.9
.0
862 REM -----------------------------------------------------------------------------------------------------------------------
863 Public Function _TrimArray(pvArray As Variant) As Variant
864 ' Remove empty strings from strings array
866 Dim sTrim As String, vTrim() As Variant, i As Integer, j As Integer, iCount As Integer
868 If Not IsArray(pvArray) Then
869 If Len(Trim(pvArray))
> 0 Then vTrim = Array(pvArray) Else vTrim = Array()
870 ElseIf UBound(pvArray)
< LBound(pvArray) Then
' Array empty
874 For i = LBound(pvArray) To UBound(pvArray)
875 If Len(Trim(pvArray(i))) =
0 Then iCount = iCount +
1
879 ElseIf iCount = UBound(pvArray) - LBound(pvArray) +
1 Then
' Array empty or all blanks
882 ReDim vTrim(LBound(pvArray) To UBound(pvArray) - iCount)
884 For i = LBound(pvArray) To UBound(pvArray)
885 If Len(Trim(pvArray(i)))
> 0 Then
886 vTrim(j) = pvArray(i)
893 _TrimArray() = vTrim()
895 End Function
' TrimArray V0.9
.0
897 REM -----------------------------------------------------------------------------------------------------------------------
898 Private Function _UpdateResultSetColumnValue(piRDBMS As Integer _
899 , poResultSet As Object _
900 , ByVal piColIndex As Integer _
901 , ByVal pvValue As Variant _
903 REM store the pvValue for the column specified by ColIndex
904 REM get type name from metadata
906 Dim iType As Integer, vDateTime As Variant, oValue As Object
907 Dim bNullable As Boolean, lSize As Long, iValueType As Integer, sValueTypeName As String
908 Const cstMaxTextLength =
65535
909 Const cstMaxBinlength =
2 *
65535
911 On Local Error Goto
0 ' Disable error handler
912 _UpdateResultSetColumnValue = False
913 With com.sun.star.sdbc.DataType
914 iType = poResultSet.MetaData.getColumnType(piColIndex)
915 iValueType = VarType(pvValue)
916 sValueTypeName = UCase(poResultSet.MetaData.getColumnTypeName(piColIndex))
917 bNullable = ( poResultSet.MetaData.IsNullable(piColIndex) = com.sun.star.sdbc.ColumnValue.NULLABLE )
919 If bNullable And IsNull(pvValue) Then
920 poResultSet.updateNull(piColIndex)
923 Case .ARRAY, .DISTINCT, .OBJECT, .OTHER, .REF, .SQLNULL, .STRUCT
924 poResultSet.updateNull(piColIndex)
925 Case .BINARY, .VARBINARY, .LONGVARBINARY, .BLOB
926 poResultSet.updateBytes(piColIndex, pvValue)
927 Case .BIT, .BOOLEAN : poResultSet.updateBoolean(piColIndex, pvValue)
928 Case .DATE : vDateTime = CreateUnoStruct(
"com.sun.star.util.Date
")
929 vDateTime.Year = Year(pvValue)
930 vDateTime.Month = Month(pvValue)
931 vDateTime.Day = Day(pvValue)
932 poResultSet.updateDate(piColIndex, vDateTime)
933 Case .DECIMAL, .NUMERIC : poResultSet.updateDouble(piColIndex, pvValue)
934 Case .DOUBLE, .REAL : poResultSet.updateDouble(piColIndex, pvValue)
935 Case .FLOAT : poResultSet.updateFloat(piColIndex, pvValue)
936 Case .INTEGER, .SMALLINT : poResultSet.updateInt(piColIndex, pvValue)
937 Case .BIGINT : poResultSet.updateLong(piColIndex, pvValue)
938 Case .DECIMAL, .NUMERIC : poResultSet.updateDouble(piColIndex, pvValue)
939 Case .TINYINT : poResultSet.updateShort(piColIndex, pvValue)
940 Case .CHAR, .VARCHAR, .LONGVARCHAR, .CLOB
941 If piRDBMS = DBMS_SQLITE And InStr(sValueTypeName,
"BINARY
")
>0 Then
' Sqlite exception ... !
942 poResultSet.updateBytes(piColIndex, pvValue)
944 poResultSet.updateString(piColIndex, pvValue)
946 Case .TIME : vDateTime = CreateUnoStruct(
"com.sun.star.util.Time
")
947 vDateTime.Hours = Hour(pvValue)
948 vDateTime.Minutes = Minute(pvValue)
949 vDateTime.Seconds = Second(pvValue)
950 'vDateTime.HundredthSeconds =
0
951 poResultSet.updateTime(piColIndex, vDateTime)
952 Case .TIMESTAMP : vDateTime = CreateUnoStruct(
"com.sun.star.util.DateTime
")
953 vDateTime.Year = Year(pvValue)
954 vDateTime.Month = Month(pvValue)
955 vDateTime.Day = Day(pvValue)
956 vDateTime.Hours = Hour(pvValue)
957 vDateTime.Minutes = Minute(pvValue)
958 vDateTime.Seconds = Second(pvValue)
959 'vDateTime.HundredthSeconds =
0
960 poResultSet.updateTimestamp(piColIndex, vDateTime)
962 If bNullable Then poResultSet.updateNull(piColIndex)
968 _UpdateResultSetColumnValue = True
970 End Function
' UpdateResultSetColumnValue V
1.6.0
972 REM -----------------------------------------------------------------------------------------------------------------------
973 Private Function _URLEncode(ByVal psToEncode As String) As String
974 ' http://www.w3schools.com/tags/ref_urlencode.asp
975 ' http://xkr.us/articles/javascript/encode-compare/
976 ' http://tools.ietf.org/html/rfc3986
978 Dim sEncoded As String, sChar As String
979 Dim lCurrentChar As Long, bQuestionMark As Boolean
981 sEncoded =
""
982 bQuestionMark = False
983 For lCurrentChar =
1 To Len(psToEncode)
984 sChar = Mid(psToEncode, lCurrentChar,
1)
986 Case
" ",
"%
"
987 sEncoded = sEncoded
& _PercentEncode(sChar)
988 Case
"?
" ' Is it the first
"?
" ?
989 If bQuestionMark Then
' "?
" introduces in a URL the arguments part
990 sEncoded = sEncoded
& _PercentEncode(sChar)
992 sEncoded = sEncoded
& sChar
996 If bQuestionMark Then
997 sEncoded = sEncoded
& _PercentEncode(sChar)
999 sEncoded = sEncoded
& "/
" ' If Windows file naming ...
1002 If bQuestionMark Then
1003 sEncoded = sEncoded
& _PercentEncode(sChar)
1005 sEncoded = sEncoded
& _UTF8Encode(sChar)
' Because IE does not support %encoding in first part of URL
1010 _URLEncode = sEncoded
1012 End Function
' _URLEncode V1.4
.0
1014 REM -----------------------------------------------------------------------------------------------------------------------
1015 Private Function _UTF8Encode(ByVal psChar As String) As String
1016 ' &-encoding of single psChar character (e.g.
"é
" becomes
"&eacute;
" or numeric equivalent
1017 ' http://www.w3schools.com/charsets/ref_html_utf8.asp
1020 Case
"""" : _UTF8Encode =
"&quot;
"
1021 Case
"&" : _UTF8Encode =
"&amp;
"
1022 Case
"<" : _UTF8Encode =
"&lt;
"
1023 Case
">" : _UTF8Encode =
"&gt;
"
1024 Case
"'" : _UTF8Encode =
"&apos;
"
1025 Case
":
",
"/
",
"?
",
"#
",
"[
",
"]
",
"@
" ' Reserved characters
1026 _UTF8Encode = psChar
1027 Case Chr(
13) : _UTF8Encode =
"" ' Carriage return
1028 Case Chr(
10) : _UTF8Encode =
"<br
>" ' Line Feed
1029 Case
< Chr(
126) : _UTF8Encode = psChar
1030 Case
"€
" : _UTF8Encode =
"&euro;
"
1031 Case Else : _UTF8Encode =
"&#
" & Asc(psChar)
& ";
"
1036 End Function
' _UTF8Encode V1.4
.0