www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using the result of a comma expression is deprecated

reply Suliman <evermind live.ru> writes:
I am getting deprecation message:
"Using the result of a comma expression is deprecated" on this 
code:

string sqlinsert = (`INSERT INTO usersshapes (userlogin, 
uploading_date, geometry_type, data) VALUES ('%s', '%s', '%s', 
'%s') `, login, uploading_date, geometry_type, data);

What's wrong with it?
Nov 27 2016
parent reply drug007 <drug2004 bk.ru> writes:
On 27.11.2016 14:07, Suliman wrote:
 I am getting deprecation message:
 "Using the result of a comma expression is deprecated" on this code:

 string sqlinsert = (`INSERT INTO usersshapes (userlogin, uploading_date,
 geometry_type, data) VALUES ('%s', '%s', '%s', '%s') `, login,
 uploading_date, geometry_type, data);

 What's wrong with it?
Didn't you miss something like class/structure/function before "(`INSERT..."? What result do you expect?
Nov 27 2016
parent reply Suliman <evermind live.ru> writes:
On Sunday, 27 November 2016 at 11:21:58 UTC, drug007 wrote:
 On 27.11.2016 14:07, Suliman wrote:
 I am getting deprecation message:
 "Using the result of a comma expression is deprecated" on this 
 code:

 string sqlinsert = (`INSERT INTO usersshapes (userlogin, 
 uploading_date,
 geometry_type, data) VALUES ('%s', '%s', '%s', '%s') `, login,
 uploading_date, geometry_type, data);

 What's wrong with it?
Didn't you miss something like class/structure/function before "(`INSERT..."? What result do you expect?
void dbInsert(string login, string uploading_date, string geometry_type, string data) { Statement stmt = conn.createStatement(); string sqlinsert = (`INSERT INTO usersshapes (userlogin, uploading_date, geometry_type, data) VALUES ('%s', '%s', '%s', '%s') `, login, uploading_date, geometry_type, data); stmt.executeUpdate(sqlinsert); scope(exit) stmt.close(); // closing } full code.
Nov 27 2016
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Sunday, 27 November 2016 at 11:49:25 UTC, Suliman wrote:
 On Sunday, 27 November 2016 at 11:21:58 UTC, drug007 wrote:
 On 27.11.2016 14:07, Suliman wrote:
 I am getting deprecation message:
 "Using the result of a comma expression is deprecated" on 
 this code:

 string sqlinsert = (`INSERT INTO usersshapes (userlogin, 
 uploading_date,
 geometry_type, data) VALUES ('%s', '%s', '%s', '%s') `, login,
 uploading_date, geometry_type, data);

 What's wrong with it?
Didn't you miss something like class/structure/function before "(`INSERT..."? What result do you expect?
void dbInsert(string login, string uploading_date, string geometry_type, string data) { Statement stmt = conn.createStatement(); string sqlinsert = (`INSERT INTO usersshapes (userlogin, uploading_date, geometry_type, data) VALUES ('%s', '%s', '%s', '%s') `, login, uploading_date, geometry_type, data); stmt.executeUpdate(sqlinsert); scope(exit) stmt.close(); // closing } full code.
Looks like you forgot a call to format before the opening parenthesis. should be: string sqlinsert = format(`INSERT INTO usersshapes (userlogin, uploading_date, geometry_type, data) VALUES ('%s', '%s', '%s', '%s') `, login, uploading_date, geometry_type, data); because what ends up happening is : string sqlinsert = data; which is almost certainly not what you want.
Nov 27 2016
next sibling parent reply Erik van Velzen <erik evanv.nl> writes:
On Sunday, 27 November 2016 at 12:13:03 UTC, Nicholas Wilson 
wrote:
 On Sunday, 27 November 2016 at 11:49:25 UTC, Suliman wrote:
 On Sunday, 27 November 2016 at 11:21:58 UTC, drug007 wrote:

 	void dbInsert(string login, string uploading_date, string 
 geometry_type, string data)
 	{
 	
 	    Statement stmt = conn.createStatement();
 		string sqlinsert = (`INSERT INTO usersshapes (userlogin, 
 uploading_date, geometry_type, data) VALUES ('%s', '%s', '%s', 
 '%s') `, login, uploading_date, geometry_type, data);
 		stmt.executeUpdate(sqlinsert);
 		scope(exit) stmt.close(); // closing
 	}

 full code.
Looks like you forgot a call to format before the opening parenthesis. should be: string sqlinsert = format(`INSERT INTO usersshapes (userlogin, uploading_date, geometry_type, data) VALUES ('%s', '%s', '%s', '%s') `, login, uploading_date, geometry_type, data); because what ends up happening is : string sqlinsert = data; which is almost certainly not what you want.
As an aside, for security reasons you should use a prepared statement. Also, this is a decent usecase for scope(exit) but it should be put earlier in the function.
Nov 27 2016
parent reply Suliman <evermind live.ru> writes:
 As an aside, for security reasons you should use a prepared 
 statement.
Even if it's server-side code and there is no any iteration with user data (they come as JSON)
 Also, this is a decent usecase for scope(exit) but it should be 
 put earlier in the function.
Am I right understand that `scope(exit)` should be always at top, otherwise it would not work (it's very strange because by the docs it's calling every time when function out of the scopes)?
Nov 27 2016
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 27 November 2016 at 16:42:17 UTC, Suliman wrote:
 Am I right understand that `scope(exit)` should be always at 
 top, otherwise it would not work (it's very strange because by 
 the docs it's calling every time when function out of the 
 scopes)?
No, scope(exit) queues the thing for execution, so it doesn't necessarily need to be at the top. void test() { scope(exit) writeln("1"); writeln("2"); scope(exit) writeln("3"); } That would print "2, 3, 1". The first line queues 1. The second line runs immediately and prints 2. The third line queues 3. When the function returns, all queued things are played back in reverse order. Thus, 3 goes first, then 1. If you returned right after the second line, you would only see "2, 1", since the queuing of the 3 would never happen.
Nov 27 2016
prev sibling parent reply Suliman <evermind live.ru> writes:
 Looks like you forgot a call to format before the opening 
 parenthesis.

 should be:
 string sqlinsert = format(`INSERT INTO usersshapes (userlogin,
  uploading_date, geometry_type, data) VALUES ('%s', '%s', '%s',
  '%s') `, login, uploading_date, geometry_type, data);

 because what ends up happening is :
     string sqlinsert = data;
 which is almost certainly not what you want.
So all string substitute must be called with `format`?
 because what ends up happening is :
     string sqlinsert = data;
 which is almost certainly not what you want.
I thought it's possible to write: string data = "foo" string sqlinsert = data or am I wrong?
Nov 27 2016
parent Mike Parker <aldacron gmail.com> writes:
On Sunday, 27 November 2016 at 16:32:26 UTC, Suliman wrote:
 Looks like you forgot a call to format before the opening 
 parenthesis.

 should be:
 string sqlinsert = format(`INSERT INTO usersshapes (userlogin,
  uploading_date, geometry_type, data) VALUES ('%s', '%s', '%s',
  '%s') `, login, uploading_date, geometry_type, data);
 So all string substitute must be called with `format`?
Yes, except when you are passing the strings to a function that does it for you, such as the write family. The language doesn't replace "%s" with strings, the format function does.
 because what ends up happening is :
     string sqlinsert = data;
 which is almost certainly not what you want.
I thought it's possible to write: string data = "foo" string sqlinsert = data or am I wrong?
No, you are not wrong. That is perfectly valid, but that's not what he meant. Your declaration of sqlinsert was made in such a way that you used several commas to separate different values, the last of which was data. The way the comma operator works, that means sqlinsert would be set to data and all the rest ignored. Consider this: void main() { import std.stdio; string s = ("foo %s","bar"); writeln(s); } This prints "bar" and "foo %s" is ignored. That's how the comma operator works outside of a function parameter list. It's also why you got the deprecation message, as this usage will eventually become illegal.
Nov 27 2016