001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.sql;
024:
025: import java.sql.PreparedStatement;
026: import java.sql.ResultSet;
027: import java.sql.SQLException;
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.Iterator;
031:
032: import javax.sql.DataSource;
033:
034: import biz.hammurapi.sql.hypersonic.HypersonicStandaloneDataSource;
035:
036: /**
037: * @author Pavel Vlasov
038: * @version $Revision: 1.6 $
039: */
040: public class Samples {
041:
042: public static void main(String[] args) throws Exception {
043: sample1();
044: }
045:
046: private static final String CREATE_TABLE_SQL = "CREATE CACHED TABLE PERSON "
047: + "(ID INTEGER IDENTITY NOT NULL PRIMARY KEY, "
048: + "FIRST_NAME VARCHAR(30), LAST_NAME VARCHAR(30))";
049:
050: interface Person {
051: String getFirstName();
052:
053: String getLastName();
054:
055: void setFirstName(String firstName);
056:
057: void setLastName(String lastName);
058: }
059:
060: private static void sample1() throws Exception {
061: DataSource dataSource = new HypersonicStandaloneDataSource(
062: "People", new Transaction() {
063:
064: public boolean execute(SQLProcessor processor)
065: throws SQLException {
066: processor.processUpdate(CREATE_TABLE_SQL, null);
067: return true;
068: }
069:
070: });
071:
072: SQLProcessor processor = new SQLProcessor(dataSource, null);
073: // processor.setMeasurementConsumer(new SimpleMeasurementConsumer());
074: processor
075: .processUpdate(
076: "INSERT INTO PERSON (FIRST_NAME, LAST_NAME) VALUES ('Pavel', 'Vlasov')",
077: null);
078: Iterator it = processor.project("SELECT * FROM PERSON", null,
079: Person.class).iterator();
080: while (it.hasNext()) {
081: System.out.println(it.next());
082: }
083:
084: sample2(processor);
085:
086: //System.out.println("SQL metrics: \n"+processor.getMeasurementConsumer());
087: }
088:
089: private static void sample2(SQLProcessor processor)
090: throws SQLException {
091: processor.processSelect("SELECT * FROM PERSON", null,
092: new RowProcessor() {
093: public boolean process(ResultSet rs)
094: throws SQLException {
095: System.out.println(rs.getInt("ID") + " "
096: + rs.getString("LAST_NAME"));
097: return true;
098: }
099: });
100: }
101:
102: private static String sample3(SQLProcessor processor)
103: throws SQLException {
104: final String[] ret = { null };
105: processor.processSelect(
106: "SELECT * FROM PERSON WHERE LAST_NAME=?",
107: new Parameterizer() {
108: public void parameterize(PreparedStatement ps)
109: throws SQLException {
110: ps.setString(1, "VLASOV");
111: }
112: }, new RowProcessor() {
113: public boolean process(ResultSet rs)
114: throws SQLException {
115: ret[0] = rs.getString("LAST_NAME");
116: return false;
117: }
118: });
119: return ret[0];
120: }
121:
122: private static void updateOrInsert(final SQLProcessor processor,
123: final String firstName, final String lastName)
124: throws SQLException {
125: final Parameterizer parameterizer = new Parameterizer() {
126: public void parameterize(PreparedStatement ps)
127: throws SQLException {
128: ps.setString(1, firstName);
129: ps.setString(2, lastName);
130: }
131: };
132:
133: processor.processSelect(
134: "SELECT * FROM PERSON WHERE LAST_NAME=?",
135: new Parameterizer() {
136: public void parameterize(PreparedStatement ps)
137: throws SQLException {
138: ps.setString(1, lastName);
139: }
140: }, new RowProcessorEx() {
141: public boolean process(ResultSet rs)
142: throws SQLException {
143: processor
144: .processUpdate(
145: "UPDATE PERSON SET FIRST_NAME=? WHERE LAST_NAME=?",
146: parameterizer);
147: return false;
148: }
149:
150: public void onEmptyResultSet() throws SQLException {
151: processor
152: .processUpdate(
153: "INSERT INTO PERSON (FIRST_NAME, LAST_NAME) VALUES (?, ?)",
154: parameterizer);
155: }
156: });
157: }
158:
159: class Person2 {
160: private String firstName;
161: private String lastName;
162:
163: public Person2(String firstName, String lastName) {
164: this .firstName = firstName;
165: this .lastName = lastName;
166: }
167:
168: public String getFirstName() {
169: return firstName;
170: }
171:
172: public void setFirstName(String firstName) {
173: this .firstName = firstName;
174: }
175:
176: public String getLastName() {
177: return lastName;
178: }
179:
180: public void setLastName(String lastName) {
181: this .lastName = lastName;
182: }
183: }
184:
185: private static void projection(SQLProcessor processor,
186: final int personId) throws Exception {
187: // Default - database backed
188: Collection c = processor.project("SELECT * FROM PERSON");
189:
190: // Constructor - torn off
191: Projector projector = new ConstructorProjector(Person2.class
192: .getConstructor(new Class[] { String.class,
193: String.class }), null);
194: c = processor.project(
195: "SELECT FIRST_NAME, LAST_NAME FROM PERSON", null,
196: projector, new ArrayList());
197:
198: // Property - paged, database backed
199: projector = new PropertyProjector(Person2.class, null, null);
200: c = processor.project(
201: "SELECT FIRST_NAME, LAST_NAME FROM PERSON", null,
202: projector);
203:
204: // Interface - paged, torn off
205: c = processor.project("SELECT * FROM PERSON", null,
206: Person.class, new ArrayList(), 3, 15);
207:
208: // Single object projection
209: projector = new PropertyProjector(Person2.class, null, null);
210: Person2 person2 = (Person2) processor.projectSingleObject(
211: "SELECT * FROM PERSON WHERE ID=2", null, projector);
212:
213: // Single interface projection
214: Person person = (Person) processor.projectSingleObject(
215: "SELECT * FROM PERSON WHERE ID=?", new Parameterizer() {
216: public void parameterize(PreparedStatement ps)
217: throws SQLException {
218: ps.setInt(1, personId);
219: }
220: }, Person.class);
221: }
222:
223: static class Person3 {
224: private String firstName;
225: private String lastName;
226:
227: public String getFirstName() {
228: return firstName;
229: }
230:
231: public void setFirstName(String firstName) {
232: this .firstName = firstName;
233: }
234:
235: public String getLastName() {
236: return lastName;
237: }
238:
239: public void setLastName(String lastName) {
240: this .lastName = lastName;
241: }
242: }
243:
244: private static void injection(SQLProcessor processor,
245: final int personId) throws Exception {
246: Person3 person = new Person3();
247: processor.inject("SELECT * FROM PERSON WHERE ID=2", null, null,
248: person);
249:
250: // Single interface projection
251: processor.inject("SELECT * FROM PERSON WHERE ID=?",
252: new Parameterizer() {
253: public void parameterize(PreparedStatement ps)
254: throws SQLException {
255: ps.setInt(1, personId);
256: }
257: }, null, person);
258: }
259: }
|