internal static List <KeyValuePair <int, int> > Create(SqlCeConnection conn, ISqlCeBulkCopyInsertAdapter adapter, SqlCeBulkCopyOptions copyOptions, string tableName) { var keepNulls = SqlCeBulkCopyTableHelpers.IsCopyOption(SqlCeBulkCopyOptions.KeepNulls, copyOptions); var retVal = new List <KeyValuePair <int, int> >(); //we use this to determine if we throw an error while building maps. int idOrdinal = SqlCeBulkCopyTableHelpers.IdentityOrdinalIgnoreOptions(conn, tableName); var destColumnData = DestinationTableDefaultMetadata.GetDataForTable(conn, tableName); //we are going to validate all of the columns but if we don't map then we will not set the HasMappings //A map is defined as //1. any column that the column order is changed //2. field exists in the dest but not the source and the dest has a default. //3. Identity column that is not 0 ?? we may be able to remove this one. //we only really care about the destination columns being mapped. If too many columns exist do we really care???? var sourceColumns = GetSourceColumns(adapter); for (int destIndex = 0; destIndex < destColumnData.Count; destIndex++) { var destColumn = destColumnData[destIndex]; var sourceIndex = sourceColumns.IndexOf(destColumn.ColumnName); //see if the source is the same as the destination ordinal //if (destIndex != sourceIndex) //we have a map if we care later //If the source index is -1 and the dest does not allow nulls or has a default, it is an error if (sourceIndex < 0) { //either we allow nulls or the ordinal is the index and therefore it is valid if (!destColumnData[destIndex].HasDefault && ((!destColumnData[destIndex].IsNullable && !keepNulls) && idOrdinal != destIndex)) { //var error = destColumnData[destIndex].HasDefault + " " + destColumnData[destIndex].IsNullable + " " + keepNulls + " " + idOrdinal + " " + destIndex; throw new ApplicationException(string.Format("Source column '{0}' does not exist and destination does not allow nulls.", destColumn.ColumnName)); } } retVal.Add(new KeyValuePair <int, int>(sourceIndex, destIndex)); } return(retVal); }
internal List <KeyValuePair <int, int> > ValidateCollection(SqlCeConnection conn, ISqlCeBulkCopyInsertAdapter adapter, SqlCeBulkCopyOptions copyOptions, string tableName) { if (Count > 0) { var retVal = new List <KeyValuePair <int, int> >(); var sourceColumns = GetSourceColumns(adapter); var destColumns = ToColumnNames(DestinationTableDefaultMetadata.GetDataForTable(conn, tableName)); foreach (SqlCeBulkCopyColumnMapping mapping in this.Items) { var sourceColumnName = (mapping.SourceColumn ?? string.Empty).ToUpper(); var destColumnName = (mapping.DestinationColumn ?? string.Empty).ToUpper(); int sourceIndex = -1; int destIndex = -1; //verify if we have a source column name that it exists if (!string.IsNullOrEmpty(sourceColumnName)) { if (!sourceColumns.Contains(sourceColumnName)) { throw new ApplicationException("No column exists with the name of " + mapping.SourceColumn + " in source."); //use collection name for error since it has original casing. } sourceIndex = sourceColumns.IndexOf(sourceColumnName); } else { if (mapping.SourceOrdinal < 0 || mapping.SourceOrdinal >= destColumns.Count) { throw new ApplicationException("No column exists at index " + mapping.SourceOrdinal + " in source."); //use collection name for error since it has original casing. } sourceIndex = mapping.SourceOrdinal; } if (!string.IsNullOrEmpty(destColumnName)) { if (destColumnName.StartsWith("[") && destColumnName.EndsWith("]")) { destColumnName = destColumnName.Substring(1, destColumnName.Length - 2); } if (!sourceColumns.Contains(destColumnName)) { string bestFit = null; foreach (var existingColumn in destColumns) { if (String.Equals(existingColumn, destColumnName, StringComparison.InvariantCultureIgnoreCase)) { bestFit = existingColumn; } } if (bestFit == null) { throw new ApplicationException("Destination column " + mapping.DestinationColumn + " does not exist in destination table " + tableName + " in database " + conn.Database + "."); //use collection name for error since it has original casing. } else { throw new ApplicationException( "Destination column " + mapping.DestinationColumn + " does not exist in destination table " + tableName + " in database " + conn.Database + "." + " Column name mappings are case specific and best found match is " + bestFit + "."); //use collection name for error since it has original casing. } } else { destIndex = destColumns.IndexOf(destColumnName); } } else { if (mapping.DestinationOrdinal < 0 || mapping.DestinationOrdinal >= destColumns.Count) { throw new ApplicationException( "No column exists at index " + mapping.DestinationOrdinal + " in destination table " + tableName + "in database " + conn.Database + "."); //use collection name for error since it has original casing. } destIndex = mapping.DestinationOrdinal; } retVal.Add(new KeyValuePair <int, int>(sourceIndex, destIndex)); } retVal.Sort((a, b) => { return(a.Key.CompareTo(b.Key)); }); return(retVal); } else { return(Create(conn, adapter, copyOptions, tableName)); } }