1<?xml version="1.0" encoding="UTF-8"?> 2<!--*********************************************************** 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 * 21 ***********************************************************--> 22 23 24<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd"> 25<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Test_DB" script:language="StarBasic">REM ***** Database Test ***** 26 27const cMessageDatabaseService = "Database Service" 28const cMessageDatabaseOpen = "Open Database" 29const cMessageDatabaseInsert = "Insert record into Database" 30const cMessageDatabaseDelete = "Delete record from Database" 31const cMessageDatabaseSeek = "Read other record from Database" 32const cMessageDatabaseClose = "Close Database" 33 34Sub TestDB 35 36 Dim oDBConnection as Object, oDataSource as Object, oDBContext as Object 37 Dim sDBName as String, sTable as String, sCurrentMessage as String 38 Dim nRowCount as Integer 39 Dim bResult as Boolean 40 Const sTestString = "Automatical Test" 41 42 On Local Error GoTo DBERROR 43 44 gCurrentTestCase = cLogfileFailed 45 LocalTestLog% = OpenLogDat (GetLogFileName(gCurrentDocTest)) 46 47 gCurrentTestCase = cDBService 48 sCurrentMessage = cMessageDatabaseService + " " + cUnoDatabaseContext 49 50 oDBContext = CreateUnoService(cUnoDatabaseContext) 51 sDBName=oDBContext.ElementNames(0) 'Names of Databases 52 53 gCurrentTestCase = cDBOpen 54 sCurrentMessage = cMessageDatabaseOpen 55 56 oDataSource = oDBContext.GetByName(sDBName) 57 sTable=oDataSource.Tables.ElementNames(0) 58 oDBConnection = oDBContext.GetByName(sDBName).GetConnection("","") 59 60 LogTestResult( "Database "+ cMessageDatabaseOpen, not IsNull (oDBConnection) ) 61 if (IsNull(oDBConnection)) then 62 Close #LocalTestLog% 63 LocalTestLog = 0 64 Exit Sub 65 End If 66 67 ' Database is open now 68 69 gCurrentTestCase = cDBService 70 sCurrentMessage = cMessageDatabaseService + " " + cUnoRowSet 71 oRowSet = createUnoService(cUnoRowSet) 72 73 if (IsNull(oRowSet)) then 74 LogTestResult( "Database "+ cMessageDatabaseService + " " + cUnoRowSet, not IsNull (oRowSet) ) 75 Exit Sub 76 else 77 LogTestResult( "Database "+ cMessageDatabaseService, TRUE ) 78 End If 79 80 gCurrentTestCase = cDBInsert 81 sCurrentMessage = cMessageDatabaseInsert 82 83 oRowSet.ActiveConnection = oDBConnection 84 85 oRowSet.CommandType = com.sun.star.sdb.CommandType.COMMAND 86 oRowSet.Command = "SELECT * FROM " + sTable 87 oRowSet.execute() 88 89 oRowSet.moveToInsertRow 90 oRowSet.updateString(5, sTestString) 91 92 oRowSet.insertRow() 93 nRowCount=oRowSet.RowCount 94 95 oRowSet.moveToCurrentRow() 96 97 bResult = (oRowSet.getString(5) = sTestString) 98 LogTestResult( "Database "+ cMessageDatabaseInsert, bResult ) 99 100 'delete only if insert passed 101 102 if (bResult) Then 103 gCurrentTestCase = cDBDelete 104 sCurrentMessage = cMessageDatabaseDelete 105 oRowSet.deleteRow() 106 bResult = (nRowCount - oRowSet.RowCount = 0) 107 if ( bResult ) Then 108 oRowSet.next() 109 bResult = (nRowCount - oRowSet.RowCount = 1) 110 End If 111 LogTestResult( "Database "+ cMessageDatabaseDelete, bResult ) 112 End If 113 114 ' read other record 115 116 gCurrentTestCase = cDBSeek 117 sCurrentMessage = cMessageDatabaseSeek 118 oRowSet.first() 119 bResult = not (oRowSet.getString(5) = sTestString) 120 LogTestResult( "Database "+ cMessageDatabaseSeek, bResult ) 121 122 gCurrentTestCase = cDBClose 123 sCurrentMessage = cMessageDatabaseClose 124 oDBConnection.Dispose() 125 LogTestResult( "Database "+ cMessageDatabaseClose, True ) 126 127 Print #LocalTestLog, "---" 128 Close #LocalTestLog% 129 LocalTestLog = 0 130 Exit Sub ' Without error 131 132 DBERROR: 133 If ( gCurrentTestCase = cLogfileFailed ) then 134 LogTestResult( " ", False ) 135 Exit Sub 136 else 137 LogTestResult( "Database "+ sCurrentMessage, FALSE ) 138 Close #LocalTestLog% 139 LocalTestLog = 0 140 End If 141 Exit Sub ' With error 142End Sub 143</script:module> 144