/// <summary> /// Adds the column where not already specified. /// </summary> private void AddColumnWhereNotSpecified(SqlDataRow row, bool when, string name, object value) { if (when && !row.Columns.ContainsKey(name)) { AddColumn(name); row.AddColumn(name, value); } }
/// <summary> /// Parses the data operations generating the underlying SQL. /// </summary> private void Parse() { if (DbTables == null) { throw new InvalidOperationException("RegisterDatabase must be invoked before parsing can occur."); } // Loop through all the schemas. foreach (var js in _json.Children <JProperty>()) { // Loop through the collection of tables. foreach (var jto in GetChildObjects(js)) { foreach (var jt in jto.Children <JProperty>()) { var sdt = new SqlDataTable(js.Name, jt.Name); if (Tables.Any(t => t.Schema == sdt.Schema && t.Name == sdt.Name)) { throw new SqlDataUpdaterException($"Table '{sdt.Schema}.{sdt.Name}' has been specified more than once."); } // Loop through the collection of rows. foreach (var jro in GetChildObjects(jt)) { var row = new SqlDataRow(sdt); foreach (var jr in jro.Children <JProperty>()) { if (jr.Value.Type == JTokenType.Object) { foreach (var jc in jro.Children <JProperty>()) { var col = sdt.IsRefData ? DatabaseRefDataColumns.CodeColumnName : sdt.DbTable.Columns.Where(x => x.IsPrimaryKey).Select(x => x.Name).SingleOrDefault(); row.AddColumn(col, GetColumnValue(jc.Name)); foreach (var jcv in jc.Values().Where(j => j.Type == JTokenType.Property).Cast <JProperty>()) { row.AddColumn(jcv.Name, GetColumnValue(jcv.Value)); } } } else { if (sdt.IsRefData && jro.Children().Count() == 1) { row.AddColumn(DatabaseRefDataColumns.CodeColumnName, GetColumnValue(jr.Name)); row.AddColumn("Text", GetColumnValue(jr.Value)); } else { row.AddColumn(jr.Name, GetColumnValue(jr.Value)); } } } sdt.AddRow(row); } if (sdt.Columns.Count > 0) { sdt.Prepare(); Tables.Add(sdt); } } } } }
/// <summary> /// Parses the data operations generating the underlying SQL. /// </summary> private void Parse() { if (DbTables == null) { throw new InvalidOperationException("RegisterDatabase must be invoked before parsing can occur."); } // Get the identifier generator configuration where applicable. var idjson = _json["^Type"]; if (idjson != null) { var typeName = idjson.ToObject <string>(); if (string.IsNullOrEmpty(typeName)) { throw new SqlDataUpdaterException($"Identifier generators property '^Type' is not a valid string."); } var type = Type.GetType(typeName, false); if (type == null || type.GetConstructor(Array.Empty <Type>()) == null) { throw new SqlDataUpdaterException($"Identifier generators Type '{typeName}' does not exist or have a default (parameter-lesss) constructor."); } var idgen = Activator.CreateInstance(type) !; IdentifierGenerators = idgen as IIdentifierGenerators; if (IdentifierGenerators == null) { throw new SqlDataUpdaterException($"Identifier generators Type '{typeName}' does not implement IIdentifierGenerators."); } } // Loop through all the schemas. foreach (var js in _json.Children <JProperty>()) { // Reserved; ignore. if (js.Name == "^Type") { continue; } // Loop through the collection of tables. foreach (var jto in GetChildObjects(js)) { foreach (var jt in jto.Children <JProperty>()) { var sdt = new SqlDataTable(js.Name, jt.Name); if (Tables.Any(t => t.Schema == sdt.Schema && t.Name == sdt.Name)) { throw new SqlDataUpdaterException($"Table '{sdt.Schema}.{sdt.Name}' has been specified more than once."); } // Loop through the collection of rows. foreach (var jro in GetChildObjects(jt)) { var row = new SqlDataRow(sdt); foreach (var jr in jro.Children <JProperty>()) { if (jr.Value.Type == JTokenType.Object) { foreach (var jc in jro.Children <JProperty>()) { var col = sdt.IsRefData ? DatabaseRefDataColumns.CodeColumnName : sdt.DbTable.Columns.Where(x => x.IsPrimaryKey).Select(x => x.Name).SingleOrDefault(); if (!string.IsNullOrEmpty(col)) { row.AddColumn(col, GetColumnValue(jc.Name)); foreach (var jcv in jc.Values().Where(j => j.Type == JTokenType.Property).Cast <JProperty>()) { row.AddColumn(jcv.Name, GetColumnValue(jcv.Value)); } } } } else { if (sdt.IsRefData && jro.Children().Count() == 1) { row.AddColumn(DatabaseRefDataColumns.CodeColumnName, GetColumnValue(jr.Name)); row.AddColumn("Text", GetColumnValue(jr.Value)); } else { row.AddColumn(jr.Name, GetColumnValue(jr.Value)); } } } sdt.AddRow(row); } if (sdt.Columns.Count > 0) { sdt.Prepare(IdentifierGenerators); Tables.Add(sdt); } } } } }