AutoSQL

Summary

Examples

Downloads

Example 1

Let's say we have the simple Java class below:


		public class Foo
		{	
			private Integer key=null;
			private String name=null;
			private String value=null;

			public Integer getKey()
			{
				return(key);
			}
			public String getName()
			{
				return(name);
			}
			public String getValue()
			{
				return(value);
			}
			public Foo(Integer key)
			{
				this.key=key;
			}
			public Foo(Integer key, String name, String value)
			{
				this.key=key;
				this.name=name;
				this.value=value;
			}
		}
		

Ok now we want to modify this object so that the AutoSQL framework can map this object to a database. To do that we first need to implement SQLObject (or SQLReadOnlyObject):


		public class Foo implements SQLObject
		{	
			// SQLObject interface implementation
			public String[] getPrimaryKeys() 
			{ 
				String primaryKeys[]={"KEY"};
				return(primaryKeys); 
			}
			public String[] getFields() 
			{
				String fields[]={"NAME","VALUE"};
				return(fields); 
			}
			public String getTableName() 
			{ 
				return("FOO"); 
			}
			public String[] getProperties() 
			{
				String properties[]={"Key","Name","Value"};
				return(_properties); 
			}

			// Original code
			private Integer key=null;
			private String name=null;
			private String value=null;

			public Integer getKey()
			{
				return(key);
			}
			public String getName()
			{
				return(name);
			}
			public String getValue()
			{
				return(value);
			}
			public Foo(Integer key)
			{
				this.key=key;
			}
			public Foo(Integer key, String name, String value)
			{
				this.key=key;
				this.name=name;
				this.value=value;
			}
		}
		

Now let's discuss what we added. The first things is the SQLObject implementation. All objects that are to work within this framework should be either derived from SQLObject (for objects that need complete INSERT/UPDATE/DELETE/SELECT functionality) or from SQLReadOnlyObject (for objects that just need SELECT functionality).

SQLObject requires four methods to be defined. The first is getPrimaryKeys which returns a list of the primary keys in the table that this object will be attached to. getFields are the rest of the fields in the table which are not primary keys. The third method is getTableName which returns a string that is the table whose rows this object represents. For these three methods the strings that they return are case isensitive.

The fourth method is getProperties and this is case sensitive. This returns the names of properties that will be mapped to the fields values described by the first two functions. The total number of fields returned by this function must be the same as the sum of the first two, first the primary keys and then the rest of the fields.

Now let's add some simple features that the framework implements. The first thing we might want to do is create new rows in the database. Adding this static method:


		public static Foo create(Foo foo)
		{
			return((Foo)SQLManager.create(foo));
		}
		

See, simple and easy. Now let's load a Foo that is in the database (we load by the primary key):


		public static Foo findByPrimaryKey(Integer key)
		{
			String where="  KEY="+key;
			return((Foo)SQLManager.load(where,new Foo()));
		}
		

There is only a minimal amount of SQL left in the class now and this little amount of SQL allows us to do all sort of neat things. Like the following:


		public static Collection findByValue(String value)
		{
			String where="  VALUE='"+value+"'";
			return(SQLManager.load(where,Foo.class));
		}
		

This one is a bit different. Instead of finding a single Foo it returns a vector of Foo's.

So in our code we could do something like this:


		// Setup the SQL manager to use an already existing connection
		SQLManager.connection=connection;	
		
		// The key we are going to use
		Integer key=new Integer(1);

		// Create a new foo
		Foo foo_a=Foo.create(new Foo(key));

		Foo foo_b=Foo.findByPrimaryKey(key);

		// Just a note - foo_a and foo_b are the same object (remember
		// no caching

		foo_b.setName("Jack Strohm");
		foo_b.setValue("Testing");

		// Save the new data to the database
		SQLManager.save(foo_b);

		// Now let's find by that finder we created
		Vector foos=Foo.findByValue("Testing");

		connection.commit();

		

This example just goes to show how you can use the AutoSQL framework to augment already written classes to use the framework.


SourceForge Logo