-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:gburgett/XFlat
- Loading branch information
Showing
2 changed files
with
195 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
### Examples | ||
|
||
Insert an instance of `Foo` with a pre-set ID | ||
```java | ||
XFlatDatabase db = new XFlatDatabase(new File("myDataDirectory")); | ||
//initialize with default config | ||
db.initialize(); | ||
|
||
Foo myFoo = new Foo(); | ||
myFoo.setId("SomeUniqueId"); | ||
Table<Foo> fooTable = db.getTable(Foo.class); | ||
fooTable.insert(myFoo); | ||
//beware DuplicateKeyException if fooTable already has a row with that ID | ||
|
||
Foo myFoo2 = fooTable.find("SomeUniqueId"); | ||
//myFoo2 is a new instance with the same data as myFoo | ||
``` | ||
|
||
Insert an instance of `Bar` into the "Foo" table - this works as long as their ID types are both compatible | ||
with the ID generator assigned to "Foo". | ||
```java | ||
Foo myFoo = new Foo(); | ||
Table<Foo> fooTable = db.getTable(Foo.class); | ||
fooTable.insert(myFoo); | ||
|
||
Bar myBar = new Bar(); | ||
Table<Bar> barTable = db.getTable(Bar.class, "Foo"); | ||
barTable.insert(myBar); | ||
|
||
Bar myBar2 = barTable.find(myBar.getId()); | ||
|
||
Bar thisIsFooData = barTable.find(myFoo.getId()); | ||
//attempts to deserialize myFoo's data as a Bar, might throw ConversionException. | ||
|
||
Element myFooData = db.getTable(Element.class, "Foo").find(myFoo.getId()); | ||
//myFooData is the XML serialized representation of myFoo. | ||
``` | ||
|
||
Use a custom ID generator for the "Foo" table - the custom class must have a no-args constructor and extend `IdGenerator`. | ||
```java | ||
XFlatDatabase db = new XFlatDatabase(new File("myDataDirectory")); | ||
//do config before calling initialize | ||
db.configureTable("Foo", TableConfig.defaultConfig | ||
.setIdGenerator(MyCustomIdGenerator.class)); | ||
|
||
db.initialize(); | ||
|
||
Foo myFoo = new Foo(); | ||
Table<Foo> fooTable = db.getTable(Foo.class); | ||
fooTable.insert(myFoo); | ||
//myFoo now has the ID generated by MyCustomIdGenerator | ||
``` | ||
|
||
Use custom converters to map `Foo` objects to XML. If this is done for every POJO that is a root of a row, | ||
then the JAXB mapper will never be invoked and the JAXB jars do not need to be on the classpath. | ||
```java | ||
XFlatDatabase db = new XFlatDatabase(new File("myDataDirectory")); | ||
//do config before calling initialize | ||
db.getConversionService().addConverter(Foo.class, Element.class, new FooToElementConverter()); | ||
db.getConversionService().addConverter(Element.class, Foo.class, new ElementToFooConverter()); | ||
|
||
db.initialize(); | ||
|
||
Foo myFoo = new Foo(); | ||
Table<Foo> fooTable = db.getTable(Foo.class); | ||
fooTable.insert(myFoo); | ||
//myFoo was converted to XML using the FooToElementConverter | ||
|
||
Foo myFoo2 = fooTable.find(myFoo.getId()); | ||
//myFoo2 was converted from XML using the ElementToFooConverter | ||
``` | ||
|
||
Query the table for the first `Foo` which has fooInt > 17 | ||
```java | ||
//XPath expressions must be compiled, consider caching often-used expressions | ||
XPathExpression<Object> expression = XPathFactory.instance().compile("foo/fooInt"); | ||
XpathQuery query = XpathQuery.gte(expression, 17); | ||
|
||
Foo found = fooTable.findOne(query); | ||
if(found == null) | ||
System.out.println("Could not find a Foo with fooInt > 17"); | ||
else | ||
System.out.println("Found foo " + found + " with fooInt " + found.getFooInt()); | ||
``` | ||
|
||
Query the table for all `Bar` with barStr == "Some String" (Bar has the barStr property mapped to an attribute | ||
using `@XmlAttribute`). | ||
```java | ||
XPathExpression<Object> expression = XPathFactory.instance().compile("bar/@barStr"); | ||
XpathQuery query = XpathQuery.eq(expression, "Some String"); | ||
|
||
Cursor<Bar> barCursor = barTable.find(query); | ||
try{ | ||
|
||
int i = 0; | ||
for(Bar bar : barCursor){ | ||
System.out.println("Found bar " + bar); | ||
i++; | ||
} | ||
System.out.println("Found " + i + " Bars"); | ||
}finally{ | ||
//Always close cursors! | ||
barCursor.close(); | ||
} | ||
``` | ||
|
||
Atomically update each `Bar` with barStr == "Some String" to set barDouble to 17.4 | ||
```java | ||
XpathQuery query = XpathQuery.eq(XPathFactory.instance().compile("bar/@barStr"), "Some String"); | ||
XpathUpdate update = XpathUpdate.set(XPathFactory.instance().compile("bar/barDouble"), 17.4); | ||
|
||
int rowsUpdated = barTable.update(query, update); | ||
|
||
System.out.println(rowsUpdated + " rows were updated"); | ||
Bar bar = barTable.findOne(query); | ||
System.out.println("updated bar has barDouble " + bar.getBarDouble()); | ||
``` | ||
|
||
An example of a table persisted to an XML file: | ||
```XML | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<db:table xmlns:db="http://xflat.gburgett.org/xflat/db" db:name="Foo"> | ||
<db:row db:id="627070d8-0b9b-4154-aa01-eafd0a388c54"> | ||
<foo db:id="627070d8-0b9b-4154-aa01-eafd0a388c54"> | ||
<fooInt>26</fooInt> | ||
</foo> | ||
</db:row> | ||
</db:table> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters