public override string[] GetCompletions(CommandCompletionContext completionContext) { var domain = this.Content.Dispatcher.Invoke(() => this.Content.Domain); var tableInfo = this.Content.Dispatcher.Invoke(() => this.Content.Table.TableInfo); return(domain.Dispatcher.Invoke(() => { var dataSet = domain.Source as CremaDataSet; var dataTable = dataSet.Tables[tableInfo.Name]; if (completionContext.Arguments.Length >= dataTable.PrimaryKey.Length) { return null; } var expItems = new List <string>(); for (var i = 0; i < completionContext.Arguments.Length; i++) { expItems.Add($"{dataTable.PrimaryKey[i].ColumnName}='{completionContext.Arguments[i]}'"); } var expression = string.Join(" AND ", expItems); var query = from item in dataTable.Select(expression) let value = CremaConvert.ChangeType(item[dataTable.PrimaryKey[completionContext.Arguments.Length]], typeof(string)) as string select value; return query.ToArray(); })); }
protected override void OnExecute() { var tableInfo = this.Content.Dispatcher.Invoke(() => this.Content.Table.TableInfo); var keys = tableInfo.Columns.Where(item => item.IsKey).ToArray(); var schema = this.CreateSchema(tableInfo.Columns); var fieldList = new List <object>(); for (var i = 0; i < this.Keys.Length; i++) { var key = keys[i]; var keyText = this.Keys[i]; var type = CremaDataTypeUtility.IsBaseType(key.DataType) ? CremaDataTypeUtility.GetType(key.DataType) : typeof(string); var value = CremaConvert.ChangeType(keyText, type); fieldList.Add(value); } var authentication = this.CommandContext.GetAuthentication(this); var tableRow = this.Content.Dispatcher.Invoke(() => this.Content.Find(authentication, fieldList.ToArray())); var terminal = new Terminal(); if (terminal.ReadString("type 'remove':") != "remove") { return; } this.Content.Dispatcher.Invoke(() => { tableRow.Delete(authentication); }); }
public static string GetRandomString(this ITableColumn tableColumn) { if (tableColumn.DefaultValue != null && RandomUtility.Next(3) == 0) { return(null); } else if (tableColumn.AllowNull == true && RandomUtility.Next(4) == 0) { return(null); } else { var template = tableColumn.Template; var dataType = tableColumn.DataType; if (CremaDataTypeUtility.IsBaseType(dataType) == false) { var type = template.GetType(dataType); return(type.GetRandomString()); } else { var value = RandomUtility.Next(CremaDataTypeUtility.GetType(dataType)); return(CremaConvert.ChangeType(value, typeof(string)) as string); } } }
protected override async Task <object[]> OnExecuteAsync(string domainID, string tableName, IDictionary <string, object> fields) { if (fields == null) { throw new ArgumentNullException(nameof(fields)); } var domain = await this.CremaHost.GetDomainAsync(Guid.Parse(domainID)); var contents = domain.Host as IEnumerable <ITableContent>; var content = contents.FirstOrDefault(item => item.Dispatcher.Invoke(() => item.Table.Name) == tableName); if (content == null) { throw new TableNotFoundException(tableName); } var authentication = this.Context.GetAuthentication(this); var tableInfo = content.Table.TableInfo; var row = await content.AddNewAsync(authentication, null); foreach (var item in fields) { var typeName = tableInfo.Columns.First(i => i.DataType == item.Key).DataType; var type = CremaDataTypeUtility.GetType(typeName); var value = CremaConvert.ChangeType(item.Value, type); await row.SetFieldAsync(authentication, item.Key, value); } await content.EndNewAsync(authentication, row); return(tableInfo.Columns.Select(item => row[item.Name]).ToArray()); }
private object GetDefaultValue(ColumnInfo columnInfo) { if (columnInfo.DefaultValue != null && CremaDataTypeUtility.IsBaseType(columnInfo.DataType) == true) { var type = CremaDataTypeUtility.GetType(columnInfo.DataType); return(CremaConvert.ChangeType(columnInfo.DefaultValue, type)); } return(columnInfo.DefaultValue); }
public void Test4() { var dataTable = new CremaDataTable(); var c1 = dataTable.Columns.Add("Column1", typeof(string)); var template = new CremaTemplate(dataTable); var defaultString = "12.3"; var defaultValue = CremaConvert.ChangeType(defaultString, typeof(float)); template.Columns[0].DefaultValue = defaultString; template.Columns[0].SetDataType(typeof(float)); template.Columns[0].SetDataType(typeof(DateTime)); Assert.AreEqual(c1.DefaultValue, template.Columns[0].DefaultValue); template.Columns[0].SetDataType(typeof(float)); Assert.AreEqual(defaultValue, template.Columns[0].DefaultValue); }
public void SetDefaultValueAsString() { var column = this.RandomOrDefault(item => item.DefaultValue == DBNull.Value && item.AutoIncrement == false); var defaultValue = column.CremaType != null?column.CremaType.GetRandomValue() : RandomUtility.Next(column.DataType); column.DefaultValue = CremaConvert.ChangeType(defaultValue, typeof(string)); if (column.DataType == typeof(DateTime)) { Assert.AreEqual(defaultValue.ToString(), column.DefaultValue.ToString()); } else { Assert.AreEqual(defaultValue, column.DefaultValue); } }
public void SetDefaultStringValue() { this.attribute.DataType = CremaDataTypeUtility.GetBaseTypes().Random(); var newDefaultValue = RandomUtility.Next(this.attribute.DataType); this.attribute.DefaultValue = CremaConvert.ChangeType(newDefaultValue, typeof(string)); Assert.AreEqual(newDefaultValue.GetType(), this.attribute.DefaultValue.GetType()); if (this.attribute.DataType == typeof(DateTime)) { Assert.AreEqual(newDefaultValue.ToString(), this.attribute.DefaultValue.ToString()); } else { Assert.AreEqual(newDefaultValue, this.attribute.DefaultValue); } }
public static async Task <string> GetRandomStringAsync(this ITableColumn tableColumn) { if (tableColumn.DefaultValue != null && RandomUtility.Next(3) == 0) { return(null); } else if (tableColumn.AllowNull == true && RandomUtility.Next(4) == 0) { return(null); } else { var template = tableColumn.Template; var dataType = tableColumn.DataType; if (CremaDataTypeUtility.IsBaseType(dataType) == false) { try { var domain = template.Domain; var dataBase = domain.GetService(typeof(IDataBase)) as IDataBase; var typeCollection = dataBase.GetService(typeof(ITypeCollection)) as ITypeCollection; var typeName = new ItemName(dataType); var type = await typeCollection.GetTypeAsync(typeName.Name, typeName.CategoryPath); return(await type.GetRandomStringAsync()); } catch (System.NullReferenceException e) { System.Diagnostics.Debugger.Launch(); throw e; } } else { var value = RandomUtility.Next(CremaDataTypeUtility.GetType(dataType)); return(CremaConvert.ChangeType(value, typeof(string)) as string); } } }
protected override async Task OnExecuteAsync(CancellationToken cancellationToken) { var tableInfo = this.Content.Dispatcher.Invoke(() => this.Content.Table.TableInfo); var keys = tableInfo.Columns.Where(item => item.IsKey).ToArray(); var schema = this.CreateSchema(tableInfo.Columns); var fieldList = new List <object>(); for (var i = 0; i < this.Keys.Length; i++) { var key = keys[i]; var keyText = this.Keys[i]; var type = CremaDataTypeUtility.IsBaseType(key.DataType) ? CremaDataTypeUtility.GetType(key.DataType) : typeof(string); var value = CremaConvert.ChangeType(keyText, type); fieldList.Add(value); } var fields = new JsonPropertiesInfo(); var authentication = this.CommandContext.GetAuthentication(this); var tableRow = await this.Content.FindAsync(authentication, fieldList.ToArray()); this.Content.Dispatcher.Invoke(() => { foreach (var item in tableInfo.Columns) { var field = tableRow[item.Name]; if (field != null || item.AllowNull == false) { fields.Add(item.Name, field); } } }); var result = fields.Execute(schema); if (result == null) { return; } foreach (var item in fields) { if (result.ContainsKey(item.Key) == false) { await tableRow.SetFieldAsync(authentication, item.Key, DBNull.Value); } } foreach (var item in result) { if (tableInfo.Columns.Any(i => i.Name == item.Key) == false) { continue; } var value1 = tableRow[item.Key]; var value2 = item.Value; if (object.Equals(value1, value2) == true) { continue; } await tableRow.SetFieldAsync(authentication, item.Key, item.Value); } }
public object ToValue() { var type = CremaDataTypeUtility.GetType(this.Type); return(CremaConvert.ChangeType(this.Value, type)); }