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=
"Database" 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 =======================================================================================================================
13 REM -----------------------------------------------------------------------------------------------------------------------
14 REM --- CLASS ROOT FIELDS ---
15 REM -----------------------------------------------------------------------------------------------------------------------
17 Private _Type As String
' Must be DATABASE
18 Private _This As Object
' Workaround for absence of This builtin function
19 Private _DbConnect As Integer
' DBCONNECTxxx constants
20 Private Title As String
21 Private Document As Object
' com.sun.star.comp.dba.ODatabaseDocument or SwXTextDocument or ScModelObj
22 Private Connection As Object
' com.sun.star.sdbc.drivers.OConnectionWrapper or com.sun.star.sdbc.XConnection
24 Private _ReadOnly As Boolean
25 Private MetaData As Object
' interface XDatabaseMetaData
26 Private Form As Object
' com.sun.star.form.XForm
27 Private FormName As String
28 Private RecordsetMax As Integer
29 Private RecordsetsColl As Object
' Collection of active recordsets
31 REM -----------------------------------------------------------------------------------------------------------------------
32 REM --- CONSTRUCTORS / DESTRUCTORS ---
33 REM -----------------------------------------------------------------------------------------------------------------------
34 Private Sub Class_Initialize()
39 Set Document = Nothing
40 Set Connection = Nothing
43 Set MetaData = Nothing
45 FormName =
""
47 Set RecordsetsColl = New Collection
48 End Sub
' Constructor
50 REM -----------------------------------------------------------------------------------------------------------------------
51 Private Sub Class_Terminate()
52 On Local Error Resume Next
53 Call CloseAllRecordsets()
54 If _DbConnect
<> DBCONNECTANY Then
55 If Not IsNull(Connection) Then
58 Set Connection = Nothing
63 Call Class_Initialize()
64 End Sub
' Destructor
66 REM -----------------------------------------------------------------------------------------------------------------------
68 Call Class_Terminate()
69 End Sub
' Explicit destructor
73 REM -----------------------------------------------------------------------------------------------------------------------
74 REM --- CLASS GET/LET/SET PROPERTIES ---
75 REM -----------------------------------------------------------------------------------------------------------------------
77 Property Get ObjectType() As String
78 ObjectType = _PropertyGet(
"ObjectType
")
79 End Property
' ObjectType (get)
81 REM -----------------------------------------------------------------------------------------------------------------------
82 REM --- CLASS METHODS ---
83 REM -----------------------------------------------------------------------------------------------------------------------
85 REM -----------------------------------------------------------------------------------------------------------------------
86 Public Function mClose() As Variant
87 ' Close the database
89 If _ErrorHandler() Then On Local Error Goto Error_Function
90 Const cstThisSub =
"Database.Close
"
91 Utils._SetCalledSub(cstThisSub)
93 If _DbConnect
<> DBCONNECTANY Then Goto Error_NotApplicable
97 Set Connection = Nothing
101 Utils._ResetCalledSub(cstThisSub)
104 TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(),
0,
1, cstThisSub)
107 TraceError(TRACEABORT, Err, Utils._CalledSub(), Erl)
109 End Function
' (m)Close
111 REM -----------------------------------------------------------------------------------------------------------------------
112 Public Sub CloseAllRecordsets()
113 ' Clean all recordsets for housekeeping
115 Dim sRecordsets() As String, i As Integer, oRecordset As Object
116 On Local Error Goto Exit_Sub
118 If IsNull(RecordsetsColl) Then Exit Sub
119 If RecordsetsColl.Count
< 1 Then Exit Sub
120 For i =
1 To RecordsetsColl.Count
121 Set oRecordset = RecordsetsColl.Item(i)
122 oRecordset.mClose(False)
' Do not remove entry in collection
124 Set RecordsetsColl = New Collection
129 End Sub
' CloseAllRecordsets V0.9
.5
131 REM -----------------------------------------------------------------------------------------------------------------------
132 Public Function CreateQueryDef(ByVal Optional pvQueryName As Variant _
133 , ByVal Optional pvSql As Variant _
134 , ByVal Optional pvOption As Variant _
136 'Return a (new) QueryDef object based on SQL statement
137 Const cstThisSub =
"Database.CreateQueryDef
"
138 Utils._SetCalledSub(cstThisSub)
141 Dim oQuery As Object, oQueries As Object, i As Integer, sQueryName As String
143 If _ErrorHandler() Then On Local Error Goto Error_Function
145 Set CreateQueryDef = Nothing
146 If _DbConnect
<> DBCONNECTBASE Then Goto Error_NotApplicable
147 If IsMissing(pvQueryName) Then Call _TraceArguments()
148 If IsMissing(pvSql) Then Call _TraceArguments()
149 If IsMissing(pvOption) Then pvOption = cstNull
151 If Not Utils._CheckArgument(pvQueryName,
1, vbString) Then Goto Exit_Function
152 If pvQueryName =
"" Then Call _TraceArguments()
153 If Not Utils._CheckArgument(pvSql,
2, vbString) Then Goto Exit_Function
154 If pvSql =
"" Then Call _TraceArguments()
155 If Not Utils._CheckArgument(pvOption,
3, Utils._AddNumeric(), Array(cstNull, dbSQLPassThrough)) Then Goto Exit_Function
157 If _ReadOnly Then Goto Error_NoUpdate
159 Set oQuery = CreateUnoService(
"com.sun.star.sdb.QueryDefinition
")
160 oQuery.rename(pvQueryName)
161 oQuery.Command = _ReplaceSquareBrackets(pvSql)
162 oQuery.EscapeProcessing = Not ( pvOption = dbSQLPassThrough )
164 Set oQueries = Document.DataSource.getQueryDefinitions()
166 For i =
0 To .getCount() -
1
167 sQueryName = .getByIndex(i).Name
168 If UCase(sQueryName) = UCase(pvQueryName) Then
169 TraceError(TRACEWARNING, ERRQUERYDEFDELETED, Utils._CalledSub(),
0, False, sQueryName)
170 .removeByName(sQueryName)
174 .insertByName(pvQueryName, oQuery)
176 Set CreateQueryDef = QueryDefs(pvQueryName)
179 Utils._ResetCalledSub(cstThisSub)
182 TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(),
0,
1, cstThisSub)
185 TraceError(TRACEFATAL, ERRNOTUPDATABLE, Utils._CalledSub(),
0)
188 TraceError(TRACEABORT, Err, cstThisSub, Erl)
190 End Function
' CreateQueryDef V1.1
.0
192 REM -----------------------------------------------------------------------------------------------------------------------
193 Public Function CreateTableDef(ByVal Optional pvTableName As Variant) As Object
194 'Return a (new/empty) TableDef object
195 Const cstThisSub =
"Database.CreateTableDef
"
196 Utils._SetCalledSub(cstThisSub)
198 Dim oTable As Object, oTables As Object, sTables() As String
199 Dim i As Integer, sTableName As String, oNewTable As Object
201 If _ErrorHandler() Then On Local Error Goto Error_Function
203 Set CreateTableDef = Nothing
204 If _DbConnect
<> DBCONNECTBASE Then Goto Error_NotApplicable
205 If IsMissing(pvTableName) Then Call _TraceArguments()
207 If Not Utils._CheckArgument(pvTableName,
1, vbString) Then Goto Exit_Function
208 If pvTableName =
"" Then Call _TraceArguments()
210 If _ReadOnly Then Goto Error_NoUpdate
212 Set oTables = Connection.getTables
214 sTables = .ElementNames()
215 ' Check existence of object and find its exact (case-sensitive) name
216 For i =
0 To UBound(sTables)
217 If UCase(pvTableName) = UCase(sTables(i)) Then
218 sTableName = sTables(i)
219 TraceError(TRACEWARNING, ERRTABLEDEFDELETED, Utils._CalledSub(),
0, False, sTableName)
220 .dropByName(sTableName)
224 Set oNewTable = New DataDef
225 oNewTable._Type = OBJTABLEDEF
226 oNewTable._Name = pvTableName
227 Set oNewTable._ParentDatabase = _This
228 Set oNewTable.TableDescriptor = .createDataDescriptor()
229 oNewTable.TableDescriptor.Name = pvTableName
232 Set CreateTabledef = oNewTable
235 Utils._ResetCalledSub(cstThisSub)
238 TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(),
0,
1, cstThisSub)
241 TraceError(TRACEFATAL, ERRNOTUPDATABLE, Utils._CalledSub(),
0)
244 TraceError(TRACEABORT, Err, cstThisSub, Erl)
246 End Function
' CreateTableDef V1.1
.0
248 REM -----------------------------------------------------------------------------------------------------------------------
249 Public Function DAvg( _
250 ByVal Optional psExpr As String _
251 , ByVal Optional psDomain As String _
252 , ByVal Optional pvCriteria As Variant _
254 ' Return average of scope
255 Const cstThisSub =
"Database.DAvg
"
256 Utils._SetCalledSub(cstThisSub)
257 If IsMissing(psExpr) Or IsMissing(psDomain) Then Call _TraceArguments()
258 DAvg = _DFunction(
"AVG
", psExpr, psDomain, Iif(IsMissing(pvCriteria),
"", pvCriteria),
"")
259 Utils._ResetCalledSub(cstThisSub)
260 End Function
' DAvg
262 REM -----------------------------------------------------------------------------------------------------------------------
263 Public Function DCount( _
264 ByVal Optional psExpr As String _
265 , ByVal Optional psDomain As String _
266 , ByVal Optional pvCriteria As Variant _
268 ' Return # of occurrences of scope
269 Const cstThisSub =
"Database.DCount
"
270 Utils._SetCalledSub(cstThisSub)
271 If IsMissing(psExpr) Or IsMissing(psDomain) Then Call _TraceArguments()
272 DCount = _DFunction(
"COUNT
", psExpr, psDomain, Iif(IsMissing(pvCriteria),
"", pvCriteria),
"")
273 Utils._ResetCalledSub(cstThisSub)
274 End Function
' DCount
276 REM -----------------------------------------------------------------------------------------------------------------------
277 Public Function DLookup( _
278 ByVal Optional psExpr As String _
279 , ByVal Optional psDomain As String _
280 , ByVal Optional pvCriteria As Variant _
281 , ByVal Optional pvOrderClause As Variant _
284 ' Return a value within a table
285 'Arguments: psExpr: an SQL expression
286 ' psDomain: a table- or queryname
287 ' pvCriteria: an optional WHERE clause
288 ' pcOrderClause: an optional order clause incl.
"DESC
" if relevant
289 'Return: Value of the psExpr if found, else Null.
290 'Author: inspired from Allen Browne. http://allenbrowne.com/ser-
42.html
292 ' 1. To find the last value, include DESC in the OrderClause, e.g.:
293 ' DLookup(
"[Surname]
& [FirstName]
",
"tblClient
", ,
"ClientID DESC
")
294 ' 2. To find the lowest non-null value of a field, use the Criteria, e.g.:
295 ' DLookup(
"ClientID
",
"tblClient
",
"Surname Is Not Null
" ,
"Surname
")
297 Const cstThisSub =
"Database.DLookup
"
298 Utils._SetCalledSub(cstThisSub)
299 If IsMissing(psExpr) Or IsMissing(psDomain) Then Call _TraceArguments()
300 DLookup = _DFunction(
"", psExpr, psDomain _
301 , Iif(IsMissing(pvCriteria),
"", pvCriteria) _
302 , Iif(IsMissing(pvOrderClause),
"", pvOrderClause) _
304 Utils._ResetCalledSub(cstThisSub)
305 End Function
' DLookup
307 REM -----------------------------------------------------------------------------------------------------------------------
308 Public Function DMax( _
309 ByVal Optional psExpr As String _
310 , ByVal Optional psDomain As String _
311 , ByVal Optional pvCriteria As Variant _
313 ' Return maximum of scope
314 Const cstThisSub =
"Database.DMax
"
315 Utils._SetCalledSub(cstThisSub)
316 If IsMissing(psExpr) Or IsMissing(psDomain) Then Call _TraceArguments()
317 DMax = _DFunction(
"MAX
", psExpr, psDomain, Iif(IsMissing(pvCriteria),
"", pvCriteria),
"")
318 Utils._ResetCalledSub(cstThisSub)
319 End Function
' DMax
321 REM -----------------------------------------------------------------------------------------------------------------------
322 Public Function DMin( _
323 ByVal Optional psExpr As String _
324 , ByVal Optional psDomain As String _
325 , ByVal Optional pvCriteria As Variant _
327 ' Return minimum of scope
328 Const cstThisSub =
"Database.DMin
"
329 Utils._SetCalledSub(cstThisSub)
330 If IsMissing(psExpr) Or IsMissing(psDomain) Then Call _TraceArguments()
331 DMin = _DFunction(
"MIN
", psExpr, psDomain, Iif(IsMissing(pvCriteria),
"", pvCriteria),
"")
332 Utils._ResetCalledSub(cstThisSub)
333 End Function
' DMin
335 REM -----------------------------------------------------------------------------------------------------------------------
336 Public Function DStDev( _
337 ByVal Optional psExpr As String _
338 , ByVal Optional psDomain As String _
339 , ByVal Optional pvCriteria As Variant _
341 ' Return standard deviation of scope
342 Const cstThisSub =
"Database.DStDev
"
343 Utils._SetCalledSub(cstThisSub)
344 If IsMissing(psExpr) Or IsMissing(psDomain) Then Call _TraceArguments()
345 DStDev = _DFunction(
"STDDEV_SAMP
", psExpr, psDomain, Iif(IsMissing(pvCriteria),
"", pvCriteria),
"")
' STDDEV not STDEV !
346 Utils._ResetCalledSub(cstThisSub)
347 End Function
' DStDev
349 REM -----------------------------------------------------------------------------------------------------------------------
350 Public Function DStDevP( _
351 ByVal Optional psExpr As String _
352 , ByVal Optional psDomain As String _
353 , ByVal Optional pvCriteria As Variant _
355 ' Return standard deviation of scope
356 Const cstThisSub =
"Database.DStDevP
"
357 Utils._SetCalledSub(cstThisSub)
358 If IsMissing(psExpr) Or IsMissing(psDomain) Then Call _TraceArguments()
359 DStDevP = _DFunction(
"STDDEV_POP
", psExpr, psDomain, Iif(IsMissing(pvCriteria),
"", pvCriteria),
"")
' STDDEV not STDEV !
360 Utils._ResetCalledSub(cstThisSub)
361 End Function
' DStDevP
363 REM -----------------------------------------------------------------------------------------------------------------------
364 Public Function DSum( _
365 ByVal Optional psExpr As String _
366 , ByVal Optional psDomain As String _
367 , ByVal Optional pvCriteria As Variant _
369 ' Return sum of scope
370 Const cstThisSub =
"Database.DSum
"
371 Utils._SetCalledSub(cstThisSub)
372 If IsMissing(psExpr) Or IsMissing(psDomain) Then Call _TraceArguments()
373 DSum = _DFunction(
"SUM
", psExpr, psDomain, Iif(IsMissing(pvCriteria),
"", pvCriteria),
"")
374 Utils._ResetCalledSub(cstThisSub)
375 End Function
' DSum
377 REM -----------------------------------------------------------------------------------------------------------------------
378 Public Function DVar( _
379 ByVal Optional psExpr As String _
380 , ByVal Optional psDomain As String _
381 , ByVal Optional pvCriteria As Variant _
383 ' Return variance of scope
384 Const cstThisSub =
"Database.DVar
"
385 Utils._SetCalledSub(cstThisSub)
386 If IsMissing(psExpr) Or IsMissing(psDomain) Then Call _TraceArguments()
387 DVar = _DFunction(
"VAR_SAMP
", psExpr, psDomain, Iif(IsMissing(pvCriteria),
"", pvCriteria),
"")
388 Utils._ResetCalledSub(cstThisSub)
389 End Function
' DVar
391 REM -----------------------------------------------------------------------------------------------------------------------
392 Public Function DVarP( _
393 ByVal Optional psExpr As String _
394 , ByVal Optional psDomain As String _
395 , ByVal Optional pvCriteria As Variant _
397 ' Return variance of scope
398 Const cstThisSub =
"Database.DVarP
"
399 Utils._SetCalledSub(cstThisSub)
400 If IsMissing(psExpr) Or IsMissing(psDomain) Then Call _TraceArguments()
401 DVarP = _DFunction(
"VAR_POP
", psExpr, psDomain, Iif(IsMissing(pvCriteria),
"", pvCriteria),
"")
402 Utils._ResetCalledSub(cstThisSub)
403 End Function
' DVarP
405 REM -----------------------------------------------------------------------------------------------------------------------
406 Public Function getProperty(Optional ByVal pvProperty As Variant) As Variant
407 ' Return property value of psProperty property name
409 Utils._SetCalledSub(
"Database.getProperty
")
410 If IsMissing(pvProperty) Then Call _TraceArguments()
411 getProperty = _PropertyGet(pvProperty)
412 Utils._ResetCalledSub(
"Database.getProperty
")
414 End Function
' getProperty
416 REM -----------------------------------------------------------------------------------------------------------------------
417 Public Function hasProperty(ByVal Optional pvProperty As Variant) As Boolean
418 ' Return True if object has a valid property called pvProperty (case-insensitive comparison !)
420 If IsMissing(pvProperty) Then hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList()) Else hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList(), pvProperty)
423 End Function
' hasProperty
425 REM -----------------------------------------------------------------------------------------------------------------------
426 Public Function OpenRecordset(ByVal Optional pvSource As Variant _
427 , ByVal Optional pvType As Variant _
428 , ByVal Optional pvOptions As Variant _
429 , ByVal Optional pvLockEdit As Variant _
431 'Return a Recordset object based on Source (= SQL, table or query name)
433 Const cstThisSub =
"Database.OpenRecordset
"
434 Utils._SetCalledSub(cstThisSub)
437 Dim lCommandType As Long, sCommand As String, oObject As Object
438 Dim sSource As String, i As Integer, iCount As Integer
439 Dim sObjects() As String, bFound As Boolean, oTables As Object, oQueries As Object
441 If _ErrorHandler() Then On Local Error Goto Error_Function
442 Set oObject = Nothing
443 If IsMissing(pvSource) Then Call _TraceArguments()
444 If pvSource =
"" Then Call _TraceArguments()
445 If IsMissing(pvType) Then
448 If Not Utils._CheckArgument(pvType,
1, Utils._AddNumeric(), dbOpenForwardOnly) Then Goto Exit_Function
450 If IsMissing(pvOptions) Then
453 If Not Utils._CheckArgument(pvOptions,
2, Utils._AddNumeric(), dbSQLPassThrough) Then Goto Exit_Function
455 If IsMissing(pvLockEdit) Then
458 If Not Utils._CheckArgument(pvLockEdit,
3, Utils._AddNumeric(), dbReadOnly) Then Goto Exit_Function
461 sSource = Split(UCase(Trim(pvSource)),
" ")(
0)
463 Case sSource =
"SELECT
"
464 lCommandType = com.sun.star.sdb.CommandType.COMMAND
465 sCommand = _ReplaceSquareBrackets(pvSource)
467 sSource = UCase(Trim(pvSource))
469 Set oTables = Connection.getTables
470 sObjects = oTables.ElementNames()
472 For i =
0 To UBound(sObjects)
473 If sSource = UCase(sObjects(i)) Then
474 sCommand = sObjects(i)
480 lCommandType = com.sun.star.sdb.CommandType.TABLE
483 Set oQueries = Connection.getQueries
484 sObjects = oQueries.ElementNames()
485 For i =
0 To UBound(sObjects)
486 If sSource = UCase(sObjects(i)) Then
487 sCommand = sObjects(i)
492 If Not bFound Then Goto Trace_NotFound
493 lCommandType = com.sun.star.sdb.CommandType.QUERY
497 Set oObject = New Recordset
499 ._CommandType = lCommandType
503 ._ForwardOnly = ( pvType = dbOpenForwardOnly )
504 ._PassThrough = ( pvOptions = dbSQLPassThrough )
505 ._ReadOnly = ( (pvLockEdit = dbReadOnly) Or _ReadOnly )
506 Set ._ParentDatabase = _This
508 RecordsetMax = RecordsetMax +
1
509 ._Name = Format(RecordsetMax,
"0000000")
510 RecordsetsColl.Add(oObject, UCase(._Name))
513 If Not ( oObject._BOF And oObject._EOF ) Then oObject.MoveFirst()
' Do nothing if resultset empty
516 Set OpenRecordset = oObject
517 Set oObject = Nothing
518 Utils._ResetCalledSub(cstThisSub)
521 TraceError(TRACEABORT, Err, cstThisSub, Erl)
524 TraceError(TRACEFATAL, ERROBJECTNOTFOUND, Utils._CalledSub(),
0, , Array(_GetLabel(
"TABLE
")
& "/
" & _GetLabel(
"QUERY
"), pvSource))
526 End Function
' OpenRecordset V1.1
.0
528 REM -----------------------------------------------------------------------------------------------------------------------
529 Public Function OpenSQL(Optional ByVal pvSQL As Variant _
530 , Optional ByVal pvOption As Variant _
532 ' Return True if the execution of the SQL statement was successful
533 ' SQL must contain a SELECT query
534 ' pvOption can force pass through mode
536 If _ErrorHandler() Then On Local Error Goto Error_Function
538 Const cstThisSub =
"Database.OpenSQL
"
539 Utils._SetCalledSub(cstThisSub)
542 If IsMissing(pvSQL) Then Call _TraceArguments()
543 If Not Utils._CheckArgument(pvSQL,
1, vbString) Then Goto Exit_Function
545 If IsMissing(pvOption) Then
548 If Not Utils._CheckArgument(pvOption,
2, Utils._AddNumeric(), Array(dbSQLPassThrough, cstNull)) Then Goto Exit_Function
550 If _DbConnect
<> DBCONNECTBASE And _DbConnect
<> DBCONNECTFORM Then Goto Error_NotApplicable
552 Dim oURL As New com.sun.star.util.URL, oDispatch As Object
553 Dim vArgs(
8) as New com.sun.star.beans.PropertyValue
555 oURL.Complete =
".component:DB/DataSourceBrowser
"
556 oDispatch = StarDesktop.queryDispatch(oURL,
"_Blank
",
8)
558 vArgs(
0).Name =
"ActiveConnection
" : vArgs(
0).Value = Connection
559 vArgs(
1).Name =
"CommandType
" : vArgs(
1).Value = com.sun.star.sdb.CommandType.COMMAND
560 vArgs(
2).Name =
"Command
" : vArgs(
2).Value = _ReplaceSquareBrackets(pvSQL)
561 vArgs(
3).Name =
"ShowMenu
" : vArgs(
3).Value = True
562 vArgs(
4).Name =
"ShowTreeView
" : vArgs(
4).Value = False
563 vArgs(
5).Name =
"ShowTreeViewButton
" : vArgs(
5).Value = False
564 vArgs(
6).Name =
"Filter
" : vArgs(
6).Value =
""
565 vArgs(
7).Name =
"ApplyFilter
" : vArgs(
7).Value = False
566 vArgs(
8).Name =
"EscapeProcessing
" : vArgs(
8).Value = CBool(Not ( pvOption = dbSQLPassThrough ))
568 oDispatch.dispatch(oURL, vArgs)
574 TraceError(TRACEABORT, Err,
"OpenSQL
", Erl)
577 TraceError(TRACEFATAL, ERRSQLSTATEMENT, Utils._CalledSub(),
0, , pvSQL)
580 TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(),
0,
1, cstThisSub)
582 End Function
' OpenSQL V1.1
.0
584 REM -----------------------------------------------------------------------------------------------------------------------
585 Public Function Properties(ByVal Optional pvIndex As Variant) As Variant
587 ' a Collection object if pvIndex absent
588 ' a Property object otherwise
590 Utils._SetCalledSub(
"Database.Properties
")
591 Dim vProperty As Variant, vPropertiesList() As Variant, sObject As String
592 vPropertiesList = _PropertiesList()
593 sObject = Utils._PCase(_Type)
594 If IsMissing(pvIndex) Then
595 vProperty = PropertiesGet._Properties(sObject,
"", vPropertiesList)
597 vProperty = PropertiesGet._Properties(sObject,
"", vPropertiesList, pvIndex)
598 vProperty._Value = _PropertyGet(vPropertiesList(pvIndex))
600 Set vProperty._ParentDatabase = _This
603 Set Properties = vProperty
604 Utils._ResetCalledSub(
"Database.Properties
")
606 End Function
' Properties
608 REM -----------------------------------------------------------------------------------------------------------------------
609 Public Function QueryDefs(ByVal Optional pvIndex As Variant, ByVal Optional pbCheck As Boolean) As Object
610 ' Collect all Queries in the database
611 ' pbCheck unpublished
613 If _ErrorHandler() Then On Local Error Goto Error_Function
614 Utils._SetCalledSub(
"Database.QueryDefs
")
615 If IsMissing(pbCheck) Then pbCheck = False
617 Dim sObjects() As String, sObjectName As String, oObject As Object
618 Dim i As Integer, bFound As Boolean, oQueries As Object
619 Set oObject = Nothing
620 If Not IsMissing(pvIndex) Then
621 If Not Utils._CheckArgument(pvIndex,
1, Utils._AddNumeric(vbString)) Then Goto Exit_Function
624 Set oQueries = Connection.getQueries
625 sObjects = oQueries.ElementNames()
627 Case IsMissing(pvIndex)
628 Set oObject = New Collect
629 oObject._CollType = COLLQUERYDEFS
630 oObject._ParentType = OBJDATABASE
631 oObject._ParentName =
""
632 Set oObject._ParentDatabase = _This
633 oObject._Count = UBound(sObjects) +
1
635 Case VarType(pvIndex) = vbString
637 ' Check existence of object and find its exact (case-sensitive) name
638 For i =
0 To UBound(sObjects)
639 If UCase(pvIndex) = UCase(sObjects(i)) Then
640 sObjectName = sObjects(i)
645 If Not bFound Then Goto Trace_NotFound
646 Case Else
' pvIndex is numeric
647 If pvIndex
< 0 Or pvIndex
> UBound(sObjects) Then Goto Trace_IndexError
648 sObjectName = sObjects(pvIndex)
651 Set oObject = New DataDef
652 oObject._Type = OBJQUERYDEF
653 oObject._Name = sObjectName
654 Set oObject._ParentDatabase = _This
655 oObject._readOnly = _ReadOnly
656 Set oObject.Query = oQueries.getByName(sObjectName)
659 Set QueryDefs = oObject
660 Set oObject = Nothing
661 Utils._ResetCalledSub(
"Database.QueryDefs
")
664 TraceError(TRACEABORT, Err,
"Database.QueryDefs
", Erl)
667 If Not pbCheck Then TraceError(TRACEFATAL, ERROBJECTNOTFOUND, Utils._CalledSub(),
0, , Array(_GetLabel(
"QUERY
"), pvIndex))
670 TraceError(TRACEFATAL, ERRCOLLECTION, Utils._CalledSub(),
0)
672 End Function
' QueryDefs V1.1
.0
674 REM -----------------------------------------------------------------------------------------------------------------------
675 Public Function Recordsets(ByVal Optional pvIndex As Variant) As Object
676 ' Collect all active recordsets
678 If _ErrorHandler() Then On Local Error Goto Error_Function
679 Utils._SetCalledSub(
"Database.Recordsets
")
681 Set Recordsets = Nothing
682 If Not IsMissing(pvIndex) Then
683 If Not Utils._CheckArgument(pvIndex,
1, Utils._AddNumeric(vbString)) Then Goto Exit_Function
686 Dim sObjects() As String, sObjectName As String, oObject As Object
687 Dim i As Integer, bFound As Boolean, oTables As Object
690 Case IsMissing(pvIndex)
691 Set oObject = New Collect
692 oObject._CollType = COLLRECORDSETS
693 oObject._ParentType = OBJDATABASE
694 oObject._ParentName =
""
695 Set oObject._ParentDatabase = _This
696 oObject._Count = RecordsetsColl.Count
697 Case VarType(pvIndex) = vbString
698 bFound = _hasRecordset(pvIndex)
699 If Not bFound Then Goto Trace_NotFound
700 Set oObject = RecordsetsColl.Item(pvIndex)
701 Case Else
' pvIndex is numeric
702 If pvIndex
< 0 Or pvIndex
>= RecordsetsColl.Count Then Goto Trace_IndexError
703 Set oObject = RecordsetsColl.Item(pvIndex +
1)
' Collection members are numbered
1 ... Count
707 Set Recordsets = oObject
708 Set oObject = Nothing
709 Utils._ResetCalledSub(
"Database.Recordsets
")
712 TraceError(TRACEABORT, Err,
"Database.Recordsets
", Erl)
715 TraceError(TRACEFATAL, ERROBJECTNOTFOUND, Utils._CalledSub(),
0, , Array(_GetLabel(
"RECORDSET
"), pvIndex))
718 TraceError(TRACEFATAL, ERRCOLLECTION, Utils._CalledSub(),
0)
720 End Function
' Recordsets V0.9
.5
722 REM -----------------------------------------------------------------------------------------------------------------------
723 Public Function RunSQL(Optional ByVal pvSQL As Variant _
724 , Optional ByVal pvOption As Variant _
726 ' Return True if the execution of the SQL statement was successful
727 ' SQL must contain an ACTION query
729 If _ErrorHandler() Then On Local Error Goto Error_Function
731 Utils._SetCalledSub(
"RunSQL
")
734 If IsMissing(pvSQL) Then Call _TraceArguments()
735 If Not Utils._CheckArgument(pvSQL,
1, vbString) Then Goto Exit_Function
737 If IsMissing(pvOption) Then
740 If Not Utils._CheckArgument(pvOption,
2, Utils._AddNumeric(), Array(cstNull, dbSQLPassThrough)) Then Goto Exit_Function
743 Dim oStatement As Object, vResult As Variant
744 Set oStatement = Connection.createStatement()
745 oStatement.EscapeProcessing = Not ( pvOption = dbSQLPassThrough )
746 On Local Error Goto SQL_Error
747 vResult = oStatement.executeUpdate(_ReplaceSquareBrackets(pvSQL))
748 On Local Error Goto Error_Function
754 TraceError(TRACEABORT, Err,
"RunSQL
", Erl)
757 TraceError(TRACEFATAL, ERRSQLSTATEMENT, Utils._CalledSub(),
0, , pvSQL)
759 End Function
' RunSQL V1.1
.0
761 REM -----------------------------------------------------------------------------------------------------------------------
762 Public Function TableDefs(ByVal Optional pvIndex As Variant, ByVal Optional pbCheck As Boolean) As Object
763 ' Collect all tables in the database
764 ' pbCheck unpublished
766 If _ErrorHandler() Then On Local Error Goto Error_Function
767 Utils._SetCalledSub(
"Database.TableDefs
")
768 If IsMissing(pbCheck) Then pbCheck = False
770 Dim sObjects() As String, sObjectName As String, oObject As Object
771 Dim i As Integer, bFound As Boolean, oTables As Object
772 Set oObject = Nothing
773 If Not IsMissing(pvIndex) Then
774 If Not Utils._CheckArgument(pvIndex,
1, Utils._AddNumeric(vbString)) Then Goto Exit_Function
777 Set oTables = Connection.getTables
778 sObjects = oTables.ElementNames()
780 Case IsMissing(pvIndex)
781 Set oObject = New Collect
782 oObject._CollType = COLLTABLEDEFS
783 oObject._ParentType = OBJDATABASE
784 oObject._ParentName =
""
785 Set oObject._ParentDatabase = _This
786 oObject._Count = UBound(sObjects) +
1
788 Case VarType(pvIndex) = vbString
790 ' Check existence of object and find its exact (case-sensitive) name
791 For i =
0 To UBound(sObjects)
792 If UCase(pvIndex) = UCase(sObjects(i)) Then
793 sObjectName = sObjects(i)
798 If Not bFound Then Goto Trace_NotFound
799 Case Else
' pvIndex is numeric
800 If pvIndex
< 0 Or pvIndex
> UBound(sObjects) Then Goto Trace_IndexError
801 sObjectName = sObjects(pvIndex)
804 Set oObject = New DataDef
805 oObject._Type = OBJTABLEDEF
806 oObject._Name = sObjectName
807 Set oObject._ParentDatabase = _This
808 oObject._ReadOnly = _ReadOnly
809 Set oObject.Table = oTables.getByName(sObjectName)
812 Set TableDefs = oObject
813 Set oObject = Nothing
814 Utils._ResetCalledSub(
"Database.TableDefs
")
817 TraceError(TRACEABORT, Err,
"Database.TableDefs
", Erl)
820 If Not pbCheck Then TraceError(TRACEFATAL, ERROBJECTNOTFOUND, Utils._CalledSub(),
0, , Array(_GetLabel(
"TABLE
"), pvIndex))
823 TraceError(TRACEFATAL, ERRCOLLECTION, Utils._CalledSub(),
0)
825 End Function
' TableDefs V1.1
.0
827 REM -----------------------------------------------------------------------------------------------------------------------
828 REM --- PRIVATE FUNCTIONS ---
829 REM -----------------------------------------------------------------------------------------------------------------------
831 REM -----------------------------------------------------------------------------------------------------------------------
832 Private Function _DFunction(ByVal psFunction As String _
833 , ByVal psExpr As String _
834 , ByVal psDomain As String _
835 , ByVal pvCriteria As Variant _
836 , ByVal Optional pvOrderClause As Variant _
838 'Arguments: psFunction an optional aggregate function
839 ' psExpr: an SQL expression [might contain an aggregate function]
840 ' psDomain: a table- or queryname
841 ' pvCriteria: an optional WHERE clause
842 ' pcOrderClause: an optional order clause incl.
"DESC
" if relevant
844 If _ErrorHandler() Then On Local Error GoTo Error_Function
846 Dim oResult As Object
'To retrieve the value to find.
847 Dim vResult As Variant
'Return value for function.
848 Dim sSql As String
'SQL statement.
849 Dim oStatement As Object
'For CreateStatement method
850 Dim sExpr As String
'For inclusion of aggregate function
851 Dim sTempField As String
'Random temporary field in SQL expression
855 If psFunction =
"" Then sExpr =
"TOP
1 " & psExpr Else sExpr = UCase(psFunction)
& "(
" & psExpr
& ")
"
858 sTempField =
"TEMP
" & Right(
"00000" & Int(
100000 * Rnd),
5)
859 sSql =
"SELECT
" & sExpr
& " AS [
" & sTempField
& "] FROM
" & psDomain
860 If pvCriteria
<> "" Then
861 sSql = sSql
& " WHERE
" & pvCriteria
863 If pvOrderClause
<> "" Then
864 sSql = sSql
& " ORDER BY
" & pvOrderClause
867 'Lookup the value.
868 Set oStatement = Connection.createStatement()
870 .ResultSetType = com.sun.star.sdbc.ResultSetType.FORWARD_ONLY
871 .ResultSetConcurrency = com.sun.star.sdbc.ResultSetConcurrency.READ_ONLY
872 .EscapeProcessing = False
873 sSql = _ReplaceSquareBrackets(sSql)
'Substitute [] by quote string
874 Set oResult = .executeQuery(sSql)
875 If Not IsNull(oResult) And Not IsEmpty(oResult) Then
876 If Not oResult.next() Then Goto Exit_Function
877 vResult = Utils._getResultSetColumnValue(oResult,
1)
882 'Assign the returned value.
884 Set oResult = Nothing
885 Set oStatement = Nothing
888 TraceError(TRACEABORT, ERRDFUNCTION, _A2B_.CalledSub,
0, , sSQL)
890 End Function
' DFunction V1.1
.0
892 REM -----------------------------------------------------------------------------------------------------------------------
893 Public Function _hasRecordset(ByVal psName As String) As Boolean
894 ' Return True if psName if in the collection of Recordsets
896 Dim oRecordset As Object
897 If _ErrorHandler() Then On Local Error Goto Error_Function
898 Set oRecordset = RecordsetsColl.Item(psName)
903 Error_Function:
' Item by key aborted
904 _hasRecordset = False
906 End Function
' _hasRecordset V0.9
.5
908 REM -----------------------------------------------------------------------------------------------------------------------
909 Private Function _PropertiesList() As Variant
911 _PropertiesList = Array(
"ObjectType
")
913 End Function
' _PropertiesList
915 REM -----------------------------------------------------------------------------------------------------------------------
916 Private Function _PropertyGet(ByVal psProperty As String) As Variant
917 ' Return property value of the psProperty property name
919 If _ErrorHandler() Then On Local Error Goto Error_Function
920 Utils._SetCalledSub(
"Database.get
" & psProperty)
921 Dim vEMPTY As Variant
922 _PropertyGet = vEMPTY
924 Select Case UCase(psProperty)
925 Case UCase(
"ObjectType
")
932 Utils._ResetCalledSub(
"Database.get
" & psProperty)
935 TraceError(TRACEFATAL, ERRPROPERTY, Utils._CalledSub(),
0, , psProperty)
936 _PropertyGet = vEMPTY
939 TraceError(TRACEABORT, Err,
"Database._PropertyGet
", Erl)
940 _PropertyGet = vEMPTY
942 End Function
' _PropertyGet
944 REM -----------------------------------------------------------------------------------------------------------------------
945 Public Function _ReplaceSquareBrackets(ByVal psSql As String) As String
946 ' Returns psSql after substitution of [] by quote character
947 ' [] square brackets in (single) quoted strings not affected
949 Dim sQuote As String
'RDBMS specific quote character
950 Dim vSubStrings() As Variant, i As Integer
951 Const cstSingleQuote =
"'"
953 sQuote = MetaData.IdentifierQuoteString
954 If sQuote =
" " Then
' IdentifierQuoteString returns a space
" " if identifier quoting is not supported.
955 _ReplaceSquareBrackets = Trim(psSql)
958 vSubStrings() = Split(psSql, cstSingleQuote)
959 For i =
0 To UBound(vSubStrings)
960 If (i Mod
2) =
0 Then
' Only even substrings are parsed for square brackets
961 vSubStrings(i) = Join(Split(vSubStrings(i),
"[
"), sQuote)
962 vSubStrings(i) = Join(Split(vSubStrings(i),
"]
"), sQuote)
966 _ReplaceSquareBrackets = Trim(Join(vSubStrings, cstSingleQuote))
968 End Function
' ReplaceSquareBrackets V1.1
.0