diff --git a/src/java/com/samskivert/depot/tests/TestRecord.java b/src/java/com/samskivert/depot/tests/TestRecord.java new file mode 100644 index 0000000..ed2abbc --- /dev/null +++ b/src/java/com/samskivert/depot/tests/TestRecord.java @@ -0,0 +1,128 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2006-2007 Michael Bayne, Pär Winzell +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.jdbc.depot.tests; + +import java.sql.Date; +import java.sql.Timestamp; + +import com.samskivert.jdbc.depot.Key; +import com.samskivert.jdbc.depot.PersistentRecord; +import com.samskivert.jdbc.depot.annotation.Entity; +import com.samskivert.jdbc.depot.annotation.Id; +import com.samskivert.jdbc.depot.annotation.Index; +import com.samskivert.jdbc.depot.expression.ColumnExp; + +import com.samskivert.util.StringUtil; + +/** + * A test persistent object. + */ +@Entity(indices={ @Index(name="createdIndex", fields={"created"}) }) +public class TestRecord extends PersistentRecord +{ + // AUTO-GENERATED: FIELDS START + /** The column identifier for the {@link #recordId} field. */ + public static final String RECORD_ID = "recordId"; + + /** The qualified column identifier for the {@link #recordId} field. */ + public static final ColumnExp RECORD_ID_C = + new ColumnExp(TestRecord.class, RECORD_ID); + + /** The column identifier for the {@link #name} field. */ + public static final String NAME = "name"; + + /** The qualified column identifier for the {@link #name} field. */ + public static final ColumnExp NAME_C = + new ColumnExp(TestRecord.class, NAME); + + /** The column identifier for the {@link #age} field. */ + public static final String AGE = "age"; + + /** The qualified column identifier for the {@link #age} field. */ + public static final ColumnExp AGE_C = + new ColumnExp(TestRecord.class, AGE); + + /** The column identifier for the {@link #homeTown} field. */ + public static final String HOME_TOWN = "homeTown"; + + /** The qualified column identifier for the {@link #homeTown} field. */ + public static final ColumnExp HOME_TOWN_C = + new ColumnExp(TestRecord.class, HOME_TOWN); + + /** The column identifier for the {@link #created} field. */ + public static final String CREATED = "created"; + + /** The qualified column identifier for the {@link #created} field. */ + public static final ColumnExp CREATED_C = + new ColumnExp(TestRecord.class, CREATED); + + /** The column identifier for the {@link #lastModified} field. */ + public static final String LAST_MODIFIED = "lastModified"; + + /** The qualified column identifier for the {@link #lastModified} field. */ + public static final ColumnExp LAST_MODIFIED_C = + new ColumnExp(TestRecord.class, LAST_MODIFIED); + + /** The column identifier for the {@link #numbers} field. */ + public static final String NUMBERS = "numbers"; + + /** The qualified column identifier for the {@link #numbers} field. */ + public static final ColumnExp NUMBERS_C = + new ColumnExp(TestRecord.class, NUMBERS); + // AUTO-GENERATED: FIELDS END + + public static final int SCHEMA_VERSION = 3; + + @Id + public int recordId; + + public String name; + + public int age; + + public String homeTown; + + public Date created; + + public Timestamp lastModified; + + public int[] numbers; + + @Override + public String toString () + { + return StringUtil.fieldsToString(this); + } + + // AUTO-GENERATED: METHODS START + /** + * Create and return a primary {@link Key} to identify a {@link TestRecord} + * with the supplied key values. + */ + public static Key getKey (int recordId) + { + return new Key( + TestRecord.class, + new String[] { RECORD_ID }, + new Comparable[] { recordId }); + } + // AUTO-GENERATED: METHODS END +} diff --git a/src/java/com/samskivert/depot/tests/TestRepository.java b/src/java/com/samskivert/depot/tests/TestRepository.java new file mode 100644 index 0000000..f6ed790 --- /dev/null +++ b/src/java/com/samskivert/depot/tests/TestRepository.java @@ -0,0 +1,138 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2006-2007 Michael Bayne, Pär Winzell +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.jdbc.depot.tests; + +import java.sql.Date; +import java.sql.Timestamp; +// import java.util.HashSet; +import java.util.Set; + +import com.samskivert.util.RandomUtil; + +import com.samskivert.jdbc.StaticConnectionProvider; +import com.samskivert.jdbc.depot.DepotRepository; +// import com.samskivert.jdbc.depot.Key; +// import com.samskivert.jdbc.depot.KeySet; +import com.samskivert.jdbc.depot.PersistenceContext; +import com.samskivert.jdbc.depot.PersistentRecord; +import com.samskivert.jdbc.depot.SchemaMigration; +import com.samskivert.jdbc.depot.annotation.Computed; +import com.samskivert.jdbc.depot.clause.Where; +import com.samskivert.jdbc.depot.expression.LiteralExp; +import com.samskivert.jdbc.depot.operator.Conditionals; + +/** + * A test tool for the Depot repository services. + */ +public class TestRepository extends DepotRepository +{ + @Computed(shadowOf=TestRecord.class) + public static class TestNameRecord extends PersistentRecord + { + public int recordId; + public String name; + public String toString () { + return recordId + ":" + name; + } + } + + public static void main (String[] args) + throws Exception + { + PersistenceContext perCtx = new PersistenceContext(); + perCtx.init("test", new StaticConnectionProvider("depot.properties"), null); + + // tests a bogus rename migration + // perCtx.registerMigration(TestRecord.class, new SchemaMigration.Rename(1, "foo", "bar")); + + // tests a custom add column migration + perCtx.registerMigration(TestRecord.class, + new SchemaMigration.Add(2, TestRecord.HOME_TOWN, "'Anytown USA'")); + + TestRepository repo = new TestRepository(perCtx); + + repo.delete(TestRecord.class, 1); + + Date now = new Date(System.currentTimeMillis()); + Timestamp tnow = new Timestamp(System.currentTimeMillis()); + + TestRecord record = new TestRecord(); + record.recordId = 1; + record.name = "Elvis"; + record.age = 99; + record.created = now; + record.homeTown = "Right here"; + record.lastModified = tnow; + record.numbers = new int[] { 9, 0, 2, 1, 0 }; + + repo.insert(record); + System.out.println(repo.load(TestRecord.class, record.recordId)); + +// record.age = 25; +// record.name = "Bob"; +// record.numbers = new int[] { 1, 2, 3, 4, 5 }; +// repo.update(record, TestRecord.AGE, TestRecord.NAME, TestRecord.NUMBERS); + + repo.updatePartial(TestRecord.class, record.recordId, + TestRecord.AGE, 25, TestRecord.NAME, "Bob", + TestRecord.NUMBERS, new int[] { 1, 2, 3, 4, 5 }); + System.out.println(repo.load(TestRecord.class, record.recordId)); + + for (int ii = 2; ii < CREATE_RECORDS; ii++) { + record = new TestRecord(); + record.recordId = ii; + record.name = "Spam!"; + record.age = RandomUtil.getInt(150); + record.homeTown = "Over there"; + record.numbers = new int[] { 5, 4, 3, 2, 1 }; + record.created = now; + record.lastModified = tnow; + repo.insert(record); + } + + System.out.println("Names " + repo.findAll(TestNameRecord.class) + "."); + System.out.println("Have " + repo.findAll(TestRecord.class).size() + " records."); + repo.deleteAll(TestRecord.class, new Where(new Conditionals.LessThan( + TestRecord.RECORD_ID_C, CREATE_RECORDS/2))); + System.out.println("Now have " + repo.findAll(TestRecord.class).size() + " records."); + repo.deleteAll(TestRecord.class, new Where(new LiteralExp("true"))); +// // TODO: try to break our In() clause +// Set> ids = new HashSet>(); +// for (int ii = 1; ii <= Conditionals.In.MAX_KEYS*2+3; ii++) { +// ids.add(TestRecord.getKey(ii)); +// } +// repo.deleteAll(TestRecord.class, new KeySet(TestRecord.class, ids)); + System.out.println("Now have " + repo.findAll(TestRecord.class).size() + " records."); + } + + public TestRepository (PersistenceContext perCtx) + { + super(perCtx); + } + + @Override // from DepotRepository + protected void getManagedRecords (Set> classes) + { + classes.add(TestRecord.class); + } + + protected static final int CREATE_RECORDS = 150; +}