Experiment based on : Pharo smalltalk glorp book
Metacello new
baseline: 'GlorpBook';
repository: 'github://rvillemeur/glorpbook/repository';
load
Domain model Class: #Person, stored in table PERSON in database.
- DescriptorSystem subclass
- DescriptorSystem subclass: #GlorpBookDescriptorSystem
- classModelForYourClass (ex: classModelForPerson) Messages use to list attributes of Model
- tableForYOURTABLE (ex: tableForPERSON) Message describing fields in the table
- descriptorForYourClass (ex: descriptorForPerson) Link between attributes ofs classModelForXXX and fields of tableForXXX
- System, session et database accessor.
- Create login message to retreive the login
- Use the Login you've just created to retreive the accessor
- You can then retrive the session session using the message sessionForLogin, which will link login, accessor and session.
Once connected to the database, you have to run in pharo workspace "session createTables" to create the table in your new database
Record you work in your session, use the message >> inUnitOfWorkDo: [ … ]
SQL | Glorp |
---|---|
Select * from TABLE | Session read: DomainClass |
Select * from TABLE where attributes = 'value' | Session read: DomainClass where: [:each | each attribute = 'value'] |
Select * from TABLE where attributes like 'value%' | Session read: DomainClass where: [:each | each attribute similarTo: 'value'] |
Update table TABLE Set attribute = 'value' Where condition = 'value2' | Instance := session readOneOf: DomainClass Where: [ :each | each condition = 'value2' ] Instance attribute: value. |
Delete from TABLE Where condition = 'value' | Instance := session readOneOf: DomainClass Where: [ :each | each condition = 'value2' ] Instance delete: value. Or (for multiple deletion) Session delete: DomainClass Where: [ :each | each condition = 'value2' ] |
Select * from TABLE where attributes = 'value' Order by | session read: DomainClass where: [:each | each attribute = 'value'];orderBy: [:each | each attribute = 'value'] |
Select * from TABLE where attributes = 'value' Group by | session read: DomainClass where: [:each | each attribute = 'value'];groupBy: [:each | each attribute = 'value'] |
Each of your persisted objects should have an instance of GlorpClassModel in the descriptor system containing all the attributes of your class.
attribute | relation |
---|---|
newAttributeNamed: #email | Used to define simple scalar/literal values such as Numbers, Strings, Dates, etc. |
newAttributeNamed: #address type: Address | Used to define 1:1 (one to one) relations with other objects of your domain model. |
newAttributeNamed: #invoices collectionOf: Invoice | Used to define 1:n (one to many) and n:m (many to many) relations of your class model with other models. |
newAttributeNamed: #invoices collection: collectionClass of: Invoice | Similar as the one above for 1:n and n:m relations, but you can define what kind of Collection is going to be used. |
newAttributeNamed: #counters dictionaryFrom: keyClass to: valueClass | Used to define an attribute that is a Dictionary where the key is key-Class and its values are instances of valueClass. |