public static TableDesc GetTableDesc(IDictionary<string, object> dbCommands) { var tableDict = (IDictionary<string, object>)dbCommands["Table"]; var table = new TableDesc(); foreach (var key in tableDict.Keys) { switch (key) { case "TableName": table.TableName = tableDict[key].ToString(); break; case "IdName": table.IdName = tableDict[key].ToString(); break; case "Id": table.Id = tableDict[key].ToString(); break; case "UidName": table.UidName = tableDict[key].ToString(); break; case "Uid": table.Uid = tableDict[key].ToString(); break; case "Values": table.Values = (IDictionary<string, object>)tableDict[key]; break; } } return table; }
public void BeforeGenerateSiteHandler(dynamic configModel) { string projectRootPath = configModel.ProjectRoot; if(File.Exists(Path.Combine(projectRootPath, "seed.json"))) { string jsonFile = File.ReadAllText(Path.Combine(projectRootPath, "seed.json")); dynamic seedJson = JsonUtils.DeserializeJson(jsonFile); var tables = seedJson.seeds["tables"]; foreach (Dictionary<string, object> table in tables) { var tableName = table["table"].ToString(); var lookupField = table["lookup_field"].ToString(); var rows = table["rows"] as ArrayList; KeyValuePair<string, object> commonField = new KeyValuePair<string,object>(); if (table.ContainsKey("common")) { // Add common fields var common = (IDictionary<string, object>)table["common"]; db.TableName = common["lookup_table"].ToString(); string lookup_field_name = common["lookup_field_name"].ToString(); var result = db.Single(where: string.Format("{0} = @0", common["search_field_name"]), args: new[] { common["search_field_value"] }); commonField = new KeyValuePair<string, object>(common["field_name"].ToString(), ((IDictionary<string, object>)result)[lookup_field_name]); } // Start data-pushing db.TableName = tableName; foreach (Dictionary<string,object> row in rows) { TableDesc tableDesc = new TableDesc(); tableDesc.UidName = lookupField; tableDesc.Uid = row[lookupField].ToString(); tableDesc.Values = new Dictionary<string, object>(); if (!String.IsNullOrEmpty(commonField.Key)) { tableDesc.Values.Add(commonField); } foreach (KeyValuePair<string, object> kv in row) { if (kv.Key.Equals(lookupField, StringComparison.InvariantCultureIgnoreCase)) { } else { tableDesc.Values.Add(kv); } } GetOrInsertTable(db, tableDesc, lookupField, row[lookupField].ToString()); } } } }
public dynamic GetOrInsertTable(DynamicModel db, TableDesc tableModel,string keyName, string keyValue) { var result = db.All(where: string.Format("{0} = @0", keyName), args: new[] { keyValue }); var count = result.Count(); if (count == 0) { IDictionary<string, object> newContent = new ExpandoObject(); newContent[keyName] = keyValue; var fieldValues = (IDictionary<string, object>)tableModel.Values; foreach (string fieldName in fieldValues.Keys) { newContent[fieldName] = fieldValues[fieldName]; } return db.Insert(newContent); } else if (count == 1) { return result.First(); } else { throw new Exception(string.Format("Multiple content with same name {0}", keyName)); } }