View previous topic :: View next topic |
Author |
Message |
jeremy_c
Joined: 09 Apr 2005 Posts: 16 Location: Ohio, USA
|
Posted: Tue Apr 26, 2005 10:20 am Post subject: Writef type functionality |
|
|
pragma wrote: |
1)
Code: |
db.queryFetchAll("select * from mytable where x='{0}' and y='{1}'",mystr,mystr2);
|
2) It would be nice to have a 'query' object that can be resued in places, instead of having to wield SQL all the time.
Code: |
Query q = db.createQuery("select * from mytable where x='{0}' and y='{1}'");
Reqsult r = q.fetchAll(mystr,mystr2);
|
|
The first would be nice and would not be hard to accomplish using a va_arg style function. I have looked into this but don't quite understand that va_arg functionality in D yet. I only looked at it briefly thinking it would be a trivial addition. The function could easily just use a prepare call to create a Statement which has the ?, ?, ? and :name:, :age:, :city: functionality, which brings us to #2.
Currently that can be accomplished using 1 of two methods:
Code: | Statement stmt = db.prepare("select * from mytable where x=? and y=?");
stmt.bind(1, mystr);
stmt.bind(2, mystr2);
Result res = stmt.execute(); |
You could also do named parameters:
Code: | Statement stmt = db.prepare("select * from mytable where x=:x: and y=:y:");
stmt.bind("x", myX);
stmt.bind("y", myY);
Result res = stmt.execute(); |
The :name: could be anything you like, it does not have to be 1 character. Also, for those who really like the {1}, {2}, you could just :1:, :2:. The escaping is automatically taken care of here by the overloaded bind functions. Again, if a string is passed, it is escaped and also put in quotes, therefore you don't need: name = ':name:', name = :name: will handle the quoting for you.
As said in earlier posts, this needs to be database dependent, not database independent like it currently is.
Jeremy |
|
Back to top |
|
|
jeremy_c
Joined: 09 Apr 2005 Posts: 16 Location: Ohio, USA
|
Posted: Tue Apr 26, 2005 10:31 am Post subject: Re: Writef type functionality |
|
|
jeremy_c wrote: | The first would be nice and would not be hard to accomplish using a va_arg style function. I have looked into this but don't quite understand that va_arg functionality in D yet. I only looked at it briefly thinking it would be a trivial addition. The function could easily just use a prepare call to create a Statement which has the ?, ?, ? and :name:, :age:, :city: functionality, which brings us to #2.
|
The
Code: | execute("SELECT * FROM names WHERE name = ? AND age = ?", "John Doe", 25); |
could be done something like: (mock code obviously):
Code: |
Result execute(char[] sql, ...) {
Statement stmt = this.query(sql);
foreach (arg; va_args) {
stmt.bind(idx, arg);
idx += 1;
}
return stmt.execute();
}
|
If anyone wants to code that function up and submit a patch, that would be terrific!
Jeremy |
|
Back to top |
|
|
MarkD
Joined: 18 Jul 2005 Posts: 8
|
Posted: Mon Aug 01, 2005 9:50 pm Post subject: |
|
|
This is the function that I'm currently using, so that I can at least go db.execute(combine("SELECT * FROM Table WHERE ID = {0}", x));
Code: | // Mark Delano #8^) (2005)
char[] combine(char[] sql, ...) {
for (int i = 0; i < _arguments.length; i++) {
if (_arguments[i] == typeid(int))
sql = replace(sql, "{" ~ toString(i) ~ "}", toString(va_arg!(int)(_argptr)));
else if (_arguments[i] == typeid(long))
sql = replace(sql, "{" ~ toString(i) ~ "}", toString(va_arg!(long)(_argptr)));
else if (_arguments[i] == typeid(float))
sql = replace(sql, "{" ~ toString(i) ~ "}", toString(va_arg!(float)(_argptr)));
else if (_arguments[i] == typeid(double))
sql = replace(sql, "{" ~ toString(i) ~ "}", toString(va_arg!(double)(_argptr)));
else if (_arguments[i] == typeid(char[]))
sql = replace(sql, "{" ~ toString(i) ~ "}", va_arg!(char[])(_argptr));
else
assert(0);
}
return sql;
}
|
_________________ Mark Delano |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|