You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2002/09/12 00:39:16 UTC
cvs commit: jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model Database.java
jstrachan 2002/09/11 15:39:16
Modified: sql/src/java/org/apache/commons/sql/model Database.java
Added: sql/src/java/org/apache/commons/sql/dynabean
SqlDynaBean.java DynaSql.java SqlDynaClass.java
SqlDynaProperty.java package.html
DataSourceDynaSql.java
Log:
Added a small experimental module which creates DynaBeans from a database model and can insert/update/delete data from a relational database using the familiar DyanBeans paradigm.
Similar in essence to a very simplified version of Simper http://netmeme.org/simper/ that is Servlets and Struts independent.
Revision Changes Path
1.1 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/dynabean/SqlDynaBean.java
Index: SqlDynaBean.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/CompilableTag.java,v 1.5 2002/05/17 15:18:12 jstrachan Exp $
* $Revision: 1.5 $
* $Date: 2002/05/17 15:18:12 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* $Id: CompilableTag.java,v 1.5 2002/05/17 15:18:12 jstrachan Exp $
*/
package org.apache.commons.sql.dynabean;
import org.apache.commons.beanutils.BasicDynaBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* SqlDynaBean is a DynaBean which can be persisted as a single row in
* a Database Table.
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @version $Revision: 1.14 $
*/
public class SqlDynaBean extends BasicDynaBean {
/** The Log to which logging calls will be made. */
private static final Log log = LogFactory.getLog( SqlDynaBean.class );
public SqlDynaBean(SqlDynaClass dynaClass) {
super(dynaClass);
}
}
1.1 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/dynabean/DynaSql.java
Index: DynaSql.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/CompilableTag.java,v 1.5 2002/05/17 15:18:12 jstrachan Exp $
* $Revision: 1.5 $
* $Date: 2002/05/17 15:18:12 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* $Id: CompilableTag.java,v 1.5 2002/05/17 15:18:12 jstrachan Exp $
*/
package org.apache.commons.sql.dynabean;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaClass;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.sql.model.Database;
import org.apache.commons.sql.model.Table;
/**
* DynaSql provides simple access to relational data
* using a Database to implement the persistent model and
* DynaBean instances for each row of a table.
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @version $Revision: 1.14 $
*/
public abstract class DynaSql {
/** The Log to which logging calls will be made. */
private static final Log log = LogFactory.getLog( DynaSql.class );
/** The current database model */
private Database database;
/** A cache of the SqlDynaClasses per table name */
private Map dynaClassCache = new HashMap();
public DynaSql() {
}
public DynaSql(Database database) {
this.database = database;
}
/**
* Creates a new DynaBean instance for the given table name.
* @return the new empty DynaBean for the given tableName or null if the
* table does not exist in the current Database model.
*/
public DynaBean newInstance(String tableName) {
SqlDynaClass dynaClass = getSqlDynaClass(tableName);
return null;
}
/**
* Stores the given DyanBean in the database, inserting it if there is no primary key
* otherwise the bean is updated in the database.
*/
public void store(DynaBean dynaBean) throws SQLException {
Connection connection = borrowConnection();
try {
if (exists(dynaBean, connection)) {
update(dynaBean, connection);
}
else {
insert(dynaBean, connection);
}
}
finally {
returnConnection(connection);
}
}
/**
* Inserts the given DyanBean in the database, assuming the primary key values are specified.
*/
public void insert(DynaBean dynaBean) throws SQLException {
Connection connection = borrowConnection();
try {
insert(dynaBean, connection);
}
finally {
returnConnection(connection);
}
}
/**
* Updates the given DyanBean in the database, assuming the primary key values are specified.
*/
public void update(DynaBean dynaBean) throws SQLException {
Connection connection = borrowConnection();
try {
update(dynaBean, connection);
}
finally {
returnConnection(connection);
}
}
/**
* Deletes the given DynaBean from the database, assuming the primary key values are specified.
*/
public void delete(DynaBean dynaBean) throws SQLException {
Connection connection = borrowConnection();
try {
SqlDynaClass dynaClass = getSqlDynaClass(dynaBean);
SqlDynaProperty[] primaryKeys = dynaClass.getPrimaryKeyProperties();
if (primaryKeys.length == 0) {
log.info( "Cannot update type: " + dynaClass + " as there are no primary keys to update" );
return;
}
String sql = createDeleteSql(dynaClass, primaryKeys);
log.info( "About to execute SQL: " + sql );
}
finally {
returnConnection(connection);
}
}
// Properties
//-------------------------------------------------------------------------
/**
* Returns the database.
* @return Database
*/
public Database getDatabase() {
return database;
}
/**
* Sets the database.
* @param database The database to set
*/
public void setDatabase(Database database) {
this.database = database;
this.dynaClassCache.clear();
}
// Implementation methods
//-------------------------------------------------------------------------
/**
* @return a new JDBC connection from the pool
*/
protected abstract Connection borrowConnection() throws SQLException;
/**
* Returns a JDBC connection back into the pool
*/
protected abstract void returnConnection(Connection connection) throws SQLException;
/**
* @return true if this dynaBean has a primary key
*/
protected boolean exists(DynaBean dynaBean, Connection connection) {
return false;
}
/**
* Creates a new primary key value, inserts the bean and returns the new item.
*/
protected void insert(DynaBean dynaBean, Connection connection) throws SQLException {
SqlDynaClass dynaClass = getSqlDynaClass(dynaBean);
SqlDynaProperty[] properties = dynaClass.getSqlDynaProperties();
if (properties.length == 0) {
log.info( "Cannot insert type: " + dynaClass + " as there are no properties" );
return;
}
String sql = createInsertSql(dynaClass, properties);
log.info( "About to execute SQL: " + sql );
}
/**
* Updates the row which maps to the given dynabean
*/
protected void update(DynaBean dynaBean, Connection connection) throws SQLException {
SqlDynaClass dynaClass = getSqlDynaClass(dynaBean);
SqlDynaProperty[] primaryKeys = dynaClass.getPrimaryKeyProperties();
if (primaryKeys.length == 0) {
log.info( "Cannot update type: " + dynaClass + " as there are no primary keys to update" );
return;
}
SqlDynaProperty[] properties = dynaClass.getNonPrimaryKeyProperties();
String sql = createUpdateSql(dynaClass, primaryKeys, properties);
log.info( "About to execute SQL: " + sql );
}
/**
* @return the SQL required to update the given item
*/
protected String createInsertSql(SqlDynaClass dynaClass, SqlDynaProperty[] properties) {
StringBuffer buffer = new StringBuffer( "insert into " );
buffer.append( dynaClass.getTableName() );
buffer.append( " (" );
int size = properties.length;
for ( int i = 0; i < size; i++ ) {
SqlDynaProperty property = properties[i];
if (i > 0) {
buffer.append(", " );
}
buffer.append( property.getName() );
}
buffer.append( ") values (" );
if ( size > 0) {
buffer.append( "?" );
for ( int i = 1; i < size; i++ ) {
buffer.append( ", ?" );
}
}
buffer.append( ")" );
return buffer.toString();
}
/**
* @return the SQL required to update the given item
*/
protected String createUpdateSql(SqlDynaClass dynaClass, SqlDynaProperty[] primaryKeys, SqlDynaProperty[] properties) {
StringBuffer buffer = new StringBuffer( "update " );
buffer.append( dynaClass.getTableName() );
buffer.append( " set " );
for ( int i = 0; i < properties.length; i++ ) {
SqlDynaProperty property = properties[i];
if (i > 0) {
buffer.append(", " );
}
buffer.append( property.getName() );
buffer.append( " = ?" );
}
buffer.append( " where " );
for (int i = 0, size = primaryKeys.length; i < size; i++ ) {
SqlDynaProperty primaryKey = primaryKeys[i];
if (i > 0) {
buffer.append(", " );
}
buffer.append( primaryKey.getName() );
buffer.append( " = ?" );
}
return buffer.toString();
}
/**
* @return the SQL required to update the given item
*/
protected String createDeleteSql(SqlDynaClass dynaClass, SqlDynaProperty[] primaryKeys) {
StringBuffer buffer = new StringBuffer( "delete " );
buffer.append( dynaClass.getTableName() );
buffer.append( " where " );
int size = primaryKeys.length;
for ( int i = 0; i < size; i++ ) {
SqlDynaProperty primaryKey = primaryKeys[i];
if (i > 0) {
buffer.append(" and " );
}
buffer.append( primaryKey.getName() );
buffer.append( " = ?" );
}
return buffer.toString();
}
/**
* @return the SqlDynaClass for the given DynaBean or throws an exception if it could not be found
*/
protected SqlDynaClass getSqlDynaClass(DynaBean dynaBean) {
DynaClass dynaClass = dynaBean.getDynaClass();
if (dynaClass instanceof SqlDynaClass) {
return (SqlDynaClass) dynaClass;
}
else {
// #### we could autogenerate an SqlDynaClass here
throw new IllegalArgumentException( "The dynaBean is not an instance of an SqlDynaClass" );
}
}
/**
* @return the SqlDynaClass for the given table name. If the SqlDynaClass does not exist
* then create a new one based on the Table definition
*/
protected SqlDynaClass getSqlDynaClass(String tableName) {
SqlDynaClass answer = (SqlDynaClass) dynaClassCache.get(tableName);
if (answer == null) {
Table table = getDatabase().findTable(tableName);
if (table != null) {
answer = createSqlDynaClass(table);
dynaClassCache.put(tableName, dynaClassCache);
}
}
return answer;
}
/**
* A Factory method to create a new SqlDynaClass instance for the given table name If the SqlDynaClass does not exist
* then create a new one based on the Table definition
*/
protected SqlDynaClass createSqlDynaClass(Table table) {
return SqlDynaClass.newInstance(table);
}
}
1.1 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/dynabean/SqlDynaClass.java
Index: SqlDynaClass.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/CompilableTag.java,v 1.5 2002/05/17 15:18:12 jstrachan Exp $
* $Revision: 1.5 $
* $Date: 2002/05/17 15:18:12 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* $Id: CompilableTag.java,v 1.5 2002/05/17 15:18:12 jstrachan Exp $
*/
package org.apache.commons.sql.dynabean;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.beanutils.BasicDynaClass;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.sql.model.Column;
import org.apache.commons.sql.model.Table;
/**
* SqlDynaClass is a DynaClass which is associated with a persistent
* Table in a Database.
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @version $Revision: 1.14 $
*/
public class SqlDynaClass extends BasicDynaClass {
/** The Log to which logging calls will be made. */
private static final Log log = LogFactory.getLog( SqlDynaClass.class );
private Table table;
private SqlDynaProperty[] primaryKeys;
private SqlDynaProperty[] nonPrimaryKeyProperties;
/**
* Creates a new SqlDynaClass instance from a Table model.
*/
public static SqlDynaClass newInstance(Table table) {
List properties = new ArrayList();
for (Iterator iter = table.getColumns().iterator(); iter.hasNext(); ) {
Column column = (Column) iter.next();
properties.add( new SqlDynaProperty(column));
}
SqlDynaProperty[] array = new SqlDynaProperty[properties.size()];
properties.toArray(array);
return new SqlDynaClass(table, array);
}
public SqlDynaClass(Table table) {
super(table.getName(), SqlDynaBean.class);
this.table = table;
}
public SqlDynaClass(Table table, SqlDynaProperty[] properties) {
super(table.getName(), SqlDynaBean.class, properties);
this.table = table;
}
/**
* @return the database Table this DynaClass maps to
*/
public Table getTable() {
return table;
}
// Helper methods
//-------------------------------------------------------------------------
/**
* @return the name of the table
*/
public String getTableName() {
return getTable().getName();
}
/**
* @return the SqlDynaProperty objects of this class
*/
public SqlDynaProperty[] getSqlDynaProperties() {
return (SqlDynaProperty[]) getDynaProperties();
}
/**
* @return an array of the primary key DynaProperty objects
*/
public SqlDynaProperty[] getPrimaryKeyProperties() {
if ( primaryKeys == null ) {
initPrimaryKeys();
}
return primaryKeys;
}
/**
* @return an array of the non-primary key DynaProperty objects
*/
public SqlDynaProperty[] getNonPrimaryKeyProperties() {
if ( nonPrimaryKeyProperties == null ) {
initPrimaryKeys();
}
return nonPrimaryKeyProperties;
}
// Implementation methods
//-------------------------------------------------------------------------
/**
* Creates the primary key and non primary key property arrays, laziliy.
*/
protected void initPrimaryKeys() {
List primaryKeyList = new ArrayList();
List otherList = new ArrayList();
DynaProperty[] properties = getDynaProperties();
for (int i = 0, size = properties.length; i < size; i++ ) {
DynaProperty property = properties[i];
if (property instanceof SqlDynaProperty) {
SqlDynaProperty sqlProperty = (SqlDynaProperty) property;
if ( sqlProperty.isPrimaryKey() ) {
primaryKeyList.add( sqlProperty );
}
else {
otherList.add( sqlProperty );
}
}
}
this.primaryKeys = new SqlDynaProperty[primaryKeyList.size()];
primaryKeyList.toArray(this.primaryKeys);
this.nonPrimaryKeyProperties = new SqlDynaProperty[otherList.size()];
otherList.toArray(this.nonPrimaryKeyProperties);
}
}
1.1 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/dynabean/SqlDynaProperty.java
Index: SqlDynaProperty.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/CompilableTag.java,v 1.5 2002/05/17 15:18:12 jstrachan Exp $
* $Revision: 1.5 $
* $Date: 2002/05/17 15:18:12 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* $Id: CompilableTag.java,v 1.5 2002/05/17 15:18:12 jstrachan Exp $
*/
package org.apache.commons.sql.dynabean;
import java.sql.Types;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.sql.model.Column;
/**
* A DynaProperty which maps to a persistent Column in a database.
* The Column describes additional relational metadata
* for the property such as whether the property is a primary key column,
* an autoIncrement column and the SQL type etc.
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @version $Revision: 1.14 $
*/
public class SqlDynaProperty extends DynaProperty {
/** The Log to which logging calls will be made. */
private static final Log log = LogFactory.getLog( SqlDynaProperty.class );
private Column column;
/**
* Construct a property that accepts any data type.
*
* @param name Name of the property being described
* @param column the database Column this property maps to
*/
public SqlDynaProperty(Column column) {
super(column.getName());
this.column = column;
}
/**
* Construct a property that accepts any data type.
*
* @param name Name of the property being described
* @param column the database Column this property maps to
*/
public SqlDynaProperty(Column column, Class type) {
super(column.getName(), type);
this.column = column;
}
/**
* @return the database Column this property maps to
*/
public Column getColumn() {
return column;
}
// Helper methods
//-------------------------------------------------------------------------
/**
* @return whether the property is part of the primary key
*/
public boolean isPrimaryKey() {
return getColumn().isPrimaryKey();
}
}
1.1 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/dynabean/package.html
Index: package.html
===================================================================
<html>
<head>
</head>
<body>
<p>
This package contains a collection of DynaBean implementation classes for creating DynaBeans from
a model and persisting them.
</p>
</body>
</html>
1.1 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/dynabean/DataSourceDynaSql.java
Index: DataSourceDynaSql.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/CompilableTag.java,v 1.5 2002/05/17 15:18:12 jstrachan Exp $
* $Revision: 1.5 $
* $Date: 2002/05/17 15:18:12 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* $Id: CompilableTag.java,v 1.5 2002/05/17 15:18:12 jstrachan Exp $
*/
package org.apache.commons.sql.dynabean;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* An implementation of DynaSql which uses a {@link DataSource} as its {@link Connection}
* pool.
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @version $Revision: 1.14 $
*/
public class DataSourceDynaSql extends DynaSql {
/** The Log to which logging calls will be made. */
private static final Log log = LogFactory.getLog( DataSourceDynaSql.class );
private DataSource dataSource;
public DataSourceDynaSql() {
}
// Implementation methods
//-------------------------------------------------------------------------
/**
* Returns the DataSource used to pool JDBC Connections.
* @return DataSource
*/
public DataSource getDataSource() {
return dataSource;
}
/**
* Sets the DataSource used to pool JDBC Connections.
* @param dataSource The dataSource to set
*/
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
// Implementation methods
//-------------------------------------------------------------------------
/**
* @return a new JDBC connection from the pool
*/
protected Connection borrowConnection() throws SQLException {
return getDataSource().getConnection();
}
/**
* Returns a JDBC connection back into the pool
*/
protected void returnConnection(Connection connection) throws SQLException {
try {
connection.close();
}
catch (Exception e) {
log.error( "Caught exception while returning connection to pool: " + e, e);
}
}
}
1.2 +23 -0 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Database.java
Index: Database.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Database.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Database.java 9 Sep 2002 13:47:56 -0000 1.1
+++ Database.java 11 Sep 2002 22:39:16 -0000 1.2
@@ -42,6 +42,29 @@
return tables;
}
+ // Helper methods
+
+ /**
+ * Finds the table with the specified name, using case insensitive matching.
+ * Note that this method is not called getTable(String) to avoid introspection
+ * problems.
+ */
+ public Table findTable(String name)
+ {
+ for (Iterator iter = tables.iterator(); iter.hasNext(); )
+ {
+ Table table = (Table) iter.next();
+
+ // table names are typically case insensitive
+ if (table.getName().equalsIgnoreCase( name ))
+ {
+ return table;
+ }
+ }
+ return null;
+ }
+
+
// Additions for PropertyUtils
public void setTable(int index, Table table)
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>