{"id":360,"date":"2008-08-21T01:47:28","date_gmt":"2008-08-21T05:47:28","guid":{"rendered":"http:\/\/www.rakkar.org\/blog\/?p=360"},"modified":"2008-08-21T01:47:28","modified_gmt":"2008-08-21T05:47:28","slug":"sql-queries-with-varidic-argument-lists","status":"publish","type":"post","link":"https:\/\/rakkar.org\/blog\/index.php\/2008\/08\/21\/sql-queries-with-varidic-argument-lists\/","title":{"rendered":"SQL queries with varidic argument lists"},"content":{"rendered":"<p>\t\t\t\tIt takes 11 lines of code to do this query in PostgreSQL with binary parameters, such as non-escaped strings or binary data. Here, I pass a parameter as a string, so have to escape it to prevent SQL injection attacks.<\/p>\n<p>char *outTemp[3];<br \/>\nint outLengths[3];<br \/>\nint formats[3];<br \/>\nformats[0]=PQEXECPARAM_FORMAT_TEXT;<br \/>\nformats[1]=PQEXECPARAM_FORMAT_BINARY; \/\/ Always happens to be binary<br \/>\nformats[2]=PQEXECPARAM_FORMAT_BINARY; \/\/ Always happens to be binary<br \/>\nsprintf(query, &#8220;INSERT INTO FileVersionHistory(applicationID, filename, createFile, changeSetID, userName) VALUES (%i, $1::text,FALSE,%i,&#8217;%s&#8217;);&#8221;, applicationID, changeSetId, GetEscapedString(userName).C_String());<br \/>\noutTemp[0]=deletedFiles.fileList[fileListIndex].filename;<br \/>\noutLengths[0]=(int)strlen(deletedFiles.fileList[fileListIndex].filename);<br \/>\nformats[0]=PQEXECPARAM_FORMAT_TEXT;<br \/>\nresult = PQexecParams(pgConn, query,1,0,outTemp,outLengths,formats,PQEXECPARAM_FORMAT_BINARY);<\/p>\n<p>With my new function, ExecVaridic, it&#8217;s one line of code.<\/p>\n<p>result = ExecVaridic(&#8220;INSERT INTO FileVersionHistory(applicationID, filename, createFile, changeSetID, userName) VALUES (%i, %s, FALSE,%i,%s);&#8221;, applicationID, deletedFiles.fileList[fileListIndex].filename, changeSetId, userName.C_String());<\/p>\n<p>Why has no one thought of this before?<\/p>\n<p>I support binary data too.<\/p>\n<p>result = ExecVaridic(&#8220;INSERT INTO fileVersionHistory(binaryData) VALUES (%c)&#8221;, binaryData, binaryDataLength);<\/p>\n<p>%c is my own extension, to mean binary data followed by length.<\/p>\n<p>No string escaping necessary.<\/p>\n<p>Here&#8217;s the code:<br \/>\n<a HREF=\"http:\/\/raknetjenkinsso.svn.sourceforge.net\/viewvc\/raknetjenkinsso\/trunk\/DependentExtensions\/AutopatcherPostgreRepository\/AutopatcherPostgreRepository.cpp?view=markup\">AutopatcherPostgreRepository.cpp<\/a><\/p>\n<p>See PostgreSQLInterface::ExecVaridic\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It takes 11 lines of code to do this query in PostgreSQL with binary parameters, such as non-escaped strings or binary data. Here, I pass a parameter as a string, so have to escape it to prevent SQL injection attacks. char *outTemp[3]; int outLengths[3]; int formats[3]; formats[0]=PQEXECPARAM_FORMAT_TEXT; formats[1]=PQEXECPARAM_FORMAT_BINARY; \/\/ Always happens to be binary formats[2]=PQEXECPARAM_FORMAT_BINARY; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/posts\/360"}],"collection":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=360"}],"version-history":[{"count":0,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/posts\/360\/revisions"}],"wp:attachment":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}