001: package simpleorm.examples;
002:
003: import simpleorm.core.*;
004:
005: /** Demonstrated Generated Keys.
006: */
007:
008: public class GeneratedKeyTest implements SConstants {
009:
010: /** This test class defines the Invoice table*/
011: public static class Invoice extends SRecordInstance {
012:
013: public static final SRecordMeta meta = new SRecordMeta(
014: Invoice.class, "XX_INVOICE");
015:
016: public static final SFieldInteger INVOICE_NR = new SFieldInteger(
017: meta, "INVOICE_NR", SFD_PRIMARY_KEY, SGENERATED_KEY
018: .pvalue(new SGeneratorSelectMax(meta)));
019: // Add SSEQUENCE_NAME.pval(Boolean.FALSE) to suppress Postgres SEQUENCES
020:
021: public static final SFieldString NAME = new SFieldString(meta,
022: "NAME", 40, SFD_DESCRIPTIVE);
023:
024: public static final SFieldDouble VALUE = new SFieldDouble(meta,
025: "VALUE");
026:
027: public SRecordMeta getMeta() {
028: return meta;
029: };
030:
031: boolean validated; // just for unit test.
032:
033: public void validateRecord() {
034: SLog.slog.fields("Validated.record " + this );
035: validated = true;
036: }
037: }
038:
039: public static void main(String[] argv) throws Exception {
040: TestUte.initializeTest(GeneratedKeyTest.class); // Look at this code.
041: try {
042: /// first using the default Select Max method
043: genTest();
044:
045: /// Now using Sequences
046: if (SConnection.getDriver().supportsKeySequences()) {
047: Invoice.INVOICE_NR.putProperty(SGENERATED_KEY,
048: new SGeneratorSequence(Invoice.meta));
049: SConnection.begin();
050: SConnection.dropTableNoError("DUAL");
051: SConnection
052: .rawUpdateDB("CREATE TABLE DUAL (COL VARCHAR(10))");
053: SConnection
054: .rawUpdateDB("INSERT INTO DUAL VALUES ('DUMMY')");
055: SConnection.commit();
056:
057: genTest();
058: }
059:
060: } finally {
061: SConnection.detachAndClose();
062: }
063: }
064:
065: /** Basic examples/tests not involving foreign keys. */
066: static void genTest() throws Exception {
067:
068: /// (re)create tables
069: SConnection.begin();
070: SConnection.dropTableNoError("XX_INVOICE");
071: SConnection.rawUpdateDB(Invoice.meta.createTableSQL());
072:
073: SGenerator gen = Invoice.meta.getSGenerator();
074: //(SGenerator)Invoice.INVOICE_NR.getProperty(SGENERATED_KEY);
075: try {
076: SConnection.rawUpdateDB(gen.dropDDL());
077: } catch (SException.JDBC ex) {
078: } // Might not exist.
079: SConnection.rawUpdateDB(gen.createDDL());
080: SConnection.commit();
081:
082: SConnection.begin();
083:
084: /// Create First
085: Invoice inv1 = (Invoice) Invoice.meta.createWithGeneratedKey();
086: inv1.validated = false;
087: SConnection.flush();
088: TestUte.assertTrue(inv1.validated);
089: inv1.setString(inv1.NAME, "First");
090: inv1.setDouble(inv1.VALUE, 123);
091: long key1 = inv1.getLong(inv1.INVOICE_NR);
092: inv1.validated = false;
093: SConnection.flush();
094: TestUte.assertTrue(inv1.validated);
095:
096: Invoice inv1a = (Invoice) Invoice.meta.findOrCreate(new Long(
097: key1));
098: TestUte.assertEqual(inv1 + "", inv1a + "");
099:
100: /// Create some Records using the SDataLoader
101: SDataLoader invoiceDL = new SDataLoader(Invoice.meta);
102: invoiceDL.insertRecords(new Object[][] { { "Second", "234" },
103: { "Third", "345" } });
104: SConnection.commit();
105:
106: // Check Result
107: SConnection.begin();
108: Invoice inv3a = (Invoice) Invoice.meta.findOrCreate(new Long(
109: key1 + 2));
110: TestUte.assertEqual(inv3a.getString(inv3a.NAME), "Third");
111:
112: Object sum = SConnection
113: .rawQueryJDBC("SELECT SUM(VALUE) FROM XX_INVOICE");
114: if (((Number) sum).intValue() != (123 + 234 + 345))
115: throw new SException.Test("Bad Value sum " + sum);
116: SConnection.commit();
117:
118: }
119: }
|