From f6545228a313e82c3fdf1c9a6e31721b78a37aa5 Mon Sep 17 00:00:00 2001 From: bernd Date: Wed, 3 Aug 2011 00:54:44 +0200 Subject: [PATCH 11/12] Add a complete demo program on how to use predefined queries to the tutorial. --- .classpath | 1 + .../eodsql/demo/DemoUsingPredefinedQueries.java | 286 ++++++++++++++++++++ eodsql/docs/documentation/tutorial.html | 7 + 3 files changed, 294 insertions(+), 0 deletions(-) create mode 100644 eodsql/docs/documentation/examples/net/lemnik/eodsql/demo/DemoUsingPredefinedQueries.java diff --git a/.classpath b/.classpath index 1a0c78b..0f6505c 100644 --- a/.classpath +++ b/.classpath @@ -4,6 +4,7 @@ + diff --git a/eodsql/docs/documentation/examples/net/lemnik/eodsql/demo/DemoUsingPredefinedQueries.java b/eodsql/docs/documentation/examples/net/lemnik/eodsql/demo/DemoUsingPredefinedQueries.java new file mode 100644 index 0000000..3f25766 --- /dev/null +++ b/eodsql/docs/documentation/examples/net/lemnik/eodsql/demo/DemoUsingPredefinedQueries.java @@ -0,0 +1,286 @@ +package net.lemnik.eodsql.demo; + +import java.io.IOException; +import java.sql.SQLException; +import java.util.Collection; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.Iterator; + +import net.lemnik.eodsql.BaseQuery; +import net.lemnik.eodsql.DataSet; +import net.lemnik.eodsql.GeneratedKeys; +import net.lemnik.eodsql.QueryTool; +import net.lemnik.eodsql.ResultColumn; +import net.lemnik.eodsql.Select; +import net.lemnik.eodsql.Update; + +import org.apache.commons.dbcp.BasicDataSource; + +/** + * A simple demo program on how to add, remove and modify rows. + * It needs to have on the classpath: + *
    + *
  • eodsql.jar
  • + *
  • h2.jar
  • + *
  • commons-dbcp.jar
  • + *
  • commons-pool.jar
  • + *
+ * + * @author Bernd Rinn + */ +public class DemoUsingPredefinedQueries { + + // @formatter:off + + /** + * A bean for storing the information about one person. + *

+ * Note that this record has to be static to ensure that it has a default constructor! + */ + static class PersonRecord { + + // Note: do not define the primary key as 'long' but as 'Long' if you intend to add + // new rows using this record as otherwise you won't get the primary key auto-generated! + @ResultColumn("pers_id") + private Long persId; + + @ResultColumn("first_name") + private String firstName; + + @ResultColumn("last_name") + private String lastName; + + @ResultColumn("date_of_birth") + private Date dateOfBirth; + + @ResultColumn("addr_id") + private long addressId; + + long getPersId() { + return persId; + } + + void setPersId(long persId) { + this.persId = persId; + } + + String getFirstName() { + return firstName; + } + + void setFirstName(String firstName) { + this.firstName = firstName; + } + + String getLastName() { + return lastName; + } + + void setLastName(String lastName) { + this.lastName = lastName; + } + + Date getDateOfBirth() { + return dateOfBirth; + } + + void setDateOfBirth(Date dateOfBirth) { + this.dateOfBirth = dateOfBirth; + } + + long getAddressId() { + return addressId; + } + + void setAddressId(long addressId) { + this.addressId = addressId; + } + + } + + /** + * A bean for storing the information about one person and its address. + *

+ * Note that this record has to be static to ensure that it has a default constructor! + */ + static class PersonAddressRecord extends PersonRecord { + + private String street; + + @ResultColumn("zip") + private int zipCode; + + private String city; + + String getStreet() { + return street; + } + + void setStreet(String street) { + this.street = street; + } + + int getZipCode() { + return zipCode; + } + + void setZipCode(int zipCode) { + this.zipCode = zipCode; + } + + String getCity() { + return city; + } + + void setCity(String city) { + this.city = city; + } + } + + /** + * This is the magical query interface. + */ + interface Query extends BaseQuery { + @Update(sql = "CREATE TABLE addresses (" + + "addr_id identity PRIMARY KEY, " + + "city varchar(64), " + + "zip integer, " + "street varchar(64) )") + void createTableAddresses(); + + @Update(sql = "CREATE TABLE persons (" + + "pers_id identity PRIMARY KEY, " + + "last_name varchar(64), " + + "first_name varchar(64), " + + "date_of_birth date DEFAULT NULL, " + + "addr_id bigint DEFAULT NULL REFERENCES addresses(addr_id) " + + " )") + void createTablePersons(); + + @Update(sql = "INSERT INTO addresses (city, zip, street) " + + "VALUES (?{2}, ?{3}, ?{1})", keys = GeneratedKeys.RETURNED_KEYS_FIRST_COLUMN) + long insertAddress(String street, String city, int zipCode); + + @Update(sql = "INSERT INTO persons (last_name, first_name, date_of_birth, addr_id) " + + "VALUES (?{2}, ?{1}, ?{3}, ?{4})") + void insertPerson(String firstName, String lastName, Date dateOfBirth, long addressId); + + @Select(sql = "SELECT * FROM persons", readOnly = false) + DataSet listPersons(); + + @Select(sql = "SELECT * FROM persons p LEFT OUTER JOIN addresses a ON p.addr_id = a.addr_id") + Collection listPersonsWithAddress(); + + @Select(sql = "SELECT addr_id FROM persons WHERE last_name = 'Fox' and first_name = 'John'") + long getAddrIdForPerson(String firstName, String lastName); + + @Update(sql = "UPDATE persons SET addr_id = " + + "(SELECT addr_id FROM persons " + + " WHERE last_name = ?{4} AND first_name = ?{3}) " + + "WHERE last_name = ?{2} AND first_name = ?{1}") + void setAddrIdForPersonToReference(String firstName, String lastName, + String refFirstName, String refLastName); + } + + private static Date getDate(int year, int month, int dayOfMonth) { + return new GregorianCalendar(year, month, dayOfMonth).getTime(); + } + + private static void setupDatabase(Query query) { + query.createTableAddresses(); + query.createTablePersons(); + + // This is the easiest way to insert a row and get the auto-generated key. + long addrIdJohn = query.insertAddress("Am Bach 1", "Hinter den Sieben Bergen", 9999); + long addrIdSara = query.insertAddress("Hinter Pfui Teufel 1", "Im Tal", 8888); + + // If you don't use the return value, you don't have yo define in the query. + query.insertPerson("Poor Little", "Goose", getDate(2008, 1, 2), addrIdJohn); + query.insertPerson("John", "Fox", getDate(1981, 5, 23), addrIdJohn); + query.insertPerson("Sara", "Bunny", getDate(1983, 2, 17), addrIdSara); + } + + private static void printPersons(Query query) { + // + // This is the easiest way to iterate over a result set. + // + for (PersonAddressRecord row : query.listPersonsWithAddress()) { + System.out.println( + row.getPersId() + + ": " + row.getFirstName() + + " " + row.getLastName() + + " *" + row.getDateOfBirth() + + " " + row.getStreet() + + ", " + row.getZipCode() + + " " + row.getCity()); + } + } + + public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException { + + // + // Setup the data source and the database. + // + BasicDataSource source = new BasicDataSource(); + // This line is only needed with JRE 5 + source.setDriverClassName("org.h2.Driver"); + source.setUrl("jdbc:h2:mem:testdb"); + source.setUsername("sa"); + Query query = QueryTool.getQuery(source, Query.class); + + setupDatabase(query); + + System.out.println("Initially"); + printPersons(query); + + // + // 1. method to change the database: update statement + // (Note how we refer to positional parameters.) + // + query.setAddrIdForPersonToReference("Sara", "Bunny", "John", "Fox"); + + System.out.println("\nAfter Sara Bunny moved..."); + printPersons(query); + + // + // 2. method to change the database: connected DataSet + // + DataSet rows = query.listPersons(); + Iterator it = rows.iterator(); + while (it.hasNext()) { + PersonRecord r = it.next(); + if (r.getLastName().equals("Goose")) { + // John eats Poor Little Goose + it.remove(); + break; + } + } + System.out.println("\nAfter John ate Poor Litte Goose..."); + printPersons(query); + + for (int i = 0; i < rows.size(); ++i) { + PersonRecord r = rows.get(i); + if (r.getLastName().equals("Bunny")) { + // It might be tempting to write: + // rows.get(i).setLastName("Fox-Bunny") + // However, this does NOT work! You need to call rows.set() to change the database. + r.setLastName("Fox-Bunny"); + rows.set(i, r); + break; + } + } + System.out.println("\nAfter John and Sara married..."); + printPersons(query); + + PersonRecord newRow = new PersonRecord(); + newRow.setLastName("Bunny-Fox"); + newRow.setFirstName("Young"); + newRow.setDateOfBirth(new Date()); + newRow.setAddressId(query.getAddrIdForPerson("John", "Fox")); + // Young Bunny-Fox is born + rows.add(newRow); + + System.out.println("\nFinally..."); + printPersons(query); + } +} diff --git a/eodsql/docs/documentation/tutorial.html b/eodsql/docs/documentation/tutorial.html index e975f12..0d09900 100644 --- a/eodsql/docs/documentation/tutorial.html +++ b/eodsql/docs/documentation/tutorial.html @@ -439,6 +439,13 @@ public DataSet<PersonDO> getUsersByName( String last, String first ) throws SQLException; +

Demo Programs

+ +

Where To From Here?

-- 1.7.6