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=
"Test_DB" script:
language=
"StarBasic">REM ***** Database Test *****
5 const cMessageDatabaseService =
"Database Service
"
6 const cMessageDatabaseOpen =
"Open Database
"
7 const cMessageDatabaseInsert =
"Insert record into Database
"
8 const cMessageDatabaseDelete =
"Delete record from Database
"
9 const cMessageDatabaseSeek =
"Read other record from Database
"
10 const cMessageDatabaseClose =
"Close Database
"
12 Sub TestDB (FilterType as Integer)
14 Dim oDBConnection as Object, oDataSource as Object, oDBContext as Object
15 Dim sDBName as String, sTable as String, sCurrentMessage as String
16 Dim nRowCount as Integer, nCurrentAction as Integer
17 Dim bResult as Boolean
18 Const sTestString =
"Automatical Test
"
21 On Local Error GoTo DBERROR
23 nCurrentAction = cLogfileFailed
24 FileChannel% = OpenLogDat (GetLogFileName(FilterType))
26 nCurrentAction = cDBService
27 sCurrentMessage = cMessageDatabaseService +
" " + cUnoDatabaseContext
29 oDBContext = CreateUnoService(cUnoDatabaseContext)
30 sDBName=oDBContext.ElementNames(
0)
'Names of Databases
32 nCurrentAction = cDBOpen
33 sCurrentMessage = cMessageDatabaseOpen
35 oDataSource = oDBContext.GetByName(sDBName)
36 sTable=oDataSource.Tables.ElementNames(
0)
37 oDBConnection = oDBContext.GetByName(sDBName).GetConnection(
"",
"")
39 LogState (not IsNull (oDBConnection),
"Database
"+ cMessageDatabaseOpen, FileChannel)
40 LogState (not IsNull (oDBConnection),
"Database
"+ cMessageDatabaseOpen, MainFileChannel)
41 SetStatus (FilterType, cDBOpen, not IsNull (oDBConnection))
42 if (IsNull(oDBConnection)) then
47 ' Database is open now
49 nCurrentAction = cDBService
50 sCurrentMessage = cMessageDatabaseService +
" " + cUnoRowSet
51 oRowSet = createUnoService(cUnoRowSet)
53 if (IsNull(oRowSet)) then
54 LogState (not IsNull (oRowSet),
"Database
"+ cMessageDatabaseService +
" " + cUnoRowSet, FileChannel)
55 LogState (not IsNull (oRowSet),
"Database
"+ cMessageDatabaseService +
" " + cUnoRowSet, MainFileChannel)
56 SetStatus (FilterType, cDBService, not IsNull (oRowSet))
59 LogState (not IsNull (oRowSet),
"Database
"+ cMessageDatabaseService, FileChannel)
60 LogState (not IsNull (oRowSet),
"Database
"+ cMessageDatabaseService, MainFileChannel)
61 SetStatus (FilterType, cDBService, not IsNull (oRowSet))
64 nCurrentAction = cDBInsert
65 sCurrentMessage = cMessageDatabaseInsert
67 oRowSet.ActiveConnection = oDBConnection
69 oRowSet.CommandType = com.sun.star.sdb.CommandType.COMMAND
70 oRowSet.Command =
"SELECT * FROM
" + sTable
73 oRowSet.moveToInsertRow
74 oRowSet.updateString(
5, sTestString)
77 nRowCount=oRowSet.RowCount
79 oRowSet.moveToCurrentRow()
81 bResult = (oRowSet.getString(
5) = sTestString)
82 LogState (bResult,
"Database
"+ cMessageDatabaseInsert, FileChannel)
83 LogState (bResult,
"Database
"+ cMessageDatabaseInsert, MainFileChannel)
84 SetStatus (FilterType, cDBInsert, bResult)
86 'delete only if insert passed
89 nCurrentAction = cDBDelete
90 sCurrentMessage = cMessageDatabaseDelete
92 bResult = (nRowCount - oRowSet.RowCount =
0)
95 bResult = (nRowCount - oRowSet.RowCount =
1)
97 LogState (bResult,
"Database
"+ cMessageDatabaseDelete, FileChannel)
98 LogState (bResult,
"Database
"+ cMessageDatabaseDelete, MainFileChannel)
99 SetStatus (FilterType, cDBDelete, bResult)
102 ' read other record
104 nCurrentAction = cDBSeek
105 sCurrentMessage = cMessageDatabaseSeek
107 bResult = not (oRowSet.getString(
5) = sTestString)
108 LogState (bResult,
"Database
"+ cMessageDatabaseSeek, FileChannel)
109 LogState (bResult,
"Database
"+ cMessageDatabaseSeek, MainFileChannel)
110 SetStatus (FilterType, cDBSeek, bResult)
112 nCurrentAction = cDBClose
113 sCurrentMessage = cMessageDatabaseClose
114 oDBConnection.Dispose()
115 LogState (True,
"Database
"+ cMessageDatabaseClose, FileChannel)
116 LogState (True,
"Database
"+ cMessageDatabaseClose, MainFileChannel)
117 SetStatus (FilterType, cDBClose, True)
119 Print #FileChannel,
"---
"
121 Exit Sub
' Without error
124 If (nCurrentAction = cLogfileFailed) then
125 SetStatus (FilterType, cDBOpen, False)
128 LogState (False,
"Database
"+ sCurrentMessage, FileChannel)
129 LogState (False,
"Database
"+ sCurrentMessage, MainFileChannel)
130 SetStatus (FilterType, nCurrentAction, False)
133 Exit Sub
' With error