Skip to content

Commit

Permalink
handling Pkeys in batch part files
Browse files Browse the repository at this point in the history
  • Loading branch information
Mimetis committed Jan 18, 2023
1 parent 640f800 commit 96753c9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
27 changes: 18 additions & 9 deletions Projects/Dotmim.Sync.Core/Serialization/LocalJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void CloseFile()
/// <summary>
/// Open the file and write header
/// </summary>
public void OpenFile(string path, SyncTable shemaTable, SyncRowState state, bool append = false)
public void OpenFile(string path, SyncTable schemaTable, SyncRowState state, bool append = false)
{
if (this.writer != null)
{
Expand Down Expand Up @@ -114,21 +114,26 @@ public void OpenFile(string path, SyncTable shemaTable, SyncRowState state, bool
this.writer.WriteStartArray();
this.writer.WriteStartObject();
this.writer.WritePropertyName("n");
this.writer.WriteValue(shemaTable.TableName);
this.writer.WriteValue(schemaTable.TableName);
this.writer.WritePropertyName("s");
this.writer.WriteValue(shemaTable.SchemaName);
this.writer.WriteValue(schemaTable.SchemaName);
this.writer.WritePropertyName("st");
this.writer.WriteValue((int)state);

this.writer.WritePropertyName("c");
this.writer.WriteStartArray();
foreach (var c in shemaTable.Columns)
foreach (var c in schemaTable.Columns)
{
this.writer.WriteStartObject();
this.writer.WritePropertyName("n");
this.writer.WriteValue(c.ColumnName);
this.writer.WritePropertyName("t");
this.writer.WriteValue(c.DataType);
if (schemaTable.IsPrimaryKey(c.ColumnName))
{
this.writer.WritePropertyName("p");
this.writer.WriteValue(1);
}
this.writer.WriteEndObject();
}

Expand All @@ -137,19 +142,19 @@ public void OpenFile(string path, SyncTable shemaTable, SyncRowState state, bool
this.writer.WriteStartArray();
this.writer.WriteWhitespace(Environment.NewLine);
}

/// <summary>
/// Append a syncrow to the writer
/// Append a sync row to the writer
/// </summary>
public async Task WriteRowToFileAsync(SyncRow row, SyncTable shemaTable)
public async Task WriteRowToFileAsync(SyncRow row, SyncTable schemaTable)
{
writer.WriteStartArray();

var innerRow = row.ToArray();

if (this.writingRowAsync != null)
{
var str = await this.writingRowAsync(shemaTable, innerRow);
var str = await this.writingRowAsync(schemaTable, innerRow);
writer.WriteValue(str);
}
else
Expand Down Expand Up @@ -426,7 +431,7 @@ private SyncTable GetSchemaTableFromReader(JsonTextReader reader, JsonSerializer
// get columns from array
var includedColumns = serializer.Deserialize<List<JObject>>(reader);

// if we dont have columns specified, we are assuming it's the same columns
// if we don't have columns specified, we are assuming it's the same columns
if (includedColumns == null || includedColumns.Count == 0)
return null;

Expand All @@ -437,9 +442,13 @@ private SyncTable GetSchemaTableFromReader(JsonTextReader reader, JsonSerializer
// column name & type from file
var includedColumnName = includedColumns[i]["n"].Value<string>();
var includedColumnType = SyncColumn.GetTypeFromAssemblyQualifiedName(includedColumns[i]["t"].Value<string>());
var isPrimaryKey = includedColumns[i].ContainsKey("p");

// Adding the column
schemaTable.Columns.Add(new SyncColumn(includedColumnName, includedColumnType));

if (isPrimaryKey)
schemaTable.PrimaryKeys.Add(includedColumnName);
}

return schemaTable;
Expand Down
7 changes: 6 additions & 1 deletion Projects/Dotmim.Sync.Core/Set/SyncTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,18 @@ public IEnumerable<SyncColumn> GetPrimaryKeysColumns()
{
foreach (var column in this.Columns.OrderBy(c => c.Ordinal))
{
var isPrimaryKey = this.PrimaryKeys.Any(pkey => column.ColumnName.Equals(pkey, SyncGlobalization.DataSourceStringComparison));
var isPrimaryKey = IsPrimaryKey(column);

if (isPrimaryKey)
yield return column;
}
}

public bool IsPrimaryKey(SyncColumn column)
{
return this.PrimaryKeys.Any(primaryKey => column.ColumnName.Equals(primaryKey, SyncGlobalization.DataSourceStringComparison));
}

/// <summary>
/// Get all filters for a selected sync table
/// </summary>
Expand Down

0 comments on commit 96753c9

Please sign in to comment.