Tutorial
Using mdTypes [EN]
von MOD | Seite 9 von 12 |
mdTypes comes with another feature, which will make things a lot easier. With the persistence api one can simply save objects and whole collections into different databases.
Currently, mdTypes supports SQLite, MySQL and an own file based format. The classes are called:
- mdSQLitePersistence
- mdMySQLPersistence
- mdTypesPersistence
All classes extend the mdPersistence class. This class is pretty empty, so usually you won't use it. Nevertheless, if you plan to create an own persistence class for another database, just extend this class, too, and it will automatically work with all mdTypes classes and programs, which are using them.
To use the persistence api with your own classes, they have to be serializable/deserializable. Have another look at page 4 to see how to do it.
So, let's try to persist any serializable class. First of all, we will create some DEFINEs:
#Define PERSISTENCE_CLASS mdSQLitePersistence
#Define DB_NAME "MyApplicationDB_mdSQLitePersistence"
As the persistent api allows to switch classes, PERSISTENCE_CLASS is just a helper for later replacements. Here it's the SQLite persistence. DB_NAME is the name of the database.
Now, we have to prepare the tables. For this, just create a mdMap(String, String) and pass an object of your class together with a table name:
Dim As MyFoo tempFoo
Dim As String temp
Dim As mdMap(String, String) tables
temp = tables.put("MyFoo", tempFoo)
You can add as many tables as you wish. The columns will be automatically created with the information of your object. But so far we don't have anything, so we have to create the database:
Dim As PERSISTENCE_CLASS sql = DB_NAME
sql.createDatabase(DB_NAME, tables)
If you want to delete the database, you can use sql.removeDatabase(DB_NAME), to update sql.updateDatabase(DB_NAME, tables). Updating a database will remove all tables, which are not passed in the list anymore, while new tables will be created. Existing tables with different columns will also be updated, if possible.
To create new objects in the database, you can do something like this:
Dim As MyFoo newFoo
'...fill newFoo with important information
sql.createObject("MyFoo", newFoo)
Zusätzliche Informationen und Funktionen | |||||||
---|---|---|---|---|---|---|---|
|