public List <DatabaseColumnViewModel> Columns(string table, ApiCall call) { var db = Kooboo.Data.DB.GetKDatabase(call.Context.WebSite); var dbTable = db.GetOrCreateTable(table); List <DatabaseColumnViewModel> result = new List <DatabaseColumnViewModel>(); foreach (var item in dbTable.Setting.Columns) { if (item.IsSystem && item.Name == IndexedDB.Dynamic.Constants.DefaultIdFieldName) { continue; } DatabaseColumnViewModel model = new DatabaseColumnViewModel() { Name = item.Name, IsIncremental = item.IsIncremental, IsUnique = item.IsUnique, IsIndex = item.IsIndex, IsPrimaryKey = item.IsPrimaryKey, Seed = item.Seed, Scale = item.Increment, IsSystem = item.IsSystem }; model.DataType = DatabaseColumnHelper.ToFrontEndDataType(item.ClrType); model.ControlType = item.ControlType; model.Setting = item.Setting; result.Add(model); } return(result); }
public List <DatabaseItemEdit> GetEdit(string tablename, string Id, ApiCall call) { var db = Kooboo.Data.DB.GetKDatabase(call.Context.WebSite); var dbTable = Kooboo.Data.DB.GetTable(db, tablename); List <DatabaseItemEdit> result = new List <DatabaseItemEdit>(); var obj = dbTable.Get(Id); foreach (var item in dbTable.Setting.Columns) { DatabaseItemEdit model = new DatabaseItemEdit() { Name = item.Name, IsIncremental = item.IsIncremental, IsUnique = item.IsUnique, IsIndex = item.IsIndex, IsPrimaryKey = item.IsPrimaryKey, Seed = item.Seed, Scale = item.Increment, IsSystem = item.IsSystem }; model.DataType = DatabaseColumnHelper.ToFrontEndDataType(item.ClrType); model.ControlType = item.ControlType; model.Setting = item.Setting; // get value if (obj != null && obj.ContainsKey(model.Name)) { model.Value = obj[model.Name]; } result.Add(model); } return(result); }
public void UpdateColumn(string tablename, List <DatabaseColumnViewModel> columns, ApiCall call) { var db = Kooboo.Data.DB.GetKDatabase(call.Context.WebSite); var table = db.GetOrCreateTable(tablename); var setting = Lib.Serializer.Copy.DeepCopy <IndexedDB.Dynamic.Setting>(table.Setting); // deleted items. setting.Columns.RemoveWhere(o => columns.Find(m => o.Name.ToLower() == m.Name.ToLower()) == null && o.Name != IndexedDB.Dynamic.Constants.DefaultIdFieldName); // update items or new added items. foreach (var item in columns) { if (item.Name == Kooboo.IndexedDB.Dynamic.Constants.DefaultIdFieldName) { continue; } var find = setting.Columns.FirstOrDefault(o => o.Name.ToLower() == item.Name.ToLower()); if (find == null) { Type datatype = DatabaseColumnHelper.ToClrType(item.DataType); setting.AppendColumn(item.Name, datatype, 0); var col = setting.Columns.FirstOrDefault(o => o.Name == item.Name); col.Setting = item.Setting; col.ControlType = item.ControlType; col.IsIncremental = item.IsIncremental; col.Seed = item.Seed; col.Increment = item.Scale; col.IsIndex = item.IsIndex; col.IsPrimaryKey = item.IsPrimaryKey; col.IsUnique = item.IsUnique; } else { find.Setting = item.Setting; find.ControlType = item.ControlType; find.IsIncremental = item.IsIncremental; find.Seed = item.Seed; find.Increment = item.Scale; find.IsIndex = item.IsIndex; find.IsPrimaryKey = item.IsPrimaryKey; find.IsUnique = item.IsUnique; } } setting.EnsurePrimaryKey(""); table.UpdateSetting(setting); table.Close(); }