Wednesday, April 20, 2011

HQL vs. SQL

I've been playing with HQL for awhile now and want to share my thoughts of it against SQL. Some developers are hesitant to switch to HQL because of the querying. I don't blame them. Writing HQL can be tougher than SQL. After fighting threw some HQL queries I have found that SQL still has its place in an HQL application. Here are some of my findings:

1. List pages and aggregates are faster and easier to write in SQL. Most of the time the data points that list pages and aggregates are going to be displayed are already hashed out and don't need to be changed much.

2. HQL is great for getting and setting simple data quickly. SQL takes too much energy and time to insert or get a piece of data. The SQL queries you write are not going to be flexible enough for the new fields people are going to request.

3. Some routines are more efficient to be ran in SQL. HQL can be slow and it brings back too much data sometimes.

With the above findings I have organized my code to do saves and single entity gets with HQL and lists and routines with SQL. With this combo I have found it to be quick to get and set the data I need. It does take knowledge of both HQL and SQL to do this, but the languages are very similar and the learner curve isn't too bad. Here is an earlier post about the similarities. I hope this brings down the hesitation for the SQL people to try and use HQL for what it's good for.

I would like to hear peoples thoughts on this matter. It seems to come up a lot in HQL conversations, so comment away.

ColdMVC: Quick start now available

After a long wait, ColdMVC finally has a quick start guide. Check it out here: http://www.coldmvc.com/quickstart.

Monday, March 28, 2011

ColdMVC: get an object's parents

Awhile back I posted on flattening an array of objects with Parent-Child relationship (link) and have been using it alot. But one issue I had with it was when I was looping through the array of objects I didn't know who my parents were for the current object I was working with. I already knew how deep thanks to the treeDepth property. I made this function to recursively go up the tree of objects and bring back an array of parent objects for an object.


<cffunction name="getObjectsParents" access="public" output="false" returntype="array">
<cfargument name="object" required="true"/>
<cfargument name="result" required="false" default="#[]#"/>

<cfif isObject(arguments.object.parent())>
<cfset arrayPrepend(arguments.result,arguments.object.parent())/>
<cfreturn getObjectsParents(arguments.object.parent(),arguments.result)/>
<cfelse>
<cfreturn arguments.result/>
</cfif>

</cffunction>

Thursday, February 3, 2011

ColdMVC: Number Tag

I've playing around with mobile web apps lately. ColdMVC has been a big help in getting them done quickly and "in the cloud".

I've been looking for a house since the market is so good right now and one thing I noticed when searching for houses online is the when you type in a price range only the keypad shows up on my phone. No letters. Just numbers. It's really nice. I viewed the source when I got back to my laptop and found out they were doing it with <input type="number" >. I decided to hack a ColdMVC tag together to hanlde this.

Here is how you call the tag...

<c:number name="miles" value="#service.miles()#">


Here is my hacked in logic...

<cfparam name="attributes.class" default="input"/>
<cfparam name="attributes.value" default=""/>
<cfif thisTag.executionMode eq "end">
<cfoutput>
<cfsavecontent variable="attributes.field">
<input type="number" name="#attributes.name#" title="#attributes.name#" value="#attributes.value#" class="#attributes.class#"/>
</cfsavecontent>
</cfoutput>

<cfset thisTag.generatedContent = coldmvc.form.field(argumentCollection=attributes) />
</cfif>


The above logic doesn't supporting binding, but it works to use. It would be cool if this tag was managed my ColdMVC HTMLHelper.cfc then I would have to worry about the attributes.

Wednesday, February 2, 2011

ColdMVC: Binding

I really like the binding option in the form tag of ColdMVC. It takes an object from the param scope, prefixes all the elements within it, with model's name (ex. user.first_name), and sets the value from the object. It's very handy. Here is an example of what it currently does:

<c:form controller="user" action="save" bind="user">
<c:hidden name="id"/>

<c:input name="first_name"/>
<c:input name="last_name"/>

<c:buttons>
<c:submit label="save"/>
</c:buttons>

</c:form>

If you wanted to bind the form to 2 or more objects it would be cool if you could use a bind tag and group a set of elements within a form.

<c:form controller="student" action="save">

<c:bind key="user">
<c:hidden name="id"/>

<c:input name="first_name"/>
<c:input name="last_name"/>
</c:bind>

<c:bind key="student">

<c:input name="student_number"/>

</c:bind>

<c:buttons>
<c:submit label="save"/>
</c:buttons>

</c:form>

Or even better on the tag itself. (I think this available already, but I am not sure).

<c:form controller="student" action="save">

<c:hidden name="id" bind="user"/>

<c:input name="first_name" bind="user"/>
<c:input name="last_name" bind="user"/>
<c:input name="student_number" bind="student"/>

<c:buttons>
<c:submit label="save"/>
</c:buttons>

</c:form>

Wednesday, January 19, 2011

Type Ahead Searches with jQuery

I was working on a mobile app and wanted to try a type ahead search to easy the user typing on a phone keyboard. I was able to make the ajax calls using jQuery to get the data, but I ran into some race conditions. As the user typed I made an ajax call but sometimes the first ajax call would take longer than the second. Thus updating the results with the first ajax calls data, because it finished later.

Example:
I starting typing "b". Then it would fire off an ajax call to get all the data with the letter "b".

Next I typed "a". So my search input box looked like this "ba". And firing off another ajax call to get all the data with the letters "ba".

I didn't know this but you can put the ajax request in a variable and kill it. Check it out.

<script type="text/javascript">
var ajaxCall;
typeAheadSearch = function(){
if(ajaxCall != undefined){
ajaxCall.abort();
}

ajaxCall = jQuery.ajax({
url: "http://www.somedomain.com/typeAheadSearch/_results.cfm",
data:{search:jQuery("##search").val()},
success:function(data){
jQuery("##results").html(data);
}
});
};
</script>

I started a variable called "ajaxCall" to store the request in. The first thing I do in the typeAheadSearch() is kill the current request by calling abort(). The next thing that I didn't know you could is that jQuery's ajax() returns the request. This is so handy.

Tuesday, January 4, 2011

CF9 computed properties

I wanted to make a computed property in CF9 just like you can on a sql table (computed column). Dan Vega had a nice how-to awhile back but it didn't show a complex example.

In my project I had a "project" class that had many ratings on it (Example: rating of 1-5, 1 being bad and 5 being great) and I wanted to have a computed property that gave me the average rating of a project. Here is what my hib file looks like for the "project" class.


<class entity-name="Project" lazy="true" name="cfc:myproject.app.model.Project" table="`Project`">
<id name="id" type="int">
<column length="10" name="ID" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="Name" sql-type="varchar(max)" />
</property>
<property name="description" type="string">
<column name="Description" sql-type="varchar(max)" />
</property>
<property type="float"
formula="(select (sum(r.rank)*100.0) / (count(pr.id) *100.0) from project as p inner join project_rating as pr on pr.project_id = p.id inner join rating as r on r.id = pr.rating_id where pr.project_id = id)"
name="rating"/>
</class>

Some key things to point out:
1. The formula is a sql query not a hql query.
2. I wrapped my entire statement in (), don't know why but it worked.
3. You always start the sql statement with the table of the class you are working on. Reason being is when you get to the "where" clause notice this piece "pr.project_id = id". "id" is not aliased because it grabs the current id of the object that is running the formula against. This is how the query will only run for single object and not all of the other db records in the project table.
4. Alias every thing except the "where id" part.

I struggled through these above. Hope this helps someone.