/
Dbfunctions.prg
391 lines (291 loc) · 12.4 KB
/
Dbfunctions.prg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
//
// Copyright (c) XSharp B.V. All Rights Reserved.
// Licensed under the Apache License, Version 2.0.
// See License.txt in the project root for license information.
//
USING XSharp
USING XSharp.RDD
USING XSharp.RDD.Enums
USING System.Collections.Generic
USING System.IO
USING System.Reflection
USING System.Linq
USING System.Text
FUNCTION _NoAlias(uArea AS USUAL) AS Error
VAR oErr := Error{EG_NOTABLE, "uArea", "Alias '"+uArea:ToString()+"' is not found"}
oErr:FuncSym := ProcName(1):Replace("FUNCTIONS:","")
RETURN oErr
/// <exclude />
FUNCTION _DbThrowErrorOnFailure(funcName AS STRING, resultToCheck AS LOGIC) AS LOGIC
IF !resultToCheck
LOCAL result := DoError(funcName) AS OBJECT
IF result IS LOGIC VAR lResult
resultToCheck := lResult
ELSEIF result IS LONG VAR liResult
resultToCheck := liResult != 0
ELSE
resultToCheck := FALSE
ENDIF
ENDIF
RETURN resultToCheck
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/alias0/*" />
FUNCTION Alias0() AS STRING
LOCAL oRDD := VoDb.CWA("Alias0", FALSE) AS IRdd
IF oRDD != NULL
RETURN oRDD:Alias
ENDIF
RETURN String.Empty
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/bof/*" />
FUNCTION Bof() AS LOGIC
RETURN VoDb.Bof()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/bof/*" />
FUNCTION Bof(uArea AS USUAL) AS LOGIC
RETURN (uArea)->(Bof())
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/dbf/*" />
FUNCTION DBF() AS STRING
RETURN VoDb.Dbf()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/dbf/*" />
FUNCTION DBF(uArea AS USUAL) AS STRING
RETURN (uArea)->(DBF())
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/dbpack/*" />
FUNCTION DbPack() AS LOGIC STRICT
RETURN _DbThrowErrorOnFailure(__FUNCTION__, VoDb.Pack())
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/dbunlock/*" />
FUNCTION DbUnLock() AS LOGIC STRICT
RETURN _DbThrowErrorOnFailure(__FUNCTION__, VoDb.Unlock(NULL_OBJECT))
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/dbunlock/*" />
FUNCTION DbUnLock(nRecord AS LONG) AS LOGIC STRICT
RETURN _DbThrowErrorOnFailure(__FUNCTION__, VoDb.Unlock(nRecord))
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/dbzap/*" />
FUNCTION DbZap() AS LOGIC STRICT
RETURN _DbThrowErrorOnFailure(__FUNCTION__, VoDb.Zap())
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/dbunlockall/*" />
FUNCTION DbUnLockAll() AS LOGIC STRICT
RETURN VoDb.UnlockAll()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/deleted/*" />
FUNCTION Deleted() AS LOGIC STRICT
RETURN VoDb.Deleted()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/deleted/*" />
FUNCTION Deleted(uArea AS USUAL) AS LOGIC STRICT
RETURN (uArea)->(Deleted())
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/eof/*" />
FUNCTION Eof() AS LOGIC
RETURN VoDb.Eof()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/eof/*" />
FUNCTION Eof(uArea AS USUAL) AS LOGIC
RETURN (uArea)->(Eof())
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/flock/*" />
FUNCTION Flock() AS LOGIC STRICT
RETURN VoDb.Flock()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/flock/*" />
FUNCTION Flock(uArea AS USUAL) AS LOGIC STRICT
RETURN (uArea)->(Flock())
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/fcount/*" />
FUNCTION FCount() AS DWORD
RETURN VoDb.FCount()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/fcount/*" />
FUNCTION FCount(uArea AS USUAL) AS DWORD
RETURN (uArea)->(FCount())
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/fieldname/*" />
FUNCTION FieldName(dwFieldPos AS DWORD) AS STRING
IF dwFieldPos > 0 .AND. dwFieldPos <= VoDb.FCount()
RETURN VoDb.FieldName(dwFieldPos)
ENDIF
RETURN String.Empty
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/fieldname/*" />
FUNCTION FieldName(dwFieldPos AS DWORD, uArea AS USUAL) AS STRING
RETURN (uArea)->(FieldName(dwFieldPos))
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/fieldsym/*" />
FUNCTION FieldSym(dwFieldPos AS DWORD) AS SYMBOL
IF dwFieldPos > 0 .AND. dwFieldPos <= VoDb.FCount()
RETURN (SYMBOL) VoDb.FieldName(dwFieldPos)
ENDIF
RETURN NULL_SYMBOL
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/fieldsym/*" />
FUNCTION FieldSym(dwFieldPos AS DWORD, uArea AS USUAL) AS SYMBOL
RETURN (uArea)->(FieldSym(dwFieldPos))
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/fieldpos/*" />
FUNCTION FieldPos(cFieldName AS STRING) AS DWORD
RETURN VoDb.FieldPos(cFieldName)
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/fieldpos/*" />
FUNCTION FieldPos(cFieldName AS STRING, nArea AS DWORD) AS DWORD
LOCAL oRDD := RuntimeState.Workareas:GetRDD(nArea) AS IRdd
IF oRDD != NULL_OBJECT
RETURN (DWORD) oRDD:FieldIndex(cFieldName)
ENDIF
RETURN 0
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/found/*" />
FUNCTION Found() AS LOGIC
RETURN VoDb.Found()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/found/*" />
FUNCTION Found(uArea AS USUAL) AS LOGIC
RETURN (uArea)->(Found())
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/header/*" />
FUNCTION Header() AS LONG
LOCAL oValue := NULL AS OBJECT
IF VoDb.Info(DBI_GETHEADERSIZE, REF oValue)
RETURN Convert.ToInt32(oValue)
ENDIF
RETURN 0
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/header/*" />
FUNCTION Header(uArea AS USUAL) AS LONG
RETURN (uArea)->(Header())
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/lastrec/*" />
FUNCTION LastRec() AS DWORD
RETURN (DWORD) VoDb.LastRec()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/lastrec/*" />
FUNCTION LastRec(uArea AS USUAL) AS DWORD
RETURN (uArea)->(LastRec())
/// <summary>Refresh the buffer for the current workarea, discarding any changes that were made.</summary>
/// <returns>
/// </returns>
FUNCTION DbBuffRefresh() AS LOGIC STRICT
RETURN _DbThrowErrorOnFailure(__FUNCTION__, VoDb.Refresh())
FUNCTION DbBuffRefresh(uArea AS USUAL) AS LOGIC STRICT
RETURN (uArea)->(DbBuffRefresh())
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/dbclearfilter/*" />
FUNCTION DbClearFilter() AS LOGIC STRICT
RETURN _DbThrowErrorOnFailure(__FUNCTION__, VoDb.ClearFilter())
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/dbclearfilter/*" />
FUNCTION DbClearFilter(uArea AS USUAL) AS LOGIC STRICT
RETURN (uArea)->(DbClearFilter())
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/dbclearrelation/*" />
FUNCTION DbClearRelation() AS LOGIC STRICT
RETURN _DbThrowErrorOnFailure(__FUNCTION__, VoDb.ClearRelation())
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/dbcloseall/*" />
FUNCTION DbCloseAll() AS LOGIC STRICT
RETURN VoDb.CloseAll()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/dbclosearea/*" />
FUNCTION DbCloseArea () AS LOGIC STRICT
RETURN VoDb.CloseArea()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/dbclosearea/*" />
FUNCTION DbCloseArea (uArea AS USUAL) AS LOGIC STRICT
RETURN (uArea)->DbCloseArea()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/dbcommit/*" />
FUNCTION DbCommit() AS LOGIC STRICT
RETURN VoDb.Commit()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/dbcommitall/*" />
FUNCTION DbCommitAll() AS LOGIC STRICT
RETURN VoDb.CommitAll()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/dbcontinue/*" />
FUNCTION DbContinue() AS LOGIC STRICT
RETURN _DbThrowErrorOnFailure(__FUNCTION__, VoDb.Continue())
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/dbdriver/*" />
FUNCTION DbDriver() AS STRING STRICT
RETURN RddName()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/dbfilter/*" />
FUNCTION DbFilter() AS STRING STRICT
RETURN VoDb.Filter()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/dbgetselect/*" />
FUNCTION DbGetSelect() AS DWORD STRICT
RETURN VoDb.GetSelect()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/dbgobottom/*" />
FUNCTION DbGoBottom() AS LOGIC STRICT
RETURN _DbThrowErrorOnFailure(__FUNCTION__, VoDb.GoBottom())
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/dbgotop/*" />
FUNCTION DbGoTop() AS LOGIC STRICT
RETURN _DbThrowErrorOnFailure(__FUNCTION__, VoDb.GoTop())
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/rddname/*" />
FUNCTION RddName () AS STRING STRICT
LOCAL cRet AS STRING
IF Used()
cRet := VoDb.RddName()
ELSE
cRet := RddSetDefault()
ENDIF
RETURN cRet
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/rddsetdefault/*" />
FUNCTION RddSetDefault () AS STRING STRICT
RETURN VoDb.RddSetDefault(NULL_STRING)
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/rddsetdefault/*" />
FUNCTION RddSetDefault (cNewSetting AS STRING) AS STRING
RETURN VoDb.RddSetDefault(cNewSetting)
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/reccount/*" />
FUNCTION RecCount() AS LONG
RETURN VoDb.LastRec()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/reccount/*" />
FUNCTION RecCount(uArea AS USUAL) AS LONG
RETURN (uArea)->(RecCount())
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/recno/*" />
FUNCTION RecNo() AS DWORD
RETURN VoDb.Recno()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/recno/*" />
FUNCTION RecNo(uArea AS USUAL) AS DWORD
RETURN (uArea)->(RecNo())
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/recsize/*" />
FUNCTION RecSize AS LONG
RETURN VoDb.RecSize()
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/recsize/*" />
FUNCTION RecSize(uArea AS USUAL) AS LONG
RETURN (uArea)->(RecSize())
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/rlock/*" />
FUNCTION RLock() AS LOGIC STRICT
RETURN VoDb.RLock(NULL)
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/rlock/*" />
FUNCTION RLock(uArea AS USUAL) AS LOGIC STRICT
RETURN (uArea)->(RLock())
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/rlock/*" />
FUNCTION RLock(cRecordNumberList AS STRING, uArea AS USUAL) AS LOGIC STRICT
LOCAL aRecords AS STRING[]
LOCAL nSelect AS DWORD
LOCAL lOk AS LOGIC
LOCAL locked AS List<INT>
IF String.IsNullOrWhiteSpace(cRecordNumberList)
RETURN FALSE
ENDIF
aRecords := cRecordNumberList:Split(<CHAR>{c','},StringSplitOptions.RemoveEmptyEntries)
nSelect := DbGetSelect()
DbSelectArea(uArea)
lOk := TRUE
locked := List<INT>{}
FOREACH VAR s IN aRecords
IF Int32.TryParse(s, OUT VAR iRecno)
IF iRecno > 0
IF ! VoDb.RLock(iRecno)
lOk := FALSE
EXIT
ENDIF
locked:Add(iRecno)
ELSEIF iRecno == 0
// Todo: FoxPro allows to lock the header
NOP
ENDIF
ELSE
lOk := FALSE
EXIT
ENDIF
NEXT
// When one record could not be locked then release all locks set here
IF ! lOk .and. locked:Count > 0
FOREACH var recno in locked
DbUnLock(recno)
NEXT
ENDIF
DbSelectArea(nSelect)
RETURN lOk
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/used/*" />
FUNCTION Used() AS LOGIC
RETURN RuntimeState.Workareas:CurrentWorkarea != NULL
/// <include file="VoFunctionDocs.xml" path="Runtimefunctions/used/*" />
FUNCTION Used(uArea AS USUAL) AS LOGIC
IF IsString(uArea) .OR. IsSymbol(uArea)
RETURN VoDbGetSelect(uArea) != 0
ENDIF
RETURN Alias(uArea) != NULL_STRING
FUNCTION DoError (cSymFunc AS STRING, nTries:= 0 AS INT) AS OBJECT
LOCAL oError AS Error
LOCAL bBlock AS ICodeblock
IF RuntimeState.LastRddError IS Error
oError := (Error) RuntimeState.LastRddError
ELSEIF RuntimeState.LastRddError != NULL_OBJECT
oError := Error{RuntimeState.LastRddError}
ELSE
oError := Error{"Unknown Error occurred" }
ENDIF
oError:FuncSym := cSymFunc
oError:Tries := nTries
bBlock := XSharp.RuntimeState.GetValue<ICodeblock>(Set.ErrorBlock)
IF bBlock != NULL
RETURN bBlock:EvalBlock(oError)
ENDIF
THROW oError