db-example.txt


\ db-example 99.3.27 7:10 pm NAB

\ Creates a new db "MyDatabase"
\ (creator id 'myid', type 'DATA')
\ and writes a string to it.

needs DataMgr
needs zstrings
needs ids
needs core-ext

: string>newhandle
( c-addr u dbr. -- handle. )
\ Create new block in storage RAM:
  2>r  dup s>d  2r>  DmNewHandle
\ Lock the handle:
  2dup 2>r MemHandleLock 2>r
\ Write the string to the block:
  swap >abs  rot s>d  2swap  0.  2r>
  DmWrite throw
\ Unlock the handle:
  2r> 2dup MemHandleUnlock drop ;

(id) myid 2constant creator-id

: dbname  z" MyDatabase" ;

: somedata  s" One Two Three Four" ;

2variable dbr
variable index

: demo ( -- )
\ If already existing, delete:
  dbname  drop >abs
  cardnum @ DmFindDatabase 
  cardnum @ DmDeleteDatabase drop

\ Create db:
  false  \ not a resource db
  [id] DATA  \ type
  creator-id  dbname  CreateDB

  DmModeReadWrite dbname
  OpenDB dbr 2!

  somedata  dbr 2@ string>newhandle

  0.  2swap  index >abs  dbr 2@
  DmAttachRecord throw

  dbr 2@ CloseDB ;

demo
.s \ should report 'stack empty'


  HTMLized by Forth2HTML