Possum Manual |
|
Table of contents |
create table persons ( person_id int primary key, first_name char(32), last_name char(32), age int ); |
@TableDefinition(name = "persons") public class Person { public final static DataSourceDao<Person> dao = new DataSourceDao<Person>(Person.class); @ColumnDefinition(name = "first_name") private String firstName; @ColumnDefinition(name = "last_name") private String lastName; @ColumnDefinition(name = "age") private int age; @IsPrimaryKey private PersonPK primaryKey; // Getters, setters... } |
public class PersonPK { @ColumnDefinition(name = "person_id") private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } } |
Person.dao.setDataSource( Resources.getInstance().getDataSource() ); |
// Create a new person class and populate it Person person = new Person(); person.setFirstName( "AAAA"); person.setLastName( "BBBB" ); person.setAge( 34 ); person.setPrimaryKey( new PersonPK() ); person.getPrimaryKey().setId( 2 ); // Save to database Person.dao.save( person ); // Load a specific person from the database PersonPK pk = new PersonPK(); pk.setId( 3 ); person = Person.dao.load( pk ); |
List<Person> persons = Person.dao.load(); |
$t | The name of the database table. |
$c | The list of column names ("C1,C2" as in "select C1,C2 from ..."). |
$uv | The list of update values ("C1=123, C2='joe'" as in "update persons set C1=123, C2='joe' where..."). |
$ic | The list of insert column names ("C1, C2" as in "insert into persons(C1,C2) values..."). |
$iv | The list of insert values ("123, 4.321" as in "insert into persons(C1,C2) values(123, 4.321)"). |
$pk | An expression listing the primary key in a where clause (C1=2 and C2='today'). |
String
sql = Person.dao.buildSql( "select * from $t" ); Translates to: "select * from persons" |
String
sql = Person.dao.buildSql("update $t set $uv where $pk", e,
getPrimaryKey(e)); // update Translates to: "update persons set first_name='AAAA', last_name='BBBB', age=34 where person_id=2" String sql = Person.dao.buildSql("buildSql("insert into $t($ic) values($iv)", e)); // insert Translates to: "insert into persons(first_name, last_name, age) values( 'AAAA', 'BBBB', 34 )" |
List<Person> persons = Person.dao.load( "select * from $t where age > 40" ); |
PreparedStatement ps = Person.dao.buildSql( "select * from $t where age > ?" ); ps.setInt( 1, cutOffAge ); List<Person> persons = Person.dao.parse( ps.executeQuery() ); |
PreparedStatement ps = Person.dao.buildSql( "select p.* from $t p, sometable s where p.age > ? and p.person_id = s.person_id" ); ps.setInt( 1, cutOffAge ); List<Person> persons = Person.dao.parse( ps.executeQuery() ); |
// DataSource layer - layer provides connection itself List<Person> persons = Person.dao.load(); // ConnectionDao layer - you provide the connection Connection c = .... List<Person> persons = Person.dao.load( c ); // BaseDao layer - You provide the connection, you build and execute the query Connection c = .... PreparedStatement ps = c.prepareStatement( Person.dao.buildSql( "select * from $t" ) ); List<Person> persons = Person.dao.parse( ps.executeQuery() ); |
Connection c = .... try { c.setAutoCommit( false ); // Save multiple persons in one transaction for( Person person : persons ) { Person.dao.save( c, person ); } c.commit(); } catch( SQLException se ) { c.rollback(); } |
public enum Color { RED( 'R' ), GREEN( 'G' ), BLUE( 'B' ); @IsEnumDataField private char code; Color( char code ) { this.code = code; } public char getCode() { return( code ); } } public class Cabinet { @ColumnDefinition( name = 'color' ) private Color color; .... |
public enum Color { RED( 'R' , 255, 0, 0), GREEN( 'G' , 0, 255, 0), BLUE( 'B', 0, 0, 255 ); @IsEnumDataField private char code; private int r; private int g; private int b; Color( char code, int r, int g, int b ) { this.code = code; this.r = r; this.g = g; this.b = b; } public char getCode() { return( code ); } public int getRed() { return( r ); } public int getGreen() { return( g ); } public int getBlue() { return( b ); } } |
@TableDefinition(name = "persons") public class Person { .... } |
@TableDefinition(name = "persons") public class Person { @ColumnDefinition(name = "first_name") private String firstName; .... } |
@TableDefinition(name = "persons") public class Person { @ColumnDefinition(name = "first_name") private String firstName; @ColumnDefinition(name = "last_name") private String lastName; public static class PK implements PrimaryKey { @ColumnDefinition(name = "person_id", isIdentity = true) private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } } @IsPrimaryKey private PK primaryKey; .... } |
public class JodaDateTimeConverter implements Converter<DateTime, java.sql.Timestamp> { /** * Get the encoded type for this converter * * @return The encoded type */ public Class getEncodedType() { return (java.sql.Timestamp.class); } /** * Get the decoded type for this converter * * @return The decoded type */ public Class getDecodedType() { return (DateTime.class); } /** * Encode an object. * * @param decoded * @return The encoded value. */ public java.sql.Timestamp encode(DateTime decoded) { return (new java.sql.Timestamp(decoded.getMillis())); } /** * Decode an object. * * @param encoded * @return The decoded value. */ public DateTime decode(java.sql.Timestamp encoded) { return (new DateTime(encoded.getTime())); } } |
Person.dao.registerConverter( new JodaDateTimeConverter() ); |
@TableDefinition(name = "persons") public class Person { public final static DataSourceDao<Person> dao = new DataSourceDao<Person>(Person.class) { public void customize() { registerConverter(new JodaDateTimeConverter()); } }; @ColumnDefinition(name = "birth_date") private DateTime birthDate; |
PreparedStatement ps = Person.dao.buildSql( "select p.* from $t p, othertable t where p.age > ? and p.person_id = t.person_id" ); ps.setInt( 1, cutOffAge ); List<Person> persons = Person.dao.parse( ps.executeQuery() ); |
create
table persons ( person_id int primary key, first_name char(32), last_name char(32), age int ); |
create
table linktable ( person_id int, group_id int, ); |
create
table groups ( group_id int primary key, group_name char(32) ); |
public interface DaoListener<E> { /** * Called whenever a new bean has been loaded with data. This is an ideal * hookup for post-load operations, such as loading dependent data etc. * * @param c The connection that was used to load the bean * @param e The loaded bean * @throws SQLException */ public void afterLoad(Connection c, E e) throws SQLException; /** * Called whenever a bean has been deleted. This is an ideal * hookup for post-delete operations, such as removing entries * from link tables etc. * * @param c The connection that was used to delete the bean * @param primaryKey The primary key of the bean * @throws SQLException */ public void afterDelete(Connection c, Object primaryKey) throws SQLException; } |
Person.dao.addDaoListener( new DefaultDaoListener<Person>() { public void afterDelete(Connection c, Object primaryKey) throws SQLException { PreparedStatement ps = null; ps = c.prepareStatement( LinkTable.dao.buildSql( "delete $t where person_id=?" ) ); ps.setInt( 1, primaryKey ); ps.executeUpdate(); ps.close(); } } ); |
package org.yourname; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.sql.DataSource; public class MyContextListener implements ServletContextListener { public void contextInitialized(ServletContextEvent servletContextEvent) { DataSource dataSource = Resources.getInstance().getDataSource(); Person.dao.setDataSource(dataSource); } public void contextDestroyed(ServletContextEvent servletContextEvent) { } } |
<!DOCTYPE
web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <!-- Define application events listeners --> <listener> <listener-class> org.yourname.MyContextListener </listener-class> </listener> .... |
public interface SqlLogger { /** * Log a sql statement that just has been built by buildSql. * * @param sql The sql statement */ public void logSql(String sql); /** * Log a result set that is about to be parsed. * * @param rs The result set * @throws SQLException */ public void logResultSet(ResultSet rs) throws SQLException; } |
DatabaseRegistry getInstance().register( new MyDatabaseHandlerImpl() ); |
07/08/2006 11:12: DatabaseRegistry:92: Installed database handler for 'HSQL Database Engine' |
public interface DatabaseHandler { /** * Get the product name for this database. This is the * string that needs to match the return value of the * standard JDBC call DatabaseMetaData.getDatabaseProductName(). * * @return A string indicating the type of database */ String getProductName(); /** * Convert an object to a string in order to include the object in a sql statement. * * @param object The object to be converted * @return A string representation of this object, usable in a sql statement */ String toString(Object object); /** * Get the value of a just inserted identity column * * @param tableName The name of the table * @param c The connection * @return The new value of the identity column * @throws SQLException */ int getIdentityValue(String tableName, Connection c) throws SQLException; } |
public class Resources() { private final static theInstance = new Resources(); private DataSource dataSource; private Resources() { this.dataSource = createDataSource(); } public static Resources getInstance() { return( theInstance ); } public DataSource getDataSource() { return( dataSource ); } public DataSource createDataSource() { BasicDataSource ds = new BasicDataSource(); ds.setUrl("jdbc:hsqldb:hsql://localhost/test"); ds.setDriverClassName("org.hsqldb.jdbcDriver"); ds.setUsername("sa"); ds.setPassword(""); return (ds); } } |