Questions and Answers -- Getting started
First steps in GLORP: What's needed to map Smalltalk objects
to database rows.
What is needed to get you started?
To use GLORP to correctly map between an RDBMS and Smalltalk
objects, you need to do the following:
- Create a descriptor class. You do this by subclassing
Glorp.DescriptorSystem.
You then add your descriptions (see below) as methods to this class.
- A table model. This describes how the table in the
database should look like. The naming convention is
#tableForTABLENAMEINUPPERCASE:. For example:
tableForMYTABLE: aTable
(aTable createFieldNamed: 'id' type: platform sequence) bePrimaryKey.
aTable createFieldNamed: 'text' type: platform text.
aTable createFieldNamed: 'size' type: platform int4.
- A mapping descriptor. This describes how the fields in
your database map to instance variables of your objects. The naming
convention here is #descriptorForYourObjectClass.
descriptorForMyObject: aDesc
| t |
t := self tableNamed: 'mytable'.
aDesc table: t.
(aDesc newMapping: DirectMapping) from: #id to: (t fieldNamed: 'id').
(aDesc newMapping: DirectMapping) from: #text to: (t fieldNamed: 'text').
(aDesc newMapping: DirectMapping) from: #size to: (t fieldNamed: 'size').
- A class model (Optional for simple cases, this can be
inferred from the other descriptions). It needs to be called
classModelForYourClassName.
classModelForMyObject: model
model
newAttributeNamed: #id;
newAttributeNamed: #text;
newAttributeNamed: #size;
- Declare all tables used. The method #allTableNames
is supposed to return a Collection of all your tables:
allTableNames
^ #( 'mytable' )
- Declare all classes used. The method #constructAllClasses
should list all classes used as in:
constructAllClasses
^ (super constructAllClasses)
add: MyClass;
yourself
You can now instruct glorp to build the tables in the database
according to your description. sess is a GlorpSession.
createSchema
"Execute me to create a fresh schema"
"First, get a GlorpSession somehow --> sess"
...
self dropTables: sess.
self createAllSequences: sess.
self createAllTables: sess.
createAllSequences: sess
sess system platform areSequencesExplicitlyCreated
ifTrue:
[sess system allSequences do:
[:each |
sess accessor createSequence: each
ifError: [:error | Transcript show: error messageText]]]
createAllTables: sess
sess system allTables do:
[:each |
sess accessor createTable: each
ifError: [:errorx | Transcript show: errorx messageText]].
sess system allTables do:
[:each |
sess accessor createTableIndexes: each
ifError: [:errorx | Transcript show: errorx messageText]].
sess system allTables do:
[:each |
sess accessor createTableFKConstraints: each
ifError: [:errorx | Transcript show: errorx messageText]]
How to connect to a database
connectToDatabase
| login accessor sess |
login := (Login new)
database: PostgreSQLPlatform new;
username: 'dbusername';
password: 'dbpassword';
connectString: 'localhost_databasename'.
accessor := DatabaseAccessor forLogin: login.
accessor loginIfError: [:err | Transcript nextPutAll: err].
(sess := GlorpSession new) system: (DBAdaptor forPlatform: login database).
sess accessor: accessor.
^sess
disconnectFromDatabase: sess
sess ifNotNil: [ sess accessor logout ]