Support for sql.date in database

vishalv20508 years ago

I am modifying the server code to generate some insights for me as well. Similar to how position attributes keep track of the distance and total distance. What I plan to do is keep track of daily insights. For this I have created a summaries table in the DB, and it just has the maxSpeed column right now. In the data manager this is what I have done to add new summary

    public void addSummary(Summary summary) throws SQLException {
        summary.setId(QueryBuilder.create(dataSource, "INSERT INTO summaries (deviceid, maxSpeed, attributes, date)\n" +
                "        VALUES (:deviceId, :maxSpeed, :attributes, :date)", true)
                .setObject(summary)
                .executeUpdate());
    }

The property date of summary is of type java.sql.Date (since I do not want the time info).
Can I get some pointers on how to enable writes of type java.sql.Date. I realise I have to write a method like setSQLDate in QueryBuilder.java

vishalv20508 years ago

After some digging around I came up with this


    public QueryBuilder setSQLDate(String name, java.sql.Date value) throws SQLException {
        for (int i : indexes(name)) {
            try {
                if (value == null) {
                    statement.setNull(i, Types.DATE);
                } else {
                    statement.setDate(i, value);
                }
            } catch (SQLException error) {
                statement.close();
                connection.close();
                throw error;
            }
        }
        return this;
    }

Does this look like it will work ? I will also modify setObject method accordingly.

vishalv20508 years ago

Yup, it works.
Well, this turned into a monologue didn't it. ;)
I will leave this up anyway in case someone wants to do something similar.

Anton Tananaev8 years ago

Just wondering why do you need sql Date?

vishalv20508 years ago

You know how you keep track of total distance in each position point, I wanted to have something like daily summaries of different parameters as part of pipeline. I now keep track of and save the daily distance and how other stats about the car. Since it is part of pipeline, at each point it is the latest value in the DB :-)

vishalv20508 years ago

Running into some other difficulties though over here, I wanted all of it in IST but the server operates in UTC, so my daily summaries are being generated according to UTC time :-| . Will try to sort that out now, in case you have any ideas, you can throw them around and I will try them out