After all the skiing, falling and falling apart on the long weekend I got a chance to work a bit too!!
Recently there have been lots of hype about dynamic languages and frameworks like Ruby on Rails. Many have blogged about pros and cons of RoR and such dynamic languages. The hype is so high that it made me learn (J)RoR. After some reading and some projects on RoR I learned that programmers love these languages because of its ease of use and I loved it too. Many also says that these kind of languages are "threat" to Java, after all Java is complex!!
Once creating a small project in RoR I figured that for a Java developer it is a hard thing to just start working in a whole different environment and start using whole different syntax because no-one likes learning curves. Finally I thought that isn't it possible to get this kind of ease of use and simplicity in Java, Java is a powerful language, isn't it?
Something compelled me to develope an api which is easier then usual JDBC way of retriving and storing data, I wanted this to be easier then Java persistence and I wanted it to be compatible with all the JDK versions। I created a project with one thing in mind, SIMPLICITY. A simple ORM model which can substanttially reduce code re-writing and is built on simple java concept of inheritance, it should involve no Rocket science.
The main idea of this project is to inherit any class from a certain class. Doing this will give the child class ability to save, and update data without writing single line of code.
For example
public class User extends Table{
}
Here "Table" is the magic class, in the above example when we extend the class User from a class Table we are actually building a relationship. We are representing a row of table by a class named User, by default this table should be named users. Let's say this table has two columns username and password, we will be able to write following code in any other class.
public static void main(String [] args){
User u = new User();
u.put("username", "sapan");
u.put("password", "pass");
u.save();
}
we can also do something like following to retrieve data.
public static void main(String [] args){
User u = new User();
u.loadColumns("1","username, password");
System.out.println(u.get("username"));
}
Above code creates an object of the class Users and loads the object with row which has id=1. the function loadColumns pre-fetches the object with this value, what it means, we will see in details later.
I will upload them in a zip file as soon as i will figure out how to do that. Till then please
Click on the above link to download Java source. The above rar file contains some files to do db connection, connection pooling and the magic class Table. To connect to your own database goto MySqlConnection.java and change the URL. There are many things that this API can do, I will be listing them all in my next post.
Finally let us go back to our older example but with one more line added.
public static void main(String [] args){
User u = new User();
u.loadColumns("1","username, password");
System.out.println(u.get("username"));
System.out.println(u.get("email"));
}
As we can see u.loadColumns doesn't contain any column named email but if the table has a column named email then the program will do the lazy binding. This means that we will query the database for email id when we call u.get("email")
Well there is some practice that a programmer must follow when creating tables. For example a table must have a primary key table, should usually be a plural name etc.. But let's say a table named "hammer" doesn't meet such criteria and it also doesn't have a primary key named "id" rather it is called "isbn"
In such a case we can create a class like this:
public class Hammer extends Table{
public Hammer(){
forTable("hammer");
idColumn("isbn");
}
}
The above class will work perfectly to create, update and select data in table named hammer.
There is also a way to run a select query but it's not the ideal one.
For example, to select data from the hammer table where isbn is greater then 20 we can do something like following.
ArrayList hammers= new Hammer().select("isbn>20", "isbn");
for (int i = 0; i <>
sorm.Enc enc = (sorm.Enc) encounters.get(i);
out.println(enc);
}
I don't feel this is ideal because before running the query we have to instantiate the Hammer class, it will be great if one can find how to have a static method so that we can run the query right from the class itself.
There are more things to explain but enthusiastic users can go ahead and have a look at the javadoc at http://www.cynosuredev.com/javadoc/index.html?sorm/Table.html
I added a newer version of sorm today (22nd Feb 08) it can be accessed from the same link above. This version of sorm package I added one more class named Query. Not difficult to understand, it represents a query and one can run this query by executing runQuery() method which returns an array of hashmap.
Example:
HashMap []results = new Query("select * from hammers").runQuery();
Once again, I am open to suggestions if we need to add utility methods to Query class. One method can be of getting query results as an xml string or document. In the newer version of sorm package I also found some bugs and tried resolving them with a hope that it won't mess something else up..
I have been waiting to write about the "owenedBy" part too. It's the way classes can communicate relationships to other classes. Lets say we know that each hammer is owned by a user, this essentially means that hammer table will have a foreign key which will point to a primary key of users table.
Let's extend above scenario of class hammer.
public class Hammer extends Table{
public Hammer(){
forTable("hammer");
idColumn("isbn");
ownedBy("sorm.User", "user_id");
}
}
In the above example class Hammer is owned by the class User. Defining this,with some overhead, enables us to write following code.
Hammer test = new Hammer();
test.loadColumns("1", "column1,column2,user_id");
test.ownedBy("user_id").get("username"); ///Stmnt 2
Here we are doing two thing in one statement. 1.)Get the hammer with id=1 and 2.)get the owner of this particular hammer. Statement 2 in above code will give you the owner's username.
The newer copy of sorm is uploaded today.
--Sapan Parikh
2 comments:
Gr8 work dude. I assume here the table 'Users' has to be explicitly created in the database. Is it possible to create the table from the code itself, simply when you create 'Users' class? Nice work.
>>Gr8 work dude. I assume here the table 'Users' has to be explicitly created in the database. Is it possible to create the table from the code itself, simply when you create
That's true, the users table should be already present. we can involve new utility which does create tables too. I am just keeping it away from the main class because then we will also have to configure numbers and name of the columns.
Post a Comment