public void Setup()
 {
     _command = new FormatAttributesCommand(AttributeStyle.Identical, new List<SearchResult>(new[]
         {
             Attribute
         }));
 }
Exemplo n.º 2
0
 public void Setup()
 {
     _command = new FormatAttributesCommand(AttributeStyle.Camel, new List <SearchResult>(new[]
     {
         Attribute
     }));
 }
Exemplo n.º 3
0
        private static async Task <(HttpStatusCode, string, List <SearchResult>)> OpenSgidQuery(string featureClass, string returnValues, SearchOptions options)
        {
            var results = new List <SearchResult>();

            if (featureClass.ToLowerInvariant().Contains("raster."))
            {
                // raster query
                var command  = new RasterQueryCommand(returnValues, options.Geometry, options.WkId);
                var response = await CommandExecutor.ExecuteCommandAsync(command);

                if (!string.IsNullOrEmpty(command.ErrorMessage))
                {
                    return(HttpStatusCode.BadRequest, command.ErrorMessage, results);
                }

                results.Add(response);

                return(HttpStatusCode.OK, string.Empty, results);
            }

            using var session = new NpgsqlConnection(ConnectionString);

            try
            {
                session.Open();
            }
            catch (Exception ex)
            {
                Log.Warning(ex, "could not connect to the database");
                Log.Fatal("could not connect to the database");
            }

            string query;

            try
            {
                query = BuildQuery(featureClass, returnValues, options);
            }
            catch (FormatException ex)
            {
                return(HttpStatusCode.BadRequest, ex.Message, results);
            }

            using var cmd = new NpgsqlCommand(query, session);
            try
            {
                using var reader = await cmd.ExecuteReaderAsync();

                while (reader.HasRows && await reader.ReadAsync())
                {
                    var attributes = new Dictionary <string, object>(reader.VisibleFieldCount);
                    var response   = new SearchResult
                    {
                        Attributes = attributes
                    };

                    for (var i = 0; i < reader.VisibleFieldCount; i++)
                    {
                        var key = reader.GetName(i);

                        if (string.Equals(key, "shape", StringComparison.InvariantCultureIgnoreCase))
                        {
                            var ntsGeometry = reader.GetValue(i) as Geometry;
                            var graphic     = CommandExecutor.ExecuteCommand(new NtsGeometryTransformerCommand(ntsGeometry, options.WkId));

                            response.Geometry = JObject.FromObject(graphic);

                            continue;
                        }

                        var value = reader.GetValue(i);
                        attributes[key] = value;

                        if (value is DBNull)
                        {
                            attributes[key] = null;
                        }
                    }

                    results.Add(response);
                }
            }
            catch (PostgresException ex)
            {
                var message = ex.MessageText;
                if (ex.SqlState == "42601")
                {
                    message = "{0} is not a valid where clause.".With(options.Predicate);
                }

                return(HttpStatusCode.BadRequest, message, results);
            }
            catch (Exception)
            {
                return(HttpStatusCode.BadRequest, "{0} probably does not exist. Check your spelling.".With(featureClass), results);
            }

            if (options.AttributeStyle != AttributeStyle.Lower)
            {
                var styleCommand = new FormatAttributesCommand(options.AttributeStyle, results, returnValues.Split(new[] { ',' }));
                results = CommandExecutor.ExecuteCommand(styleCommand);
            }

            return(HttpStatusCode.OK, string.Empty, results);
        }