public void SetCollection(string collectionName) { this["@metadata"] = new SqlMigrationDocument { ["@collection"] = collectionName }; }
private void SetEmbeddedDocuments(SqlMigrationDocument document, SqlTable parentTable) { foreach (var childTable in parentTable.EmbeddedTables) { var parentValues = GetValuesFromColumns(parentTable.GetReader(), parentTable.PrimaryKeys); var childColumns = childTable.GetColumnsReferencingParentTable(); SqlReader childReader; if (childColumns.Count > parentTable.PrimaryKeys.Count || parentTable.IsEmbedded) { childReader = childTable.GetReaderWhere(parentValues); } else { childReader = childTable.GetReader(); } if (childReader.HasValue() == false && childReader.Read() == false) { continue; } var continueLoop = false; while (CompareValues(parentValues, GetValuesFromColumns(childReader, childColumns), out var isBigger) == false) { if (isBigger == false && childReader.Read()) { continue; } continueLoop = true; // If parent value is greater than child value => childReader move to next, otherwise, parentReader move to next break; } if (continueLoop) { continue; } do { var innerDocument = FromReaderInternal(childReader, childTable); document.Append(childTable.NewName, innerDocument); if (childReader.Read() == false) { break; } } while (CompareValues(parentValues, GetValuesFromColumns(childReader, childColumns), out _)); } }
private void SetEmbeddedDocuments(SqlMigrationDocument document, SqlTable parentTable) { foreach (var childTable in parentTable.EmbeddedTables) { var parentValues = GetValuesFromColumns(parentTable.GetReader(), parentTable.PrimaryKeys); // values of referenced columns var childColumns = childTable.GetColumnsReferencingParentTable(); // values of referencing columns SqlReader childReader; if (childColumns.Count > parentTable.PrimaryKeys.Count || parentTable.IsEmbedded) { childReader = childTable.GetReaderWhere(parentValues); // This happens in a case when we can not iterate the embedded table only once and have to use multiple queries. } else { childReader = childTable.GetReader(); } if (childReader.HasValue() == false && childReader.Read() == false) { continue; } var continueLoop = false; while (CompareValues(parentValues, GetValuesFromColumns(childReader, childColumns), out var isBigger) == false) { if (isBigger == false && childReader.Read()) { continue; } continueLoop = true; // If parent value is greater than child value => childReader move to next. Otherwise => parentReader move to next break; } if (continueLoop) { continue; } do { var innerDocument = FromReaderInternal(childReader, childTable); document.Append(childTable.NewName, innerDocument); if (childReader.Read() == false) { break; } } while (CompareValues(parentValues, GetValuesFromColumns(childReader, childColumns), out _)); } }
private void ValidateQueryContainsAllKeys(SqlMigrationDocument document, SqlTable table) { var id = document.Id; var count = id.Count(c => c == '/'); if (count < table.PrimaryKeys.Count) { AddError(SqlMigrationImportResult.Error.ErrorType.InvalidQuery, $"Query for table '{table.Name}' must select all primary keys", table.Name); } }
public void Append(string key, SqlMigrationDocument sqlMigrationDocument) { if (this[key] == null) { this[key] = new List <SqlMigrationDocument>(); } var lst = (List <SqlMigrationDocument>) this[key]; lst.Add(sqlMigrationDocument); }
private SqlMigrationDocument FromReaderInternal(SqlReader reader, SqlTable table, SqlDatabase.Validator validator = null) { var document = new SqlMigrationDocument(table.Name); var id = table.NewName; if (table.IsEmbedded == false) { document.SetCollection(id); } for (var i = 0; i < reader.FieldCount; i++) { var columnName = reader.GetName(i); var isPrimaryKey = table.PrimaryKeys.Contains(columnName); var isForeignKey = table.ForeignKeys.TryGetValue(columnName, out var foreignKeyTableName); object value; try { value = reader[i]; } catch (Exception e) { if (!(e is PlatformNotSupportedException)) { throw; } var isKey = isPrimaryKey || isForeignKey; if (Options.SkipUnsupportedTypes == false) { var message = $"Cannot read column '{columnName}' in table '{table.Name}'. (Unsupported type: {reader.GetDataTypeName(i)}) Error: {e}"; if (validator != null) { validator.AddError(SqlMigrationImportResult.Error.ErrorType.UnsupportedType, message, table.Name, columnName); } else { throw new InvalidOperationException(message, e); } } else if (isKey) { var message = $"Cannot skip unsupported KEY column '{columnName}' in table '{table.Name}'. (Unsupported type: {reader.GetDataTypeName(i)})"; if (validator != null) { validator.AddError(SqlMigrationImportResult.Error.ErrorType.UnsupportedType, message, table.Name, columnName); } else { throw new InvalidOperationException(message, e); } } continue; } if (isPrimaryKey) { id += $"/{value}"; if (isForeignKey == false && table.IsEmbedded == false) { continue; } } var isNullOrEmpty = value is DBNull || string.IsNullOrWhiteSpace(value.ToString()); if (Options.BinaryToAttachment && reader.GetFieldType(i) == typeof(byte[])) { if (isNullOrEmpty == false) { _currentAttachments.Add($"{columnName}_{_currentAttachments.Count}", (byte[])value); } } else { if (isForeignKey && isNullOrEmpty == false && table.Database.TryGetNewName(foreignKeyTableName, out var newName)) { value = $"{newName}/{value}"; } document.Set(columnName, value, Options.TrimStrings); } } document.Id = id; if (validator == null) { SetEmbeddedDocuments(document, table); } return(document); }
private BlittableJsonReaderObject ValidateConvertableToBlittable(SqlTable table, SqlMigrationDocument document) { try { return(document.ToBllitable(_context)); } catch (Exception e) { AddError(SqlMigrationImportResult.Error.ErrorType.ParseError, $"Cannot convert document to bllittable. Error: {e}", table.Name); return(null); } }