git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/mutateops@1009 f203690c-595d-4dc9-a70b-905162fa7fd2tags/jackcess-2.1.5
import java.io.IOException; | import java.io.IOException; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.Arrays; | import java.util.Arrays; | ||||
import java.util.Collection; | |||||
import java.util.Iterator; | |||||
import java.util.List; | import java.util.List; | ||||
import java.util.Map; | import java.util.Map; | ||||
* Searches for an index with the given column names. | * Searches for an index with the given column names. | ||||
*/ | */ | ||||
private CursorBuilder setIndexByColumns(List<String> searchColumns) { | private CursorBuilder setIndexByColumns(List<String> searchColumns) { | ||||
boolean found = false; | |||||
for(IndexImpl index : _table.getIndexes()) { | |||||
Collection<? extends Index.Column> indexColumns = index.getColumns(); | |||||
if(indexColumns.size() != searchColumns.size()) { | |||||
continue; | |||||
} | |||||
Iterator<String> sIter = searchColumns.iterator(); | |||||
Iterator<? extends Index.Column> iIter = indexColumns.iterator(); | |||||
boolean matches = true; | |||||
while(sIter.hasNext()) { | |||||
String sColName = sIter.next(); | |||||
String iColName = iIter.next().getName(); | |||||
if((sColName != iColName) && | |||||
((sColName == null) || !sColName.equalsIgnoreCase(iColName))) { | |||||
matches = false; | |||||
break; | |||||
} | |||||
} | |||||
if(matches) { | |||||
_index = index; | |||||
found = true; | |||||
break; | |||||
} | |||||
} | |||||
if(!found) { | |||||
IndexImpl index = _table.findIndexForColumns(searchColumns, false); | |||||
if(index == null) { | |||||
throw new IllegalArgumentException("Index with columns " + | throw new IllegalArgumentException("Index with columns " + | ||||
searchColumns + | searchColumns + | ||||
" does not exist in table " + _table); | " does not exist in table " + _table); | ||||
} | } | ||||
_index = index; | |||||
return this; | return this; | ||||
} | } | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.Collection; | |||||
import java.util.HashSet; | |||||
import java.util.List; | import java.util.List; | ||||
import java.util.Set; | import java.util.Set; | ||||
import com.healthmarketscience.jackcess.Column; | |||||
import com.healthmarketscience.jackcess.IndexBuilder; | import com.healthmarketscience.jackcess.IndexBuilder; | ||||
import com.healthmarketscience.jackcess.RelationshipBuilder; | import com.healthmarketscience.jackcess.RelationshipBuilder; | ||||
validate(); | validate(); | ||||
// FIXME determine if rel is one to one (integ enforced and both unique) | |||||
_flags = _relationship.getFlags(); | _flags = _relationship.getFlags(); | ||||
// need to determine the one-to-one flag on our own | |||||
if(isOneToOne()) { | |||||
_flags |= RelationshipImpl.ONE_TO_ONE_FLAG; | |||||
} | |||||
getPageChannel().startExclusiveWrite(); | getPageChannel().startExclusiveWrite(); | ||||
try { | try { | ||||
return cols; | return cols; | ||||
} | } | ||||
private static Collection<String> getColumnNames( | |||||
List<ColumnImpl> cols, Collection<String> colNames) { | |||||
for(ColumnImpl col : cols) { | |||||
colNames.add(col.getName()); | |||||
} | |||||
return colNames; | |||||
} | |||||
private boolean isOneToOne() { | |||||
// a relationship is one to one if the two sides of the relationship have | |||||
// unique indexes on the relevant columns | |||||
IndexImpl idx = _primaryTable.findIndexForColumns( | |||||
getColumnNames(_primaryCols, new HashSet<String>()), true); | |||||
if(idx == null) { | |||||
return false; | |||||
} | |||||
idx = _secondaryTable.findIndexForColumns( | |||||
getColumnNames(_secondaryCols, new HashSet<String>()), true); | |||||
return (idx != null); | |||||
} | |||||
private static String getTableErrorContext( | private static String getTableErrorContext( | ||||
TableImpl table, List<ColumnImpl> cols, | TableImpl table, List<ColumnImpl> cols, | ||||
String tableName, List<String> colNames) { | |||||
String tableName, Collection<String> colNames) { | |||||
if(table != null) { | if(table != null) { | ||||
tableName = table.getName(); | tableName = table.getName(); | ||||
} | } | ||||
if(cols != null) { | if(cols != null) { | ||||
colNames = new ArrayList<String>(); | |||||
for(ColumnImpl col : cols) { | |||||
colNames.add(col.getName()); | |||||
} | |||||
colNames = getColumnNames(cols, new ArrayList<String>()); | |||||
} | } | ||||
return CustomToStringStyle.valueBuilder(tableName) | return CustomToStringStyle.valueBuilder(tableName) |
import com.healthmarketscience.jackcess.ColumnBuilder; | import com.healthmarketscience.jackcess.ColumnBuilder; | ||||
import com.healthmarketscience.jackcess.ConstraintViolationException; | import com.healthmarketscience.jackcess.ConstraintViolationException; | ||||
import com.healthmarketscience.jackcess.CursorBuilder; | import com.healthmarketscience.jackcess.CursorBuilder; | ||||
import com.healthmarketscience.jackcess.Index; | |||||
import com.healthmarketscience.jackcess.IndexBuilder; | import com.healthmarketscience.jackcess.IndexBuilder; | ||||
import com.healthmarketscience.jackcess.JackcessException; | import com.healthmarketscience.jackcess.JackcessException; | ||||
import com.healthmarketscience.jackcess.PropertyMap; | import com.healthmarketscience.jackcess.PropertyMap; | ||||
return _indexCount; | return _indexCount; | ||||
} | } | ||||
public IndexImpl findIndexForColumns(Collection<String> searchColumns, | |||||
boolean uniqueOnly) { | |||||
for(IndexImpl index : _indexes) { | |||||
Collection<? extends Index.Column> indexColumns = index.getColumns(); | |||||
if(indexColumns.size() != searchColumns.size()) { | |||||
continue; | |||||
} | |||||
Iterator<String> sIter = searchColumns.iterator(); | |||||
Iterator<? extends Index.Column> iIter = indexColumns.iterator(); | |||||
boolean matches = true; | |||||
while(sIter.hasNext()) { | |||||
String sColName = sIter.next(); | |||||
String iColName = iIter.next().getName(); | |||||
if((sColName != iColName) && | |||||
((sColName == null) || !sColName.equalsIgnoreCase(iColName))) { | |||||
matches = false; | |||||
break; | |||||
} | |||||
} | |||||
if(matches && (!uniqueOnly || index.isUnique())) { | |||||
return index; | |||||
} | |||||
} | |||||
return null; | |||||
} | |||||
List<ColumnImpl> getAutoNumberColumns() { | List<ColumnImpl> getAutoNumberColumns() { | ||||
return _autoNumColumns; | return _autoNumColumns; | ||||
} | } |