Hello,
i have a very common problem and I wonder how to resolve it.
I have a database full of places with their coordinates. I have created a service (places) as a remote sql database in my dsp.
I Know the sql request to find the 5 nearest place given some coordinates…
How can I do à web service in dreamfactory that would return the 5 nearest place given some coordinates ? How can I launch some spécific sql request via the api ?
You may be able to query the data directly with a REST API call from the client. It depends on how you’ve stored the coordinates data. The REST API lets you pass in a filter parameter with AND / OR clauses across multiple fields, same as SQL. See the docs here https://github.com/dreamfactorysoftware/dsp-core/wiki/Database-Retrieving-Records and this short screencast https://www.youtube.com/watch?v=BkRkI1BoMis. You can try filters in the API docs tab in the admin console…e.g. in the filter field of API docs for a GET on a table, just enter the table name and filter, like age < 30 AND last_name = “Smith”. You’ll see how the API call is formed.
You can create a View in your remote SQL database. Views are exposed to the REST API and you can call views directly.
And when I put the ‘AS distance’, it returns me something like:
{
"error": [
{
"context": null,
"message": "Failed to retrieve records from 'places'.\nSQLSTATE[HY000]: General error: Did not find column name '( 6371 * acos( cos( radians(45.012448) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(0.000812) ) + sin( radians(45.012448) ) * sin(radians(lat)) ) ) * 1000 AS distance' in the defined columns; it will not be bound",
"code": 500
}
]
}
is there a way to change the fields name (to use the ‘AS something’) ?
The Idea is to be able to add in the filters something like:
The REST API can fetch data for pre-defined fields…in other words if Distance is a field in your database, then you can easily pass in a filter to the API call, like distance < 2000.
It seems like you need to calculate distance programmatically. Calculating distance at run-time for every record on a large data set is expensive. I’d recommend having a distance field in your database and use a stored procedure / trigger to calculate distance based on coordinates when each place record is created and updated. Also probably index the distance field.
Then query the distance field directly with the REST API.
Well … thank you for the advice… but I have ton compute the distance field at runtime…
because I don’t know where I will be when I want to check where is the nearest place
SO, I can’t have a distance field.
in other word… what I want to do is not possible ?
Something like that:
Select comp
From table
Where (table.col + 2) as comp
is not possible ?
So what I have to do is a procedure that return a computed table right ?
Wondering if this was ever solved? I have the same issue. I can used the stored procedure, but in my case (as well as this authors) the lat/long needs to be passed in to the stored procedure. Can we use parameters for the stored procedure?