1<?xml version="1.0" encoding="UTF-8"?> 2<!--********************************************************************** 3* 4* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5* 6* Copyright 2000, 2010 Oracle and/or its affiliates. 7* 8* OpenOffice.org - a multi-platform office productivity suite 9* 10* This file is part of OpenOffice.org. 11* 12* OpenOffice.org is free software: you can redistribute it and/or modify 13* it under the terms of the GNU Lesser General Public License version 3 14* only, as published by the Free Software Foundation. 15* 16* OpenOffice.org is distributed in the hope that it will be useful, 17* but WITHOUT ANY WARRANTY; without even the implied warranty of 18* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19* GNU Lesser General Public License version 3 for more details 20* (a copy is included in the LICENSE file that accompanied this code). 21* 22* You should have received a copy of the GNU Lesser General Public License 23* version 3 along with OpenOffice.org. If not, see 24* <http://www.openoffice.org/license.html> 25* for a copy of the LGPLv3 License. 26* 27**********************************************************************--> 28<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd"> 29<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Test_DB" script:language="StarBasic">REM ***** Database Test ***** 30 31const cMessageDatabaseService = "Database Service" 32const cMessageDatabaseOpen = "Open Database" 33const cMessageDatabaseInsert = "Insert record into Database" 34const cMessageDatabaseDelete = "Delete record from Database" 35const cMessageDatabaseSeek = "Read other record from Database" 36const cMessageDatabaseClose = "Close Database" 37 38Sub TestDB 39 40 Dim oDBConnection as Object, oDataSource as Object, oDBContext as Object 41 Dim sDBName as String, sTable as String, sCurrentMessage as String 42 Dim nRowCount as Integer 43 Dim bResult as Boolean 44 Const sTestString = "Automatical Test" 45 46 On Local Error GoTo DBERROR 47 48 gCurrentTestCase = cLogfileFailed 49 LocalTestLog% = OpenLogDat (GetLogFileName(gCurrentDocTest)) 50 51 gCurrentTestCase = cDBService 52 sCurrentMessage = cMessageDatabaseService + " " + cUnoDatabaseContext 53 54 oDBContext = CreateUnoService(cUnoDatabaseContext) 55 sDBName=oDBContext.ElementNames(0) 'Names of Databases 56 57 gCurrentTestCase = cDBOpen 58 sCurrentMessage = cMessageDatabaseOpen 59 60 oDataSource = oDBContext.GetByName(sDBName) 61 sTable=oDataSource.Tables.ElementNames(0) 62 oDBConnection = oDBContext.GetByName(sDBName).GetConnection("","") 63 64 LogTestResult( "Database "+ cMessageDatabaseOpen, not IsNull (oDBConnection) ) 65 if (IsNull(oDBConnection)) then 66 Close #LocalTestLog% 67 LocalTestLog = 0 68 Exit Sub 69 End If 70 71 ' Database is open now 72 73 gCurrentTestCase = cDBService 74 sCurrentMessage = cMessageDatabaseService + " " + cUnoRowSet 75 oRowSet = createUnoService(cUnoRowSet) 76 77 if (IsNull(oRowSet)) then 78 LogTestResult( "Database "+ cMessageDatabaseService + " " + cUnoRowSet, not IsNull (oRowSet) ) 79 Exit Sub 80 else 81 LogTestResult( "Database "+ cMessageDatabaseService, TRUE ) 82 End If 83 84 gCurrentTestCase = cDBInsert 85 sCurrentMessage = cMessageDatabaseInsert 86 87 oRowSet.ActiveConnection = oDBConnection 88 89 oRowSet.CommandType = com.sun.star.sdb.CommandType.COMMAND 90 oRowSet.Command = "SELECT * FROM " + sTable 91 oRowSet.execute() 92 93 oRowSet.moveToInsertRow 94 oRowSet.updateString(5, sTestString) 95 96 oRowSet.insertRow() 97 nRowCount=oRowSet.RowCount 98 99 oRowSet.moveToCurrentRow() 100 101 bResult = (oRowSet.getString(5) = sTestString) 102 LogTestResult( "Database "+ cMessageDatabaseInsert, bResult ) 103 104 'delete only if insert passed 105 106 if (bResult) Then 107 gCurrentTestCase = cDBDelete 108 sCurrentMessage = cMessageDatabaseDelete 109 oRowSet.deleteRow() 110 bResult = (nRowCount - oRowSet.RowCount = 0) 111 if ( bResult ) Then 112 oRowSet.next() 113 bResult = (nRowCount - oRowSet.RowCount = 1) 114 End If 115 LogTestResult( "Database "+ cMessageDatabaseDelete, bResult ) 116 End If 117 118 ' read other record 119 120 gCurrentTestCase = cDBSeek 121 sCurrentMessage = cMessageDatabaseSeek 122 oRowSet.first() 123 bResult = not (oRowSet.getString(5) = sTestString) 124 LogTestResult( "Database "+ cMessageDatabaseSeek, bResult ) 125 126 gCurrentTestCase = cDBClose 127 sCurrentMessage = cMessageDatabaseClose 128 oDBConnection.Dispose() 129 LogTestResult( "Database "+ cMessageDatabaseClose, True ) 130 131 Print #LocalTestLog, "---" 132 Close #LocalTestLog% 133 LocalTestLog = 0 134 Exit Sub ' Without error 135 136 DBERROR: 137 If ( gCurrentTestCase = cLogfileFailed ) then 138 LogTestResult( " ", False ) 139 Exit Sub 140 else 141 LogTestResult( "Database "+ sCurrentMessage, FALSE ) 142 Close #LocalTestLog% 143 LocalTestLog = 0 144 End If 145 Exit Sub ' With error 146End Sub 147</script:module> 148