Tuesday, October 8, 2013

Railo CFML - Get Query Data by full ROW

Methods previously described for obtaining query data by column name and row number (variables.myquery["columnName"][rowNumber]) are correct, but not convenient for getting a full row of query data.

I'm running Railo 4.1.  And this is a cool solution. Too bad this can't be done the way we would want outright to get a full row of data, but the following method allows us to get what we want through a few hoops.

When you serializeJSON(variables.myquery) it changes the query to a JSON formatted cfml struct object with two items, "Columns" and "Data". Both of these are arrays of data. The "data" array is a two-dimensional array for rows and then columnar data.

The issue is that now we have an unusable string. Then if we re-serialize it it's NOT a query but rather usable regular struct in the format described above.


Assume we already have a query variable named 'variables.myquery'. Then look at the following code:

<cfset variables.myqueryobj = deserializeJSON(serializeJSON(variables.myquery)) />

Now you get the two dimensional array by getting this:
<cfset variables.allrowsarray = variables.myqueryobj.data />

And you get one query row array by getting this:
<cfset variables.allrowsarray = variables.myqueryobj.data[1] />
OR the last row this way:
<cfset variables.allrowsarray = variables.myqueryobj.data[variables.myquery.recordCount] />

And you can get individual column values by column order number iteration:
<cfset variables.allrowsarray = variables.myqueryobj.data[1][1] />


Now this might be slow and possibly unwise with large query results, but this is a cool solution nonetheless.

I haven't tested this in older version of railo or Adobe CFML. Let me know if this works for you.

Thursday, September 5, 2013

Javascript Object Literal - get the number of key/value pairs

This was tricky but a friend finally figured it out, I needed to know if the object passed into a function had anything in it.
This was the solution:
  var x = {t1:1,t2:2,t3:3};
console.log(Object.keys(x).length);
This outputs '3'.

Thanks buddy!

Friday, August 9, 2013

Custom CFML BOOL() function

I needed a convenient way to get a trusted Boolean value and didn't want to be doing inline conditions repeatedly, so I write a little function called BOOL to do this:


<cffunction name="bool" access="public" returntype="boolean">
<cfargument name="in" required="No" type="any" default="" />
<cftry>
<cfreturn (arguments.in EQ "" OR arguments.in EQ false ? false : true) />
<cfcatch type="any">
<cfreturn false />
</cfcatch>
</cftry>
</cffunction>

I'm surprised Railo and Adobe don't have this as a native function. There is the old YesNoFormat() function but that throws errors on strings or anything complex. No good at all.

With Javascript, and other true scripting languages I assume, you can take nearly anything and put two exclamation marks in front if it and it's boolean: var x = 'a'; return !!x; I love the bang-bang. I use that often as well.


Also, on the above function, If you need a guaranteed 1 or 0 result you can run VAL(BOOL(variables.whatever)) and that forces true/false into 1/0.

I have a component with functions like this which I re-use on most projects, and I put it in the server scope, or application scope depending on the environment, so I can call server.bool().


That's it. Short and sweet.

Wednesday, June 12, 2013

Java Sass Compass (SCSS) - [SOLVED!]

After hours of research, trial, frustration, and disappointment, I am very happy to say I have the matter solved.

I need to publish this solution to Java Compass Sass (SCSS). From my source links at the bottom it appears people have done this exact thing before, but I haven't seen it blogged about, hence this post. (I don't like re-posting existing knowledge, I don't need to be the source if it's easily available elsewhere.) There appears to already be a jazz.jar on github which is this exact thing, but I couldn't get it to work. I'll post this on my github account later as well.

UPDATED: I have posted here on Github: JCompass


In the end it was simple and elegant without the mess.

  1. Download the latest copy of JRuby  (At the time of this posting it's version 1.7.4)
    (http://www.jruby.org/download Find the "Complete Jar", second link from the bottom in the top group of links for the latest version)

    (The rest is command line)

  2. # Make a copy of jruby-complete-1.7.4.jar for the new singleton combined jar file
    cp jruby-complete-1.7.4.jar jruby-complete.jar

  3. # Now install these 3 gems locally:

    # Without this I got errors.
    java -jar jruby-complete.jar -S gem install -i ./compass-gems shared --no-rdoc --no-ri

    # I flagged "--pre" to get the latest pre-release version, skip that if you want.
    java -jar jruby-complete.jar -S gem install -i ./compass-gems sass --pre --no-rdoc --no-ri

    # Again, I flagged "--pre" here. Also, I had heard this includes the sass gem as a dependency, but I installed it separately to be safe.
    java -jar jruby-complete.jar -S gem install -i ./compass-gems compass --pre --no-rdoc --no-ri

  4. # Now rename the jruby complete jar to the name for the new jar app, or we lose the original and have to go download it again
    mv jruby-complete.jar jcompass.jar

  5. # Now compile together
    jar uf jcompass.jar -C compass-gems .    # including the space and period at the end

  6. # test it out:
    java -jar jcompass.jar -S compass compile --help
    java -jar jcompass.jar -S compass compile --sass-dir [relative or abs path to your .scss files] --css-dir [relative or asb path to destination .css files] --force


That's it.  

You can download my version of jcompass.jar here.



Sources that helped me compile this, and many thanks to them:

http://blog.nicksieger.com/articles/2009/01/10/jruby-1-1-6-gems-in-a-jar/
http://stackoverflow.com/questions/16188626/how-to-use-compass-through-jruby-for-embedding-in-my-soft
http://stackoverflow.com/questions/15549617/cleanest-way-to-run-susy-compass-and-sass-within-jruby-complete

Tuesday, April 23, 2013

Coldfusion inline cfquery (cfset like cfscript)

I'm a spotty blogger at best. I like to blog about fun discoveries, time-saving finds, success stories, solutions, or things that I don't find already being discussed in the Coldfusion railo community.

I had a fun find today. I don't code in CFSCRIPT. But I think it's fascinating. I prefer scripting languages but cfscript was so far behind the times for so long that I never thought it would be possible to write an entire application in cfscript. ...and I really dislike alternating between tag-coding and script-code in the same project. That is why I never embraced it. So I do a lot of short-hand one-off script-like things inside <cfset ... /> tags.

So I;ll share this one. Today I wanted a one-line statement to get a single value from the database, I have changed this code be able to hit mysql but not require any schema.

This is an inline query getting a single column. for some reason I really liked this.

<cfset variables.yearid = variables.qryRes = new Query().setSQL("select YEAR(NOW()) as yid").setDatasource(request.mydb).execute().getResult().yid />

Now, that's a long one-liner, but it did exactly what I was hoping to do in a single line. This is not possible with tag-coding. and it wouldn't be as practical with a complex query, but for a single value it skips all the multiple variables otherwise required for the query and the result and the new variable for the single value from the query column desired. This gets right down to business when the target is solo. And that simplicity won my heart.

I love one-liners!!!

Back to monotony of the daily grind.

See ya.


NOTE: the above code sample was run on Railo 4 with MySQL database.