I'm trying to debug a cf query and cannot do this because of his complex structure.The code is following:
<cfquery name="qQuery" datasource="#variables.datasource#">
<cfloop index="i" from="1" to="#ArrayLen(aSQL)#" step="1">
<cfif IsSimpleValue(aSQL[i])>
<cfset temp = aSQL[i]>#Trim(DMPreserveSingleQuotes(temp))#
<cfelseif IsStruct(aSQL[i])>
<cfset aSQL[i] = queryparam(argumentCollection=aSQL[i])>
<cfswitch expression="#aSQL[i].cfsqltype#">
<cfcase value="CF_SQL_BIT">
#getBooleanSqlValue(aSQL[i].value)#
</cfcase>
<cfcase value="CF_SQL_DATE,CF_SQL_DATETIME">
#CreateODBCDateTime(aSQL[i].value)#
</cfcase>
<cfdefaultcase>
<!--- <cfif ListFindNoCase(variables.dectypes,aSQL[i].cfsqltype)>#Val(aSQL[i].value)#<cfelse> --->
<cfqueryparam value="#aSQL[i].value#" cfsqltype="#aSQL[i].cfsqltype#" maxlength="#aSQL[i].maxlength#" scale="#aSQL[i].scale#" null="#aSQL[i].null#" list="#aSQL[i].list#" separator="#aSQL[i].separator#">
<!--- </cfif> --->
</cfdefaultcase>
</cfswitch>
</cfif>
</cfloop>
</cfquery>
If I run <cfdump var="#qQuery#"> it's not working nor cfoutput, I get undefined qQuery error. How can I find what query is executing behind ? I don't want to use MS SQL profiler.
Thanks,
Take everything inside the query and wrap it in a cfsavecontent instead. Output that result.
If you place the cfsavecontent inside the cfquery tags, you don't even need to worry about the cfqueryparam tags barfing, although you do need to re-output that saved content inside the query. See http://coldflint.blogspot.com/2016/01/debugging-queries-dirty-way.html
Basically, you should have this:
<cfquery name="qQuery" datasource="#variables.datasource#">
<cfsavecontent variable="sqlContent">
<cfloop index="i" from="1" to="#ArrayLen(aSQL)#" step="1">
<cfif IsSimpleValue(aSQL[i])>
<cfset temp = aSQL[i]>#Trim(DMPreserveSingleQuotes(temp))#
<cfelseif IsStruct(aSQL[i])>
<cfset aSQL[i] = queryparam(argumentCollection=aSQL[i])>
<cfswitch expression="#aSQL[i].cfsqltype#">
<cfcase value="CF_SQL_BIT">
#getBooleanSqlValue(aSQL[i].value)#
</cfcase>
<cfcase value="CF_SQL_DATE,CF_SQL_DATETIME">
#CreateODBCDateTime(aSQL[i].value)#
</cfcase>
<cfdefaultcase>
<!--- <cfif ListFindNoCase(variables.dectypes,aSQL[i].cfsqltype)>#Val(aSQL[i].value)#<cfelse> --->
<cfqueryparam value="#aSQL[i].value#" cfsqltype="#aSQL[i].cfsqltype#" maxlength="#aSQL[i].maxlength#" scale="#aSQL[i].scale#" null="#aSQL[i].null#" list="#aSQL[i].list#" separator="#aSQL[i].separator#">
<!--- </cfif> --->
</cfdefaultcase>
</cfswitch>
</cfif>
</cfloop>
</cfsavecontent>
#sqlContent#
</cfquery>
<pre>#sqlContent#</pre>
Do make sure to put everything back to normal once you're done debugging.
If this question is more about HOW to debug or get some output you can work with, cftry and cfcatch are your friends.
<cftry>
---code logic---
<cfcatch>
<cfdump var="#cfcatch#">
</cfcatch>
</cfctry>
This should provide a complete dump of whatever errors ColdFusion encounters as well as SQL statements that were attempted, if there is indeed a syntax error generated by the loopy logic.
You have to work from the inside out this.
As I look at this query, I note that it is split on IsSimpleValue() and IsStruct(). So run this
<cfquery name="qQuery" datasource="#variables.datasource#">
<cfloop index="i" from="1" to="#ArrayLen(aSQL)#" step="1">
<cfif IsSimpleValue(aSQL[i])>
<cfset temp = aSQL[i]>#Trim(DMPreserveSingleQuotes(temp))#
</cfif>
</cfloop>
</cfquery>
Note that temp is never used.
The rest of the code creates <cfqueryparam>s
Conclusion
This code cannot work. It cannot create valid SQL.
Your error has nothing to do with the sql being generated by the code in the cfquery block. It's an undefined variable. If there was a problem with the code you displayed, the error message would be different.
Troublehoot as follows. First comment out all the code inside that query and replace it with:
select 1 record
This is valid for MS SQL. Leave everything else unchanged.
Running the page will produce the same error. You then have to determine why the query is not running. Likely, you have something like this going on:
<cfif some Condition is met>
run the query
</cfif>
dump the query
You'll have to determine why your condition wasn't met and make sure your page runs properly when it isn't.
The solution was to put <cftry> outside <cfloop> but not outside <cfquery>.I found that I forgot to send one parametter.
So the code is following:
<cfquery name="qQuery" datasource="#variables.datasource#">
<cftry>
<cfloop index="i" from="1" to="#ArrayLen(aSQL)#" step="1">
<cfif IsSimpleValue(aSQL[i])>
<cfset temp = aSQL[i]>#Trim(DMPreserveSingleQuotes(temp))#
<cfelseif IsStruct(aSQL[i])>
<cfset aSQL[i] = queryparam(argumentCollection=aSQL[i])>
<cfswitch expression="#aSQL[i].cfsqltype#">
<cfcase value="CF_SQL_BIT">
#getBooleanSqlValue(aSQL[i].value)#
</cfcase>
<cfcase value="CF_SQL_DATE,CF_SQL_DATETIME">
#CreateODBCDateTime(aSQL[i].value)#
</cfcase>
<cfdefaultcase>
<cfqueryparam value="#aSQL[i].value#" cfsqltype="#aSQL[i].cfsqltype#" maxlength="#aSQL[i].maxlength#" scale="#aSQL[i].scale#" null="#aSQL[i].null#" list="#aSQL[i].list#" separator="#aSQL[i].separator#">
</cfdefaultcase>
</cfswitch>
</cfif>
</cfloop>
<cfcatch>
<cfdump var="#cfcatch#" >
</cfcatch>
</cftry>
</cfquery
Related
I am trying to use cfpdf and keep getting the following error:
String index out of range: -1
I don't understand why. I'm running ColdFusion 11 on Debian Linux.
<CFIF FileExists("#getDirectoryFromPath(getCurrentTemplatePath())#REPORT.pdf")>
<cfpdfform
action="read"
source="#getDirectoryFromPath(getCurrentTemplatePath())#REPORT.pdf" xmldata="x"
result="r">
</cfpdfform>
<cfdump var="#x#" label="XMLData">
<cfdump var="#r#" label="Result">
<CFELSE>
File doesn't exist
</CFIF>
Exact error:
String index out of range: -1
The error occurred in /var/www/www.test.com/test.cfm: line 2
1 : <CFIF FileExists("#getDirectoryFromPath(getCurrentTemplatePath())#REPORT.pdf")>
2 : <cfpdfform
action="read"
source="#getDirectoryFromPath(getCurrentTemplatePath())#REPORT.pdf"
xmldata="x" result="r">
3 : </cfpdfform>
4 : <cfdump var="#x#" label="XMLData">
I have had similar issues in the past. The problem has been using non Adobe programs to create the pdf - making a pdf form in Libreoffice, for example, can cause this issue. I assume some internal formatting in the file that CF is looking for is missing.
You can try opening in Acrobat and resaving it.
Having a bit of trouble inserting a LIKE and using a wildcard within my parameter in my query using coldfusion. Here is what currently works:
<cfquery name="sample" datasource="database"><!---Take the query here--->
SELECT * <!---select all--->
FROM table <!---from the table "table"
WHERE
<cfloop from="1" to="#listLen(selectList1)#" index="i">
#ListGetAt(selectList1, i)# = <cfqueryparam cfsqltype="cf_sql_varchar" value="#ListGetAt(selectList2,i)#" /> <!---
search column name = query parameter
using the same index in both lists
(selectList1) (selectList2) --->
<cfif i neq listLen(selectList1)>AND</cfif> <!---append an "AND" if we are on any but
the very last element of the list (in that
case we don't need an "AND"--->
</cfloop>
</cfquery>
My goal is to get the code somewhere where I can type in a parameter like so (but it is not working currently - Error reads cannot execute query)
<cfquery name="sample" datasource="database"><!---Take the query here--->
SELECT * <!---select all--->
FROM table <!---from the table "table"
WHERE
<cfloop from="1" to="#listLen(selectList1)#" index="i">
#ListGetAt(selectList1, i)# LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#ListGetAt(selectList2,i)#%" /> <!---
search column name = query parameter
using the same index in both lists
(selectList1) (selectList2) --->
<cfif i neq listLen(selectList1)>AND</cfif> <!---append an "AND" if we are on any but
the very last element of the list (in that
case we don't need an "AND"--->
</cfloop>
</cfquery>
Your syntax looks fine. I use the same kind of logic in some of my code running against Microsoft SQL Server.
Try this to debug your code and see the SQL that is being generated.
<!--- Comment out your query tag to debug <cfquery name="sample" datasource="database"> --->
<cfoutput> <!--- add a cfoutput tag to "see" your generated code --->
SELECT *
FROM table
WHERE
<cfloop from="1" to="#listLen(selectList1)#" index="i">
#ListGetAt(selectList1, i)# LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#ListGetAt(selectList2,i)#%" />
<cfif i neq listLen(selectList1)>AND</cfif>
</cfloop>
</cfoutput> <!--- add a cfoutput tag to "see" your generated code --->
<!--- Comment out your query tag to debug </cfquery> --->
This will most likely break the rest of your code that is dependent on the query. I usually just stick a <cfabort> tag after the closing </cfoutput> tag to stop processing and just show me the SQL.
The purpose of this is to output the generated SQL code to your browser. Look it over and see if you can spot the error. You could also copy and paste the generated SQL into your query tool (query analyzer) and test it that way.
My error report told me that an error has occurred when an user tried uploading an empty file to my server (don't ask why the user did that - I don't know) and now I want to catch that exception which said "No data was received in the uploaded file". I wonder if there is a better way than putting a <CFTRY> around the <CFFILE action="upload"> like this:
<CFTRY>
<CFFILE action="upload" destination="#expandpath("upload")#" filefield="form.file" nameconflict="makeunique" />
<CFCATCH>
<!--- handle that error --->
</CFCATCH>
</CFTRY>
Try/Catch is the way I usually handle it.
<cftry>
<cffile action="upload" ...>
<cfcatch type="any">
<cfif Find("Saving empty (zero-length) files is prohibited", CFCatch.Detail) GT 0>
<!--- Create a zero length file on disk and continue processing as usual --->
<cffile action="write" file="..." output="">
<cfelse>
<cfrethrow>
</cfif>
</cfcatch>
</cftry>
So, I'm trying to migrate a database from Textpattern CMS to something more generic. There are some textpattern-specific commands inside of articles that pull in images. I want to turn these into generic HTML image links. At the moment, they look like this in the sql file:
<txp:upm_image image_id="4" form="dose" />
I want to turn these into something more like this:
<img src="4.jpg" class="dose" />
I've had some luck with TextWrangler doing some regex stuff, but I'm stumped. Any ideas on how to find & replace all of these image paths?
EDIT:
For future reference, here's what I ended up doing in PHP to output it:
$body = $post['Body_html'];
$pattern = '/txp:upm_image image_id="([0-9]+)" form="([^"]*)"/i';
$replacement = 'img src="/images/$1.jpg" class="$2"';
$body = preg_replace($pattern, $replacement, $body);
// outputed <img src="/images/59.jpg" class="dose" />
I wouldn't use grep; it's sed you want
$ echo '<txp:upm_image image_id="4" form="dose" />' | sed -e 's/^.*image_id="\([[:digit:]]*\)".*form="\([[:alpha:]]*\)".*/<img src="\1.jpg" class="\2" \/>/'
<img src="4.jpg" class="dose" />
$
if your class has alphanumeric characters, use [[:alnum:]]
(works on macos darwin)
Not sure which tool you are using but try this regex solution: Search for this:
<txp:upm_image\s+image_id="(\d+)"\s+form="([^"]*)"\s*\/>
And replace with this:
<img src="$1.jpg" class="$2" />
Note that this only works for txp tags having the same form as your example. It will fail if there are txp tags having extra attributes, or if they are in a different order.
Am I missing something obvious? The docos state that:
-htmlSuite : Run a
single HTML Selenese (Selenium Core) suite and then exit
immediately ...
but my instance doesn't seem to (it also doesn't write to the html output either)
I am running : java -jar /opt/selenium-server-1.0.3/selenium-server.jar -debug -htmlSuite *googlechrome http://www.google.co.uk ~/tsg1.ts ~/g.out
I have also given up trying to put xml into this editor - but all I am doing is opening www.google.co.uk and looking for "about google" - which works fine in the firefox ide, but never exits on RC... Any ideas ?! The same happens with *firefox as well :-(
Ok - I dug around and found the following.
The selenium IDE generates an ERRONEOUS line in the "heading" which somehow breaks the -htmlsuite option (ie it never exits). Comment the line out as per this example :
`
< table id="suiteTable" cellpadding="1" cellspacing="1" border="1" class="selenium" >
< tbody >
< !-- < tr >< td >< b > Test Suite< / b >< /td >< /tr > -- >
< tr >< td >< a href="test_case_google" >test_case_google< /a >< /td >< /tr >
< /tbody >< /table >
< /body >
< /html >
`
Try with quotes:
java -jar /opt/selenium-server-1.0.3/selenium-server.jar -debug -htmlSuite '*googlechrome" "http://www.google.co.uk" "~/tsg1.ts" "~/g.out"
I only have this problem with firefox. Running with chrome seems to work fine.
If you do what Richard Green suggests you only get to run one test (the last one it seems). The "Test Suite" line tells selenium that it is a test suite and needs to run all the tests present.
So the problem seems to be that in firefox at least it wont jump from test to test when running a test suite.
For firefox if you do what is described in this issue: https://code.google.com/p/selenium/issues/detail?id=1759 it works. Chrome works too.
If by any chance the link doesn't work the workaround is to remove the first two lines of the files:
... yanked out the first two lines of each file. The ones starting with:
xml version...
DOCTYPE html PUBLIC...
You "must" save all your html test with ".html" extension and reference them in testSuit.html with the ".html" extension.
So, instead of:
<table id="suiteTable" cellpadding="1" cellspacing="1" border="1" class="selenium">
<tbody>
<tr><td><b>Test Suite</b></td></tr>
<tr><td>test_case_google</td></tr>
</tbody>
</table>
It "must" be:
<table id="suiteTable" cellpadding="1" cellspacing="1" border="1" class="selenium">
<tbody>
<tr><td><b>Test Suite</b></td></tr>
<tr><td>test_case_google</td></tr>
</tbody>
</table>