//see DataDictionaryToWire private static ColDefResponse ToWire(SecurityGuard guard, TableDef source, ColDef c, IUser user) { bool isDBAssignedKey = source.PrimaryKeyColName == c.Name && source.DatabaseAssignsKey; return(new ColDefResponse { PermissionLevel = (int)guard.FinalLevel(null, source.Name, c.Name), AllowSort = c.AllowSort, ForeignKeyDatonTypeName = c.ForeignKeyDatonTypeName, LeftJoin = ToWire(c.LeftJoin), SelectBehavior = ToWire(c.SelectBehavior), ImageUrlColumName = c.Image?.UrlColumName, IsComputed = c.IsComputed || isDBAssignedKey, IsMainColumn = c.IsMainColumn, IsVisibleInDropdown = c.IsVisibleInDropdown, LengthValidationMessage = DataDictionary.ResolvePrompt(c.LengthValidationMessage, user, defaultValue: null), MaxLength = c.MaxLength, MaxNumberValue = c.MaxNumberValue, MinLength = c.MinLength, MinNumberValue = c.MinNumberValue, Name = CamelCasify(c.Name), Prompt = DataDictionary.ResolvePrompt(c.Prompt, user, c.Name), RangeValidationMessage = DataDictionary.ResolvePrompt(c.RangeValidationMessage, user, defaultValue: null), Regex = c.Regex, RegexValidationMessage = DataDictionary.ResolvePrompt(c.RegexValidationMessage, user, defaultValue: null), WireType = c.WireType }); }
/// <summary> /// Get a standard or custom value from this row /// </summary> public object GetValue(ColDef coldef) { if (coldef.IsCustom) { return(GetCustom(coldef.Name)); } return(GetType().GetField(coldef.Name).GetValue(this)); }
/// <summary> /// Set a standard or custom value in this row /// </summary> public void SetValue(ColDef coldef, object value) { if (coldef.IsCustom) { SetCustom(coldef.Name, value); } else { GetType().GetField(coldef.Name).SetValue(this, value); } }
/// <summary> /// Add a custom column to the table /// </summary> public ColDef AddCustomColum(string name, Type cstype, string wireType) { var coldef = new ColDef { IsCustom = true, Name = name, CSType = cstype, WireType = wireType }; Cols.Add(coldef); HasCustomColumns = true; return(coldef); }
private void ValidateCol(ColDef coldef, object value) { string valueS = value == null ? "" : value.ToString(); string prompt = DataDictionary.ResolvePrompt(coldef.Prompt, User, coldef.Name); //string length if (coldef.CSType == typeof(string)) { bool minOK = valueS.Length >= coldef.MinLength, maxOK = coldef.MaxLength == 0 || valueS.Length <= coldef.MaxLength; if (!minOK || !maxOK) { Errors.Add(string.Format(DataDictionary.ResolvePrompt(coldef.LengthValidationMessage, User, defaultValue: "{0} must be {2} to {1} characters"), prompt, coldef.MaxLength, coldef.MinLength)); } } //numeric range if (Utils.IsSupportedNumericType(coldef.CSType) && coldef.MaxNumberValue != 0 && coldef.MinNumberValue != 0) { bool isOK; if (value == null) { isOK = false; } else { decimal d = Convert.ToDecimal(value); bool minOK = d >= coldef.MinNumberValue, maxOK = coldef.MaxNumberValue != 0 && d <= coldef.MaxNumberValue; isOK = minOK && maxOK; } if (!isOK) { Errors.Add(string.Format(DataDictionary.ResolvePrompt(coldef.RangeValidationMessage, User, defaultValue: "{0} must be in range {1}-{2}"), prompt, coldef.MinNumberValue, coldef.MaxNumberValue)); } } //regex if (coldef.CSType == typeof(string) && !string.IsNullOrEmpty(coldef.Regex)) { if (!Regex.IsMatch(valueS, coldef.Regex)) { Errors.Add(string.Format(DataDictionary.ResolvePrompt(coldef.RegexValidationMessage, User, defaultValue: "{0} does not fit required pattern"), prompt)); } } }
/// <summary> /// Write a value (quoted string or number); the caller should have written the property name to the writer already /// </summary> private static void WriteColValue(JsonTextWriter writer, Type rowType, Row row, ColDef coldef) { object value; if (coldef.IsCustom) { value = row.GetCustom(coldef.Name); } else { value = rowType.GetField(coldef.Name).GetValue(row); } string jsonValue = FormatRawJsonValue(coldef, value); writer.WriteRawValue(jsonValue); }
/// <summary> /// Format a value for json raw output, or for creating a persiston key as a string /// </summary> /// <param name="value">any supported value or null</param> public static string FormatRawJsonValue(ColDef coldef, object value) { string jsonQuote(string s) => JsonConvert.ToString(s); //null if (value == null) { return("null"); } //bool if (value is bool vbool) { return(vbool ? "true" : "false"); } //numbers to string with invariant formatting if (value is byte vbyte) { return(vbyte.ToString(CultureInfo.InvariantCulture)); } if (value is Int16 vi16) { return(vi16.ToString(CultureInfo.InvariantCulture)); } if (value is Int32 vi32) { return(vi32.ToString(CultureInfo.InvariantCulture)); } if (value is Int64 vi64) { return(vi64.ToString(CultureInfo.InvariantCulture)); } if (value is double vdouble) { return(vdouble.ToString(CultureInfo.InvariantCulture)); } if (value is decimal vdec) { return(vdec.ToString(CultureInfo.InvariantCulture)); } //quoted string if (value is string vs) { return(jsonQuote(vs)); } //date and datetime if (value is DateTime vdate) { if (coldef.WireType == Constants.TYPE_DATE || coldef.WireType == Constants.TYPE_NDATE) { return(jsonQuote(vdate.Date.ToString("yyyyMMdd"))); } else { return(jsonQuote(vdate.ToString("yyyyMMddHHmm"))); } } //byte[] if (value is byte[] vba) { return(jsonQuote(Convert.ToBase64String(vba))); } //unknown throw new Exception($"Type {value.GetType().Name} not supported"); }
/// <summary> /// Generate the expression to use in the SELECT clause. For regular columns this is just the column name. For left-joined /// columns, this is a sub-select statement. /// </summary> /// <param name="dbdef"></param> /// <param name="tabledef"></param> /// <param name="coldef"></param> /// <returns></returns> protected virtual string SqlColumnExpression(DataDictionary dbdef, TableDef tabledef, ColDef coldef) { //auto-left-joined col if (coldef.LeftJoin != null) { var fkCol = tabledef.FindCol(coldef.LeftJoin.ForeignKeyColumnName); if (fkCol == null) { throw new Exception($"Invalid foreign key column name in LeftJoin info on {coldef.Name}; it must be the name of a column in the same table"); } if (fkCol.ForeignKeyDatonTypeName == null) { throw new Exception($"Invalid use of foreign key column in LeftJoin; {fkCol.Name} must use a ForeignKey annotation to identify the foriegn table"); } var foreignTabledef = dbdef.FindDef(fkCol.ForeignKeyDatonTypeName).MainTableDef; string tableAlias = "_t_" + (++MaxtDynamicAliasUsed); return($"(select {coldef.LeftJoin.RemoteDisplayColumnName} from {foreignTabledef.SqlTableName} {tableAlias} where {tableAlias}.{foreignTabledef.PrimaryKeyColName}={tabledef.SqlTableName}.{fkCol.Name})"); } if (coldef.IsCustom || coldef.IsComputed) { throw new Exception("Cannot load custom or computed column from database"); } //regular col return(coldef.Name); }
public ViewonCriterion(ColDef c, string packedValue) { ColDef = c; PackedValue = packedValue; }