xref: /aoo41x/main/qadevOOo/runner/util/DBTools.java (revision e6b649b5)
1ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ef39d40dSAndrew Rist  * distributed with this work for additional information
6ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10ef39d40dSAndrew Rist  *
11ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12ef39d40dSAndrew Rist  *
13ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18ef39d40dSAndrew Rist  * under the License.
19ef39d40dSAndrew Rist  *
20ef39d40dSAndrew Rist  *************************************************************/
21ef39d40dSAndrew Rist 
22ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package util;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.uno.Exception;
27cdf0e10cSrcweir import java.io.PrintWriter ;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir // access the implementations via names
30cdf0e10cSrcweir import com.sun.star.uno.XInterface;
31cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
32cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
35cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
36cdf0e10cSrcweir import com.sun.star.sdbc.XConnection ;
37cdf0e10cSrcweir import com.sun.star.sdbc.XResultSet ;
38cdf0e10cSrcweir import com.sun.star.sdbc.XResultSetUpdate ;
39cdf0e10cSrcweir import com.sun.star.sdbc.XStatement ;
40cdf0e10cSrcweir import com.sun.star.sdbc.XRowUpdate ;
41cdf0e10cSrcweir import com.sun.star.util.Date ;
42cdf0e10cSrcweir import com.sun.star.uno.XNamingService ;
43cdf0e10cSrcweir import com.sun.star.task.XInteractionHandler ;
44cdf0e10cSrcweir import com.sun.star.sdb.XCompletedConnection ;
45cdf0e10cSrcweir import com.sun.star.container.XEnumeration ;
46cdf0e10cSrcweir import com.sun.star.container.XEnumerationAccess ;
47cdf0e10cSrcweir import com.sun.star.io.XInputStream ;
48cdf0e10cSrcweir import com.sun.star.io.XTextInputStream ;
49cdf0e10cSrcweir import com.sun.star.io.XDataInputStream ;
50cdf0e10cSrcweir import com.sun.star.container.XNameAccess ;
51cdf0e10cSrcweir import com.sun.star.frame.XStorable;
52cdf0e10cSrcweir import com.sun.star.sdb.XDocumentDataSource;
53cdf0e10cSrcweir import com.sun.star.sdbc.XCloseable ;
54cdf0e10cSrcweir import java.sql.Statement;
55cdf0e10cSrcweir import java.sql.Connection;
56cdf0e10cSrcweir import java.sql.DriverManager;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir /**
59cdf0e10cSrcweir * Provides useful methods for working with SOffice databases.
60cdf0e10cSrcweir * Database creation, data transfering, outputting infromation.
61cdf0e10cSrcweir */
62cdf0e10cSrcweir public class DBTools {
63cdf0e10cSrcweir 
64cdf0e10cSrcweir     private XMultiServiceFactory xMSF = null ;
65cdf0e10cSrcweir     private XNamingService dbContext = null ;
66cdf0e10cSrcweir     private PrintWriter m_log = null;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir     //JDBC driver
69cdf0e10cSrcweir     public final static String TST_JDBC_DRIVER = "org.gjt.mm.mysql.Driver";
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     // constants for TestDB table column indexes
72cdf0e10cSrcweir     public final static int TST_STRING = 1 ;
73cdf0e10cSrcweir     public final static int TST_INT = 2 ;
74cdf0e10cSrcweir     public final static int TST_DOUBLE = 5 ;
75cdf0e10cSrcweir     public final static int TST_DATE = 6 ;
76cdf0e10cSrcweir     public final static int TST_BOOLEAN = 10 ;
77cdf0e10cSrcweir     public final static int TST_CHARACTER_STREAM = 11 ;
78cdf0e10cSrcweir     public final static int TST_BINARY_STREAM = 12 ;
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     // constants for TestDB columns names
81cdf0e10cSrcweir     public final static String TST_STRING_F = "_TEXT" ;
82cdf0e10cSrcweir     public final static String TST_INT_F = "_INT" ;
83cdf0e10cSrcweir     public final static String TST_DOUBLE_F = "_DOUBLE" ;
84cdf0e10cSrcweir     public final static String TST_DATE_F = "_DATE" ;
85cdf0e10cSrcweir     public final static String TST_BOOLEAN_F = "_BOOL" ;
86cdf0e10cSrcweir     public final static String TST_CHARACTER_STREAM_F = "_MEMO1" ;
87cdf0e10cSrcweir     public final static String TST_BINARY_STREAM_F = "_MEMO2" ;
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     /**
90cdf0e10cSrcweir     * Values for filling test table.
91cdf0e10cSrcweir     */
92cdf0e10cSrcweir     public final static Object[][] TST_TABLE_VALUES = new Object[][] {
93cdf0e10cSrcweir         {"String1", new Integer(1), null, null, new Double(1.1),
94cdf0e10cSrcweir          new Date((short) 1,(short) 1, (short) 2001), null, null, null,
95cdf0e10cSrcweir          Boolean.TRUE, null, null},
96cdf0e10cSrcweir         {"String2", new Integer(2), null, null, new Double(1.2),
97cdf0e10cSrcweir          new Date((short) 2, (short) 1,(short)  2001), null, null, null,
98cdf0e10cSrcweir          Boolean.FALSE, null, null},
99cdf0e10cSrcweir         {null, null, null, null, null,
100cdf0e10cSrcweir          null, null, null, null,
101cdf0e10cSrcweir          null, null, null}
102cdf0e10cSrcweir     } ;
103cdf0e10cSrcweir 
104cdf0e10cSrcweir     /**
105cdf0e10cSrcweir     * Array of lengths of streams for each row in of the
106cdf0e10cSrcweir     * <code>TST_TABLE_VALUES</code> constants.
107cdf0e10cSrcweir     */
108cdf0e10cSrcweir     public final static int[] TST_STREAM_LENGTHS = {0, 0, 0} ;
109cdf0e10cSrcweir 
110cdf0e10cSrcweir     /**
111cdf0e10cSrcweir     * It's just a structure with some useful methods for representing
112cdf0e10cSrcweir     * <code>com.sun.star.sdb.DataSource</code> service. All this
113cdf0e10cSrcweir     * service's properties are stored in appropriate class fields.
114cdf0e10cSrcweir     * Class also allows to construct its instances using service
115cdf0e10cSrcweir     * information, and create new service instance upon class
116cdf0e10cSrcweir     * fields.
117cdf0e10cSrcweir     * @see com.sun.star.sdb.DataSource
118cdf0e10cSrcweir     */
119cdf0e10cSrcweir     public class DataSourceInfo {
120cdf0e10cSrcweir         /**
121cdf0e10cSrcweir         * Representation of <code>'Name'</code> property.
122cdf0e10cSrcweir         */
123cdf0e10cSrcweir         public String Name = null ;
124cdf0e10cSrcweir         /**
125cdf0e10cSrcweir         * Representation of <code>'URL'</code> property.
126cdf0e10cSrcweir         */
127cdf0e10cSrcweir         public String URL = null ;
128cdf0e10cSrcweir         /**
129cdf0e10cSrcweir         * Representation of <code>'Info'</code> property.
130cdf0e10cSrcweir         */
131cdf0e10cSrcweir         public PropertyValue[] Info = null ;
132cdf0e10cSrcweir         /**
133cdf0e10cSrcweir         * Representation of <code>'User'</code> property.
134cdf0e10cSrcweir         */
135cdf0e10cSrcweir         public String User = null ;
136cdf0e10cSrcweir         /**
137cdf0e10cSrcweir         * Representation of <code>'Password'</code> property.
138cdf0e10cSrcweir         */
139cdf0e10cSrcweir         public String Password = null ;
140cdf0e10cSrcweir         /**
141cdf0e10cSrcweir         * Representation of <code>'IsPasswordRequired'</code> property.
142cdf0e10cSrcweir         */
143cdf0e10cSrcweir         public Boolean IsPasswordRequired = null ;
144cdf0e10cSrcweir         /**
145cdf0e10cSrcweir         * Representation of <code>'SuppressVersionColumns'</code> property.
146cdf0e10cSrcweir         */
147cdf0e10cSrcweir         public Boolean SuppressVersionColumns = null ;
148cdf0e10cSrcweir         /**
149cdf0e10cSrcweir         * Representation of <code>'IsReadOnly'</code> property.
150cdf0e10cSrcweir         */
151cdf0e10cSrcweir         public Boolean IsReadOnly = null ;
152cdf0e10cSrcweir         /**
153cdf0e10cSrcweir         * Representation of <code>'TableFilter'</code> property.
154cdf0e10cSrcweir         */
155cdf0e10cSrcweir         public String[] TableFilter = null ;
156cdf0e10cSrcweir         /**
157cdf0e10cSrcweir         * Representation of <code>'TableTypeFilter'</code> property.
158cdf0e10cSrcweir         */
159cdf0e10cSrcweir         public String[] TableTypeFilter = null ;
160cdf0e10cSrcweir 
161cdf0e10cSrcweir         /**
162cdf0e10cSrcweir         * Creates an empty instance.
163cdf0e10cSrcweir         */
DataSourceInfo()164cdf0e10cSrcweir         public DataSourceInfo()
165cdf0e10cSrcweir         {
166cdf0e10cSrcweir         }
167cdf0e10cSrcweir 
168cdf0e10cSrcweir         /**
169cdf0e10cSrcweir         * Creates an instance laying upon specified DataSource.
170cdf0e10cSrcweir         * @param dataSource All source properties are copied into
171cdf0e10cSrcweir         * class fields.
172cdf0e10cSrcweir         */
DataSourceInfo(Object dataSource)173cdf0e10cSrcweir         public DataSourceInfo(Object dataSource) {
174cdf0e10cSrcweir             XPropertySet xProps = (XPropertySet)
175cdf0e10cSrcweir                 UnoRuntime.queryInterface(XPropertySet.class, dataSource) ;
176cdf0e10cSrcweir 
177cdf0e10cSrcweir             try {
178cdf0e10cSrcweir                 Name = (String)xProps.getPropertyValue("Name") ;
179cdf0e10cSrcweir                 URL = (String)xProps.getPropertyValue("URL") ;
180cdf0e10cSrcweir                 Info = (PropertyValue[])xProps.getPropertyValue("Info") ;
181cdf0e10cSrcweir                 User = (String)xProps.getPropertyValue("User") ;
182cdf0e10cSrcweir                 Password = (String)xProps.getPropertyValue("Password") ;
183cdf0e10cSrcweir                 IsPasswordRequired = (Boolean)xProps.getPropertyValue("IsPasswordRequired") ;
184cdf0e10cSrcweir                 SuppressVersionColumns = (Boolean)
185cdf0e10cSrcweir                     xProps.getPropertyValue("SuppressVersionColumns") ;
186cdf0e10cSrcweir                 IsReadOnly = (Boolean)xProps.getPropertyValue("IsReadOnly") ;
187cdf0e10cSrcweir                 TableFilter = (String[])xProps.getPropertyValue("TableFilter") ;
188cdf0e10cSrcweir                 TableTypeFilter = (String[])xProps.getPropertyValue("TableTypeFilter") ;
189cdf0e10cSrcweir             } catch (com.sun.star.beans.UnknownPropertyException e) {
190cdf0e10cSrcweir                 System.err.println("util.DBTools.DataSourceInfo: Error retrieving property") ;
191cdf0e10cSrcweir                 e.printStackTrace(System.err) ;
192cdf0e10cSrcweir             } catch (com.sun.star.lang.WrappedTargetException e) {
193cdf0e10cSrcweir                 System.err.println("util.DBTools.DataSourceInfo: Error retrieving property") ;
194cdf0e10cSrcweir                 e.printStackTrace(System.err) ;
195cdf0e10cSrcweir             }
196cdf0e10cSrcweir         }
197cdf0e10cSrcweir 
198cdf0e10cSrcweir         /**
199cdf0e10cSrcweir         * Prints datasource info.
200cdf0e10cSrcweir         * @param out Stream to which information is printed.
201cdf0e10cSrcweir         */
printInfo(PrintWriter out)202cdf0e10cSrcweir         public void printInfo(PrintWriter out) {
203cdf0e10cSrcweir             out.println("Name = '" + Name + "'") ;
204cdf0e10cSrcweir             out.println("  URL = '" + URL + "'") ;
205cdf0e10cSrcweir             out.print("  Info = ") ;
206cdf0e10cSrcweir             if (Info == null) out.println("null") ;
207cdf0e10cSrcweir             else {
208cdf0e10cSrcweir                 out.print("{") ;
209cdf0e10cSrcweir                 for (int i = 0; i < Info.length; i++) {
210cdf0e10cSrcweir                     out.print(Info[i].Name + " = '" + Info[i].Value + "'") ;
211cdf0e10cSrcweir                     if (i + 1 < Info.length) out.print("; ") ;
212cdf0e10cSrcweir                 }
213cdf0e10cSrcweir                 out.println("}") ;
214cdf0e10cSrcweir             }
215cdf0e10cSrcweir             out.println("  User = '" + User + "'") ;
216cdf0e10cSrcweir             out.println("  Password = '" + Password + "'") ;
217cdf0e10cSrcweir             out.println("  IsPasswordRequired = '" + IsPasswordRequired + "'") ;
218cdf0e10cSrcweir             out.println("  SuppressVersionColumns = '" + SuppressVersionColumns + "'") ;
219cdf0e10cSrcweir             out.println("  IsReadOnly = '" + IsReadOnly + "'") ;
220cdf0e10cSrcweir             out.print("  TableFilter = ") ;
221cdf0e10cSrcweir             if (TableFilter == null) out.println("null") ;
222cdf0e10cSrcweir             else {
223cdf0e10cSrcweir                 out.print("{") ;
224cdf0e10cSrcweir                 for (int i = 0; i < TableFilter.length; i++) {
225cdf0e10cSrcweir                     out.print("'" + TableFilter[i] + "'") ;
226cdf0e10cSrcweir                     if (i+1 < TableFilter.length) out.print("; ");
227cdf0e10cSrcweir                 }
228cdf0e10cSrcweir                 out.println("}") ;
229cdf0e10cSrcweir             }
230cdf0e10cSrcweir             out.print("  TableTypeFilter = ") ;
231cdf0e10cSrcweir             if (TableTypeFilter == null) out.println("null") ;
232cdf0e10cSrcweir             else {
233cdf0e10cSrcweir                 out.print("{") ;
234cdf0e10cSrcweir                 for (int i = 0; i < TableTypeFilter.length; i++) {
235cdf0e10cSrcweir                     out.print("'" + TableTypeFilter[i] + "'") ;
236cdf0e10cSrcweir                     if (i+1 < TableTypeFilter.length) out.print("; ");
237cdf0e10cSrcweir                 }
238cdf0e10cSrcweir                 out.println("}") ;
239cdf0e10cSrcweir             }
240cdf0e10cSrcweir         }
241cdf0e10cSrcweir 
242cdf0e10cSrcweir         /**
243cdf0e10cSrcweir         * Creates new <code>com.sun.star.sdb.DataSource</code> service
244cdf0e10cSrcweir         * instance and copies all fields (which are not null) to
245cdf0e10cSrcweir         * appropriate service properties.
246cdf0e10cSrcweir         * @return <code>com.sun.star.sdb.DataSource</code> service.
247cdf0e10cSrcweir         */
getDataSourceService()248cdf0e10cSrcweir         public Object getDataSourceService() throws Exception
249cdf0e10cSrcweir         {
250cdf0e10cSrcweir             Object src = src = xMSF.createInstance("com.sun.star.sdb.DataSource") ;
251cdf0e10cSrcweir 
252cdf0e10cSrcweir             XPropertySet props = (XPropertySet) UnoRuntime.queryInterface
253cdf0e10cSrcweir                 (XPropertySet.class, src) ;
254cdf0e10cSrcweir 
255cdf0e10cSrcweir             if (Name != null) props.setPropertyValue("Name", Name) ;
256cdf0e10cSrcweir             if (URL != null) props.setPropertyValue("URL", URL) ;
257cdf0e10cSrcweir             if (Info != null) props.setPropertyValue("Info", Info) ;
258cdf0e10cSrcweir             if (User != null) props.setPropertyValue("User", User) ;
259cdf0e10cSrcweir             if (Password != null) props.setPropertyValue("Password", Password) ;
260cdf0e10cSrcweir             if (IsPasswordRequired != null) props.setPropertyValue("IsPasswordRequired", IsPasswordRequired) ;
261cdf0e10cSrcweir             if (SuppressVersionColumns != null) props.setPropertyValue("SuppressVersionColumns", SuppressVersionColumns) ;
262cdf0e10cSrcweir             if (IsReadOnly != null) props.setPropertyValue("IsReadOnly", IsReadOnly) ;
263cdf0e10cSrcweir             if (TableFilter != null) props.setPropertyValue("TableFilter", TableFilter) ;
264cdf0e10cSrcweir             if (TableTypeFilter != null) props.setPropertyValue("TableTypeFilter", TableTypeFilter) ;
265cdf0e10cSrcweir 
266cdf0e10cSrcweir             return src ;
267cdf0e10cSrcweir         }
268cdf0e10cSrcweir     }
269cdf0e10cSrcweir 
270cdf0e10cSrcweir     /**
271cdf0e10cSrcweir     * Creates class instance.
272cdf0e10cSrcweir     * @param xMSF <code>XMultiServiceFactory</code>.
273cdf0e10cSrcweir     */
DBTools(XMultiServiceFactory xMSF, PrintWriter _logger )274cdf0e10cSrcweir     public DBTools(XMultiServiceFactory xMSF, PrintWriter _logger )
275cdf0e10cSrcweir     {
276cdf0e10cSrcweir         this.xMSF = xMSF ;
277cdf0e10cSrcweir         this.m_log = _logger;
278cdf0e10cSrcweir 
279cdf0e10cSrcweir         try {
280cdf0e10cSrcweir             Object cont = xMSF.createInstance("com.sun.star.sdb.DatabaseContext") ;
281cdf0e10cSrcweir 
282cdf0e10cSrcweir             dbContext = (XNamingService) UnoRuntime.queryInterface
283cdf0e10cSrcweir                 (XNamingService.class, cont) ;
284cdf0e10cSrcweir 
285cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {}
286cdf0e10cSrcweir     }
287cdf0e10cSrcweir 
288cdf0e10cSrcweir     /**
289cdf0e10cSrcweir     * Returns new instance of <code>DataSourceInfo</code> class.
290cdf0e10cSrcweir     */
newDataSourceInfo()291cdf0e10cSrcweir     public DataSourceInfo newDataSourceInfo() { return new DataSourceInfo() ;}
292cdf0e10cSrcweir 
293cdf0e10cSrcweir     /**
294cdf0e10cSrcweir     * Returns new instance of <code>DataSourceInfo</code> class.
295cdf0e10cSrcweir     */
newDataSourceInfo(Object dataSource)296cdf0e10cSrcweir     public DataSourceInfo newDataSourceInfo(Object dataSource) {
297cdf0e10cSrcweir         return new DataSourceInfo(dataSource);
298cdf0e10cSrcweir     }
299cdf0e10cSrcweir 
300cdf0e10cSrcweir     /**
301cdf0e10cSrcweir     * Registers the datasource on the specified name in
302cdf0e10cSrcweir     * <code>DatabaseContext</code> service.
303cdf0e10cSrcweir     * @param name Name which dataSource will have in global context.
304cdf0e10cSrcweir     * @param dataSource <code>DataSource</code> object which is to
305cdf0e10cSrcweir     * be registered.
306cdf0e10cSrcweir     */
registerDB(String name, Object dataSource)307cdf0e10cSrcweir     public void registerDB(String name, Object dataSource)
308cdf0e10cSrcweir         throws com.sun.star.uno.Exception {
309cdf0e10cSrcweir 
310cdf0e10cSrcweir         dbContext.registerObject(name, dataSource) ;
311cdf0e10cSrcweir     }
312cdf0e10cSrcweir 
313cdf0e10cSrcweir 
314cdf0e10cSrcweir     /**
315cdf0e10cSrcweir     * First tries to revoke the datasource with the specified
316cdf0e10cSrcweir     * name and then registers a new one.
317cdf0e10cSrcweir     * @param name Name which dataSource will have in global context.
318cdf0e10cSrcweir     * @param dataSource <code>DataSource</code> object which is to
319cdf0e10cSrcweir     * be registered.
320cdf0e10cSrcweir     */
reRegisterDB(String name, Object dataSource)321cdf0e10cSrcweir     public void reRegisterDB(String name, Object dataSource)
322cdf0e10cSrcweir         throws com.sun.star.uno.Exception {
323cdf0e10cSrcweir 
324cdf0e10cSrcweir         try {
325cdf0e10cSrcweir             revokeDB(name) ;
326cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {}
327cdf0e10cSrcweir 
328cdf0e10cSrcweir         XDocumentDataSource xDDS = (XDocumentDataSource)
329cdf0e10cSrcweir         UnoRuntime.queryInterface(XDocumentDataSource.class, dataSource);
330cdf0e10cSrcweir         XStorable store = (XStorable) UnoRuntime.queryInterface(XStorable.class,
331cdf0e10cSrcweir                 xDDS.getDatabaseDocument());
332cdf0e10cSrcweir         String aFile = utils.getOfficeTemp(xMSF) + name + ".odb";
333cdf0e10cSrcweir         store.storeAsURL(aFile, new PropertyValue[] {  });
334cdf0e10cSrcweir 
335cdf0e10cSrcweir         registerDB(name, dataSource) ;
336cdf0e10cSrcweir     }
337cdf0e10cSrcweir 
338cdf0e10cSrcweir     /**
339cdf0e10cSrcweir     * RESERVED. Not used.
340cdf0e10cSrcweir     */
connectToTextDB(String contextName, String dbDir, String fileExtension)341cdf0e10cSrcweir     public XConnection connectToTextDB(String contextName,
342cdf0e10cSrcweir         String dbDir, String fileExtension)
343cdf0e10cSrcweir                             throws com.sun.star.uno.Exception {
344cdf0e10cSrcweir 
345cdf0e10cSrcweir         try {
346cdf0e10cSrcweir             XInterface newSource = (XInterface) xMSF.createInstance
347cdf0e10cSrcweir                 ("com.sun.star.sdb.DataSource") ;
348cdf0e10cSrcweir 
349cdf0e10cSrcweir             XPropertySet xSrcProp = (XPropertySet)
350cdf0e10cSrcweir                 UnoRuntime.queryInterface(XPropertySet.class, newSource);
351cdf0e10cSrcweir 
352cdf0e10cSrcweir             xSrcProp.setPropertyValue("URL", "sdbc:text:" + dirToUrl(dbDir));
353cdf0e10cSrcweir 
354cdf0e10cSrcweir             PropertyValue extParam = new PropertyValue() ;
355cdf0e10cSrcweir             extParam.Name = "EXT" ;
356cdf0e10cSrcweir             extParam.Value = fileExtension ;
357cdf0e10cSrcweir 
358cdf0e10cSrcweir             xSrcProp.setPropertyValue("Info", new PropertyValue[] {extParam}) ;
359cdf0e10cSrcweir 
360cdf0e10cSrcweir             dbContext.registerObject(contextName, newSource) ;
361cdf0e10cSrcweir 
362cdf0e10cSrcweir             Object handler = xMSF.createInstance("com.sun.star.sdb.InteractionHandler");
363cdf0e10cSrcweir             XInteractionHandler xHandler = (XInteractionHandler)
364cdf0e10cSrcweir                 UnoRuntime.queryInterface(XInteractionHandler.class, handler) ;
365cdf0e10cSrcweir 
366cdf0e10cSrcweir             XCompletedConnection xSrcCon = (XCompletedConnection)
367cdf0e10cSrcweir                 UnoRuntime.queryInterface(XCompletedConnection.class, newSource) ;
368cdf0e10cSrcweir 
369cdf0e10cSrcweir             XConnection con = xSrcCon.connectWithCompletion(xHandler) ;
370cdf0e10cSrcweir 
371cdf0e10cSrcweir             return con ;
372cdf0e10cSrcweir         } finally {
373cdf0e10cSrcweir             try {
374cdf0e10cSrcweir                 dbContext.revokeObject(contextName) ;
375cdf0e10cSrcweir             } catch (Exception e) {}
376cdf0e10cSrcweir         }
377cdf0e10cSrcweir     }
378cdf0e10cSrcweir 
379cdf0e10cSrcweir     /**
380cdf0e10cSrcweir     * Registers DBase database (directory with DBF files) in the
381cdf0e10cSrcweir     * global DB context, then connects to it.
382cdf0e10cSrcweir     * @param contextName Name under which DB will be registered.
383cdf0e10cSrcweir     * @param dbDir The directory with DBF tables.
384cdf0e10cSrcweir     * @return Connection to the DB.
385cdf0e10cSrcweir     */
connectToDBase(String contextName, String dbDir)386cdf0e10cSrcweir     public XConnection connectToDBase(String contextName,
387cdf0e10cSrcweir         String dbDir)
388cdf0e10cSrcweir         throws com.sun.star.uno.Exception {
389cdf0e10cSrcweir 
390cdf0e10cSrcweir         try {
391cdf0e10cSrcweir             XInterface newSource = (XInterface) xMSF.createInstance
392cdf0e10cSrcweir                 ("com.sun.star.sdb.DataSource") ;
393cdf0e10cSrcweir 
394cdf0e10cSrcweir             XPropertySet xSrcProp = (XPropertySet)
395cdf0e10cSrcweir                 UnoRuntime.queryInterface(XPropertySet.class, newSource);
396cdf0e10cSrcweir             xSrcProp.setPropertyValue("URL", "sdbc:dbase:" + dirToUrl(dbDir));
397cdf0e10cSrcweir 
398cdf0e10cSrcweir             dbContext.registerObject(contextName, newSource) ;
399cdf0e10cSrcweir 
400cdf0e10cSrcweir             XConnection con = connectToSource(newSource) ;
401cdf0e10cSrcweir 
402cdf0e10cSrcweir             return con ;
403cdf0e10cSrcweir         } catch(com.sun.star.uno.Exception e) {
404cdf0e10cSrcweir             try {
405cdf0e10cSrcweir                 dbContext.revokeObject(contextName) ;
406cdf0e10cSrcweir             } catch (Exception ex) {}
407cdf0e10cSrcweir 
408cdf0e10cSrcweir             throw e ;
409cdf0e10cSrcweir         }
410cdf0e10cSrcweir     }
411cdf0e10cSrcweir 
412cdf0e10cSrcweir     /**
413cdf0e10cSrcweir     * Performs connection to DataSource specified.
414cdf0e10cSrcweir     * @param dbSource <code>com.sun.star.sdb.DataSource</code> service
415*e6b649b5SPedro Giffuni     *     specified data source which must be already registered in the
416*e6b649b5SPedro Giffuni     *     <code>DatabaseContext</code> service.
417cdf0e10cSrcweir     * @return Connection to the data source.
418cdf0e10cSrcweir     */
connectToSource(Object dbSource)419cdf0e10cSrcweir     public XConnection connectToSource(Object dbSource)
420cdf0e10cSrcweir         throws com.sun.star.uno.Exception {
421cdf0e10cSrcweir 
422cdf0e10cSrcweir         Object handler = xMSF.createInstance("com.sun.star.sdb.InteractionHandler");
423cdf0e10cSrcweir         XInteractionHandler xHandler = (XInteractionHandler)
424cdf0e10cSrcweir             UnoRuntime.queryInterface(XInteractionHandler.class, handler) ;
425cdf0e10cSrcweir 
426cdf0e10cSrcweir         XCompletedConnection xSrcCon = (XCompletedConnection)
427cdf0e10cSrcweir             UnoRuntime.queryInterface(XCompletedConnection.class, dbSource) ;
428cdf0e10cSrcweir 
429cdf0e10cSrcweir         return xSrcCon.connectWithCompletion(xHandler) ;
430cdf0e10cSrcweir     }
431cdf0e10cSrcweir 
432cdf0e10cSrcweir     /**
433cdf0e10cSrcweir     * Registers Test data source in the <code>DatabaseContext</code> service.
434cdf0e10cSrcweir     * This source always has name <code>'APITestDatabase'</code> and it
435cdf0e10cSrcweir     * is registered in subdirectory <code>TestDB</code> of directory
436cdf0e10cSrcweir     * <code>docPath</code> which is supposed to be a directory with test
437cdf0e10cSrcweir     * documents, but can be any other (it must have subdirectory with DBF
438cdf0e10cSrcweir     * tables). If such data source doesn't exists or exists with
439cdf0e10cSrcweir     * different URL it is recreated and reregistered.
440cdf0e10cSrcweir     * @param docPath Path to database <code>TestDB</code> directory.
441cdf0e10cSrcweir     * @return <code>com.sun.star.sdb.DataSource</code> service
442cdf0e10cSrcweir     * implementation which represents TestDB.
443cdf0e10cSrcweir     */
registerTestDB(String docPath)444cdf0e10cSrcweir     public Object registerTestDB(String docPath)
445cdf0e10cSrcweir         throws com.sun.star.uno.Exception {
446cdf0e10cSrcweir 
447cdf0e10cSrcweir         String testURL = null ;
448cdf0e10cSrcweir         if (docPath.endsWith("/") || docPath.endsWith("\\"))
449cdf0e10cSrcweir             testURL = dirToUrl(docPath + "TestDB") ;
450cdf0e10cSrcweir         else
451cdf0e10cSrcweir             testURL = dirToUrl(docPath + "/" + "TestDB") ;
452cdf0e10cSrcweir         testURL = "sdbc:dbase:" + testURL ;
453cdf0e10cSrcweir 
454cdf0e10cSrcweir         String existURL = null ;
455cdf0e10cSrcweir 
456cdf0e10cSrcweir         XNameAccess na = (XNameAccess) UnoRuntime.queryInterface
457cdf0e10cSrcweir             (XNameAccess.class, dbContext) ;
458cdf0e10cSrcweir 
459cdf0e10cSrcweir         Object src = null ;
460cdf0e10cSrcweir         if (na.hasByName("APITestDatabase")) {
461cdf0e10cSrcweir             src = dbContext.getRegisteredObject("APITestDatabase") ;
462cdf0e10cSrcweir 
463cdf0e10cSrcweir             XPropertySet srcPs = (XPropertySet) UnoRuntime.queryInterface
464cdf0e10cSrcweir                 (XPropertySet.class, src) ;
465cdf0e10cSrcweir 
466cdf0e10cSrcweir             existURL = (String) srcPs.getPropertyValue("URL") ;
467cdf0e10cSrcweir         }
468cdf0e10cSrcweir 
469cdf0e10cSrcweir         if (src == null || !testURL.equals(existURL)) {
470cdf0e10cSrcweir             // test data source must be reregistered.
471cdf0e10cSrcweir             DataSourceInfo info = new DataSourceInfo() ;
472cdf0e10cSrcweir             info.URL = testURL ;
473cdf0e10cSrcweir             src = info.getDataSourceService() ;
474cdf0e10cSrcweir             reRegisterDB("APITestDatabase", src) ;
475cdf0e10cSrcweir             src = dbContext.getRegisteredObject("APITestDatabase") ;
476cdf0e10cSrcweir         }
477cdf0e10cSrcweir 
478cdf0e10cSrcweir         return src ;
479cdf0e10cSrcweir     }
480cdf0e10cSrcweir 
481cdf0e10cSrcweir     /**
482cdf0e10cSrcweir     * Connects to <code>DataSource</code> specially created for testing.
483cdf0e10cSrcweir     * This source always has name <code>'APITestDatabase'</code> and it
484cdf0e10cSrcweir     * is registered in subdirectory <code>TestDB</code> of directory
485cdf0e10cSrcweir     * <code>docPath</code> which is supposed to be a directory with test
486cdf0e10cSrcweir     * documents, but can be any other (it must have subdirectory with DBF
487cdf0e10cSrcweir     * tables). If such data source doesn't exists or exists with
488cdf0e10cSrcweir     * different URL it is recreated and reregistered. Finally connection
489cdf0e10cSrcweir     * performed.
490cdf0e10cSrcweir     * @param docPath Path to database <code>TestDB</code> directory.
491cdf0e10cSrcweir     * @return Connection to test database.
492cdf0e10cSrcweir     */
connectToTestDB(String docPath)493cdf0e10cSrcweir     public XConnection connectToTestDB(String docPath)
494cdf0e10cSrcweir         throws com.sun.star.uno.Exception {
495cdf0e10cSrcweir 
496cdf0e10cSrcweir         return connectToSource(registerTestDB(docPath)) ;
497cdf0e10cSrcweir     }
498cdf0e10cSrcweir 
499cdf0e10cSrcweir     /**
500cdf0e10cSrcweir     * Empties the table in the specified source.
501cdf0e10cSrcweir     * @param con Connection to the DataSource where appropriate
502cdf0e10cSrcweir     * table exists.
503cdf0e10cSrcweir     * @param table The name of the table where all rows will be deleted.
504cdf0e10cSrcweir     * @return Number of rows deleted.
505cdf0e10cSrcweir     */
506cdf0e10cSrcweir 
507cdf0e10cSrcweir     // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
508cdf0e10cSrcweir     // Currently doesn't work because of bugs 85509, 85510
509cdf0e10cSrcweir 
deleteAllRows(XConnection con, String table)510cdf0e10cSrcweir     public int deleteAllRows(XConnection con, String table)
511cdf0e10cSrcweir         throws com.sun.star.sdbc.SQLException {
512cdf0e10cSrcweir 
513cdf0e10cSrcweir         XStatement stat = con.createStatement() ;
514cdf0e10cSrcweir 
515cdf0e10cSrcweir         XResultSet set = stat.executeQuery("SELECT * FROM " + table) ;
516cdf0e10cSrcweir 
517cdf0e10cSrcweir         XResultSetUpdate updt = (XResultSetUpdate) UnoRuntime.queryInterface
518cdf0e10cSrcweir             (XResultSetUpdate.class, set) ;
519cdf0e10cSrcweir 
520cdf0e10cSrcweir         int count = 0 ;
521cdf0e10cSrcweir         set.last() ;
522cdf0e10cSrcweir         int rowNum = set.getRow() ;
523cdf0e10cSrcweir         set.first() ;
524cdf0e10cSrcweir 
525cdf0e10cSrcweir         for (int i = 0; i < rowNum; i++) {
526cdf0e10cSrcweir             updt.deleteRow() ;
527cdf0e10cSrcweir             set.next() ;
528cdf0e10cSrcweir             count ++ ;
529cdf0e10cSrcweir         }
530cdf0e10cSrcweir 
531cdf0e10cSrcweir         XCloseable xClose = (XCloseable) UnoRuntime.queryInterface
532cdf0e10cSrcweir             (XCloseable.class, set) ;
533cdf0e10cSrcweir         xClose.close() ;
534cdf0e10cSrcweir 
535cdf0e10cSrcweir         return count ;
536cdf0e10cSrcweir     }
537cdf0e10cSrcweir 
538cdf0e10cSrcweir     /**
539cdf0e10cSrcweir     * Inserts row into test table of the specified connection.
540cdf0e10cSrcweir     * Test table has some predefined format which includes as much
541cdf0e10cSrcweir     * field types as possible. For every column type constants
542cdf0e10cSrcweir     * {@link #TST_STRING TST_STRING}, {@link #TST_INT TST_INT}, etc.
543cdf0e10cSrcweir     * are declared for column index fast find.
544cdf0e10cSrcweir     * @param con Connection to data source where test table exists.
545cdf0e10cSrcweir     * @param table Test table name.
546cdf0e10cSrcweir     * @param values Values to be inserted into test table. Values of
547cdf0e10cSrcweir     * this array inserted into appropriate fields depending on their
548cdf0e10cSrcweir     * types. So <code>String</code> value of the array is inserted
549cdf0e10cSrcweir     * into the field of <code>CHARACTER</code> type, etc.
550cdf0e10cSrcweir     * @param streamLength Is optional. It is used only if in values
551cdf0e10cSrcweir     * list <code>XCharacterInputStream</code> or <code>XBinaryInputStream
552cdf0e10cSrcweir     * </code> types specified. In this case the parameter specifies
553cdf0e10cSrcweir     * the length of the stream for inserting.
554cdf0e10cSrcweir     */
addRowToTestTable(XConnection con, String table, Object[] values, int streamLength)555cdf0e10cSrcweir     public void addRowToTestTable(XConnection con, String table, Object[] values,
556cdf0e10cSrcweir         int streamLength)
557cdf0e10cSrcweir         throws com.sun.star.sdbc.SQLException {
558cdf0e10cSrcweir 
559cdf0e10cSrcweir         XStatement stat = con.createStatement() ;
560cdf0e10cSrcweir 
561cdf0e10cSrcweir         XResultSet set = stat.executeQuery("SELECT * FROM " + table) ;
562cdf0e10cSrcweir 
563cdf0e10cSrcweir         XResultSetUpdate updt = (XResultSetUpdate) UnoRuntime.queryInterface
564cdf0e10cSrcweir             (XResultSetUpdate.class, set) ;
565cdf0e10cSrcweir 
566cdf0e10cSrcweir         XRowUpdate rowUpdt = (XRowUpdate) UnoRuntime.queryInterface
567cdf0e10cSrcweir             (XRowUpdate.class, set) ;
568cdf0e10cSrcweir 
569cdf0e10cSrcweir         updt.moveToInsertRow() ;
570cdf0e10cSrcweir 
571cdf0e10cSrcweir         for (int i = 0; i < values.length; i++) {
572cdf0e10cSrcweir             if (values[i] instanceof String) {
573cdf0e10cSrcweir                 rowUpdt.updateString(TST_STRING, (String) values[i]) ;
574cdf0e10cSrcweir             } else
575cdf0e10cSrcweir             if (values[i] instanceof Integer) {
576cdf0e10cSrcweir                 rowUpdt.updateInt(TST_INT, ((Integer) values[i]).intValue()) ;
577cdf0e10cSrcweir             } else
578cdf0e10cSrcweir             if (values[i] instanceof Double) {
579cdf0e10cSrcweir                 rowUpdt.updateDouble(TST_DOUBLE, ((Double) values[i]).doubleValue()) ;
580cdf0e10cSrcweir             } else
581cdf0e10cSrcweir             if (values[i] instanceof Date) {
582cdf0e10cSrcweir                 rowUpdt.updateDate(TST_DATE, (Date) values[i]) ;
583cdf0e10cSrcweir             } else
584cdf0e10cSrcweir             if (values[i] instanceof Boolean) {
585cdf0e10cSrcweir                 rowUpdt.updateBoolean(TST_BOOLEAN, ((Boolean) values[i]).booleanValue()) ;
586cdf0e10cSrcweir             } else
587cdf0e10cSrcweir             if (values[i] instanceof XTextInputStream) {
588cdf0e10cSrcweir                 rowUpdt.updateCharacterStream(TST_CHARACTER_STREAM, (XInputStream) values[i],
589cdf0e10cSrcweir                     streamLength) ;
590cdf0e10cSrcweir             } else
591cdf0e10cSrcweir             if (values[i] instanceof XDataInputStream) {
592cdf0e10cSrcweir                 rowUpdt.updateBinaryStream(TST_BINARY_STREAM, (XInputStream) values[i],
593cdf0e10cSrcweir                     streamLength) ;
594cdf0e10cSrcweir             }
595cdf0e10cSrcweir         }
596cdf0e10cSrcweir 
597cdf0e10cSrcweir         updt.insertRow() ;
598cdf0e10cSrcweir 
599cdf0e10cSrcweir         XCloseable xClose = (XCloseable) UnoRuntime.queryInterface
600cdf0e10cSrcweir             (XCloseable.class, set) ;
601cdf0e10cSrcweir         xClose.close() ;
602cdf0e10cSrcweir     }
603cdf0e10cSrcweir 
604cdf0e10cSrcweir     /**
605cdf0e10cSrcweir     * Initializes test table specified of the connection specified.
606cdf0e10cSrcweir     * Deletes all record from table, and then inserts data from
607cdf0e10cSrcweir     * <code>TST_TABLE_VALUES</code> constant array. <p>
608cdf0e10cSrcweir     * Test table has some predefined format which includes as much
609cdf0e10cSrcweir     * field types as possible. For every column type constants
610cdf0e10cSrcweir     * {@link #TST_STRING TST_STRING}, {@link #TST_INT TST_INT}, etc.
611cdf0e10cSrcweir     * are declared for column index fast find.
612cdf0e10cSrcweir     * @param con Connection to data source where test table exists.
613cdf0e10cSrcweir     * @param table Test table name.
614cdf0e10cSrcweir     */
initializeTestTable(XConnection con, String table)615cdf0e10cSrcweir     public void initializeTestTable(XConnection con, String table)
616cdf0e10cSrcweir         throws com.sun.star.sdbc.SQLException {
617cdf0e10cSrcweir 
618cdf0e10cSrcweir         deleteAllRows(con, table) ;
619cdf0e10cSrcweir 
620cdf0e10cSrcweir         for (int i = 0; i < TST_TABLE_VALUES.length; i++) {
621cdf0e10cSrcweir             addRowToTestTable(con, table, TST_TABLE_VALUES[i], TST_STREAM_LENGTHS[i]) ;
622cdf0e10cSrcweir         }
623cdf0e10cSrcweir     }
624cdf0e10cSrcweir 
625cdf0e10cSrcweir     /**
626cdf0e10cSrcweir     * Prints full info about currently registered DataSource's.
627cdf0e10cSrcweir     */
printRegisteredDatabasesInfo(PrintWriter out)628cdf0e10cSrcweir     public void printRegisteredDatabasesInfo(PrintWriter out) {
629cdf0e10cSrcweir         XEnumerationAccess dbContEA = (XEnumerationAccess)
630cdf0e10cSrcweir             UnoRuntime.queryInterface(XEnumerationAccess.class, dbContext) ;
631cdf0e10cSrcweir 
632cdf0e10cSrcweir         XEnumeration xEnum = dbContEA.createEnumeration() ;
633cdf0e10cSrcweir 
634cdf0e10cSrcweir         out.println("DatabaseContext registered DataSource's :") ;
635cdf0e10cSrcweir         while (xEnum.hasMoreElements()) {
636cdf0e10cSrcweir             try {
637cdf0e10cSrcweir                 DataSourceInfo inf = new DataSourceInfo(xEnum.nextElement()) ;
638cdf0e10cSrcweir                 inf.printInfo(out) ;
639cdf0e10cSrcweir             } catch (com.sun.star.container.NoSuchElementException e) {}
640cdf0e10cSrcweir             catch (com.sun.star.lang.WrappedTargetException e) {}
641cdf0e10cSrcweir         }
642cdf0e10cSrcweir     }
643cdf0e10cSrcweir 
644cdf0e10cSrcweir     /**
645cdf0e10cSrcweir     * Convert system pathname to SOffice URL string
646cdf0e10cSrcweir     * (for example 'C:\Temp\DBDir\' -> 'file:///C|/Temp/DBDir/').
647cdf0e10cSrcweir     * (for example '\\server\Temp\DBDir\' -> 'file://server/Temp/DBDir/').
648cdf0e10cSrcweir     * Already converted string retured unchanged.
649cdf0e10cSrcweir     */
dirToUrl(String dir)650cdf0e10cSrcweir     public static String dirToUrl(String dir) {
651cdf0e10cSrcweir         String retVal = null;
652cdf0e10cSrcweir         if (dir.startsWith("file:/")) retVal = dir;
653cdf0e10cSrcweir         else {
654cdf0e10cSrcweir             retVal = dir.replace(':', '|').replace('\\', '/');
655cdf0e10cSrcweir 
656cdf0e10cSrcweir             if (dir.startsWith("\\\\")) {
657cdf0e10cSrcweir                 retVal = "file:" + retVal;
658cdf0e10cSrcweir             }
659cdf0e10cSrcweir 
660cdf0e10cSrcweir             else retVal = "file:///" + retVal ;
661cdf0e10cSrcweir         }
662cdf0e10cSrcweir         return retVal;
663cdf0e10cSrcweir     }
664cdf0e10cSrcweir 
665cdf0e10cSrcweir     /**
666cdf0e10cSrcweir     * Revokes datasource from global DB context.
667cdf0e10cSrcweir     * @param name DataSource name to be revoked.
668cdf0e10cSrcweir     */
revokeDB(String name)669cdf0e10cSrcweir     public void revokeDB(String name) throws com.sun.star.uno.Exception
670cdf0e10cSrcweir     {
671cdf0e10cSrcweir         dbContext.revokeObject(name) ;
672cdf0e10cSrcweir     }
673cdf0e10cSrcweir 
674cdf0e10cSrcweir     /**
675cdf0e10cSrcweir     * Initializes test table specified of the connection specified
676cdf0e10cSrcweir     * using JDBC driver. Drops table with the name <code>tbl_name</code>,
677cdf0e10cSrcweir     * creates new table with this name and then inserts data from
678cdf0e10cSrcweir     * <code>TST_TABLE_VALUES</code> constant array. <p>
679cdf0e10cSrcweir     * Test table has some predefined format which includes as much
680cdf0e10cSrcweir     * field types as possible. For every column type constants
681cdf0e10cSrcweir     * {@link #TST_STRING TST_STRING}, {@link #TST_INT TST_INT}, etc.
682cdf0e10cSrcweir     * are declared for column index fast find.
683cdf0e10cSrcweir     * @param tbl_name Test table name.
684cdf0e10cSrcweir     */
initTestTableUsingJDBC(String tbl_name, DataSourceInfo dsi)685cdf0e10cSrcweir     public void initTestTableUsingJDBC(String tbl_name, DataSourceInfo dsi)
686cdf0e10cSrcweir         throws java.sql.SQLException,
687cdf0e10cSrcweir                ClassNotFoundException {
688cdf0e10cSrcweir         //register jdbc driver
689cdf0e10cSrcweir         if ( dsi.Info[0].Name.equals("JavaDriverClass") ) {
690cdf0e10cSrcweir             Class.forName((String)dsi.Info[0].Value);
691cdf0e10cSrcweir         } else {
692cdf0e10cSrcweir             Class.forName(TST_JDBC_DRIVER);
693cdf0e10cSrcweir         }
694cdf0e10cSrcweir 
695cdf0e10cSrcweir         //getting connection
696cdf0e10cSrcweir         Connection connection = null;
697cdf0e10cSrcweir 
698cdf0e10cSrcweir         connection = DriverManager.getConnection(
699cdf0e10cSrcweir             dsi.URL, dsi.User, dsi.Password);
700cdf0e10cSrcweir         Statement statement = connection.createStatement();
701cdf0e10cSrcweir 
702cdf0e10cSrcweir         //drop table
703cdf0e10cSrcweir         dropMySQLTable(statement, tbl_name);
704cdf0e10cSrcweir 
705cdf0e10cSrcweir         //create table
706cdf0e10cSrcweir         createMySQLTable(statement, tbl_name);
707cdf0e10cSrcweir 
708cdf0e10cSrcweir         //insert some content
709cdf0e10cSrcweir         insertContentMySQLTable(statement, tbl_name);
710cdf0e10cSrcweir     }
711cdf0e10cSrcweir 
712cdf0e10cSrcweir     /**
713cdf0e10cSrcweir     * Inserts data from <code>TST_TABLE_VALUES</code> constant array
714cdf0e10cSrcweir     * to test table <code>tbl_name</code>.
715cdf0e10cSrcweir     * @param statement object used for executing a static SQL
716cdf0e10cSrcweir     * statement and obtaining the results produced by it.
717cdf0e10cSrcweir     * @param tbl_name Test table name.
718cdf0e10cSrcweir     */
insertContentMySQLTable(Statement statement, String tbl_name)719cdf0e10cSrcweir     protected void insertContentMySQLTable(Statement statement, String tbl_name)
720cdf0e10cSrcweir         throws java.sql.SQLException {
721cdf0e10cSrcweir 
722cdf0e10cSrcweir 
723cdf0e10cSrcweir         for(int i = 0; i < DBTools.TST_TABLE_VALUES.length; i++) {
724cdf0e10cSrcweir             String query = "insert into " + tbl_name + " values (";
725cdf0e10cSrcweir             int j = 0;
726cdf0e10cSrcweir             while(j < DBTools.TST_TABLE_VALUES[i].length) {
727cdf0e10cSrcweir                 if (j > 0) {
728cdf0e10cSrcweir                     query += ", ";
729cdf0e10cSrcweir                 }
730cdf0e10cSrcweir                 Object value = DBTools.TST_TABLE_VALUES[i][j];
731cdf0e10cSrcweir                 if (value instanceof String ||
732cdf0e10cSrcweir                     value instanceof Date) {
733cdf0e10cSrcweir                     query += "'";
734cdf0e10cSrcweir                 }
735cdf0e10cSrcweir                 if (value instanceof Date) {
736cdf0e10cSrcweir                     Date date = (Date)value;
737cdf0e10cSrcweir                     query += date.Year + "-" + date.Month +
738cdf0e10cSrcweir                         "-" + date.Day;
739cdf0e10cSrcweir                 } else if (value instanceof Boolean) {
740cdf0e10cSrcweir                     query += (((Boolean)value).booleanValue())
741cdf0e10cSrcweir                         ? "1" : "0";
742cdf0e10cSrcweir                 } else {
743cdf0e10cSrcweir                     query += value;
744cdf0e10cSrcweir                 }
745cdf0e10cSrcweir 
746cdf0e10cSrcweir                 if (value instanceof String ||
747cdf0e10cSrcweir                     value instanceof Date) {
748cdf0e10cSrcweir                     query += "'";
749cdf0e10cSrcweir                 }
750cdf0e10cSrcweir                 j++;
751cdf0e10cSrcweir             }
752cdf0e10cSrcweir             query += ")";
753cdf0e10cSrcweir             statement.executeUpdate(query);
754cdf0e10cSrcweir         }
755cdf0e10cSrcweir     }
756cdf0e10cSrcweir 
757cdf0e10cSrcweir     /**
758cdf0e10cSrcweir      * Creates test table specified.
759cdf0e10cSrcweir      * Test table has some predefined format which includes as much
760cdf0e10cSrcweir      * field types as possible. For every column type constants
761cdf0e10cSrcweir      * {@link #TST_STRING TST_STRING}, {@link #TST_INT TST_INT}, etc.
762cdf0e10cSrcweir      * are declared for column index fast find.
763cdf0e10cSrcweir      * @param statement object used for executing a static SQL
764cdf0e10cSrcweir      * statement and obtaining the results produced by it.
765*e6b649b5SPedro Giffuni      * @param tbl_name Test table name.
766cdf0e10cSrcweir      */
createMySQLTable(Statement statement, String tbl_name)767cdf0e10cSrcweir     protected void createMySQLTable(Statement statement, String tbl_name)
768cdf0e10cSrcweir         throws java.sql.SQLException {
769cdf0e10cSrcweir 
770cdf0e10cSrcweir         final String empty_col_name = "Column";
771cdf0e10cSrcweir         int c = 0;
772cdf0e10cSrcweir         String query = "create table " + tbl_name + " (";
773cdf0e10cSrcweir         for (int i = 0; i < TST_TABLE_VALUES[0].length; i++) {
774cdf0e10cSrcweir             if (i > 0) query += ",";
775cdf0e10cSrcweir 
776cdf0e10cSrcweir             switch(i + 1) {
777cdf0e10cSrcweir                 case TST_BINARY_STREAM:
778cdf0e10cSrcweir                     query += TST_BINARY_STREAM_F + " BLOB";
779cdf0e10cSrcweir                     break;
780cdf0e10cSrcweir                 case TST_BOOLEAN:
781cdf0e10cSrcweir                     query += TST_BOOLEAN_F + " TINYINT";
782cdf0e10cSrcweir                     break;
783cdf0e10cSrcweir                 case TST_CHARACTER_STREAM:
784cdf0e10cSrcweir                     query += TST_CHARACTER_STREAM_F + " TEXT";
785cdf0e10cSrcweir                     break;
786cdf0e10cSrcweir                 case TST_DATE:
787cdf0e10cSrcweir                     query += TST_DATE_F + " DATE";
788cdf0e10cSrcweir                     break;
789cdf0e10cSrcweir                 case TST_DOUBLE:
790cdf0e10cSrcweir                     query += TST_DOUBLE_F + " DOUBLE";
791cdf0e10cSrcweir                     break;
792cdf0e10cSrcweir                 case TST_INT:
793cdf0e10cSrcweir                     query += TST_INT_F + " INT";
794cdf0e10cSrcweir                     break;
795cdf0e10cSrcweir                 case TST_STRING:
796cdf0e10cSrcweir                     query += TST_STRING_F + " TEXT";
797cdf0e10cSrcweir                     break;
798cdf0e10cSrcweir                 default: query += empty_col_name + (c++) + " INT";
799cdf0e10cSrcweir                          if (c == 1) {
800cdf0e10cSrcweir                             query += " NOT NULL AUTO_INCREMENT";
801cdf0e10cSrcweir                          }
802cdf0e10cSrcweir             }
803cdf0e10cSrcweir         }
804cdf0e10cSrcweir         query += ", PRIMARY KEY (" + empty_col_name + "0)";
805cdf0e10cSrcweir         query += ")";
806cdf0e10cSrcweir         statement.execute(query);
807cdf0e10cSrcweir     }
808cdf0e10cSrcweir 
809cdf0e10cSrcweir     /**
810cdf0e10cSrcweir      * Drops table.
811cdf0e10cSrcweir      * @param statement object used for executing a static SQL
812cdf0e10cSrcweir      * statement and obtaining the results produced by it.
813*e6b649b5SPedro Giffuni      * @param tbl_name Test table name.
814cdf0e10cSrcweir      */
dropMySQLTable(Statement statement, String tbl_name)815cdf0e10cSrcweir     protected void dropMySQLTable(Statement statement, String tbl_name)
816cdf0e10cSrcweir         throws java.sql.SQLException {
817cdf0e10cSrcweir         statement.executeUpdate("drop table if exists " + tbl_name);
818cdf0e10cSrcweir     }
819cdf0e10cSrcweir }
820