coldfusion cfquery.getMetaData() - coldfusion

I have a simple cfquery object:
<cfquery name="getP" datasource="DS">
select top 5 * from table
</cfquery>
When I do a dump of getP.getMetaData(), I clearly see getPrecision(int) in the method list. Though, when I perform getP.getMetaData().getPrecision(1), I get an error:
java.lang.UnsupportedOperationException: getPrecision()
What am I doing wrong?
The error:

The javadocs say it's not supported yet, hence it throws that error.
http://sqlitejdbc.sourceforge.jp/org/sqlite/jdbc/JdbcParameterMetaData.html

Related

Why doesn't Lucee consider column alias name in QoQ

I was comparing the following queries in my local CF & Lucee servers. The ColdFusion server throws a QoQ runtime error. However, when I execute the same queries in Lucee, it returns the needed query results.
<cfquery name="getusers" datasource="myDSN">
SELECT
UC.UserContactName, U.UserID
FROM Users U
INNER JOIN UserContacts UC ON U.UserID = UC.UserID
WHERE U.UserID in (99,52,41,76,15)
</cfquery>
<cfquery name="getContactName" dbtype="query">
SELECT UserContactName FROM getusers
WHERE U.UserID = <cfqueryparam value="76" cfsqltype="cf_sql_integer">
</cfquery>
<cfdump var="#getContactName#" />
The CF server throws this error on the above code, because it considers the alias name:
The selected column reference U.UserID does not match any table in the
FROM table list.
However, the Lucee server doesn't consider the alias name, ran runs the above without error. Does anyone know why the Lucee server does not consider the QoQ column alias name? Please share your thoughts or suggestions about this.
The getusers query results in the columns:
UserContactName | UserID
A QoQ on getusers no longer has any table related information stored. If you reference U (as in U.UserID), ACF expects a (new) reference in the QoQ to be able to resolve what U even means.
Lucee on the other hand stores these information and can resolve it using an alias. You can check the source code how it parses the QoQ statement.
Regardless of the capabilities of Lucee, I suggest to remove former aliases in QoQ statements. Consider debugging a QoQ statement when your query dump doesn't even hint you about stored aliases.

PreparedStatement.setNString(int,java.lang.String) error trying to use Query of Queries

I have a simple Query of Queries statement like this:
<cfquery name="rsProductTypeID" dbtype="query">
SELECT
ProductTypeID
FROM
rsProductTypes
WHERE
ProductType = <cfqueryparam value="Computers" cfsqltype="cf_sql_nvarchar"/>;
</cfquery>
All it is doing is asking for the primary key value (ProductTypeID) from the recordset rsProductType depending on which ProductType we are asking for.
For some reason the above query gives me this error:
PreparedStatement.setNString(int,java.lang.String)
But if I change the Query of Queries and remove the <cfqueryparam> part and replace it with normal text, then it works fine. Here is the working code:
<cfquery name="rsProductTypeID" dbtype="query">
SELECT
ProductTypeID
FROM
rsProductTypes
WHERE
ProductType = 'Computers';
</cfquery>
I can't tell what the difference is between the code that is causing it to fail when using <cfqueryparam> and not.
I think I figured it out... when ColdFusion returns a recordset, it doesn't maintain the original datatype from the database. In my database ProductType is defined as nvarchar(50) but ColdFusion returns it as varchar format. So the cfsqltype attribute was causing the error. I changed the cfsqltype attribute to cf_sql_varchar and it works fine. Very annoying but what can you do.
I posted it as a self-answer in case anyone else gets this error. Not sure if that's against the rules or not.
cf_sql_nvarchar is not a valid value for the cfsqltype attribute of cfqueryparam
Docs: https://wikidocs.adobe.com/wiki/display/coldfusionen/cfqueryparam

CF QoQ is throwing runtime error. "Column reference is not a column in any of the tables of the FROM table list."

In my code, I first create the Query Object:
<cfset memberData = QueryNew('slug,pos,firstname,lastname,email') />
<cfset temp = QueryAddRow(memberData, #numMembers#) />
<!--- LOOP POPULATES QUERY OBJECT --->
<cfloop...</cfloop>
I can then verify that it has been populated by running the following (which outputs as expected):
<cfoutput query="memberData">
#slug# - #pos#<br>
</cfoutput>
I then try to query the memberData Query Object and all hell breaks loose. If I run:
<cfquery name="members" dbtype="query">
SELECT slug,pos,firstname,lastname
FROM memberData
WHERE slug = #slug#
</cfquery>
I get this error:
Query Of Queries runtime error.
The select column reference [university] is not a column in any of the tables of the FROM table list.
In the output test mentioned above, I can verify that "university" is one of the values in the slug column. Clearly I'm missing something in my approach, but I'm baffled as to what it might be. Any help would be greatly appreciated!
Query Of Queries runtime error.
The select column reference
[university] is not a column in any of the tables of the FROM table
list
Your error was caused by absence of quotes in where clause and nothing else:
This expression find rows where the value in slug column equals the value in CF slug variable (String):
WHERE slug = '#slug#'
On the other hand this expression means find rows where value in the slug column equals the value contained in the column named in the CF slug variable (String):
WHERE slug = #slug#
The most likely cause of why you needed to change to SELECT * is CF query caching. So changing it back now should solve the problem. And always use <cfqueryparam .../> as suggested by "Al Everett"
Well, it's not quite answering the question asked, but it's close enough for what I needed:
<cfquery name="members" dbtype="query">
SELECT *
FROM memberData
WHERE slug = '#slug#'
</cfquery>
I had tried the wrapping #slug# in single quotes prior to posting with no success, but doing that plus changing the query to SELECT * fixed the issue. For my content, * only adds one more value retrieved, so not really a problem in slowing down the process.

cfqueryparam with uniqueidentifier type in table

I have a query where I am attempting to insert values into a table and one of these values (ImportID) is of type uniqueidentifier in the database. I have looked in the adobe CF documentation and saw that both cf_sql_char and cf_sql_idstamp should work for the cfquery param for my uniqueidentifier type. I also read this thread which says the same thing: What is the most appropriate Coldfusion cfsqltype to use for MS SQL's uniqueidentifier field type?
I have found a few other places that use the "maxlength" parameter for the cfqueryparam tag and cf_sql_char as the type but have not been able to make my code work. I keep getting an error along the lines of "Conversion failed when converting from a character string to uniqueidentifier." I'm including my code where I generate the UID and attempt the insert below (I replaced some of the variable names and took out all of the other columns for the import):
<cfscript>
ImportID = createUUID();
</cfscript>
<cfquery name="INSERTTableName" datasource="#Application.Datasource#">
INSERT INTO TableNameImport(
ImportID
)
VALUES(
<cfqueryparam value="#variables.ImportID#" cfsqltype="CF_SQL_CHAR">
)
</cfquery>
Any help would be greatly appreciated. Thanks in advance.
*********************************EDIT********************************
Found the answer:
<cfquery name="INSERTTableName" datasource="#Application.Datasource#">
DECLARE #ImportID uniqueidentifier
SET #ImportID = NEWID()
INSERT INTO TableNameImport(
ImportID
)
VALUES(
#ImportID
)
</cfquery>
The problem was that the UUID created from the coldfusion call is a different format from the one created in Microsoft SQL Server.
You are correct. SQL uses a GUID and createUUID manks a UUID. Both are 32hex value but use a differnt text format (location of dashes). And the use a different calculation to generate the random value. You can utilize UUID's if you change the text formating to have the GUID dash locations or send the binary value. For your example above that works you might also want return the GUID if cf needs a reference to that value.

Coldfusion Undefined Variables - but they're not

I hope I'm missing something really obvious here, because for the life of me I can't figure this one out at all!
I have a cfquery with some joins as follows;
SELECT f.*, p.ID AS prID, p.product_name, p.shortname, i.newthumb,
(SELECT AVG(reviewrating) FROM product_reviews AS pr WHERE pr.productid=p.id) AS reviewrating,
(SELECT description FROM product_descriptions AS d WHERE d.productid=p.id LIMIT 1) AS description
FROM followers_p f
LEFT JOIN products p
ON f.productID=p.ID
LEFT JOIN product_images i
ON i.productid=p.ID
WHERE f.wUserID='#getuser.wUserID#'
ORDER BY f.cID DESC
That executes fine and a cfdump of the query shows data is returned as it should be.
Further down the page, I'm running the cfoutput query. However, it's giving me an error 'Variable NEWTHUMB is undefined. '
I've tried grabbing the image as a subquery with Limit 1 and a few other things, but the result is always the same - cfdump shows the correct data, cfoutput gives an undefined error.
Anything else I can try to figure this one out? I've noticed a similar issue occurring on another page too.
Edit - the cfoutput code;
<cfoutput query="getproductfollow">
<img alt="#product_name#" src="#newthumb#">
#product_name#</span>
</cfoutput>
The name of your query isn't in your code, so I can't guarantee this is correct. It seems to be that your error is in your reference to your newthumb variable.
Your query should look like this:
<cfquery name="getproductfollow">
// SQL GOES HERE
</cfquery>
Your output should look like this:
<cfoutput query="getproductfollow">
<img alt="#product_name#" src="#newthumb#">
#product_name#</span>
</cfoutput>
You should show your ENTIRE query, wrapped in the as well as your . That would be really helpful.