Titanium JIRA Archive
Alloy (ALOY)

[ALOY-535] Need support for prepared statements

GitHub Issuen/a
TypeImprovement
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2013-02-25T22:19:45.000+0000
Affected Version/sAlloy 1.0.3
Fix Version/sAlloy 1.1.0, 2013 Sprint 05
ComponentsRuntime
Labelsapi
ReporterThomas Wilkinson
AssigneeTony Lukasavage
Created2013-02-25T15:06:25.000+0000
Updated2013-04-09T19:33:25.000+0000

Description

update (2/25/2013)

The new syntax will be, allowing for the existing syntax as well as allowing for a prepared statement to be defined with an object.
// plain query
collection.fetch({
    query: 'select * from some_table where column1 = "somevalue"'
});

// prepared statement
collection.fetch({
    query: {
        statement: 'select * from some_table where column1 = ?',
        params: [values]
    }
});

original

Currently there is no support for prepared statements. The only way to make an SQL statement with parameters is to have code insert the parameters. This is a security risk, a bad practice, and reflects poorly on Alloy models as a whole. Currently I have to: collection.fetch({query: "select * from some_table where column1 = '" + value + "'"); Ideally I would like to: collection.fetch({query: "select * from some_table where column1 = ?" + params: [value]}); Assuming this uses the Ti.Database.execute() in the background, this should be a small change as the execute() method already supports this.

Comments

  1. Tony Lukasavage 2013-02-25

    The proposed syntax doesn't make much sense, in that it's using inline concatenation of arguments, making it look like some kind of string:
       collection.fetch({
           query: "select * from some_table where column1 = ?" + params: [value]
       });
       
    I'm assuming you meant something like this:
       collection.fetch({
           query: "select * from some_table where column1 = ?",
           params: [value]
       });
       
    I like the idea of keeping the query and args contained in a single property of the fetch object though, to keep them clearly coupled as fetch can have other arguments:
       collection.fetch({
           query: {
               statement: "select * from some_table where column1 = ?",
               params: [values]
           }
       });
       
  2. Thomas Wilkinson 2013-02-25

    You're correct in your assumption, I intended a comma instead of a plus sign. However, I do like your proposal better. Thank you.
  3. Federico Casali 2013-04-09

    Sample code available in https://github.com/appcelerator/alloy/tree/1_1_X/test/apps/models/sql_queries Verified as fixed. Alloy: 1.1.0-cr CLI version: 3.1.0-cr TiSDK: 3.1.0.v20130408154547 Closing.

JSON Source