i have a query i'm trying to construct via queryne...
# cfml-general
w
i have a query i'm trying to construct via querynew() where i need a couple of columns with spaces in their names. querynew() is (rightly) giving me the finger when i have columns with spaces in the column list being passed to it:
querynew( "colOne,column two")
. has anyone ever tried to do this and found a way to massage cf to recognize these columns? tried quoting and square bracketing the columns in the list but no workee
m
Copy code
<cfscript>

qry = QueryNew("colOne");
QueryAddColumn(qry, "column two", []);
WriteDump(qry);
</cfscript>
w
tried that, doesn't work in ACF
m
lol i had ran trycf as adobe when i failed on your first example and then i accidentally closed the tab and then i didn't switch back to adobe. Sorry.
w
i feel like i can get halfway there by constructing an array of structs, but i still need the final result to have been converted to (and behave like) a standard query object
m
Tried hacking it with Query of Queries but that didn't work as well
Hack coming up...
Copy code
<cfscript>

qry = QueryNew("test");
qry.setColumnNames(["test test"]);
WriteDump(qry);
</cfscript>
w
promising. let me mess with it, thanks
i don't recall ever using/seeing setColumnNames. did you have to dump the query object to see that method?
m
yes
Copy code
qry = QueryNew("test");
WRiteDUmp(qry.getClass().getName());

tst = CreateObject("java", "coldfusion.sql.QueryTable");
WriteDump(tst);
w
cool. appreciate it. we'll see if i can add rows to it and assign values to that column 😉
can add a row. cannot reference the column "test test" without error:
querySetCell( qry, "test test", 123)
c
No perfect solution here, but might give you some ideas: https://blog.simplicityweb.co.uk/125/renaming-cfml-query-columns
✔️ 1
w
giggity
thanks, will play with that
👍 1
seems that setColumnNames is the way to go, provided i do things in the proper sequence: https://trycf.com/gist/e4bf19f156979a477e946a1da1e82848/acf?theme=monokai
b
It is not my intention to rain on the parade. I just thought I'd share a word of caution. If I faced so much hassle when trying to create a column name containing a space, I would not go ahead with it. I would take it to be a warning of unpredictable behaviour, or even errors, to come.
w
or that i'm trying to interface with legacy code and db schemas created by people i'd like to meet in a dark alley
that is, THEY already created the unpredictable behavior. i'm just trying to deal with the fallout of that in my new code which is [trying to] interface with it
g
Some random thoughts? Can you change the database column name? (I realise that you might just say "no"... But have you actually investigated it?) What is the results of doing so? What breaks - how much work is it to fix what breaks VS trying to get "column names with spaces" to work in your current task(s)? Can you create a view or stored procedure(s) on the DB - to get rid of the space? Can you duplicate the existing table(s) (getting rid of the space(s) and use triggers/stored procedures to keep both in sync when the other changes? Bonus points - if you have a DBA - can you offload this work to someone else! 🙂
a
If you just want to fake a query you can do it with
DeSerializeJSON
https://trycf.com/gist/3f9360739f0c5e10f5e6141bf3030466/acf2023?theme=monokai
w
fwiw, the setColumnNames() method ultimately worked out the way i needed for what i need to do, so thanks to those for suggesting it
👍🏼 1