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=
"Recordset" script:
language=
"StarBasic">
4 REM =======================================================================================================================
5 REM === The Access2Base library is a part of the LibreOffice project. ===
6 REM === Full documentation is available on http://www.access2base.com ===
7 REM =======================================================================================================================
14 REM -----------------------------------------------------------------------------------------------------------------------
15 REM --- CLASS ROOT FIELDS ---
16 REM -----------------------------------------------------------------------------------------------------------------------
18 Private _Type As String
' Must be RECORDSET
19 Private _This As Object
' Workaround for absence of This builtin function
20 Private _Parent As Object
21 Private _Name As String
' Unique, generated
22 Private _Fields() As Variant
23 Private _ParentName As String
24 Private _ParentType As String
25 Private _ParentDatabase As Object
26 Private _ForwardOnly As Boolean
27 Private _PassThrough As Boolean
28 Private _ReadOnly As Boolean
29 Private _CommandType As Long
30 Private _Command As String
31 Private _DataSet As Boolean
' True if execute() successful
32 Private _BOF As Boolean
33 Private _EOF As Boolean
34 Private _Filter As String
35 Private _EditMode As Integer
' dbEditxxx constants
36 Private _BookmarkBeforeNew As Variant
37 Private _BookmarkLastModified As Variant
38 Private _IsClone As Boolean
39 Private _ManageChunks As Variant
' Array of ChunkDescriptors
40 Private RowSet As Object
' com.sun.star.comp.dba.ORowSet
43 ChunksRequested As Boolean
45 ChunkType As Integer
' vbString or vbByte
50 REM -----------------------------------------------------------------------------------------------------------------------
51 REM --- CONSTRUCTORS / DESTRUCTORS ---
52 REM -----------------------------------------------------------------------------------------------------------------------
53 Private Sub Class_Initialize()
59 _ParentName =
""
60 Set _ParentDatabase = Nothing
61 _ParentType =
""
66 _Command =
""
70 _Filter =
""
71 _EditMode = dbEditNone
72 _BookmarkBeforeNew = Null
73 _BookmarkLastModified = Null
75 Set _ManageChunks = Array()
77 End Sub
' Constructor
79 REM -----------------------------------------------------------------------------------------------------------------------
80 Private Sub Class_Terminate()
81 On Local Error Resume Next
85 REM -----------------------------------------------------------------------------------------------------------------------
86 REM --- CLASS GET/LET/SET PROPERTIES ---
87 REM -----------------------------------------------------------------------------------------------------------------------
89 REM -----------------------------------------------------------------------------------------------------------------------
90 Property Get AbsolutePosition() As Variant
91 AbsolutePosition = _PropertyGet(
"AbsolutePosition
")
92 End Property
' AbsolutePosition (get)
94 Property Let AbsolutePosition(ByVal pvValue As Variant)
95 Call _PropertySet(
"AbsolutePosition
", pvValue)
96 End Property
' AbsolutePosition (set)
98 REM -----------------------------------------------------------------------------------------------------------------------
99 Property Get BOF() As Boolean
100 BOF = _PropertyGet(
"BOF
")
101 End Property
' BOF (get)
103 REM -----------------------------------------------------------------------------------------------------------------------
104 Property Get Bookmark() As Variant
105 Bookmark = _PropertyGet(
"Bookmark
")
106 End Property
' Bookmark (get)
108 Property Let Bookmark(ByVal pvValue As Variant)
109 Call _PropertySet(
"Bookmark
", pvValue)
110 End Property
' Bookmark (set)
112 REM -----------------------------------------------------------------------------------------------------------------------
113 Property Get Bookmarkable() As Boolean
114 Bookmarkable = _PropertyGet(
"Bookmarkable
")
115 End Property
' Bookmarkable (get)
117 REM -----------------------------------------------------------------------------------------------------------------------
118 Property Get EOF() As Boolean
119 EOF = _PropertyGet(
"EOF
")
120 End Property
' EOF (get)
122 REM -----------------------------------------------------------------------------------------------------------------------
123 Property Get EditMode() As Integer
124 EditMode = _PropertyGet(
"EditMode
")
125 End Property
' EditMode (get)
127 REM -----------------------------------------------------------------------------------------------------------------------
128 Property Get Filter() As Variant
129 Filter = _PropertyGet(
"Filter
")
130 End Property
' Filter (get)
132 Property Let Filter(ByVal pvValue As Variant)
133 Call _PropertySet(
"Filter
", pvValue)
134 End Property
' Filter (set)
136 REM -----------------------------------------------------------------------------------------------------------------------
137 Property Get LastModified() As Variant
138 ' DO NOT PUBLISH
139 LastModified = _PropertyGet(
"LastModified
")
140 End Property
' LastModified (get)
142 REM -----------------------------------------------------------------------------------------------------------------------
143 Property Get Name() As String
144 Name = _PropertyGet(
"Name
")
145 End Property
' Name (get)
147 REM -----------------------------------------------------------------------------------------------------------------------
148 Property Get ObjectType() As String
149 ObjectType = _PropertyGet(
"ObjectType
")
150 End Property
' ObjectType (get)
152 REM -----------------------------------------------------------------------------------------------------------------------
153 Property Get RecordCount() As Long
154 RecordCount = _PropertyGet(
"RecordCount
")
155 End Property
' RecordCount (get)
157 REM -----------------------------------------------------------------------------------------------------------------------
158 REM --- CLASS METHODS ---
159 REM -----------------------------------------------------------------------------------------------------------------------
161 REM -----------------------------------------------------------------------------------------------------------------------
162 Public Function AddNew() As Boolean
163 ' Initiates the creation of a new record
165 Const cstThisSub =
"Recordset.AddNew
"
166 Dim i As Integer, iFieldsCount As Integer, oField As Object
167 Dim sDefault As String, oColumn As Object
168 Dim iValue As Integer, lValue As Long, sgValue As Single, dbValue As Double, dValue As Date
170 If _ErrorHandler() Then On Local Error Goto Error_Function
171 Utils._SetCalledSub(cstThisSub)
175 'Is inserting a new row allowed ?
176 If _ForwardOnly Or _ReadOnly Then Goto Error_NoUpdate
177 If Not .CanUpdateInsertedRows Then Goto Error_NoUpdate
178 If Not .IsBookmarkable Then Goto Error_NoUpdate
179 If _EditMode
<> dbEditNone Then CancelUpdate()
180 If _BOF And _EOF Then
' Records before first or after last do not have a bookmark
181 _BookmarkBeforeNew =
"_BOF_
"
182 ElseIf .isBeforeFirst() Then
183 _BookmarkBeforeNew =
"_BOF_
"
184 ElseIf .isAfterLast() Then
185 _BookmarkBeforeNew =
"_EOF_
"
187 _BookmarkBeforeNew = .getBookmark()
192 'Set all fields to their default value
193 iFieldsCount = Fields().Count
194 On Local Error Resume Next
' Do not stop if default setting fails
195 For i =
0 To iFieldsCount -
1
196 Set oField = Fields(i)
197 Set oColumn = oField.Column
198 sDefault = oField.DefaultValue
199 If sDefault =
"" Then
' No default value
200 If oColumn.IsNullable = com.sun.star.sdbc.ColumnValue.NULLABLE Then oColumn.updateNull()
202 With com.sun.star.sdbc.DataType
203 Select Case oColumn.Type
205 If sDefault =
"1" Then oColumn.updateBoolean(True) Else oColumn.updateBoolean(False)
207 iValue = CInt(sDefault)
208 If iValue
>= -
128 And iValue
<= +
127 Then oColumn.updateShort(iValue)
210 lValue = CLng(sDefault)
211 If lValue
>= -
32768 And lValue
<=
32767 Then oColumn.updateInt(lValue)
213 lValue = CLng(sDefault)
214 If lValue
>= -
2147483648 And lValue
<=
2147483647 Then oColumn.updateInt(lValue)
216 lValue = CLng(sDefault)
217 Column.updateLong(lValue)
' No proper type conversion for HYPER data type
219 sgValue = CSng(sDefault)
220 If Abs(sgValue)
< 3.402823E38 And Abs(sgValue)
> 1.401298E-45 Then oColumn.updateFloat(sgValue)
222 dbValue = CDbl(sDefault)
223 'If Abs(dbValue)
< 1.79769313486232E308 And Abs(dbValue)
> 4.94065645841247E-307 Then oColumn.updateDouble(dbValue)
224 oColumn.updateDouble(dbValue)
225 Case .NUMERIC, .DECIMAL
226 dbValue = CDbl(sDefault)
227 If Utils._hasUNOProperty(Column,
"Scale
") Then
228 If Column.Scale
> 0 Then
229 'If Abs(dbValue)
< 1.79769313486232E308 And Abs(dbValue)
> 4.94065645841247E-307 Then oColumn.updateDouble(dbValue)
230 oColumn.updateDouble(dbValue)
232 oColumn.updateString(sDefault)
235 oColumn.updateString(sDefault)
237 Case .CHAR, .VARCHAR, .LONGVARCHAR
238 oColumn.updateString(sDefault)
' vbString
240 dValue = DateValue(sDefault)
241 vTemp = New com.sun.star.util.Date
244 .Month = Month(dValue)
247 oColumn.updateDate(vTemp)
249 dValue = TimeValue(sDefault)
250 vTemp = New com.sun.star.util.Time
252 .Hours = Hour(dValue)
253 .Minutes = Minute(dValue)
254 .Seconds = Second(dValue)
255 '.HundredthSeconds =
0
257 oColumn.updateTime(vTemp)
259 dValue = DateValue(sDefault)
260 vTemp = New com.sun.star.util.DateTime
263 .Month = Month(dValue)
265 .Hours = Hour(dValue)
266 .Minutes = Minute(dValue)
267 .Seconds = Second(dValue)
268 '.HundredthSeconds =
0
270 oColumn.updateTimestamp(vTemp)
271 ' Case .BINARY, .VARBINARY, .LONGVARBINARY
280 If _ErrorHandler() Then On Local Error Goto Error_Function Else On Local Error Goto
0
282 _EditMode = dbEditAdd
286 Utils._ResetCalledSub(cstThisSub)
289 TraceError(TRACEABORT, Err, cstThisSub, Erl)
292 TraceError(TRACEFATAL, ERRNOTUPDATABLE, Utils._CalledSub(),
0)
294 End Function
' AddNew
296 REM -----------------------------------------------------------------------------------------------------------------------
297 Public Function CancelUpdate() As Boolean
298 ' Cancel any edit action
300 Const cstThisSub =
"Recordset.CancelUpdate
"
302 If _ErrorHandler() Then On Local Error Goto Error_Function
303 Utils._SetCalledSub(cstThisSub)
307 Select Case _EditMode
310 _AppendChunkClose(True)
311 If Not IsNull(_BookmarkBeforeNew) Then
312 Select Case _BookmarkBeforeNew
313 Case
"_BOF_
" : .beforeFirst()
314 Case
"_EOF_
" : .afterLast()
315 Case Else : .moveToBookmark(_BookmarkBeforeNew)
318 Case dbEditInProgress
320 _AppendChunkClose(True)
324 _EditMode = dbEditNone
325 _BookmarkBeforeNew = Null
326 _BookmarkLastModified = Null
330 Utils._ResetCalledSub(cstThisSub)
333 TraceError(TRACEABORT, Err, cstThisSub, Erl)
335 End Function
' CancelUpdate
337 REM -----------------------------------------------------------------------------------------------------------------------
338 Public Function Clone() As Object
339 ' Duplicate an existing recordset
341 Const cstThisSub =
"Recordset.Clone
"
344 Dim iType As Integer, iOptions As Integer, iLockEdit As Integer
345 If _ErrorHandler() Then On Local Error Goto Error_Function
346 Utils._SetCalledSub(cstThisSub)
349 If _IsClone Then Goto Error_Clone
350 If _ForwardOnly Then iType = dbOpenForwardOnly Else iType = cstNull
351 If _PassThrough Then iOptions = dbSQLPassThrough Else iOptions = cstNull
352 iLockEdit = dbReadOnly
' Always read-only
354 Set Clone = OpenRecordset(iType, iOptions, iLockEdit, True)
357 Utils._ResetCalledSub(cstThisSub)
360 TraceError(TRACEABORT, Err, cstThisSub, Erl)
363 TraceError(TRACEFATAL, ERRRECORDSETCLONE, Utils._CalledSub(),
0)
365 End Function
' Clone
367 REM -----------------------------------------------------------------------------------------------------------------------
368 Public Function mClose(ByVal Optional pbRemove As Boolean) As Variant
369 ' Dispose UNO objects
370 ' If pbRemove = True, remove recordset from Recordsets collection
372 Const cstThisSub =
"Recordset.Close
"
375 If _ErrorHandler() Then On Local Error Goto Exit_Function
' Do not stop execution
376 Utils._SetCalledSub(cstThisSub)
377 If Not IsNull(RowSet) Then
385 _Command =
""
386 _ParentName =
""
387 _ParentType =
""
391 _Filter =
""
392 _EditMode = dbEditNone
393 _BookmarkBeforeNew = Null
394 _BookmarkLastModified = Null
396 For i =
0 To UBound(_Fields)
397 If Not IsNull(_Fields(i)) Then
399 Set _Fields(i) = Nothing
404 If IsMissing(pbRemove) Then pbRemove = True
405 If pbRemove Then _ParentDatabase.RecordsetsColl.Remove(_Name)
406 Set _ParentDatabase = Nothing
409 Utils._ResetCalledSub(cstThisSub)
411 End Function
' Close
413 REM -----------------------------------------------------------------------------------------------------------------------
414 Public Function Delete() As Boolean
415 ' Deletes the current record
417 Const cstThisSub =
"Recordset.Delete
"
419 If _ErrorHandler() Then On Local Error Goto Error_Function
420 Utils._SetCalledSub(cstThisSub)
423 'Is deleting a row allowed ?
424 If _ForwardOnly Or _ReadOnly Then Goto Error_NoUpdate
425 If _EditMode
<> dbEditNone Then
429 If RowSet.rowDeleted() Then Goto Error_RowDeleted
435 Utils._ResetCalledSub(cstThisSub)
438 TraceError(TRACEABORT, Err, cstThisSub, Erl)
441 TraceError(TRACEFATAL, ERRNOTUPDATABLE, Utils._CalledSub(),
0)
444 TraceError(TRACEFATAL, ERRROWDELETED, Utils._CalledSub(),
0)
447 TraceError(TRACEFATAL, ERRUPDATESEQUENCE, Utils._CalledSub(),
0,
1)
449 End Function
' Delete
451 REM -----------------------------------------------------------------------------------------------------------------------
452 Public Function Edit() As Boolean
453 ' Updates the current record
455 Const cstThisSub =
"Recordset.Edit
"
457 If _ErrorHandler() Then On Local Error Goto Error_Function
458 Utils._SetCalledSub(cstThisSub)
461 'Is updating a row allowed ?
462 If _ForwardOnly Or _ReadOnly Then Goto Error_NoUpdate
463 If _EditMode
<> dbEditNone Then CancelUpdate()
464 If RowSet.rowDeleted() Then Goto Error_RowDeleted
466 _EditMode = dbEditInProgress
470 Utils._ResetCalledSub(cstThisSub)
473 TraceError(TRACEABORT, Err, cstThisSub, Erl)
476 TraceError(TRACEFATAL, ERRNOTUPDATABLE, Utils._CalledSub(),
0)
479 TraceError(TRACEFATAL, ERRROWDELETED, Utils._CalledSub(),
0)
481 End Function
' Edit
483 REM -----------------------------------------------------------------------------------------------------------------------
484 Public Function Fields(ByVal Optional pvIndex As Variant) As Object
486 If _ErrorHandler() Then On Local Error Goto Error_Function
487 Const cstThisSub =
"Recordset.Fields
"
488 Utils._SetCalledSub(cstThisSub)
491 If Not IsMissing(pvIndex) Then
492 If Not Utils._CheckArgument(pvIndex,
1, Utils._AddNumeric(vbString)) Then Goto Exit_Function
495 Dim sObjects() As String, sObjectName As String, oObject As Object
496 Dim i As Integer, oFields As Object, iIndex As Integer
498 ' No argument, return a collection
499 If IsMissing(pvIndex) Then
500 Set oObject = New Collect
501 Set oObject._This = oObject
502 oObject._CollType = COLLFIELDS
503 Set oObject._Parent = _This
504 oObject._Count = RowSet.getColumns().Count
508 Set oFields = RowSet.getColumns()
509 sObjects = oFields.ElementNames()
511 ' Argument is the field name
512 If VarType(pvIndex) = vbString Then
514 ' Check existence of object and find its exact (case-sensitive) name
515 For i =
0 To UBound(sObjects)
516 If UCase(pvIndex) = UCase(sObjects(i)) Then
517 sObjectName = sObjects(i)
522 If iIndex
< 0 Then Goto Trace_NotFound
523 ' Argument is numeric
525 If pvIndex
< 0 Or pvIndex
> UBound(sObjects) Then Goto Trace_IndexError
526 sObjectName = sObjects(pvIndex)
530 ' Check if field object already buffered in _Fields() array
531 If UBound(_Fields)
< 0 Then
' Initialize _Fields
532 ReDim _Fields(
0 To UBound(sObjects))
533 For i =
0 To UBound(sObjects)
534 Set _Fields(i) = Nothing
537 If Not IsNull(_Fields(iIndex)) Then
538 Set oObject = _Fields(iIndex)
539 ' Otherwise create new field object
541 Set oObject = New Field
542 Set oObject._This = oObject
543 oObject._Name = sObjectName
544 Set oObject.Column = oFields.getByName(sObjectName)
545 If Utils._hasUNOProperty(oObject.Column,
"Precision
") Then oObject._Precision = oObject.Column.Precision
546 oObject._ParentName = _Name
547 oObject._ParentType = _Type
548 Set oObject._ParentDatabase = _ParentDatabase
549 Set oObject._ParentRecordset = _This
550 Set _Fields(iIndex) = oObject
555 Set oObject = Nothing
556 Utils._ResetCalledSub(cstThisSub)
559 TraceError(TRACEABORT, Err, cstThisSub, Erl)
562 TraceError(TRACEFATAL, ERROBJECTNOTFOUND, Utils._CalledSub(),
0, , Array(_GetLabel(
"FIELD
"), pvIndex))
565 TraceError(TRACEFATAL, ERRCOLLECTION, Utils._CalledSub(),
0)
567 End Function
' Fields
569 REM -----------------------------------------------------------------------------------------------------------------------
570 Public Function getProperty(Optional ByVal pvProperty As Variant) As Variant
571 ' Return property value of psProperty property name
573 Const cstThisSub =
"Recordset.getProperty
"
574 Utils._SetCalledSub(cstThisSub)
575 If IsMissing(pvProperty) Then Call _TraceArguments()
576 getProperty = _PropertyGet(pvProperty)
577 Utils._ResetCalledSub(cstThisSub)
579 End Function
' getProperty
581 REM -----------------------------------------------------------------------------------------------------------------------
582 Public Function GetRows(ByVal Optional pvNumRows As variant, ByVal Optional pbStrDate As Boolean) As Variant
583 ' UNPUBLISHED - pbStrDate = True forces all dates to be converted into strings
585 If _ErrorHandler() Then On Local Error Goto Error_Function
586 Const cstThisSub =
"Recordset.GetRows
"
587 Utils._SetCalledSub(cstThisSub)
588 If IsMissing(pbStrDate) Then pbStrDate = False
590 Dim vMatrix() As Variant, lSize As Long, iNumFields As Integer, i As Integer
592 If IsMissing(pvNumRows) Then Call _TraceArguments()
593 If Not Utils._CheckArgument(pvNumRows,
1, Utils._AddNumeric()) Then Goto Exit_Function
594 If pvNumRows
< 1 Then Goto Trace_Error
595 If IsNull(RowSet) Then Goto Trace_Closed
596 If Not _DataSet Then Goto Exit_Function
598 If _EditMode
<> dbEditNone Then CancelUpdate()
600 If _EOF Then Goto Exit_Function
603 iNumFields = RowSet.getColumns().Count -
1
604 If iNumFields
< 0 Then Goto Exit_Function
606 ReDim vMatrix(
0 To iNumFields,
0 To pvNumRows -
1)
608 Do While Not _EOF And lSize
< pvNumRows -
1
610 For i =
0 To iNumFields
611 vMatrix(i, lSize) = Utils._getResultSetColumnValue(RowSet, i +
1)
612 If pbStrDate And IsDate(vMatrix(i, lSize)) Then vMatrix(i, lSize) = _CStr(vMatrix(i, lSize))
614 _Move(
"NEXT
")
616 If lSize
< pvNumRows -
1 Then
' Resize to number of fetched records
617 ReDim Preserve vMatrix(
0 To iNumFields,
0 To lSize)
621 GetRows() = vMatrix()
622 Utils._ResetCalledSub(cstThisSub)
625 TraceError(TRACEABORT, Err, cstThisSub, Erl)
628 TraceError(TRACEFATAL, ERRWRONGARGUMENT, Utils._CalledSub(),
0, , Array(
1, pvNumRows))
629 Set Controls = Nothing
632 TraceError(TRACEFATAL, ERRRECORDSETCLOSED, Utils._CalledSub(),
0)
634 End Function
' GetRows V1.1
.0
636 REM -----------------------------------------------------------------------------------------------------------------------
637 Public Function hasProperty(ByVal Optional pvProperty As Variant) As Boolean
638 ' Return True if object has a valid property called pvProperty (case-insensitive comparison !)
640 Const cstThisSub =
"Recordset.hasProperty
"
641 Utils._SetCalledSub(cstThisSub)
642 If IsMissing(pvProperty) Then hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList()) Else hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList(), pvProperty)
643 Utils._ResetCalledSub(cstThisSub)
646 End Function
' hasProperty
648 REM -----------------------------------------------------------------------------------------------------------------------
649 Public Function Move(ByVal Optional pvRelative As Variant, ByVal Optional pvBookmark As variant) As Boolean
650 ' Move record pointer Relative rows vs. bookmark or current record
652 If IsMissing(pvRelative) Then Call _TraceArguments()
653 If Not Utils._CheckArgument(pvRelative,
1, Utils._AddNumeric()) Then Goto Exit_Function
655 If IsMissing(pvBookmark) Then Move = _Move(pvRelative) Else Move = _Move(pvRelative, pvBookmark)
659 End Function
' Move
661 REM -----------------------------------------------------------------------------------------------------------------------
662 Public Function MoveFirst() As Boolean
663 MoveFirst = _Move(
"First
")
664 End Function
' MoveFirst
666 REM -----------------------------------------------------------------------------------------------------------------------
667 Public Function MoveLast() As Boolean
668 MoveLast = _Move(
"Last
")
669 End Function
' MoveLast
671 REM -----------------------------------------------------------------------------------------------------------------------
672 Public Function MoveNext() As Boolean
673 MoveNext = _Move(
"Next
")
674 End Function
' MoveNext
676 REM -----------------------------------------------------------------------------------------------------------------------
677 Public Function MovePrevious() As Boolean
678 MovePrevious = _Move(
"Previous
")
679 End Function
' MovePrevious
681 REM -----------------------------------------------------------------------------------------------------------------------
682 Public Function OpenRecordset(ByVal Optional pvType As Variant _
683 , ByVal Optional pvOptions As Variant _
684 , ByVal Optional pvLockEdit As Variant _
685 , ByVal Optional pbClone As Boolean) As Object
686 'Return a Recordset object based on current recordset object with filter addition
688 If _ErrorHandler() Then On Local Error Goto Error_Function
689 Dim cstThisSub As String
690 cstThisSub = Utils._PCase(_Type)
& ".OpenRecordset
"
691 Utils._SetCalledSub(cstThisSub)
692 Set OpenRecordset = Nothing
695 Dim oObject As Object
696 Set oObject = Nothing
697 If IsMissing(pvType) Then
700 If Not Utils._CheckArgument(pvType,
1, Utils._AddNumeric(), Array(cstNull, dbOpenForwardOnly)) Then Goto Exit_Function
702 If IsMissing(pvOptions) Then
705 If Not Utils._CheckArgument(pvOptions,
2, Utils._AddNumeric(), Array(cstNull, dbSQLPassThrough)) Then Goto Exit_Function
707 If IsMissing(pvLockEdit) Then
710 If Not Utils._CheckArgument(pvLockEdit,
3, Utils._AddNumeric(), Array(cstNull, dbReadOnly)) Then Goto Exit_Function
712 If IsMissing(pbClone) Then pbClone = False
' pbClone is a not published argument
714 Set oObject = New Recordset
716 ._CommandType = _CommandType
720 Set ._ParentDatabase = _ParentDatabase
722 ._ForwardOnly = ( pvType = dbOpenForwardOnly )
723 ._PassThrough = ( pvOptions = dbSQLPassThrough )
724 ._ReadOnly = ( (pvLockEdit = dbReadOnly) Or _ReadOnly )
726 Case pbClone : Call ._Initialize(, RowSet)
727 Case _Filter
<> "" : Call ._Initialize(_Filter)
728 Case Else : Call ._Initialize()
732 .RecordsetMax = .RecordsetMax +
1
733 oObject._Name = Format(.RecordsetMax,
"0000000")
734 .RecordsetsColl.Add(oObject, UCase(oObject._Name))
737 If Not ( oObject._BOF And oObject._EOF ) Then oObject.MoveFirst()
' Do nothing if resultset empty
740 Set OpenRecordset = oObject
741 Set oObject = Nothing
742 Utils._ResetCalledSub(cstThisSub)
745 TraceError(TRACEABORT, Err, Utils._CalledSub(), Erl)
747 End Function
' OpenRecordset
749 REM -----------------------------------------------------------------------------------------------------------------------
750 Public Function Properties(ByVal Optional pvIndex As Variant) As Variant
752 ' a Collection object if pvIndex absent
753 ' a Property object otherwise
755 Const cstThisSub =
"Recordset.Properties
"
756 Utils._SetCalledSub(cstThisSub)
757 Dim vProperty As Variant, vPropertiesList() As Variant, sObject As String
758 vPropertiesList = _PropertiesList()
759 sObject = Utils._PCase(_Type)
760 If IsMissing(pvIndex) Then
761 vProperty = PropertiesGet._Properties(sObject, _This, vPropertiesList)
763 vProperty = PropertiesGet._Properties(sObject, _This, vPropertiesList, pvIndex)
764 vProperty._Value = _PropertyGet(vPropertiesList(pvIndex))
766 Set vProperty._ParentDatabase = _ParentDatabase
769 Set Properties = vProperty
770 Utils._ResetCalledSub(cstThisSub)
772 End Function
' Properties
774 REM -----------------------------------------------------------------------------------------------------------------------
775 Public Function setProperty(ByVal Optional psProperty As String, ByVal Optional pvValue As Variant) As Boolean
776 ' Return True if property setting OK
777 Const cstThisSub =
"Recordset.setProperty
"
778 Utils._SetCalledSub(cstThisSub)
779 setProperty = _PropertySet(psProperty, pvValue)
780 Utils._ResetCalledSub(cstThisSub)
783 REM -----------------------------------------------------------------------------------------------------------------------
784 Public Function Update() As Boolean
785 ' Finalize the updates of the current record
787 Const cstThisSub =
"Recordset.Update
"
789 If _ErrorHandler() Then On Local Error Goto Error_Function
790 Utils._SetCalledSub(cstThisSub)
793 'Is updating a row allowed ?
794 If _ForwardOnly Or _ReadOnly Then Goto Error_NoUpdate
796 If .rowDeleted() Then Goto Error_RowDeleted
797 Select Case _EditMode
799 Goto Trace_Error_Update
801 _AppendChunkClose(False)
802 If .IsNew And .IsModified Then .insertRow()
803 _BookmarkLastModified = .getBookmark()
804 If Not IsNull(_BookmarkBeforeNew) Then
805 Select Case _BookmarkBeforeNew
806 Case
"_BOF_
" : .beforeFirst()
807 Case
"_EOF_
" : .afterLast()
808 Case Else : .moveToBookmark(_BookmarkBeforeNew)
811 Case dbEditInProgress
812 _AppendChunkClose(False)
815 _BookmarkLastModified = .getBookmark()
819 _EditMode = dbEditNone
823 Utils._ResetCalledSub(cstThisSub)
826 TraceError(TRACEABORT, Err, cstThisSub, Erl)
829 TraceError(TRACEFATAL, ERRNOTUPDATABLE, Utils._CalledSub(),
0)
832 TraceError(TRACEFATAL, ERRUPDATESEQUENCE, Utils._CalledSub(),
0,
1)
835 TraceError(TRACEFATAL, ERRROWDELETED, Utils._CalledSub(),
0)
837 End Function
' Update
839 REM -----------------------------------------------------------------------------------------------------------------------
840 REM --- PRIVATE FUNCTIONS ---
841 REM -----------------------------------------------------------------------------------------------------------------------
843 REM -----------------------------------------------------------------------------------------------------------------------
844 Public Function _AppendChunk(ByVal psFieldName As String, ByRef pvChunk As Variant, piChunkType) As Boolean
845 ' Write chunk at the end of the file dedicated to the given field
847 If _ErrorHandler() Then On Local Error GoTo Error_Function
848 Dim oFileAccess As Object
849 Dim i As Integer, oChunk As Object, iChunk As Integer
851 ' Do nothing if chunk meaningless
853 If IsNull(pvChunk) Then GoTo Exit_Function
854 If IsArray(pvChunk) Then
855 If UBound(pvChunk)
< LBound(pvChunk) Then GoTo Exit_Function
' Empty array
858 ' Find or create relevant chunk entry
860 For i =
0 To UBound(_ManageChunks)
861 Set oChunk = _ManageChunks(i)
862 If oChunk.FieldName = psFieldName Then
868 _AppendChunkInit(psFieldName)
869 iChunk = UBound(_ManageChunks)
872 Set oChunk = _ManageChunks(iChunk)
874 If Not .ChunksRequested Then
' First chunk
875 .ChunksRequested = True
876 .ChunkType = piChunkType
877 .FileName = Utils._GetRandomFileName(_Name)
878 Set oFileAccess = CreateUnoService(
"com.sun.star.ucb.SimpleFileAccess
")
879 .FileHandler = oFileAccess.openFileWrite(.FileName)
881 .FileHandler.writeBytes(pvChunk)
888 TraceError(TRACEABORT, Err,
"Recordset._AppendChunk
", Erl)
890 End Function
' AppendChunk V1.5
.0
892 REM -----------------------------------------------------------------------------------------------------------------------
893 Public Function _AppendChunkClose(ByVal pbCancel As Boolean) As Boolean
894 ' Stores file content to database field(s)
895 ' Called from Update() [pbCancel = False] or CancelUpdate() [pbCancel = True]
897 If _ErrorHandler() Then On Local Error GoTo Error_Function
898 Dim oFileAccess As Object, oStream As Object, lFileLength As Long, oField As Object
899 Dim i As Integer, oChunk As Object
901 _AppendChunkClose = False
902 For i =
0 To UBound(_ManageChunks)
903 Set oChunk = _ManageChunks(i)
905 If Not .ChunksRequested Then GoTo Exit_Function
906 If IsNull(.FileHandler) Then GoTo Exit_Function
907 .Filehandler.closeOutput
908 Set oFileAccess = CreateUnoService(
"com.sun.star.ucb.SimpleFileAccess
")
909 ' Copy file to field
911 Set oStream = oFileAccess.openFileRead(.FileName)
912 lFileLength = oStream.getLength()
913 If lFileLength
> 0 Then
914 Set oField = RowSet.getColumns.getByName(.FieldName)
915 Select Case .ChunkType
917 oField.updateBinaryStream(oStream, lFileLength)
918 ' Case vbString
' DOES NOT WORK FOR CHARACTER TYPES
919 ' oField.updateCharacterStream(oStream, lFileLength)
924 If oFileAccess.exists(.FileName) Then oFileAccess.kill(.FileName)
927 Set _ManageChunks = Array()
928 _AppendChunkClose = True
933 TraceError(TRACEABORT, Err,
"Recordset._AppendChunkClose
", Erl)
935 End Function
' AppendChunkClose V1.5
.0
937 REM -----------------------------------------------------------------------------------------------------------------------
938 Public Function _AppendChunkInit(psFieldName As String) As Boolean
939 ' Initialize chunks manager
942 iSize = UBound(_ManageChunks) +
1
943 ReDim Preserve _ManageChunks(
0 To iSize)
944 Set _ManageChunks(iSize) = New ChunkDescriptor
945 With _ManageChunks(iSize)
946 .ChunksRequested = False
947 .FieldName = psFieldName
948 .FileName =
""
949 Set .FileHandler = Nothing
952 End Function
' AppendChunkInit V1.5
.0
954 REM -----------------------------------------------------------------------------------------------------------------------
955 Public Sub _Initialize(ByVal Optional pvFilter As Variant, Optional poRowSet As Object)
956 ' Initialize new recordset
958 Dim sFilter As String
960 If _Command =
"" Then Exit Sub
962 If _ErrorHandler() Then On Local Error Goto Error_Sub
963 If VarType(pvFilter) = vbError Then
964 sFilter =
""
965 ElseIf IsMissing(pvFilter) Then
966 sFilter =
""
970 If Not IsMissing(poRowSet) Then
' Clone
971 Set RowSet = poRowSet.createResultSet()
973 RowSet.last()
' Solves bookmark desynchro when parent bookmark is used ?!?
975 Set RowSet = CreateUnoService(
"com.sun.star.sdb.RowSet
")
978 If IsNull(.ActiveConnection) Then Set .ActiveConnection = _ParentDatabase.Connection
979 .CommandType = _CommandType
981 If _ForwardOnly Then .ResultSetType = com.sun.star.sdbc.ResultSetType.FORWARD_ONLY _
982 Else .ResultSetType = com.sun.star.sdbc.ResultSetType.SCROLL_SENSITIVE
983 If _PassThrough Then .EscapeProcessing = False _
984 Else .EscapeProcessing = True
986 .ResultSetConcurrency = com.sun.star.sdbc.ResultSetConcurrency.READ_ONLY
987 .TransactionIsolation = com.sun.star.sdbc.TransactionIsolation.READ_UNCOMMITTED
' Dirty read
989 .ResultSetConcurrency = com.sun.star.sdbc.ResultSetConcurrency.UPDATABLE
990 .TransactionIsolation = com.sun.star.sdbc.TransactionIsolation.READ_COMMITTED
994 If sFilter
<> "" Then
' Filter must be set before execute()
995 RowSet.Filter = sFilter
996 RowSet.ApplyFilter = True
998 On Local Error Goto SQL_Error
1000 On Local Error Goto Error_Sub
1003 'If the Recordset contains no records, the BOF and EOF properties are True, and there is no current record.
1004 _BOF = ( RowSet.IsRowCountFinal And RowSet.RowCount =
0 )
1010 TraceError(TRACEFATAL, ERRSQLSTATEMENT, Utils._CalledSub(),
0, , _Command)
1013 TraceError(TRACEABORT, Err,
"Recordset._Initialize
", Erl)
1015 End Sub
' _Initialize
1017 REM -----------------------------------------------------------------------------------------------------------------------
1018 Public Function _Move(pvTarget As Variant, ByVal Optional pvBookmark As Variant, ByVal Optional pbAbsolute As Boolean) As Boolean
1019 'Move to the first, last, next, or previous record in a specified Recordset object and make that record the current record.
1021 Dim cstThisSub As String
1022 cstThisSub =
"Recordset.Move
" & Iif(VarType(pvTarget) = vbString, pvTarget,
"")
1023 Utils._SetCalledSub(cstThisSub)
1024 If _ErrorHandler() Then On Local Error Goto Error_Function
1026 If IsNull(RowSet) Then Goto Trace_Closed
1027 If Not _DataSet Then Goto Trace_NoData
1028 If _BOF And _EOF Then Goto Trace_NoData
1030 CancelUpdate()
' Any Move cancels all updates, even Move(
0) !
1032 Dim l As Long, lRow As Long
1034 Select Case VarType(pvTarget)
1036 Select Case UCase(pvTarget)
1037 Case
"FIRST
"
1038 If _ForwardOnly Then
1039 If Not ( .isBeforeFirst() Or .isFirst() ) Then
1047 Case
"LAST
"
1048 If _ForwardOnly Then
1049 If .isAfterLast() Then Goto Trace_Forward
1050 Do While Not ( .isRowCountFinal And .Row = .RowCount )
' isLast() = True after reading of first records chunk
1056 Case
"NEXT
"
1057 If _EOF Then Goto Trace_OutOfRange
1059 Case
"PREVIOUS
"
1060 If _ForwardOnly Then Goto Trace_Forward
1061 If _BOF Then Goto Trace_OutOfRange
1064 Case Else
' Relative or absolute move
1065 If IsMissing(pbAbsolute) Then pbAbsolute = False
' Relative move is default
1066 If _ForwardOnly And pvTarget
< 0 then Goto Trace_Forward
1067 If IsMissing(pvBookmark) Then
1068 If pvTarget =
0 Then Goto Exit_Function
' Do nothing
1069 If _ForwardOnly Then
1070 If pbAbsolute Then lRow = .getRow() Else lRow =
0
1071 For l =
1 To pvTarget - lRow
1072 If .isAfterLast() Then Exit For
1076 If pbAbsolute Then .absolute(pvTarget) Else .relative(pvTarget)
1078 Else
' Move is always relative when bookmark argument present
1079 If _ForwardOnly Then Goto Trace_Forward
1080 If pvTarget =
0 Then
1081 .moveToBookmark(pvBookmark)
1083 .moveRelativeToBookmark(pvBookmark, pvTarget)
1088 _BOF = .isBeforeFirst()
' https://forum.openoffice.org/en/forum/viewtopic.php?f=
47&t=
76640
1089 _EOF = .isAfterlast()
1090 If _BOF Or _EOF Then
1093 If .rowDeleted() Then Goto Error_RowDeleted
1094 If .rowUpdated() Then .refreshRow()
1100 Utils._ResetCalledSub(cstThisSub)
1102 Exit_Close:
' Force close of recordset when error raised
1106 TraceError(TRACEABORT, Err, cstThisSub, Erl)
1109 TraceError(TRACEFATAL, ERRRECORDSETFORWARD, Utils._CalledSub(),
0)
1112 TraceError(TRACEFATAL, ERRRECORDSETNODATA, Utils._CalledSub(),
0)
1115 TraceError(TRACEFATAL, ERRRECORDSETRANGE, Utils._CalledSub(),
0)
1118 TraceError(TRACEFATAL, ERRROWDELETED, Utils._CalledSub(),
0)
1121 TraceError(TRACEFATAL, ERRRECORDSETCLOSED, Utils._CalledSub(),
0)
1123 End Function
' Move
1125 REM -----------------------------------------------------------------------------------------------------------------------
1126 Private Function _PropertiesList() As Variant
1128 _PropertiesList = Array(
"AbsolutePosition
",
"BOF
",
"Bookmarkable
",
"Bookmark
",
"EditMode
" _
1129 ,
"EOF
",
"Filter
",
"LastModified
",
"Name
",
"ObjectType
" ,
"RecordCount
" _
1132 End Function
' _PropertiesList
1134 REM -----------------------------------------------------------------------------------------------------------------------
1135 Private Function _PropertyGet(ByVal psProperty As String) As Variant
1136 ' Return property value of the psProperty property name
1138 If _ErrorHandler() Then On Local Error Goto Error_Function
1139 Dim cstThisSub As String
1140 cstThisSub =
"Recordset.get
"
1141 Utils._SetCalledSub(cstThisSub
& psProperty)
1143 _PropertyGet = EMPTY
1145 Select Case UCase(psProperty)
1146 Case UCase(
"AbsolutePosition
")
1147 If IsNull(RowSet) Then Goto Trace_Closed
1150 Case _BOF And _EOF : _PropertyGet = -
1
1151 Case .isBeforeFirst() Or .isAfterLast() : _PropertyGet = -
1
1152 Case Else : _PropertyGet = .getRow()
' Not getRow() -
1 as MSAccess requires
1155 Case UCase(
"BOF
")
1156 If IsNull(RowSet) Then Goto Trace_Closed
1158 Case _BOF And _EOF : _PropertyGet = True
1159 Case RowSet.isBeforeFirst() : _PropertyGet = True
1160 Case Else : _PropertyGet = False
1162 Case UCase(
"Bookmarkable
")
1163 If IsNull(RowSet) Then Goto Trace_Closed
1164 If _ForwardOnly Then _PropertyGet = False Else _PropertyGet = RowSet.IsBookmarkable
1165 Case UCase(
"Bookmark
")
1166 If IsNull(RowSet) Then Goto Trace_Closed
1167 If RowSet.IsBookmarkable And Not _ForwardOnly Then
1168 If _BOF Or _EOF Then _PropertyGet = Null Else _PropertyGet = RowSet.getBookmark()
1171 If _ForwardOnly Then Goto Trace_Forward
1173 Case UCase(
"EditMode
")
1174 If IsNull(RowSet) Then Goto Trace_Closed
1175 _PropertyGet = _EditMode
1176 Case UCase(
"EOF
")
1177 If IsNull(RowSet) Then Goto Trace_Closed
1179 Case _BOF And _EOF : _PropertyGet = True
1180 Case RowSet.isAfterLast() : _PropertyGet = True
1181 Case Else : _PropertyGet = False
1183 Case UCase(
"Filter
")
1184 If IsNull(RowSet) Then Goto Trace_Closed
1185 _PropertyGet = RowSet.Filter
1186 Case UCase(
"LastModified
")
1187 If IsNull(RowSet) Then Goto Trace_Closed
1188 If RowSet.IsBookmarkable And Not _ForwardOnly Then
1189 _PropertyGet = _BookmarkLastModified
1192 If _ForwardOnly Then Goto Trace_Forward
1194 Case UCase(
"Name
")
1195 _PropertyGet = _Name
1196 Case UCase(
"ObjectType
")
1197 _PropertyGet = _Type
1198 Case UCase(
"RecordCount
")
1199 If IsNull(RowSet) Then Goto Trace_Closed
1200 _PropertyGet = RowSet.RowCount
1206 Utils._ResetCalledSub(cstThisSub
& psProperty)
1209 TraceError(TRACEFATAL, ERRPROPERTY, Utils._CalledSub(),
0, , psProperty)
1210 _PropertyGet = EMPTY
1213 TraceError(TRACEFATAL, ERRRECORDSETFORWARD, Utils._CalledSub(),
0)
1216 TraceError(TRACEFATAL, ERRRECORDSETCLOSED, Utils._CalledSub(),
0)
1219 TraceError(TRACEABORT, Err, cstThisSub
& "._PropertyGet
", Erl)
1220 _PropertyGet = EMPTY
1222 End Function
' _PropertyGet
1224 REM -----------------------------------------------------------------------------------------------------------------------
1225 Private Function _PropertySet(ByVal psProperty As String, ByVal pvValue As Variant) As Boolean
1227 Dim cstThisSub As String
1228 cstThisSub =
"Recordset.set
"
1229 Utils._SetCalledSub(cstThisSub
& psProperty)
1230 If _ErrorHandler() Then On Local Error Goto Error_Function
1234 Dim iArgNr As Integer
1235 Dim oObject As Object
1237 If _IsLeft(_A2B_.CalledSub,
"Recordset.
") Then iArgNr =
1 Else iArgNr =
2
1238 Select Case UCase(psProperty)
1239 Case UCase(
"AbsolutePosition
")
1240 If Not Utils._CheckArgument(pvValue, iArgNr, Utils._AddNumeric(), , False) Then Goto Trace_Error_Value
1241 If pvValue
< 1 Then Goto Trace_Error_Value
1242 _Move(pvValue, , True)
1243 Case UCase(
"Bookmark
")
1244 If IsNull(RowSet) Then Goto Trace_Closed
1246 Case UCase(
"Filter
")
1247 If IsNull(RowSet) Then Goto Trace_Closed
1248 If Not Utils._CheckArgument(pvValue, iArgNr, vbString, , False) Then Goto Trace_Error_Value
1249 _Filter = _ParentDatabase._ReplaceSquareBrackets(pvValue)
1255 Utils._ResetCalledSub(cstThisSub
& psProperty)
1258 TraceError(TRACEFATAL, ERRPROPERTY, Utils._CalledSub(),
0,
1, psProperty)
1259 _PropertySet = False
1262 TraceError(TRACEFATAL, ERRPROPERTYVALUE, Utils._CalledSub(),
0,
1, Array(pvValue, psProperty))
1263 _PropertySet = False
1266 TraceError(TRACEFATAL, ERRRECORDSETCLOSED, Utils._CalledSub(),
0)
1269 TraceError(TRACEABORT, Err, Utils._CalledSub(), Erl)
1270 _PropertySet = False
1272 End Function
' _PropertySet