public string GenerateSql(string eql, string parameters) { ValidationException validation = new ValidationException(); List <DataSourceParameter> dsParams = ProcessParametersText(parameters); List <EqlParameter> eqlParams = new List <EqlParameter>(); foreach (var dsPar in dsParams) { eqlParams.Add(ConvertDataSourceParameterToEqlParameter(dsPar)); } EqlBuilder builder = new EqlBuilder(eql); var result = builder.Build(eqlParams); if (result.Errors.Count > 0) { foreach (var err in result.Errors) { if (err.Line.HasValue || err.Column.HasValue) { validation.AddError("eql", $"{err.Message} [{err.Line},{err.Column}]"); } else { validation.AddError("eql", err.Message); } } } validation.CheckAndThrow(); return(result.Sql); }
public DatabaseDataSource Update(Guid id, string name, string description, int weight, string eql, string parameters) { ValidationException validation = new ValidationException(); if (string.IsNullOrWhiteSpace(eql)) { throw new ArgumentException(nameof(eql)); } List <EqlParameter> eqlParams = new List <EqlParameter>(); List <DataSourceParameter> dsParams = new List <DataSourceParameter>(); if (!string.IsNullOrWhiteSpace(parameters)) { dsParams = ProcessParametersText(parameters); foreach (var dsPar in dsParams) { eqlParams.Add(ConvertDataSourceParameterToEqlParameter(dsPar)); } } EqlBuilder builder = new EqlBuilder(eql); var result = builder.Build(eqlParams); if (result.Errors.Count > 0) { foreach (var err in result.Errors) { if (err.Line.HasValue || err.Column.HasValue) { validation.AddError("eql", $"{err.Message} [{err.Line},{err.Column}]"); } else { validation.AddError("eql", err.Message); } } } validation.CheckAndThrow(); foreach (var par in result.Parameters) { if (!eqlParams.Any(x => x.ParameterName == par.ParameterName)) { validation.AddError("parameters", $"Parameter '{par.ParameterName}' is missing."); } } validation.CheckAndThrow(); DatabaseDataSource ds = new DatabaseDataSource(); ds.Id = id; ds.Name = name; ds.Description = description; ds.EqlText = eql; ds.SqlText = result.Sql; ds.EntityName = result.FromEntity.Name; ds.Parameters.AddRange(dsParams); ds.Fields.AddRange(ProcessFieldsMeta(result.Meta)); if (string.IsNullOrWhiteSpace(ds.Name)) { validation.AddError("name", "Name is required."); } else { var existingDS = GetDatabaseDataSourceByName(ds.Name); if (existingDS != null && existingDS.Id != ds.Id) { validation.AddError("name", "Another DataSource with same name already exists."); } } if (string.IsNullOrWhiteSpace(ds.EqlText)) { validation.AddError("eql", "Eql is required."); } if (string.IsNullOrWhiteSpace(ds.SqlText)) { validation.AddError("sql", "Sql is required."); } validation.CheckAndThrow(); rep.Update(ds.Id, ds.Name, ds.Description, ds.Weight, ds.EqlText, ds.SqlText, JsonConvert.SerializeObject(ds.Parameters), JsonConvert.SerializeObject(ds.Fields), ds.EntityName); RemoveFromCache(); return(GetDatabaseDataSourceById(ds.Id)); }