/* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
/** * It's just a structure with some useful methods for representing * <code>com.sun.star.sdb.DataSource</code> service. All this * service's properties are stored in appropriate class fields. * Class also allows to construct its instances using service * information, and create new service instance upon class * fields. * @see com.sun.star.sdb.DataSource
*/ publicclass DataSourceInfo { /** * Representation of <code>'URL'</code> property.
*/ public String URL = null ; /** * Representation of <code>'Info'</code> property.
*/ public PropertyValue[] Info = null ; /** * Representation of <code>'User'</code> property.
*/ public String User = null ; /** * Representation of <code>'Password'</code> property.
*/ public String Password = null ; /** * Representation of <code>'IsPasswordRequired'</code> property.
*/ publicBoolean IsPasswordRequired = null ;
/** * Creates new <code>com.sun.star.sdb.DataSource</code> service * instance and copies all fields (which are not null) to * appropriate service properties. * @return <code>com.sun.star.sdb.DataSource</code> service.
*/ public Object getDataSourceService() throws Exception
{
Object src = xMSF.createInstance("com.sun.star.sdb.DataSource") ;
/** * Returns new instance of <code>DataSourceInfo</code> class.
*/ public DataSourceInfo newDataSourceInfo() { returnnew DataSourceInfo() ;}
/** * Registers the datasource on the specified name in * <code>DatabaseContext</code> service. * @param name Name which dataSource will have in global context. * @param dataSource <code>DataSource</code> object which is to * be registered.
*/ privatevoid registerDB(String name, Object dataSource) throws com.sun.star.uno.Exception {
dbContext.registerObject(name, dataSource) ;
}
/** * First tries to revoke the datasource with the specified * name and then registers a new one. * @param name Name which dataSource will have in global context. * @param dataSource <code>DataSource</code> object which is to * be registered.
*/ publicvoid reRegisterDB(String name, Object dataSource) throws com.sun.star.uno.Exception {
XDocumentDataSource xDDS = UnoRuntime.queryInterface(XDocumentDataSource.class, dataSource);
XStorable store = UnoRuntime.queryInterface(XStorable.class,
xDDS.getDatabaseDocument());
String aFile = utils.getOfficeTemp(xMSF) + name + ".odb";
store.storeAsURL(aFile, new PropertyValue[] { });
registerDB(name, dataSource) ;
}
/** * Performs connection to DataSource specified. * @param dbSource <code>com.sun.star.sdb.DataSource</code> service * specified data source which must be already registered in the * <code>DatabaseContext</code> service. * @return Connection to the data source.
*/ public XConnection connectToSource(Object dbSource) throws com.sun.star.uno.Exception {
/** * Revokes datasource from global DB context. * @param name DataSource name to be revoked.
*/ publicvoid revokeDB(String name) throws com.sun.star.uno.Exception
{
dbContext.revokeObject(name) ;
}
/** * Initializes test table specified of the connection specified * using JDBC driver. Drops table with the name <code>tbl_name</code>, * creates new table with this name and then inserts data from * <code>TST_TABLE_VALUES</code> constant array. <p> * Test table has some predefined format which includes as much * field types as possible. For every column type constants * {@link #TST_STRING TST_STRING}, {@link #TST_INT TST_INT}, etc. * are declared for column index fast find. * @param tbl_name Test table name.
*/ publicvoid initTestTableUsingJDBC(String tbl_name, DataSourceInfo dsi) throws java.sql.SQLException,
ClassNotFoundException {
//insert some content
insertContentMySQLTable(statement, tbl_name);
} finally { if (statement != null)
statement.close();
}
} finally { if (connection != null)
connection.close();
}
}
/** * Inserts data from <code>TST_TABLE_VALUES</code> constant array * to test table <code>tbl_name</code>. * @param statement object used for executing a static SQL * statement and obtaining the results produced by it. * @param tbl_name Test table name.
*/ privatevoid insertContentMySQLTable(Statement statement, String tbl_name) throws java.sql.SQLException {
for(int i = 0; i < DBTools.TST_TABLE_VALUES.length; i++) {
StringBuilder query = new StringBuilder("insert into " + tbl_name + " values ("); int j = 0; while(j < DBTools.TST_TABLE_VALUES[i].length) { if (j > 0) {
query.append(", ");
}
Object value = DBTools.TST_TABLE_VALUES[i][j]; if (value instanceof String ||
value instanceof Date) {
query.append("'");
} if (value instanceof Date) {
Date date = (Date)value;
query.append(date.Year).append("-").append(date.Month).append( "-").append(date.Day);
} elseif (value instanceofBoolean) {
query.append((((Boolean)value).booleanValue())
? "1" : "0");
} else {
query.append(value);
}
if (value instanceof String ||
value instanceof Date) {
query.append("'");
}
j++;
}
query.append(")");
statement.executeUpdate(query.toString());
}
}
/** * Creates test table specified. * Test table has some predefined format which includes as much * field types as possible. For every column type constants * {@link #TST_STRING TST_STRING}, {@link #TST_INT TST_INT}, etc. * are declared for column index fast find. * @param statement object used for executing a static SQL * statement and obtaining the results produced by it. * @param tbl_name Test table name.
*/ privatevoid createMySQLTable(Statement statement, String tbl_name) throws java.sql.SQLException {
final String empty_col_name = "Column"; int c = 0;
String query = "create table " + tbl_name + " ("; for (int i = 0; i < TST_TABLE_VALUES[0].length; i++) { if (i > 0) query += ",";
/** * Drops table. * @param statement object used for executing a static SQL * statement and obtaining the results produced by it. * @param tbl_name Test table name.
*/ privatevoid dropMySQLTable(Statement statement, String tbl_name) throws java.sql.SQLException {
statement.executeUpdate("drop table if exists " + tbl_name);
}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.1 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.