Thursday, June 25, 2009

Weekly Source Code: LINQ for Java Part 2

I hope everybody had a nice week, even if the winter is upon us - freezing cold with rain etc. Time to get the blankets / heaters out.

Last week I introduced JoSQL, this week I continue to show a sample JoSQL query... we start off with a keep it simple (KISS) sample with some points to remember.

Sample Query:

The goal of this JoSQL query is to get a list of contracts that all have a given product:

List contracts = .... // here we get a list of contracts

Query q = new Query();
q.parse("select * from com.sales.Contract where productId = :p");
q.setVariable("p", 5);
QueryResults qr = q.execute(contracts);
List contractResult = qr.getResults ();
  1. All JoSQL classes are kept in the org.josql package, Eclipse IDE will import these nicely for you.
  2. We start by creating a Query object instance this is where our query starts.
  3. q.parse will check your query for any issues, an exception will occure if the query is incorrect. Note that we need to provide the full package location of our Contract class else JoSQL wouldn't find the class. This can cause a problem if you refactor your code, like move the Contract class to a different package location but don't update the JoSQL query.
  4. The following line we set the :p variable using setVariable method.
  5. We then execute the query and put the results into qr which is an instance of QueryResults.
  6. contractResult will contain the result.
JoSQL uses reflection so "productId" in the query is a getter method like "getProductId()" on the Contract class.

Friday, June 19, 2009

Weekly Source Code: LINQ for Java

Its been a long couple of weeks due to my current work load, working average 12 hours+ per day isn't fun. So I finally got some free time over a lunch break to continue with my weekly source code.

At work Java is our main programming language but its not so advance like C# (not to start a language war here!) one of them are LINQ but LINQ is also available in Java!


What Is LINQ?

Language Integrated Query (LINQ) was added to C# 3.0 / VB.Net 9.0 in MS Visual Studio.Net 2008. Its a query language part of the underline language for easy query of information like a database, online resources like a webservice or in our case collections (list, arrays, hashmaps etc). Different LINQ providers existing for collections, databases, Amazon, Facebook, Twitter, LDAP etc. Visit Wikipedia for more about LINQ.

Let's Meet: JoSQL

JoSQL is LINQ for Java that allow you to easily query collections (lists, arrays etc) using a SQL line syntax.

References

Disadvantages:

  • No compile time checking since the LINQ code will only be evaluated when query.parse() method get called at run-time.
  • You need to indicate the full package + class name in your query
  • If you wish to use "NOT IN" or "IN" in the WHERE clause then you need to override the toString() method of the class since it needs to know which field to use to do the comparison with.

I will write more JoSQL samples in the next weekly source code. Also my solution to the toString() problem.


Tuesday, May 12, 2009

I'm Still Alive...

I haven't blogged for awhile but yes I'm still alive... just been very hectic with work and studying for my Java certification.

You can always follow me on Twitter to see what I'm up too.

Thursday, April 30, 2009

Looking For A Chess Partner

I love playing chess its a smart interesting game, I'm looking for a chess partner to play email chess against using Chess Rally.

If your interested send me an email.

Friday, April 24, 2009

JavaScript Isn't So Weak

Alot of developers think that JavaScript isn't a powerful programming language to be used for professional development.  

Yes JavaScript have some weak points (just like every other programming language) But if you look around you will find websites like this one where it show how powerful JavaScript is for writting online games.

JavaScript is even popular for creating ritch Web 2.0 applications that include technology like AJAX etc. 

So the lesson to learn is to never judge before you actual used it... Like you can never judge C if you never wrote a line of code in it.

There will be no weekly source code this week.

Wednesday, April 15, 2009

Weekly Source Code: JavaScript.... Big Reason Why It Sucks

Today in the Web 2.0 world you do alot of JavaScript coding being DHTML, all the set of frameworks (jQuery etc) or do AJAX. The biggest thing that all JavaScript coders (even the pros) moan about isn't how difficult it is to test/debug JavaScript (tools like Firebug or JavaScript support in MS Visual Studio.Net 2008 IE make this very easy) but the dreadful word: "browser compatibility".... Yes Internet Explorer (IE) vs Firefox etc. All the things that make you want to scream / run away crying!

Its such a pain for me, since I use Google Chrome as my main default browser so everything just works but then later I found that it doesn't work in IE because of some browser compatibility issue or a weird way that IE handle some JavaScript.... The main problem here is that the browser companies (Microsoft, Mozilla etc) doesn't come together and work on a solid JavaScript standard. Ladies & gents, that is the reason why we've language standards!! There is a promise that the next version of JavaScript will be standard accross all browsers but thats only "promises".

For example:
var name = "Lennie";
alert(name[0]);
where you want the first characters e.g. "L" but in IE (IE is always the bad child it seams...) it returns "undefined" :-(

So you need to write it:

var name = "Lennie";
alert(name.charAt(0));

this works fine in all browsers.

So something to consider when your coding in JavaScript again.


Wednesday, April 8, 2009

Weekly Source Code: PHP Say When Its The Easter Date

Today its Wednesday, 8 April 2009 in a couple of days on Sunday its Easter when we eat a lot of chocolate eggs and spend time with family.

Because Easter is always on a Sunday the date differs each year. By surprise I found that PHP has a easter_date function that returns the easter sunday date given a year or if no year parameter provided for the current year.


Nice if you want to run a special event on your website or offer free choclates on Easter (if you do please let me know!).

Happy Easter!