After creating a new database, create two tables which will be filled with mass data. I prepared a simple SQL script for creating tables below (you might be required to change it slightly so it can work with your DBMS):
create table Customer( id numeric(18, 0), first_name varchar(255), last_name varchar(255), email varchar(255), order_id numeric(18, 0), foreign key order_id references Order(id) ) create table Order( id numeric(18, 0), product_number int8 )
Now you have to prepare Scala specification for mass-data loading. You could use SQLParser to make things easier and pre-generate it from the DDL script. I'm not going to use it here, since right now it supports selected products only.
First let's describe database schema in Scala specification:
DatabaseSpecification( new Table( "Customer", 100, Map ( "id" -> null, "first_name" -> null, "last_name" -> null, "email" -> null, "order_id" -> null) ), new Table( "Order", 100, Map ( "id" -> null, "product_number" -> null) ) )
I believe it is pretty straightforward and resembles typical DDL script. Description consists of all tables and columns you'd like to fill. The magic number '100' - 2nd argument of Table constructor - states how many tuples should be filled for each table.
As you've probably noticed there is always a 'null' value on the right side of an arrow. It has to be changed to a certain generator class for every column.
Now you have to decide which generator you'd like to use for each column. Of course if there were no existing generators you could write one by subclassing Generator trait.
Map ( "id" -> new Increment(), "first_name" -> new StringGenerator(5, 12, StringConstants.ALPHABETIC), "last_name" -> new StringGenerator(5, 12, StringConstants.ALPHABETIC), "email" -> new StringGenerator(5, 10) + new Constant("@") + new StringGenerator(5, 10) + new Constant(".com") "order_id" -> new Unique(new ForeignKey("order", "id", loader.getConnection)) )
Map ( "id" -> new Increment(), "product_number" -> new NumberGenerator(1, 500) )
You should prepare a file (dage.properties) with connection properties and put it in the same directory as scala file:
dage.jdbc.driver_class=org.postgresql.Driver dage.jdbc.url=jdbc:postgresql://localhost:5432/example01 dage.jdbc.username=<YOUR_USERNAME> dage.jdbc.password=<YOUR_PASSWORD>
Now you're ready to execute the specification:
object Example { def main(args : Array[String]) = { val properties = new Properties(); properties.load(Example.getClass().getResourceAsStream("dage.properties")); val loader = new DatabaseLoader(properties); val specification = DatabaseSpecification( new Table( "Customer", 100, Map ( "id" -> new Increment(), "first_name" -> new StringGenerator(5, 12), "last_name" -> new StringGenerator(5, 12), "email" -> new StringGenerator(5, 10) + new Constant("@"), + new StringGenerator(5, 10) + new Constant(".com"), "order_id" -> new Unique(new ForeignKey("order", "id", loader.getConnection)) ), new Table( "Order", 100, Map ( "id" -> new Increment(), "product_number" -> new NumberGenerator(1, 500)) ) ) loader.fillDatabase(specification); } }
This is it :) Pretty easy, ha ? More information about Dage features can be found in Scaladoc .