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">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 RECORDSET
18 Private _Name As String
' Unique, generated
19 Private _ParentName As String
20 Private _ParentType As String
21 Private _ParentDatabase As Object
22 Private _ForwardOnly As Boolean
23 Private _PassThrough As Boolean
24 Private _ReadOnly As Boolean
25 Private _CommandType As Long
26 Private _Command As String
27 Private _DataSet As Boolean
' True if execute() successful
28 Private _BOF As Boolean
29 Private _EOF As Boolean
30 Private _Filter As String
31 Private _EditMode As Integer
' dbEditxxx constants
32 Private _BookmarkBeforeNew As Variant
33 Private _BookmarkLastModified As Variant
34 Private _IsClone As Boolean
35 Private RowSet As Object
' com.sun.star.comp.dba.ORowSet
37 REM -----------------------------------------------------------------------------------------------------------------------
38 REM --- CONSTRUCTORS / DESTRUCTORS ---
39 REM -----------------------------------------------------------------------------------------------------------------------
40 Private Sub Class_Initialize()
43 _ParentName =
""
44 Set _ParentDatabase = Nothing
45 _ParentType =
""
50 _Command =
""
54 _Filter =
""
55 _EditMode = dbEditNone
56 _BookmarkBeforeNew = Null
57 _BookmarkLastModified = Null
60 End Sub
' Constructor
62 REM -----------------------------------------------------------------------------------------------------------------------
63 Private Sub Class_Terminate()
64 On Local Error Resume Next
68 REM -----------------------------------------------------------------------------------------------------------------------
69 REM --- CLASS GET/LET/SET PROPERTIES ---
70 REM -----------------------------------------------------------------------------------------------------------------------
72 REM -----------------------------------------------------------------------------------------------------------------------
73 Property Get AbsolutePosition() As Variant
74 AbsolutePosition = _PropertyGet(
"AbsolutePosition
")
75 End Property
' AbsolutePosition (get)
77 Property Let AbsolutePosition(ByVal pvValue As Variant)
78 Call _PropertySet(
"AbsolutePosition
", pvValue)
79 End Property
' AbsolutePosition (set)
81 REM -----------------------------------------------------------------------------------------------------------------------
82 Property Get BOF() As Boolean
83 BOF = _PropertyGet(
"BOF
")
84 End Property
' BOF (get)
86 REM -----------------------------------------------------------------------------------------------------------------------
87 Property Get Bookmark() As Variant
88 Bookmark = _PropertyGet(
"Bookmark
")
89 End Property
' Bookmark (get)
91 Property Let Bookmark(ByVal pvValue As Variant)
92 Call _PropertySet(
"Bookmark
", pvValue)
93 End Property
' Bookmark (set)
95 REM -----------------------------------------------------------------------------------------------------------------------
96 Property Get Bookmarkable() As Boolean
97 Bookmarkable = _PropertyGet(
"Bookmarkable
")
98 End Property
' Bookmarkable (get)
100 REM -----------------------------------------------------------------------------------------------------------------------
101 Property Get EOF() As Boolean
102 EOF = _PropertyGet(
"EOF
")
103 End Property
' EOF (get)
105 REM -----------------------------------------------------------------------------------------------------------------------
106 Property Get EditMode() As Boolean
107 EditMode = _PropertyGet(
"EditMode
")
108 End Property
' EditMode (get)
110 REM -----------------------------------------------------------------------------------------------------------------------
111 Property Get Filter() As Variant
112 Filter = _PropertyGet(
"Filter
")
113 End Property
' Filter (get)
115 Property Let Filter(ByVal pvValue As Variant)
116 Call _PropertySet(
"Filter
", pvValue)
117 End Property
' Filter (set)
119 REM -----------------------------------------------------------------------------------------------------------------------
120 Property Get LastModified() As Variant
121 ' DO NOT PUBLISH
122 LastModified = _PropertyGet(
"LastModified
")
123 End Property
' LastModified (get)
125 REM -----------------------------------------------------------------------------------------------------------------------
126 Property Get Name() As String
127 Name = _PropertyGet(
"Name
")
128 End Property
' Name (get)
130 REM -----------------------------------------------------------------------------------------------------------------------
131 Property Get ObjectType() As String
132 ObjectType = _PropertyGet(
"ObjectType
")
133 End Property
' ObjectType (get)
135 REM -----------------------------------------------------------------------------------------------------------------------
136 Property Get RecordCount() As Long
137 RecordCount = _PropertyGet(
"RecordCount
")
138 End Property
' RecordCount (get)
140 REM -----------------------------------------------------------------------------------------------------------------------
141 REM --- CLASS METHODS ---
142 REM -----------------------------------------------------------------------------------------------------------------------
144 REM -----------------------------------------------------------------------------------------------------------------------
145 Public Function AddNew() As Boolean
146 ' Initiates the creation of a new record
148 Const cstThisSub =
"Recordset.AddNew
"
149 Dim i As Integer, iFieldsCount As Integer, oField As Object
150 Dim sdefault As String, oColumn As Object
151 Dim iValue As Integer, lValue As Long, sgValue As Single, dbValue As Double, dValue As Date
153 If _ErrorHandler() Then On Local Error Goto Error_Function
154 Utils._SetCalledSub(cstThisSub)
158 'Is inserting a new row allowed ?
159 If _ForwardOnly Or _ReadOnly Then Goto Error_NoUpdate
160 If Not .CanUpdateInsertedRows Then Goto Error_NoUpdate
161 If Not .IsBookmarkable Then Goto Error_NoUpdate
162 If _EditMode
<> dbEditNone Then CancelUpdate()
163 If _BOF And _EOF Then
' Records before first or after last do not have a bookmark
164 _BookmarkBeforeNew =
"_BOF_
"
165 ElseIf .isBeforeFirst() Then
166 _BookmarkBeforeNew =
"_BOF_
"
167 ElseIf .isAfterLast() Then
168 _BookmarkBeforeNew =
"_EOF_
"
170 _BookmarkBeforeNew = .getBookmark()
175 'Set all fields to their default value
176 iFieldsCount = Fields().Count
177 On Local Error Resume Next
' Do not stop if default setting fails
178 For i =
0 To iFieldsCount -
1
179 Set oField = Fields(i)
180 Set oColumn = oField.Column
181 If Utils._hasUNOProperty(oColumn,
"DefaultValue
") Then
' Default value in database set via SQL statement
182 sDefault = oColumn.DefaultValue
183 ElseIf Utils._hasUNOProperty(oColumn,
"ControlDefault
") Then
' Default value set in Base via table edition
184 If IsEmpty(oColumn.ControlDefault) Then sdefault =
"" Else sDefault = oColumn.ControlDefault
186 sdefault =
""
188 If sDefault =
"" Then
189 If oColumn.IsNullable = com.sun.star.sdbc.ColumnValue.NULLABLE Then oColumn.updateNull()
190 Else
' No default value
191 With com.sun.star.sdbc.DataType
192 Select Case oColumn.Type
194 If sDefault =
"1" Then oColumn.updateBoolean(True) Else oColumn.updateBoolean(False)
196 iValue = CInt(sDefault)
197 If iValue
>= -
128 And iValue
<= +
127 Then oColumn.updateShort(iValue)
199 lValue = CLng(sDefault)
200 If lValue
>= -
32768 And lValue
<=
32767 Then oColumn.updateInt(lValue)
202 lValue = CLng(sDefault)
203 If lValue
>= -
2147483648 And lValue
<=
2147483647 Then oColumn.updateInt(lValue)
205 lValue = CLng(sDefault)
206 Column.updateLong(lValue)
' No proper type conversion for HYPER data type
208 sgValue = CSng(sDefault)
209 If Abs(sgValue)
< 3.402823E38 And Abs(sgValue)
> 1.401298E-45 Then oColumn.updateFloat(sgValue)
211 dbValue = CDbl(sDefault)
212 'If Abs(dbValue)
< 1.79769313486232E308 And Abs(dbValue)
> 4.94065645841247E-307 Then oColumn.updateDouble(dbValue)
213 oColumn.updateDouble(dbValue)
214 Case .NUMERIC, .DECIMAL
215 dbValue = CDbl(sDefault)
216 If Utils._hasUNOProperty(Column,
"Scale
") Then
217 If Column.Scale
> 0 Then
218 'If Abs(dbValue)
< 1.79769313486232E308 And Abs(dbValue)
> 4.94065645841247E-307 Then oColumn.updateDouble(dbValue)
219 oColumn.updateDouble(dbValue)
221 oColumn.updateString(sdefault)
224 oColumn.updateString(sdefault)
226 Case .CHAR, .VARCHAR, .LONGVARCHAR
227 oColumn.updateString(sdefault)
' vbString
229 dValue = DateValue(sDefault)
230 vTemp = New com.sun.star.util.Date
233 .Month = Month(dValue)
236 oColumn.updateDate(vTemp)
238 dValue = TimeValue(sDefault)
239 vTemp = New com.sun.star.util.Time
241 .Hours = Hour(dValue)
242 .Minutes = Minute(dValue)
243 .Seconds = Second(dValue)
244 '.HundredthSeconds =
0
246 oColumn.updateTime(vTemp)
248 dValue = DateValue(sDefault)
249 vTemp = New com.sun.star.util.DateTime
252 .Month = Month(dValue)
254 .Hours = Hour(dValue)
255 .Minutes = Minute(dValue)
256 .Seconds = Second(dValue)
257 '.HundredthSeconds =
0
259 oColumn.updateTimestamp(vTemp)
260 ' Case .BINARY, .VARBINARY, .LONGVARBINARY
269 If _ErrorHandler() Then On Local Error Goto Error_Function Else On Local Error Goto
0
271 _EditMode = dbEditAdd
275 Utils._ResetCalledSub(cstThisSub)
278 TraceError(TRACEABORT, Err, cstThisSub, Erl)
281 TraceError(TRACEFATAL, ERRNOTUPDATABLE, Utils._CalledSub(),
0)
283 End Function
' AddNew
285 REM -----------------------------------------------------------------------------------------------------------------------
286 Public Function CancelUpdate() As Boolean
287 ' Cancel any edit action
289 Const cstThisSub =
"Recordset.CancelUpdate
"
291 If _ErrorHandler() Then On Local Error Goto Error_Function
292 Utils._SetCalledSub(cstThisSub)
296 Select Case _EditMode
299 If Not IsNull(_BookmarkBeforeNew) Then
300 Select Case _BookmarkBeforeNew
301 Case
"_BOF_
" : .beforeFirst()
302 Case
"_EOF_
" : .afterLast()
303 Case Else : .moveToBookmark(_BookmarkBeforeNew)
306 Case dbEditInProgress
311 _EditMode = dbEditNone
312 _BookmarkBeforeNew = Null
313 _BookmarkLastModified = Null
317 Utils._ResetCalledSub(cstThisSub)
320 TraceError(TRACEABORT, Err, cstThisSub, Erl)
322 End Function
' CancelUpdate
324 REM -----------------------------------------------------------------------------------------------------------------------
325 Public Function Clone() As Object
326 ' Duplicate an existing recordset
328 Const cstThisSub =
"Recordset.Clone
"
331 Dim iType As Integer, iOptions As Integer, iLockEdit As Integer
332 If _ErrorHandler() Then On Local Error Goto Error_Function
333 Utils._SetCalledSub(cstThisSub)
336 If _IsClone Then Goto Error_Clone
337 If _ForwardOnly Then iType = dbOpenForwardOnly Else iType = cstNull
338 If _PassThrough Then iOptions = dbSQLPassThrough Else iOptions = cstNull
339 iLockEdit = dbReadOnly
' Always read-only
341 Set Clone = OpenRecordset(iType, iOptions, iLockEdit, True)
344 Utils._ResetCalledSub(cstThisSub)
347 TraceError(TRACEABORT, Err, cstThisSub, Erl)
350 TraceError(TRACEFATAL, ERRRECORDSETCLONE, Utils._CalledSub(),
0)
352 End Function
' Clone
354 REM -----------------------------------------------------------------------------------------------------------------------
355 Public Function mClose(ByVal Optional pbRemove As Boolean) As Variant
356 ' Dispose UNO objects
357 ' If pbRemove = True, remove recordset from Recordsets collection
359 Const cstThisSub =
"Recordset.Close
"
361 If _ErrorHandler() Then On Local Error Goto Exit_Function
' Do not stop execution
362 Utils._SetCalledSub(cstThisSub)
363 If Not IsNull(RowSet) Then
371 _Command =
""
372 _ParentName =
""
373 _ParentType =
""
377 _Filter =
""
378 _EditMode = dbEditNone
379 _BookmarkBeforeNew = Null
380 _BookmarkLastModified = Null
383 If IsMissing(pbRemove) Then pbRemove = True
384 If pbRemove Then _ParentDatabase.RecordsetsColl.Remove(_Name)
385 Set _ParentDatabase = Nothing
388 Utils._ResetCalledSub(cstThisSub)
390 End Function
' Close
392 REM -----------------------------------------------------------------------------------------------------------------------
393 Public Function Delete() As Boolean
394 ' Deletes the current record
396 Const cstThisSub =
"Recordset.Delete
"
398 If _ErrorHandler() Then On Local Error Goto Error_Function
399 Utils._SetCalledSub(cstThisSub)
402 'Is deleting a row allowed ?
403 If _ForwardOnly Or _ReadOnly Then Goto Error_NoUpdate
404 If _EditMode
<> dbEditNone Then
408 If RowSet.rowDeleted() Then Goto Error_RowDeleted
414 Utils._ResetCalledSub(cstThisSub)
417 TraceError(TRACEABORT, Err, cstThisSub, Erl)
420 TraceError(TRACEFATAL, ERRNOTUPDATABLE, Utils._CalledSub(),
0)
423 TraceError(TRACEFATAL, ERRROWDELETED, Utils._CalledSub(),
0)
426 TraceError(TRACEFATAL, ERRUPDATESEQUENCE, Utils._CalledSub(),
0,
1)
428 End Function
' Delete
430 REM -----------------------------------------------------------------------------------------------------------------------
431 Public Function Edit() As Boolean
432 ' Updates the current record
434 Const cstThisSub =
"Recordset.Edit
"
436 If _ErrorHandler() Then On Local Error Goto Error_Function
437 Utils._SetCalledSub(cstThisSub)
440 'Is updating a row allowed ?
441 If _ForwardOnly Or _ReadOnly Then Goto Error_NoUpdate
442 If _EditMode
<> dbEditNone Then CancelUpdate()
443 If RowSet.rowDeleted() Then Goto Error_RowDeleted
445 _EditMode = dbEditInProgress
449 Utils._ResetCalledSub(cstThisSub)
452 TraceError(TRACEABORT, Err, cstThisSub, Erl)
455 TraceError(TRACEFATAL, ERRNOTUPDATABLE, Utils._CalledSub(),
0)
458 TraceError(TRACEFATAL, ERRROWDELETED, Utils._CalledSub(),
0)
460 End Function
' Edit
462 REM -----------------------------------------------------------------------------------------------------------------------
463 Public Function Fields(ByVal Optional pvIndex As variant) As Object
465 If _ErrorHandler() Then On Local Error Goto Error_Function
466 Const cstThisSub =
"Recordset.Fields
"
467 Utils._SetCalledSub(cstThisSub)
470 If Not IsMissing(pvIndex) Then
471 If Not Utils._CheckArgument(pvIndex,
1, Utils._AddNumeric(vbString)) Then Goto Exit_Function
474 Dim sObjects() As String, sObjectName As String, oObject As Object
475 Dim i As Integer, bFound As Boolean, oFields As Object
477 Set oFields = RowSet.getColumns()
478 sObjects = oFields.ElementNames()
480 Case IsMissing(pvIndex)
481 Set oObject = New Collect
482 oObject._CollType = COLLFIELDS
483 oObject._ParentType = OBJRECORDSET
484 oObject._ParentName = _Name
485 Set oObject._ParentDatabase = _ParentDatabase
486 oObject._Count = UBound(sObjects) +
1
488 Case VarType(pvIndex) = vbString
490 ' Check existence of object and find its exact (case-sensitive) name
491 For i =
0 To UBound(sObjects)
492 If UCase(pvIndex) = UCase(sObjects(i)) Then
493 sObjectName = sObjects(i)
498 If Not bFound Then Goto Trace_NotFound
499 Case Else
' pvIndex is numeric
500 If pvIndex
< 0 Or pvIndex
> UBound(sObjects) Then Goto Trace_IndexError
501 sObjectName = sObjects(pvIndex)
504 Set oObject = New Field
505 oObject._Name = sObjectName
506 Set oObject.Column = oFields.getByName(sObjectName)
507 oObject._ParentName = _Name
508 oObject._ParentType = _Type
509 Set oObject._ParentDatabase = _ParentDatabase
513 Set oObject = Nothing
514 Utils._ResetCalledSub(cstThisSub)
517 TraceError(TRACEABORT, Err, cstThisSub, Erl)
520 TraceError(TRACEFATAL, ERROBJECTNOTFOUND, Utils._CalledSub(),
0, , Array(_GetLabel(
"FIELD
"), pvIndex))
523 TraceError(TRACEFATAL, ERRCOLLECTION, Utils._CalledSub(),
0)
525 End Function
' Fields
527 REM -----------------------------------------------------------------------------------------------------------------------
528 Public Function getProperty(Optional ByVal pvProperty As Variant) As Variant
529 ' Return property value of psProperty property name
531 Const cstThisSub =
"Recordset.getProperty
"
532 Utils._SetCalledSub(cstThisSub)
533 If IsMissing(pvProperty) Then Call _TraceArguments()
534 getProperty = _PropertyGet(pvProperty)
535 Utils._ResetCalledSub(cstThisSub)
537 End Function
' getProperty
539 REM -----------------------------------------------------------------------------------------------------------------------
540 Public Function GetRows(ByVal Optional pvNumRows As variant) As Variant
542 If _ErrorHandler() Then On Local Error Goto Error_Function
543 Const cstThisSub =
"Recordset.GetRows
"
544 Utils._SetCalledSub(cstThisSub)
546 Dim vMatrix() As Variant, lSize As Long, iNumFields As Integer, i As Integer
548 If IsMissing(pvNumRows) Then Call _TraceArguments()
549 If Not Utils._CheckArgument(pvNumRows,
1, Utils._AddNumeric()) Then Goto Exit_Function
550 If pvNumRows
< 1 Then Goto Trace_Error
551 If IsNull(RowSet) Then Goto Trace_Closed
552 If Not _DataSet Then Goto Exit_Function
554 If _EditMode
<> dbEditNone Then CancelUpdate()
556 If _EOF Then Goto Exit_Function
559 iNumFields = RowSet.getColumns().Count -
1
560 If iNumFields
< 0 Then Goto Exit_Function
562 ReDim vMatrix(
0 To pvNumRows -
1,
0 To iNumFields)
' Conscious opposite of MSAccess !!
564 Do While Not _EOF And lSize
< pvNumRows -
1
566 For i =
0 To iNumFields
567 vMatrix(lSize, i) = _getResultSetColumnValue(RowSet, i +
1)
569 _Move(
"NEXT
")
571 If lSize
< pvNumRows -
1 Then
' Resize to number of fetched records
572 ReDim Preserve vMatrix(
0 To lSize,
0 To iNumFields)
576 GetRows() = vMatrix()
577 Utils._ResetCalledSub(cstThisSub)
580 TraceError(TRACEABORT, Err, cstThisSub, Erl)
583 TraceError(TRACEFATAL, ERRWRONGARGUMENT, Utils._CalledSub(),
0, , Array(
1, pvNumRows))
584 Set Controls = Nothing
587 TraceError(TRACEFATAL, ERRRECORDSETCLOSED, Utils._CalledSub(),
0)
589 End Function
' GetRows V1.1
.0
591 REM -----------------------------------------------------------------------------------------------------------------------
592 Public Function hasProperty(ByVal Optional pvProperty As Variant) As Boolean
593 ' Return True if object has a valid property called pvProperty (case-insensitive comparison !)
595 Const cstThisSub =
"Recordset.hasProperty
"
596 Utils._SetCalledSub(cstThisSub)
597 If IsMissing(pvProperty) Then hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList()) Else hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList(), pvProperty)
598 Utils._ResetCalledSub(cstThisSub)
601 End Function
' hasProperty
603 REM -----------------------------------------------------------------------------------------------------------------------
604 Public Function Move(ByVal Optional pvRelative As Variant, ByVal Optional pvBookmark As variant) As Boolean
605 ' Move record pointer Relative rows vs. bookmark or current record
607 If IsMissing(pvRelative) Then Call _TraceArguments()
608 If Not Utils._CheckArgument(pvRelative,
1, Utils._AddNumeric()) Then Goto Exit_Function
610 If IsMissing(pvBookmark) Then Move = _Move(pvRelative) Else Move = _Move(pvRelative, pvBookmark)
614 End Function
' Move
616 REM -----------------------------------------------------------------------------------------------------------------------
617 Public Function MoveFirst() As Boolean
618 MoveFirst = _Move(
"First
")
619 End Function
' MoveFirst
621 REM -----------------------------------------------------------------------------------------------------------------------
622 Public Function MoveLast() As Boolean
623 MoveLast = _Move(
"Last
")
624 End Function
' MoveLast
626 REM -----------------------------------------------------------------------------------------------------------------------
627 Public Function MoveNext() As Boolean
628 MoveNext = _Move(
"Next
")
629 End Function
' MoveNext
631 REM -----------------------------------------------------------------------------------------------------------------------
632 Public Function MovePrevious() As Boolean
633 MovePrevious = _Move(
"Previous
")
634 End Function
' MovePrevious
636 REM -----------------------------------------------------------------------------------------------------------------------
637 Public Function OpenRecordset(ByVal Optional pvType As Variant _
638 , ByVal Optional pvOptions As Variant _
639 , ByVal Optional pvLockEdit As Variant _
640 , ByVal Optional pbClone As Boolean) As Object
641 'Return a Recordset object based on currentrecordset object with filter addition
643 If _ErrorHandler() Then On Local Error Goto Error_Function
644 Dim cstThisSub As String
645 cstThisSub = Utils._PCase(_Type)
& ".OpenRecordset
"
646 Utils._SetCalledSub(cstThisSub)
647 Set OpenRecordset = Nothing
650 Dim oObject As Object
651 Set oObject = Nothing
652 If IsMissing(pvType) Then
655 If Not Utils._CheckArgument(pvType,
1, Utils._AddNumeric(), Array(cstNull, dbOpenForwardOnly)) Then Goto Exit_Function
657 If IsMissing(pvOptions) Then
660 If Not Utils._CheckArgument(pvOptions,
2, Utils._AddNumeric(), Array(cstNull, dbSQLPassThrough)) Then Goto Exit_Function
662 If IsMissing(pvLockEdit) Then
665 If Not Utils._CheckArgument(pvLockEdit,
3, Utils._AddNumeric(), Array(cstNull, dbReadOnly)) Then Goto Exit_Function
667 If IsMissing(pbClone) Then pbClone = False
' pbClone is a not published argument
669 Set oObject = New Recordset
671 ._CommandType = _CommandType
675 Set ._ParentDatabase = _ParentDatabase
676 ._ForwardOnly = ( pvType = dbOpenForwardOnly )
677 ._PassThrough = ( pvOptions = dbSQLPassThrough )
678 ._ReadOnly = ( (pvLockEdit = dbReadOnly) Or _ReadOnly )
680 Case pbClone : Call ._Initialize(, RowSet)
681 Case _Filter
<> "" : Call ._Initialize(_Filter)
682 Case Else : Call ._Initialize()
686 .RecordsetMax = .RecordsetMax +
1
687 oObject._Name = Format(.RecordsetMax,
"0000000")
688 .RecordsetsColl.Add(oObject, UCase(oObject._Name))
691 If Not ( oObject._BOF And oObject._EOF ) Then oObject.MoveFirst()
' Do nothing if resultset empty
694 Set OpenRecordset = oObject
695 Set oObject = Nothing
696 Utils._ResetCalledSub(cstThisSub)
699 TraceError(TRACEABORT, Err, Utils._CalledSub(), Erl)
701 End Function
' OpenRecordset
703 REM -----------------------------------------------------------------------------------------------------------------------
704 Public Function Properties(ByVal Optional pvIndex As Variant) As Variant
706 ' a Collection object if pvIndex absent
707 ' a Property object otherwise
709 Const cstThisSub =
"Recordset.Properties
"
710 Utils._SetCalledSub(cstThisSub)
711 Dim vProperty As Variant, vPropertiesList() As Variant, sObject As String
712 vPropertiesList = _PropertiesList()
713 sObject = Utils._PCase(_Type)
714 If IsMissing(pvIndex) Then
715 vProperty = PropertiesGet._Properties(sObject, _Name, vPropertiesList)
717 vProperty = PropertiesGet._Properties(sObject, _Name, vPropertiesList, pvIndex)
718 vProperty._Value = _PropertyGet(vPropertiesList(pvIndex))
720 Set vProperty._ParentDatabase = _ParentDatabase
723 Set Properties = vProperty
724 Utils._ResetCalledSub(cstThisSub)
726 End Function
' Properties
728 REM -----------------------------------------------------------------------------------------------------------------------
729 Public Function setProperty(ByVal Optional psProperty As String, ByVal Optional pvValue As Variant) As Boolean
730 ' Return True if property setting OK
731 Const cstThisSub =
"Recordset.setProperty
"
732 Utils._SetCalledSub(cstThisSub)
733 setProperty = _PropertySet(psProperty, pvValue)
734 Utils._ResetCalledSub(cstThisSub)
737 REM -----------------------------------------------------------------------------------------------------------------------
738 Public Function Update() As Boolean
739 ' Finalize the updates of the current record
741 Const cstThisSub =
"Recordset.Update
"
743 If _ErrorHandler() Then On Local Error Goto Error_Function
744 Utils._SetCalledSub(cstThisSub)
747 'Is updating a row allowed ?
748 If _ForwardOnly Or _ReadOnly Then Goto Error_NoUpdate
750 If .rowDeleted() Then Goto Error_RowDeleted
751 Select Case _EditMode
753 Goto Trace_Error_Update
755 If .IsNew And .IsModified Then .insertRow()
756 _BookmarkLastModified = .getBookmark()
757 If Not IsNull(_BookmarkBeforeNew) Then
758 Select Case _BookmarkBeforeNew
759 Case
"_BOF_
" : .beforeFirst()
760 Case
"_EOF_
" : .afterLast()
761 Case Else : .moveToBookmark(_BookmarkBeforeNew)
764 Case dbEditInProgress
767 _BookmarkLastModified = .getBookmark()
771 _EditMode = dbEditNone
775 Utils._ResetCalledSub(cstThisSub)
778 TraceError(TRACEABORT, Err, cstThisSub, Erl)
781 TraceError(TRACEFATAL, ERRNOTUPDATABLE, Utils._CalledSub(),
0)
784 TraceError(TRACEFATAL, ERRUPDATESEQUENCE, Utils._CalledSub(),
0,
1)
787 TraceError(TRACEFATAL, ERRROWDELETED, Utils._CalledSub(),
0)
789 End Function
' Update
791 REM -----------------------------------------------------------------------------------------------------------------------
792 REM --- PRIVATE FUNCTIONS ---
793 REM -----------------------------------------------------------------------------------------------------------------------
795 REM -----------------------------------------------------------------------------------------------------------------------
796 Public Sub _Initialize(ByVal Optional pvFilter As Variant, Optional poRowSet As Object)
797 ' Initialize new recordset
799 If _Command =
"" Then Exit Sub
801 If _ErrorHandler() Then On Local Error Goto Error_Sub
802 If IsMissing(pvFilter) Then pvFilter =
""
803 If Not IsMissing(poRowSet) Then
' Clone
804 Set RowSet = poRowSet.createResultSet()
806 RowSet.last()
' Solves bookmark desynchro when parent bookmark is used ?!?
808 Set RowSet = CreateUnoService(
"com.sun.star.sdb.RowSet
")
811 If IsNull(.ActiveConnection) Then Set .ActiveConnection = _ParentDatabase.Connection
812 .CommandType = _CommandType
814 If _ForwardOnly Then .ResultSetType = com.sun.star.sdbc.ResultSetType.FORWARD_ONLY _
815 Else .ResultSetType = com.sun.star.sdbc.ResultSetType.SCROLL_SENSITIVE
816 If _PassThrough Then .EscapeProcessing = False _
817 Else .EscapeProcessing = True
819 .ResultSetConcurrency = com.sun.star.sdbc.ResultSetConcurrency.READ_ONLY
820 .TransactionIsolation = com.sun.star.sdbc.TransactionIsolation.READ_UNCOMMITTED
' Dirty read
822 .ResultSetConcurrency = com.sun.star.sdbc.ResultSetConcurrency.UPDATABLE
823 .TransactionIsolation = com.sun.star.sdbc.TransactionIsolation.READ_COMMITTED
827 If Not IsMissing(pvFilter) Then
' Filter must be set before execute()
828 If pvFilter
<> "" Then
829 RowSet.Filter = pvFilter
830 RowSet.ApplyFilter = True
833 On Local Error Goto SQL_Error
835 On Local Error Goto Error_Sub
838 'If the Recordset contains no records, the BOF and EOF properties are True, and there is no current record.
839 _BOF = ( RowSet.IsRowCountFinal And RowSet.RowCount =
0 )
845 TraceError(TRACEFATAL, ERRSQLSTATEMENT, Utils._CalledSub(),
0, , _Command)
848 TraceError(TRACEABORT, Err,
"Recordset._Initialize
", Erl)
850 End Sub
' _Initialize
852 REM -----------------------------------------------------------------------------------------------------------------------
853 Public Function _Move(pvTarget As Variant, ByVal Optional pvBookmark As Variant, ByVal Optional pbAbsolute As Boolean) As Boolean
854 'Move to the first, last, next, or previous record in a specified Recordset object and make that record the current record.
856 Dim cstThisSub As String
857 cstThisSub =
"Recordset.Move
" & Iif(VarType(pvTarget) = vbString, pvTarget,
"")
858 Utils._SetCalledSub(cstThisSub)
859 If _ErrorHandler() Then On Local Error Goto Error_Function
861 If IsNull(RowSet) Then Goto Trace_Closed
862 If Not _DataSet Then Goto Trace_NoData
863 If _BOF And _EOF Then Goto Trace_NoData
865 CancelUpdate()
' Any Move cancels all updates, even Move(
0) !
867 Dim l As Long, lRow As Long
869 Select Case VarType(pvTarget)
871 Select Case UCase(pvTarget)
872 Case
"FIRST
"
874 If Not ( .isBeforeFirst() Or .isFirst() ) Then
882 Case
"LAST
"
884 If .isAfterLast() Then Goto Trace_Forward
885 Do While Not ( .isRowCountFinal And .Row = .RowCount )
' isLast() = True after reading of first records chunk
891 Case
"NEXT
"
892 If _EOF Then Goto Trace_OutOfRange
894 Case
"PREVIOUS
"
895 If _ForwardOnly Then Goto Trace_Forward
896 If _BOF Then Goto Trace_OutOfRange
899 Case Else
' Relative or absolute move
900 If IsMissing(pbAbsolute) Then pbAbsolute = False
' Relative move is default
901 If _ForwardOnly And pvTarget
< 0 then Goto Trace_Forward
902 If IsMissing(pvBookmark) Then
903 If pvTarget =
0 Then Goto Exit_Function
' Do nothing
905 If pbAbsolute Then lRow = .getRow() Else lRow =
0
906 For l =
1 To pvTarget - lRow
907 If .isAfterLast() Then Exit For
911 If pbAbsolute Then .absolute(pvTarget) Else .relative(pvTarget)
913 Else
' Move is always relative when bookmark argument present
914 If _ForwardOnly Then Goto Trace_Forward
916 .moveToBookmark(pvBookmark)
918 .moveRelativeToBookmark(pvBookmark, pvTarget)
923 _BOF = .isBeforeFirst()
' https://forum.openoffice.org/en/forum/viewtopic.php?f=
47&t=
76640
924 _EOF = .isAfterlast()
928 If .rowDeleted() Then Goto Error_RowDeleted
929 If .rowUpdated() Then .refreshRow()
935 Utils._ResetCalledSub(cstThisSub)
937 Exit_Close:
' Force close of recordset when error raised
941 TraceError(TRACEABORT, Err, cstThisSub, Erl)
944 TraceError(TRACEFATAL, ERRRECORDSETFORWARD, Utils._CalledSub(),
0)
947 TraceError(TRACEFATAL, ERRRECORDSETNODATA, Utils._CalledSub(),
0)
950 TraceError(TRACEFATAL, ERRRECORDSETRANGE, Utils._CalledSub(),
0)
953 TraceError(TRACEFATAL, ERRROWDELETED, Utils._CalledSub(),
0)
956 TraceError(TRACEFATAL, ERRRECORDSETCLOSED, Utils._CalledSub(),
0)
958 End Function
' Move
960 REM -----------------------------------------------------------------------------------------------------------------------
961 Private Function _PropertiesList() As Variant
963 _PropertiesList = Array(
"AbsolutePosition
",
"BOF
",
"Bookmarkable
",
"Bookmark
",
"EditMode
" _
964 ,
"EOF
",
"Filter
",
"LastModified
",
"Name
",
"ObjectType
" ,
"RecordCount
" _
966 End Function
' _PropertiesList
968 REM -----------------------------------------------------------------------------------------------------------------------
969 Private Function _PropertyGet(ByVal psProperty As String) As Variant
970 ' Return property value of the psProperty property name
972 If _ErrorHandler() Then On Local Error Goto Error_Function
973 Dim cstThisSub As String
974 cstThisSub =
"Recordset.get
"
975 Utils._SetCalledSub(cstThisSub
& psProperty)
977 Dim vEMPTY As Variant
978 _PropertyGet = vEMPTY
980 Select Case UCase(psProperty)
981 Case UCase(
"AbsolutePosition
")
982 If IsNull(RowSet) Then Goto Trace_Closed
985 Case _BOF And _EOF : _PropertyGet = -
1
986 Case .isBeforeFirst() Or .isAfterLast() : _PropertyGet = -
1
987 Case Else : _PropertyGet = .getRow()
' Not getRow() -
1 as MSAccess requires
990 Case UCase(
"BOF
")
991 If IsNull(RowSet) Then Goto Trace_Closed
993 Case _BOF And _EOF : _PropertyGet = True
994 Case RowSet.isBeforeFirst() : _PropertyGet = True
995 Case Else : _PropertyGet = False
997 Case UCase(
"Bookmarkable
")
998 If IsNull(RowSet) Then Goto Trace_Closed
999 If _ForwardOnly Then _PropertyGet = False Else _PropertyGet = RowSet.IsBookmarkable
1000 Case UCase(
"Bookmark
")
1001 If IsNull(RowSet) Then Goto Trace_Closed
1002 If RowSet.IsBookmarkable And Not _ForwardOnly Then
1003 If _BOF Or _EOF Then _PropertyGet = Null Else _PropertyGet = RowSet.getBookmark()
1006 If _ForwardOnly Then Goto Trace_Forward
1008 Case UCase(
"EditMode
")
1009 If IsNull(RowSet) Then Goto Trace_Closed
1010 _PropertyGet = _EditMode
1011 Case UCase(
"EOF
")
1012 If IsNull(RowSet) Then Goto Trace_Closed
1014 Case _BOF And _EOF : _PropertyGet = True
1015 Case RowSet.isAfterLast() : _PropertyGet = True
1016 Case Else : _PropertyGet = False
1018 Case UCase(
"Filter
")
1019 If IsNull(RowSet) Then Goto Trace_Closed
1020 _PropertyGet = RowSet.Filter
1021 Case UCase(
"LastModified
")
1022 If IsNull(RowSet) Then Goto Trace_Closed
1023 If RowSet.IsBookmarkable And Not _ForwardOnly Then
1024 _PropertyGet = _BookmarkLastModified
1027 If _ForwardOnly Then Goto Trace_Forward
1029 Case UCase(
"Name
")
1030 _PropertyGet = _Name
1031 Case UCase(
"ObjectType
")
1032 _PropertyGet = _Type
1033 Case UCase(
"RecordCount
")
1034 If IsNull(RowSet) Then Goto Trace_Closed
1035 _PropertyGet = RowSet.RowCount
1041 Utils._ResetCalledSub(cstThisSub
& psProperty)
1044 TraceError(TRACEFATAL, ERRPROPERTY, Utils._CalledSub(),
0, , psProperty)
1045 _PropertyGet = vEMPTY
1048 TraceError(TRACEFATAL, ERRRECORDSETFORWARD, Utils._CalledSub(),
0)
1051 TraceError(TRACEFATAL, ERRRECORDSETCLOSED, Utils._CalledSub(),
0)
1054 TraceError(TRACEABORT, Err, cstThisSub
& "._PropertyGet
", Erl)
1055 _PropertyGet = vEMPTY
1057 End Function
' _PropertyGet
1059 REM -----------------------------------------------------------------------------------------------------------------------
1060 Private Function _PropertySet(ByVal psProperty As String, ByVal pvValue As Variant) As Boolean
1062 Dim cstThisSub As String
1063 cstThisSub =
"Recordset.set
"
1064 Utils._SetCalledSub(cstThisSub
& psProperty)
1065 If _ErrorHandler() Then On Local Error Goto Error_Function
1069 Dim iArgNr As Integer
1070 Dim oObject As Object
1072 If _IsLeft(_A2B_.CalledSub,
"Recordset.
") Then iArgNr =
1 Else iArgNr =
2
1073 Select Case UCase(psProperty)
1074 Case UCase(
"AbsolutePosition
")
1075 If Not Utils._CheckArgument(pvValue, iArgNr, Utils._AddNumeric(), , False) Then Goto Trace_Error_Value
1076 If pvValue
< 1 Then Goto Trace_Error_Value
1077 _Move(pvValue, , True)
1078 Case UCase(
"Bookmark
")
1079 If IsNull(RowSet) Then Goto Trace_Closed
1081 Case UCase(
"Filter
")
1082 If IsNull(RowSet) Then Goto Trace_Closed
1083 If Not Utils._CheckArgument(pvValue, iArgNr, vbString, , False) Then Goto Trace_Error_Value
1084 _Filter = _ParentDatabase._ReplaceSquareBrackets(pvValue)
1090 Utils._ResetCalledSub(cstThisSub
& psProperty)
1093 TraceError(TRACEFATAL, ERRPROPERTY, Utils._CalledSub(),
0,
1, psProperty)
1094 _PropertySet = False
1097 TraceError(TRACEFATAL, ERRPROPERTYVALUE, Utils._CalledSub(),
0,
1, Array(pvValue, psProperty))
1098 _PropertySet = False
1101 TraceError(TRACEFATAL, ERRRECORDSETCLOSED, Utils._CalledSub(),
0)
1104 TraceError(TRACEABORT, Err, Utils._CalledSub(), Erl)
1105 _PropertySet = False
1107 End Function
' _PropertySet