diff --git a/contrib/opportunity/src/opportunity_model.nit b/contrib/opportunity/src/opportunity_model.nit index bfd092acd9..f9954d984b 100644 --- a/contrib/opportunity/src/opportunity_model.nit +++ b/contrib/opportunity/src/opportunity_model.nit @@ -57,7 +57,7 @@ class OpportunityDB # Find a `People` by its id, or `null` if it could not be found fun find_people_by_id(id: Int): nullable People do - var req = select("* from people where id={id};") + var req = select_with("* FROM people WHERE id = ?", id) for i in req do return new People.from_db(i[0].to_i, i[1].to_s, i[2].to_s) end @@ -66,7 +66,7 @@ class OpportunityDB # Find a `Meetup` by its id or `null` if it could not be found fun find_meetup_by_id(id: String): nullable Meetup do - var req = select("* FROM meetups where id={id.to_sql_string};") + var req = select_with("* FROM meetups WHERE id = ?", id) for i in req do return new Meetup.from_db(i[0].to_s, i[1].to_s, i[2].to_s, i[3].to_s, i[4].to_i) end @@ -75,7 +75,7 @@ class OpportunityDB # Find an `Answer` by its `id` or `null` if it could not be found fun find_answer_by_id(id: Int): nullable Answer do - var req = select("* FROM answers WHERE id={id};") + var req = select_with("* FROM answers WHERE id = ?", id) for i in req do return new Answer.from_db(i[0].to_i, i[2].to_s) end @@ -105,7 +105,7 @@ class OpportunityDB # # Returns true if sucessful, false otherwise fun remove_people_by_id(id: Int): Bool do - var rq = execute("DELETE FROM people WHERE id = {id};") + var rq = execute_with("DELETE FROM people WHERE id = ?", id) if not rq then print_error "Opportunity error deleting people {id}" print_error error or else "Unknown error" @@ -154,7 +154,7 @@ class People # NOTE: If `self` does not exist in the Database, no answers will be fetched fun load_answers(db: OpportunityDB, meetup: Meetup) do self.answers = new HashMap[Answer, Int] - var req = db.select("answers.id, answers.name, part_answers.value FROM part_answers, answers WHERE part_answers.id_part={id} AND answers.id=part_answers.id_ans AND answers.meetup_id={meetup.id.html_escape.to_sql_string} GROUP BY answers.id;") + var req = db.select_with("answers.id, answers.name, part_answers.value FROM part_answers, answers WHERE part_answers.id_part = ? AND answers.id = part_answers.id_ans AND answers.meetup_id = ? GROUP BY answers.id", id, meetup.id) for i in req do var ans = new Answer.from_db(i[0].to_i, i[1].to_s) answers[ans] = i[2].to_i @@ -165,14 +165,14 @@ class People redef fun commit(db) do if id == -1 then - if not db.execute("INSERT INTO people (name,surname) VALUES ({name.html_escape.to_sql_string}, {surname.html_escape.to_sql_string});") then + if not db.execute_with("INSERT INTO people (name, surname) VALUES (?, ?)", name.html_escape, surname.html_escape) then print_error "Opportunity error while adding people {self}" print_error db.error or else "Unknown error" return false end id = db.last_insert_rowid else - if not db.execute("UPDATE people SET name={name.html_escape.to_sql_string}, surname={surname.html_escape.to_sql_string} WHERE ID={id};") then + if not db.execute_with("UPDATE people SET name = ?, surname = ? WHERE id = ?", name.html_escape, surname.html_escape, id) then print_error "Opportunity error while updating people {self}" print_error db.error or else "Unknown error" return false @@ -181,16 +181,16 @@ class People for i,j in answers do if i.id == -1 then i.commit(db) var val = j - var s = db.select("* FROM part_answers WHERE id_part={id} AND id_ans={i.id}") + var s = db.select_with("* FROM part_answers WHERE id_part = ? AND id_ans = ?", id, i.id) if s != null and s.iterator.is_ok then - if not db.execute("UPDATE part_answers SET value={j} WHERE id_part={id} AND id_ans={i.id};") then + if not db.execute_with("UPDATE part_answers SET value = ? WHERE id_part = ? AND id_ans = ?", j, id, i.id) then print_error "Opportunity error while updating part_answers {id}|{i.id} = {j}" print_error db.error or else "Unknown error" return false end continue end - if not db.execute("INSERT INTO part_answers(id_part, id_ans, value) VALUES ({id},{i.id},{val});") then + if not db.execute_with("INSERT INTO part_answers (id_part, id_ans, value) VALUES (?, ?, ?)", id, i.id, val) then print_error "Opportunity error while adding part_answers {id}|{i.id}|{j}" print_error db.error or else "Unknown error" return false @@ -226,7 +226,7 @@ class Meetup if id == "" then return new Array[Answer] end - var res = db.select("id, name FROM answers WHERE meetup_id={id.to_sql_string}") + var res = db.select_with("id, name FROM answers WHERE meetup_id = ?", id) var ans = new Array[Answer] for i in res do ans.add new Answer.from_db(i[0].to_i, i[1].to_s) @@ -236,7 +236,7 @@ class Meetup # Gets the list of the participants of a `Meetup` fun participants(db: OpportunityDB): Array[People] do - var resp = db.select("people.* FROM people, meetups, answers, part_answers WHERE meetups.id={id.to_sql_string} AND answers.meetup_id={id.to_sql_string} AND part_answers.id_ans=answers.id AND people.id=part_answers.id_part GROUP BY people.id;") + var resp = db.select_with("people.* FROM people, meetups, answers, part_answers WHERE meetups.id = ? AND answers.meetup_id = ? AND part_answers.id_ans = answers.id AND people.id = part_answers.id_part GROUP BY people.id", id, id) var arr = new Array[People] for i in resp do arr.add (new People.from_db(i[0].to_i, i[1].to_s, i[2].to_s)) @@ -248,7 +248,7 @@ class Meetup if id == "" then var time = get_time var tmpid = (name + date + place + time.to_s).sha1.hexdigest - if not db.execute("INSERT INTO meetups (id, name, date, place, answer_mode) VALUES({tmpid.to_sql_string}, {name.html_escape.to_sql_string}, {date.html_escape.to_sql_string}, {place.html_escape.to_sql_string}, {answer_mode});") then + if not db.execute_with("INSERT INTO meetups (id, name, date, place, answer_mode) VALUES (?, ?, ?, ?, ?)", tmpid, name.html_escape, date.html_escape, place.html_escape, answer_mode) then print_error "Opportunity error recording entry Meetup {self}" print_error db.error or else "Unknown error" return false @@ -256,7 +256,7 @@ class Meetup id = tmpid return true else - return db.execute("UPDATE meetups SET name={name.html_escape.to_sql_string}, date={date.html_escape.to_sql_string}, place={place.html_escape.to_sql_string}, answer_mode={answer_mode} WHERE ID={id.to_sql_string};") + return db.execute_with("UPDATE meetups SET name = ?, date = ?, place = ?, answer_mode = ? WHERE id = ?", name.html_escape, date.html_escape, place.html_escape, answer_mode, id) end end @@ -290,7 +290,7 @@ class Answer # REQUIRE: is loaded in database fun load_meetup(db: OpportunityDB): Meetup do assert id != -1 - var res = db.select("meetups.* FROM meetups, answers WHERE answers.id={id} AND answers.meetup_id=meetups.id;") + var res = db.select_with("meetups.* FROM meetups, answers WHERE answers.id = ? AND answers.meetup_id = meetups.id", id) for i in res do return new Meetup.from_db(i[0].to_s, i[1].to_s, i[2].to_s, i[3].to_s, i[4].to_i) end @@ -302,7 +302,7 @@ class Answer fun count(db: OpportunityDB): Int do if id == -1 then return -1 var count = 0 - var res = db.select("part_answers.value FROM part_answers WHERE part_answers.id_ans={id};") + var res = db.select_with("part_answers.value FROM part_answers WHERE part_answers.id_ans = ?", id) if meetup == null then meetup = load_meetup(db) for i in res do if meetup.answer_mode == 0 then @@ -318,7 +318,7 @@ class Answer fun score(db: OpportunityDB): Int do if id == -1 then return -1 var score = 0 - var res = db.select("part_answers.value FROM part_answers WHERE part_answers.id_ans={id};") + var res = db.select_with("part_answers.value FROM part_answers WHERE part_answers.id_ans = ?", id) for i in res do score += i[0].to_i end @@ -335,14 +335,14 @@ class Answer end end if id == -1 then - if not db.execute("INSERT INTO answers (name, meetup_id) VALUES({name.html_escape.to_sql_string}, {m.id.to_sql_string});") then + if not db.execute_with("INSERT INTO answers (name, meetup_id) VALUES (?, ?)", name.html_escape, m.id) then print_error "Opportunity error creating {self} in database" print_error db.error or else "Unknown error" return false end id = db.last_insert_rowid else - if not db.execute("UPDATE answers SET name=({name.html_escape.to_sql_string}) WHERE meetup_id={m.id.to_sql_string};") then + if not db.execute_with("UPDATE answers SET name = ? WHERE meetup_id = ?", name.html_escape, m.id) then print_error "Opportunity error updating {self} in database" print_error db.error or else "Unknown error" return false diff --git a/contrib/opportunity/src/templates/languages/fr/LC_MESSAGES/boilerplate.po b/contrib/opportunity/src/templates/languages/fr/LC_MESSAGES/boilerplate.po index ab9b9f7a9a..7443638985 100644 --- a/contrib/opportunity/src/templates/languages/fr/LC_MESSAGES/boilerplate.po +++ b/contrib/opportunity/src/templates/languages/fr/LC_MESSAGES/boilerplate.po @@ -1,3 +1,8 @@ +msgid "" +msgstr "" +"Language: fr\n" +"Content-Type: text/plain; charset=UTF-8\n" + msgid "Opportunity - The meetup planner" msgstr "Opportunité - L'organisateur d'événements" diff --git a/contrib/opportunity/src/templates/languages/fr/LC_MESSAGES/meetup.po b/contrib/opportunity/src/templates/languages/fr/LC_MESSAGES/meetup.po index 4a9861ca53..92f8427f1f 100644 --- a/contrib/opportunity/src/templates/languages/fr/LC_MESSAGES/meetup.po +++ b/contrib/opportunity/src/templates/languages/fr/LC_MESSAGES/meetup.po @@ -1,3 +1,8 @@ +msgid "" +msgstr "" +"Language: fr\n" +"Content-Type: text/plain; charset=UTF-8\n" + msgid "When:" msgstr "Quand:" diff --git a/contrib/opportunity/src/templates/languages/fr/LC_MESSAGES/meetup_confirmation.po b/contrib/opportunity/src/templates/languages/fr/LC_MESSAGES/meetup_confirmation.po index 113e4b1d39..e1f2efdb95 100644 --- a/contrib/opportunity/src/templates/languages/fr/LC_MESSAGES/meetup_confirmation.po +++ b/contrib/opportunity/src/templates/languages/fr/LC_MESSAGES/meetup_confirmation.po @@ -1,3 +1,8 @@ +msgid "" +msgstr "" +"Language: fr\n" +"Content-Type: text/plain; charset=UTF-8\n" + msgid "Your meetup was successfully created" msgstr "Votre événement a été créé avec succès" diff --git a/contrib/opportunity/src/templates/languages/fr/LC_MESSAGES/meetup_creation.po b/contrib/opportunity/src/templates/languages/fr/LC_MESSAGES/meetup_creation.po index 67c749071b..ef2f09c16a 100644 --- a/contrib/opportunity/src/templates/languages/fr/LC_MESSAGES/meetup_creation.po +++ b/contrib/opportunity/src/templates/languages/fr/LC_MESSAGES/meetup_creation.po @@ -1,3 +1,8 @@ +msgid "" +msgstr "" +"Language: fr\n" +"Content-Type: text/plain; charset=UTF-8\n" + #: templates::meetup_creation 52--109:129, templates::meetup_creation 125--24:44 msgid "Another opportunity" msgstr "Une autre opportunité" diff --git a/contrib/opportunity/src/templates/languages/fr/LC_MESSAGES/welcome.po b/contrib/opportunity/src/templates/languages/fr/LC_MESSAGES/welcome.po index 78cbf353e6..470652d27c 100644 --- a/contrib/opportunity/src/templates/languages/fr/LC_MESSAGES/welcome.po +++ b/contrib/opportunity/src/templates/languages/fr/LC_MESSAGES/welcome.po @@ -1,3 +1,8 @@ +msgid "" +msgstr "" +"Language: fr\n" +"Content-Type: text/plain; charset=UTF-8\n" + msgid "Welcome to Opportunity!" msgstr "Bienvenue sur Opportunité!" diff --git a/lib/sqlite3/native_sqlite3.nit b/lib/sqlite3/native_sqlite3.nit index 8e49c9657e..6da8ae58aa 100644 --- a/lib/sqlite3/native_sqlite3.nit +++ b/lib/sqlite3/native_sqlite3.nit @@ -127,6 +127,31 @@ extern class NativeStatement `{sqlite3_stmt*`} return sqlite3_column_count(self); `} + # Number of `?` parameters in this statement + fun bind_parameter_count: Int `{ return sqlite3_bind_parameter_count(self); `} + + # Bind an integer to the parameter `i`, counting from 1 + fun bind_int(i, value: Int): Sqlite3Code `{ return sqlite3_bind_int64(self, i, value); `} + + # Bind a float to the parameter `i`, counting from 1 + fun bind_double(i: Int, value: Float): Sqlite3Code `{ return sqlite3_bind_double(self, i, value); `} + + # Bind a copy of the text `value` to the parameter `i`, counting from 1 + fun bind_text(i: Int, value: CString): Sqlite3Code `{ + return sqlite3_bind_text(self, i, value, -1, SQLITE_TRANSIENT); + `} + + # Bind a copy of `value`, of `length` bytes, to the parameter `i`, counting from 1 + fun bind_blob(i: Int, value: Pointer, length: Int): Sqlite3Code `{ + return sqlite3_bind_blob(self, i, value, length, SQLITE_TRANSIENT); + `} + + # Bind null to the parameter `i`, counting from 1 + fun bind_null(i: Int): Sqlite3Code `{ return sqlite3_bind_null(self, i); `} + + # Reset all parameters to null + fun clear_bindings: Sqlite3Code `{ return sqlite3_clear_bindings(self); `} + # Reset this statement to its original state, to be reexecuted fun reset: Sqlite3Code `{ return sqlite3_reset(self); `} diff --git a/lib/sqlite3/sqlite3.nit b/lib/sqlite3/sqlite3.nit index daa980b623..d28e0a8406 100644 --- a/lib/sqlite3/sqlite3.nit +++ b/lib/sqlite3/sqlite3.nit @@ -16,6 +16,19 @@ # Services to manipulate a Sqlite3 database # +# ~~~ +# var db = new Sqlite3DB.open(":memory:") +# +# assert db.create_table("users (name TEXT, age INTEGER)") +# assert db.insert_with("INTO users VALUES (?, ?)", "Bob", 42) +# +# for row in db.select_with("age FROM users WHERE name = ?", "Bob") do +# assert row[0].to_i == 42 +# end +# +# db.close +# ~~~ +# # For more information, refer to the documentation of http://www.sqlite.org/docs.html module sqlite3 @@ -65,6 +78,28 @@ class Sqlite3DB return stmt end + # Prepare a statement from `sql`, binding `values` to `?` placeholders + # + # Returns the `Statement` or `null` on error. + # + # ~~~ + # var db = new Sqlite3DB.open(":memory:") + # var stmt = db.prepare_with("SELECT ? + ?", 1, 2) + # assert stmt != null + # for row in stmt do assert row[0].to_i == 3 + # db.close + # ~~~ + fun prepare_with(sql: Text, values: nullable Sqlite3Data...): nullable Statement + do + var stmt = prepare(sql) + if stmt == null then return null + if not stmt.bind_all(values) then + stmt.close + return null + end + return stmt + end + # Execute the `sql` statement and return `true` on success fun execute(sql: Text): Bool do @@ -72,26 +107,71 @@ class Sqlite3DB return err.is_ok end - # Create a table on the DB with a statement beginning with "CREATE TABLE ", followed by `rest` + # Execute `sql` binding `values` to `?` placeholders + # + # Returns whether the statement succeeded. + # + # ~~~ + # var db = new Sqlite3DB.open(":memory:") + # assert db.create_table("users (name TEXT)") + # assert db.execute_with("INSERT INTO users VALUES (?)", "Bob") + # assert not db.execute_with("INSERT INTO users VALUES (?, ?)", "Bob") + # db.close + # ~~~ + fun execute_with(sql: Text, values: nullable Sqlite3Data...): Bool + do + var stmt = prepare_with(sql, values...) + if stmt == null then return false + + var err = stmt.native_statement.step + stmt.close + return err.is_done or err.is_row + end + + # Create a table with a statement beginning with "CREATE TABLE ", followed by `rest` # # This method does not escape special characters. fun create_table(rest: Text): Bool do return execute("CREATE TABLE " + rest) - # Insert in the DB with a statement beginning with "INSERT ", followed by `rest` + # Insert using a statement beginning with "INSERT ", followed by `rest` # - # This method does not escape special characters. + # This method does not escape special characters, see `insert_with`. fun insert(rest: Text): Bool do return execute("INSERT " + rest) - # Replace in the DB with a statement beginning with "REPLACE", followed by `rest` + # Insert with "INSERT " followed by `rest`, binding `values` to `?` placeholders # - # This method does not escape special characters. + # Returns whether the statement succeeded. + fun insert_with(rest: Text, values: nullable Sqlite3Data...): Bool + do + return execute_with("INSERT " + rest, values...) + end + + # Replace using a statement beginning with "REPLACE ", followed by `rest` + # + # This method does not escape special characters, see `replace_with`. fun replace(rest: Text): Bool do return execute("REPLACE " + rest) - # Select from the DB with a statement beginning with "SELECT ", followed by `rest` + # Replace with "REPLACE " followed by `rest`, binding `values` to `?` placeholders # - # This method does not escape special characters. + # Returns whether the statement succeeded. + fun replace_with(rest: Text, values: nullable Sqlite3Data...): Bool + do + return execute_with("REPLACE " + rest, values...) + end + + # Select with a statement beginning with "SELECT ", followed by `rest` + # + # This method does not escape special characters, see `select_with`. fun select(rest: Text): nullable Statement do return prepare("SELECT " + rest) + # Select with "SELECT " followed by `rest`, binding `values` to `?` placeholders + # + # Returns the `Statement` or `null` on error. + fun select_with(rest: Text, values: nullable Sqlite3Data...): nullable Statement + do + return prepare_with("SELECT " + rest, values...) + end + # TODO add more prefix here as needed # The latest error message, or `null` if there is none @@ -142,6 +222,60 @@ class Statement native_statement.finalize end + # Bind `value` to the parameter at `index`, counting from 1 + # + # Returns whether the binding succeeded. + # + # ~~~ + # var db = new Sqlite3DB.open(":memory:") + # var stmt = db.prepare("SELECT ?, ?, ?, ?") + # assert stmt != null + # assert stmt.bind(1, 1) + # assert stmt.bind(2, 2.5) + # assert stmt.bind(3, "three") + # assert stmt.bind(4, null) + # assert not stmt.bind(5, 5) + # for row in stmt do + # assert row[0].value == 1 + # assert row[1].value == 2.5 + # assert row[2].value == "three" + # assert row[3].value == null + # end + # db.close + # ~~~ + fun bind(index: Int, value: nullable Sqlite3Data): Bool + do + assert is_open else print_error "Error: bind on a closed statement" + + if value == null then return native_statement.bind_null(index).is_ok + + return value.bind_to(self, index) + end + + # Bind individual `values` to the parameters + # + # Returns whether all the bindings succeeded. + fun bind_all(values: SequenceRead[nullable Sqlite3Data]): Bool + do + assert is_open else print_error "Error: bind_all on a closed statement" + + var len_match = values.length == native_statement.bind_parameter_count + if not len_match then return false + + for i in [0 .. values.length[ do + if not bind(i + 1, values[i]) then return false + end + + return true + end + + # Reset all parameters to null + fun clear_bindings + do + assert is_open else print_error "Error: clear_bindings on a closed statement" + native_statement.clear_bindings + end + # Reset this statement and return a `StatementIterator` to iterate over the result fun iterator: StatementIterator do @@ -312,13 +446,36 @@ class StatementIterator end # A data type supported by Sqlite3 -interface Sqlite3Data end +interface Sqlite3Data + private fun bind_to(statement: Statement, index: Int): Bool is abstract +end -redef universal Int super Sqlite3Data end +redef universal Int + super Sqlite3Data -redef universal Float super Sqlite3Data end + redef fun bind_to(statement, index) + do + return statement.native_statement.bind_int(index, self).is_ok + end +end -redef class String super Sqlite3Data end +redef universal Float + super Sqlite3Data + + redef fun bind_to(statement, index) + do + return statement.native_statement.bind_double(index, self).is_ok + end +end + +redef class String + super Sqlite3Data + + redef fun bind_to(statement, index) + do + return statement.native_statement.bind_text(index, to_cstring).is_ok + end +end redef class Text @@ -358,4 +515,9 @@ class Blob # Size of the blob var length: Int + + redef fun bind_to(statement, index) + do + return statement.native_statement.bind_blob(index, pointer, length).is_ok + end end