-
Notifications
You must be signed in to change notification settings - Fork 4
Overrides: Type metadata
Every so often you will need to override some of the setup of a SpodObject, for example the TableName or the identifier. Currently this is possible with the use of the Type metadata.
[Type(name='NewTableName')] public class OldTableName extends SpodObject {
This is excellent for when you need to migrate certain features to another table or wish to have a certain table name that just makes more sense when viewing the database.
Another thing you can do with the Type annotation is override the default unique identifier (or id). Currently Spod will try it's best efforts to locate the id for the class which will be it's primary identifier for all consequent look ups. If for some reason you would like to not to call this, you can simply use the following metadata
[Type(identifier='type')] public var newIdentifierName : int;
You can also make you identifier not be a type of number, but subsequent calls to selectById
will fail. So you will have to write your own methods to retrieve that item (in the future I will implement selectByxxx, but it's not on my priority list at them moment).
From version 0.5 onwards it is possible to use the Type annotation to be able to change the names of columns for within the database. This is also idea if you need to mary a class to an existing database and you do not want to change your class. It's also handy to enable you to refactor the class without changing your database column names. If you did change your class you would have to manually update the database when Spod throws an SchemaError.
[Type(name='altIdentifierName')] public var newIdentifierName : int;
There are a few clauses here though...
- The Performance of Spod drops. This is because of the underlying way AIR SQLite Statement works. It requires a type of class, so when you do a select it automatically uses a type of class i.e. User has property name and the database has a column called name and the two will be synced. This can no longer happen when we've got a Type annotation. Currently I use a Object type (of dynamic) which allows the statement to push any column values on to said Object. So when the results are parsed from the SQLResult class I then loop through the results and mary the dynamic Object type to the statically made type. So if you renamed name on User to username, it will come out of the Object as username and then using the Spod schema I push the value on to name in User. Object creation twice as well as the for loop, unfortunately this is how AIR SQLite works and there isn't much I can do about this, although one possibility is to use some sort of Mixin to for go this, but that would require ByteCode injection and could be even slower.
- When doing a
WHERE
clause you will have to use the Type annotation name and not the class property name. The value you feed into theWHERE
clause will be the one in the end SQL statement so it can't be the class property (maybe in the future).