com.rapiddweller.jdbacl.identity.SubNkPkQueryIdentity Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rd-lib-jdbacl Show documentation
Show all versions of rd-lib-jdbacl Show documentation
'jdbacl' stands for 'Java DataBase ACcess Layer' and provides utilities for accessing JDBC databases from
Java programs, retrieving meta information in an object model and querying database data.
'rapiddweller jdbacl' is forked from Databene jdbacl by Volker Bergmann.
The newest version!
/*
* (c) Copyright 2010-2014 by Volker Bergmann. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, is permitted under the terms of the
* GNU General Public License (GPL).
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
* REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
* HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 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.
*/
package com.rapiddweller.jdbacl.identity;
import com.rapiddweller.common.ArrayFormat;
import com.rapiddweller.common.HeavyweightIterator;
import com.rapiddweller.common.IOUtil;
import com.rapiddweller.common.SystemInfo;
import com.rapiddweller.common.exception.ExceptionFactory;
import com.rapiddweller.common.iterator.ConvertingIterator;
import com.rapiddweller.common.iterator.TabularIterator;
import com.rapiddweller.jdbacl.ArrayResultSetIterator;
import com.rapiddweller.jdbacl.DatabaseDialect;
import com.rapiddweller.jdbacl.DatabaseDialectManager;
import com.rapiddweller.jdbacl.QueryIterator;
import com.rapiddweller.jdbacl.ResultSetConverter;
import com.rapiddweller.jdbacl.SQLUtil;
import com.rapiddweller.jdbacl.model.DBTable;
import com.rapiddweller.jdbacl.model.Database;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Iterator;
/**
* {@link IdentityModel} for tables which are owned by another table but have a sub identity
* (resulting in a one-to-many relationship between parent and child).
* Their natural key is composed from the owner row's natural key and a sub key for the row itself.
* Created: 01.09.2010 09:24:26
*
* @author Volker Bergmann
* @since 0.6.4
*/
public class SubNkPkQueryIdentity extends IdentityModel {
private final String[] parentTableNames; // TODO support multiple 'parent' and 'parentColumns' property
private String subNkPkQuery;
private final IdentityProvider identityProvider;
/**
* Instantiates a new Sub nk pk query identity.
*
* @param tableName the table name
* @param parentTableNames the parent table names
* @param identityProvider the identity provider
*/
public SubNkPkQueryIdentity(String tableName, String[] parentTableNames, IdentityProvider identityProvider) {
super(tableName);
this.parentTableNames = parentTableNames;
this.identityProvider = identityProvider;
}
// properties ------------------------------------------------------------------------------------------------------
/**
* Sets sub nk pk query.
*
* @param subNkPkQuery the sub nk pk query
*/
public void setSubNkPkQuery(String subNkPkQuery) {
this.subNkPkQuery = subNkPkQuery;
}
@Override
public String getDescription() {
return "Sub identity of (" + ArrayFormat.format(parentTableNames) + "):" +
SystemInfo.getLineSeparator() + subNkPkQuery;
}
// implementation --------------------------------------------------------------------------------------------------
@Override
public TabularIterator createNkPkIterator(Connection connection, String dbId, KeyMapper mapper, Database database) {
return new RecursiveIterator(connection, dbId, mapper, database);
}
// helper class for recursive iteration ----------------------------------------------------------------------------
/**
* The type Recursive iterator.
*/
public class RecursiveIterator implements TabularIterator {
/**
* The Connection.
*/
final Connection connection;
/**
* The Db id.
*/
final String dbId;
/**
* The Mapper.
*/
final KeyMapper mapper;
/**
* The Owner pk iterator.
*/
final HeavyweightIterator