1

I'm trying to allow users to add routes to points on a leaflet map, using sql queries to a database of points. Because I want each point to have more than one route that passes through it, I added additional columns: "route2", "routenumber2" till "route5", "routenumber5" to the original "route" and "routenumber".

The following code does work, but I figured it's repetitive to write it out five times, each time only with a different set of columns.

if (e.target && e.target.id == "submitrest") {

        const enteredRoute = document.getElementById("input_Route").value; // from user form input
        const enteredNumber = document.getElementById("input_routenumber").value;
        const enteredNickname = document.getElementById("input_nick").value;
        const enteredID = document.getElementById("input_cartodb_id").value;

        cartoData.eachLayer(function (layer) {

            // SQL to put layer in -- Building the SQL query
            let sql =
                "UPDATE stops " +
                "SET area = '" + enteredRoute + "', " +
                "route_number = '" + enteredNumber + "', " +
                "nickname = '" + enteredNickname + "' " +
                "WHERE cartodb_id = " + enteredID + ";"
            console.log(sql);

            if (enteredRoute && enteredNickname && enteredNumber && enteredID) {
                fetch(url1, {
                    method: "POST",
                    headers: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    body: "q=" + encodeURI(sql)
                })
                    .then(function (response) {
                        return response.json();
                    })
                    .then(function (data) {
                        console.log("Data saved:", data);
                    })

What I am asking is if, like how document.getElementById allows the user to input different data each time and send the data using the same code, there is a less repetitive way to make an sql query function that allows me to put the data into "route3" and "routenumber3" if "route2" and "routenumber2" are already full, and so on.

What I would like is a row in CartoDB to have five sets/columns routes and routenumbers that a user can input data to, preferably with less duplicated code.

So far I've tried using Javascript class constructors and the Factory Method. Can I get these to allow the the columns specified in the sql query to be variable? Thank you

Winni
  • 31
  • 3
  • 1
    Normalize data structure. Each route should be a record in related dependent table, not a column. – June7 Mar 11 '21 at 17:45
  • Never ever send an SQL query like that to the server... anybody could just rewrite it like `delete from stops` and it will be executed without complaining. You send the parameters and on the server side you build the query using parameters place holders (NOT string concatenation). Since you are (will be) sending all the parameters, it is then easy to switch column or to work on a normalized dataset. – JGH Mar 11 '21 at 19:01
  • Unless you can make @JGH suggestion work. – June7 Mar 12 '21 at 04:57
  • @June7 oh? I thought they were also saying I'd better normalize. Because SQL Server will not accept a table name or a column name as a parameter, first I was going to try to use a CASE Statement from this [answer](https://stackoverflow.com/a/10092975/15376656) to change the columns. – Winni Mar 12 '21 at 05:13
  • Okay, maybe they did concur with normalization and were just making an additional recommendation about parameters. In Access I could use VBA to modify a query object to change table and fields or to just create a new query object. I have no idea about capabilities of apps you use. – June7 Mar 12 '21 at 05:42
  • It is better to normalize the data, so the day you want 4, or 6 , or X routes you don't have to rewrite half of the app. That being said, using Postgres, you can have a look at `dynamic queries`. If you (properly) build such query on the server side it offers protections against SQL injection and you will be able to dynamically set the table/column names – JGH Mar 12 '21 at 12:57
  • @JGH It took me a bit to see how I could render data from different tables on one leaflet popup, but I see now that using parametres to query related tables is better than trying to vary the columns, so thank you! – Winni Mar 15 '21 at 04:40

0 Answers0