Hugoware

The product of a web developer with a little too much caffeine

jLinq And Accepting Parameters

leave a comment »

One of the more complicated parts of jLinq is that any commands, especially query style commands, can accept different number of parameters at any given time. When using the features like command memorizing and field memorizing the function has to assume the values to use.

Another feature that is in development is to make it so that jLinq will accept different types of parameters for commands and then determine the best way to handle them.

For example, the other day I was writing some unit tests for jLinq and entered in the following query…

jLinq.from(data.users)
  .less("first", 5)
  .select();

Okay, all names less than 5 characters, right? Ah… oops…

So that doesn’t work… why is that? Well looking into the code for the ‘less()’ command we find the following.

{name:"greater", count:1, type:"query",
method:function(query, value) {
	return query.when({
		number:function() {
			return (query.value > value);
		},
		string:function() {
			return (query.value.length > value.toString().length);
		},
		array:function() {								
			return (query.value.length > value.length);
		},
		other:function() {
			return (query.value > value);
		}
	});					
}},

So if you notice that command – toString() – you’ll see our problem. Matching against a string field assumes the parameter should be treated as a string. The query is technically evaluating the 5 as a string. Or more specifically, the length of the string…

//the correct way that doesn't work in 2.1.1
record.first.length < 5.toString().length // or 1 (one)
&#91;/sourcecode&#93;

The majority if the time this is going to be correct, but occasionally we run into a query that it doesn't work with.

As of now most of the queries are having a second look to make sure the correct comparison is done. This is being done using a new query helper method named <strong>as()</strong>. This way, if a command expect a number, then it will search for all numeric versions for the value that was passed in.


//checks for the type of value - if it is already a number, then return it
//otherwise, check for a length value
value = query.helper.as(value, {
  number:function(v) { return v; },
  other:function(v) { return v.length; }
});

This way you can focus more on expressing your queries and less concern about what gets passed into it.

Written by hugoware

May 18, 2009 at 5:11 am

Leave a comment