private static DatabaseDataSource DataRowToModelConvert(DataRow inputObj)
        {
            if (inputObj == null)
            {
                return(null);
            }

            var outputObj = new DatabaseDataSource();

            outputObj.Id          = (Guid)inputObj["id"];
            outputObj.Name        = (string)inputObj["name"];
            outputObj.Description = (string)inputObj["description"];
            outputObj.Weight      = (int)inputObj["weight"];
            outputObj.EqlText     = (string)inputObj["eql_text"];
            outputObj.SqlText     = (string)inputObj["sql_text"];
            outputObj.Parameters.AddRange(JsonConvert.DeserializeObject <List <DataSourceParameter> >((string)inputObj["parameters_json"]).ToArray());
            outputObj.Fields.AddRange(JsonConvert.DeserializeObject <List <DataSourceModelFieldMeta> >((string)inputObj["fields_json"]).ToArray());
            outputObj.EntityName = (string)inputObj["entity_name"];

            //clean data source parameters from leading @ character
            foreach (var par in outputObj.Parameters)
            {
                if (par.Name.StartsWith("@"))
                {
                    par.Name = par.Name.Substring(1);
                }
            }

            return(outputObj);
        }
Exemple #2
0
        private object ExecDataSource(DSW dsWrapper)
        {
            if (dsWrapper.DataSource.Type == DataSourceType.CODE)
            {
                Dictionary <string, object> arguments = new Dictionary <string, object>();
                if (dsWrapper.DataSource.Parameters != null)
                {
                    foreach (var par in dsWrapper.DataSource.Parameters)
                    {
                        var    pageDSParam = dsWrapper.PageDataSource.Parameters.SingleOrDefault(x => x.Name == par.Name);
                        string value       = par.Value;
                        if (pageDSParam != null)
                        {
                            value = ProcessParameterValue(pageDSParam.Value);
                            if (string.IsNullOrWhiteSpace(value))
                            {
                                value = ProcessParameterValue(par.Value);
                                value = CheckProcessDefaultValue(value);
                                var ds      = dsMan.Get(dsWrapper.PageDataSource.DataSourceId);
                                var dsParam = ds.Parameters.SingleOrDefault(x => x.Name == par.Name);
                                if (dsParam != null)
                                {
                                    arguments[dsParam.Name] = dsMan.GetDataSourceParameterValue(new DataSourceParameter {
                                        Name = dsParam.Name, Type = dsParam.Type, Value = value
                                    });
                                }
                                else
                                {
                                    arguments[par.Name] = value;
                                }
                            }
                            else
                            {
                                value = CheckProcessDefaultValue(value);
                                arguments[pageDSParam.Name] = dsMan.GetDataSourceParameterValue(new DataSourceParameter {
                                    Name = pageDSParam.Name, Type = pageDSParam.Type, Value = value
                                });
                            }
                        }
                        else
                        {
                            value = ProcessParameterValue(value);
                            value = CheckProcessDefaultValue(value);
                            var ds      = dsMan.Get(dsWrapper.PageDataSource.DataSourceId);
                            var dsParam = ds.Parameters.SingleOrDefault(x => x.Name == par.Name);
                            if (dsParam != null)
                            {
                                arguments[dsParam.Name] = dsMan.GetDataSourceParameterValue(new DataSourceParameter {
                                    Name = dsParam.Name, Type = dsParam.Type, Value = value
                                });
                            }
                            else
                            {
                                arguments[par.Name] = value;
                            }
                        }
                    }
                }

                arguments["PageModel"] = this;
                var codeDS = (CodeDataSource)dsWrapper.DataSource;
                return(codeDS.Execute(arguments));
            }
            else if (dsWrapper.DataSource.Type == DataSourceType.DATABASE)
            {
                var eqlParameters = new List <EqlParameter>();
                if (dsWrapper.DataSource.Parameters != null)
                {
                    foreach (var par in dsWrapper.DataSource.Parameters)
                    {
                        var    pageDSParam = dsWrapper.PageDataSource.Parameters.SingleOrDefault(x => x.Name == par.Name);
                        string value       = par.Value;
                        if (pageDSParam != null)
                        {
                            value = ProcessParameterValue(pageDSParam.Value);
                            if (string.IsNullOrWhiteSpace(value))
                            {
                                value = ProcessParameterValue(par.Value);
                                value = CheckProcessDefaultValue(value);
                                var ds      = dsMan.Get(dsWrapper.PageDataSource.DataSourceId);
                                var dsParam = ds.Parameters.SingleOrDefault(x => x.Name == par.Name);
                                if (dsParam != null)
                                {
                                    eqlParameters.Add(dsMan.ConvertDataSourceParameterToEqlParameter(new DataSourceParameter {
                                        Name = dsParam.Name, Type = dsParam.Type, Value = value
                                    }));
                                }
                                else
                                {
                                    eqlParameters.Add(new EqlParameter(par.Name, value));
                                }
                            }
                            else
                            {
                                value = CheckProcessDefaultValue(value);
                                eqlParameters.Add(dsMan.ConvertDataSourceParameterToEqlParameter(new DataSourceParameter {
                                    Name = pageDSParam.Name, Type = pageDSParam.Type, Value = value
                                }));
                            }
                        }
                        else
                        {
                            value = ProcessParameterValue(value);
                            value = CheckProcessDefaultValue(value);
                            var ds      = dsMan.Get(dsWrapper.PageDataSource.DataSourceId);
                            var dsParam = ds.Parameters.SingleOrDefault(x => x.Name == par.Name);
                            if (dsParam != null)
                            {
                                eqlParameters.Add(dsMan.ConvertDataSourceParameterToEqlParameter(new DataSourceParameter {
                                    Name = dsParam.Name, Type = dsParam.Type, Value = value
                                }));
                            }
                            else
                            {
                                eqlParameters.Add(new EqlParameter(par.Name, value));
                            }
                        }
                    }
                }

                DatabaseDataSource dbDs = (DatabaseDataSource)dsWrapper.DataSource;
                return(dsMan.Execute(dbDs.Id, eqlParameters));
            }
            else
            {
                throw new Exception("Not supported data source type.");
            }
        }
        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));
        }