Last modified 3 years ago Last modified on 02/13/2011 02:35:39 PM

TOC? http://www.eobjects.dk/resources/IconMetaModel.png

MetaModel Developers guide

About | Developers guide | Webcast demo | Database compliancy | License | Download

This page will help you get starting with developing applications using the MetaModel API. We recommend that you refer to the Javadoc API Documentation for specifics with single classes or methods:

New: This wiki page is no longer to be considered the main homepage of MetaModel! This serves as a community place for documentation collaboration and ad hoc writing of snippets and small pieces of information.

Please be sure to visit the new official MetaModel website at http://metamodel.eobjects.org

Example use

DataContext dataContext;
//We can handle different types of datastores transparently of their type.
if (useJdbc()) {
  java.sql.Connection connection = ... //Obtain a JDBC connection
  dataContext = DataContextFactory.createJdbcDataContext(connection);
} else if (useCsv()) {
  dataContext = DataContextFactory.createCsvDataContext(new File("my_csv_file.csv"));
} else if (useExcel()) {
  dataContext = DataContextFactory.createExcelDataContext(new File("my_spreadsheet.xls"));
} else if (useOpenOffice()) {
  dataContext = DataContextFactory.createOpenOfficeDataContext(new File("my_oo_db.odb"));
} else if (useAccess()) {
  dataContext = DataContextFactory.createAccessDataContext(new File("my_access_db.mdb"));
} else if (useDbase()) {
  dataContext = DataContextFactory.createDbaseDataContext(new File("my_dbase_db.dbf"));
} else {
  dataContext = new DataContext(new MyCustomDataContextStrategy());
}

//All datastores has schemas, which in turn consist of tables and columns.
Schema[] schemas = dataContext.getSchemas();
Table customerTable = schemas[0].getTableByName("customer");
Column idColumn = customerTable.getColumnByName("id");
Column nameColumn = customerTable.getColumnByName("name");

//Queries are also transparent meaning that even complex queries with filters, groupings and sorting can be executed on any datastore.
Query q = new Query();
q.select(idColumn, nameColumn);
q.from(customerTable);
q.orderBy(idColumn).orderBy(nameColumn);

Column zipCodeColumn = customerTable.getColumnByName("zipCode");
q.where(zipCodeColumn, OperatorType.EQUALS_TO, 10005);

System.out.println(q);
//Yields: SELECT customer.id, customer.name FROM customer WHERE customer.zip_code = 10005 ORDER BY customer.id ASC, customer.name ASC

//Even though the query can be executed using SQL, it can also execute using the built-in query engine, which enables queries for datastores like CSV-files and Spreadsheets.
DataSet dataSet = dataContext.executeQuery(q);
while (dataSet.next()) {
  Row row = dataSet.getRow();
  System.out.println(row);
}

More examples and tutorials

Some more examples of using MetaModel in your code is available here:

The Query model

Abandoning the string-based query in favor of a type-safe and standard-based object oriented query model is in it-self quite an achievement. Depending on your experience with vendor-specific querying it may be perceived as a challenge and there are admittedly some trade-offs to the MetaModel approach. To give you an overview of the querying capabilities of MetaModel, here's an overview of the query model and the querying functionality that it contains:

The anatomy of a query

A query in MetaModel is based very closely on the SQL-99 specification. This means that the query is composed of these six clauses:

SELECT FROM WHERE GROUP BY HAVING ORDER BY

Additionally the query model can encompass subquerying, meaning that the FROM clause can contain other queries as well as tables and joins.

Functions available in SELECT clause

The following functions can be used in the SELECT clause to calculate aggregated values.

SUM COUNT AVG MAX MIN

Note also that COUNT can be parameterized with the special expression "*", meaning "count rows". We do, however, recommend adding this special select item using the Query.selectCount() method.

Operators available in WHERE / HAVING clause

These operators can be used to filter the result in either the WHERE clause or the HAVING clause.

EQUALS_TO (=) DIFFERENT_FROM (<>) LIKE HIGHER_THAN (>) LOWER_THAN (<)