Data type conversion
Some datastores have less expressive data types than what you need - and certainly less expressive than Java's type system. Therefore MetaModel provides an API for automatic conversion of data types between their physical values and their logical values.
An obvious example is CSV files, which only host String values, since their storage is plain text files. But often times there are columns within a CSV file that should be interpreted eg. as numbers, dates or booleans. To overcome this issue, you can apply MetaModel's type converters. It will look like this:
1 UpdateableDataContext dc = DataContextFactory.createCsvDataContext(...); 2 Column numberCol = dc.getColumnByQualifiedLabel("my_number"); 3 Column booleanCol = dc.getColumnByQualifiedLabel("my_boolean"); 4 dc = Converters.addTypeConverter(dc, numberCol, new StringToIntegerConverter()); 5 dc = Converters.addTypeConverter(dc, booleanCol, new StringToBooleanConverter());
Afterwards, all queries and updates on these columns will automatically get their types converted, so that your view of the data can use the proper Java types.
You can also ask MetaModel to auto-detect the converters to apply. For doing that MetaModel will inspect the result of a query and assert if any of the columns are plausible candidates for conversion:
1 UpdateableDataContext dc = DataContextFactory.createCsvDataContext(...); 2 Table table = dc.getDefaultSchema().getTables(); 3 Map<Column, TypeConverter<?,?>> converters = 4 Converters.autoDetectConverters(dc, table, 1000); // query at max 1000 rows 5 dc = Converters.addTypeConverters(dc, converters);
For more information, take a look at the Converters class which has useful static methods for applying converters easily.