Poor Man's SQL
select
For convenience, a select Lisp glue function is provided as a front-end to the 'select' predicate. Note that this function does not evaluate its arguments (it is intended for interactive use), and that it supports only a subset of the predicate's functionality. The syntax resembles SELECT in the SQL language, for example:# SELECT * FROM Person : (select +Person) # Step through the whole database {2-o} (+Man) nm "Adalbert Ferdinand Berengar Viktor of Prussia" dat 688253 ma {2-j} pa {2-h} fin 711698 {2-1B} (+Man) nm "Albert Edward" dat 664554 job "Prince" mate {2-f} kids ({2-1C} {2-1D} {2-1E} {2-1F} {2-1G} {2-1H} {2-1I} {2-g} {2-a}) fin 680370 ...
# SELECT * FROM Person WHERE nm LIKE "%Edward%" : (select +Person nm "Edward") # Show all Edwards {2-;} (+Man) nm "Edward" dat 717346 job "Prince" ma {2-:} pa {2-A} {2-1B} (+Man) nm "Albert Edward" dat 664554 job "Prince" kids ({2-1C} {2-1D} {2-1E} {2-1F} {2-1G} {2-1H} {2-1I} {2-g} {2-a}) mate {2-f} fin 680370 ...
# SELECT nm, dat FROM Person WHERE nm LIKE "%Edward%" : (select nm dat +Person nm "Edward") "Edward" "1964-03-10" {2-;} "Albert Edward" "1819-08-26" {2-1B} "George Edward" NIL {2-R} "Edward Augustus Hanover" NIL {2-1K} ...
# SELECT dat, fin, p1.nm, p2.nm # FROM Person p1, Person p2 # WHERE p1.nm LIKE "%Edward%" # AND p1.job LIKE "King%" # AND p1.mate = p2.mate -- Actually, in a SQL model we'd need # -- another table here for the join : (select dat fin nm (mate nm) +Person nm "Edward" job "King") "1894-06-23" "1972-05-28" "Edward VIII" "Wallace Simpson" {2-T} "1841-11-09" NIL "Edward VII" "Alexandra of Denmark" {2-a} -> NIL
update
In addition (just to stay with the SQL terminology ;-), there is also an update function. It is a front-end to the set!> and put!> transaction methods, and should be used when single objects in the database have to be modified by hand.In principle, it would also be possible to use the edit function to modify a database object. This is not recommended, however, because 'edit' does not know about relations to other objects (like Links, Joints and index trees) and may easily cause database corruption.
In the most general case, the value of a property in a database object is changed with the 'put!>' method. Let's look at "Edward" from the previous examples:
: (show '{2-;}) {2R} (+Man) job "Prince" nm "Edward" dat 717346 ma {2-:} pa {20A} -> {2-;}
We might change the name to "Johnny" with 'put!>':
: (put!> '{2-;} 'nm "Johnny") -> "Johnny"However, an easier and less error-prone prone way - especially when more than one property has to be changed - is using update. It presents the value (the list of classes) and then each property on its own line, allowing the user to change it with the command line editor.
Just hitting ENTER will leave that property unchanged. To modify it, you'll typically hit ESC to get into command mode, and move the cursor to the point of change.
For properties with nested list structures ('+List +Bag'), 'update' will recurse into the data structure.
: (update '{2-;}) {2-;} (+Man) # ENTER nm "Johnny" # Modified the name to "Johnny" ma {2-:} # ENTER pa {2-A} # ENTER dat 1960-03-10 # Modified the year from "1964" to "1960" job "Prince" # ENTER -> {2-;}All changes are committed immediately, observing the rules of database synchronization so that any another user looking at the same object will have his GUI updated correctly.
To abort 'update', hit 'Ctrl-X'.
If only a single property has to be changed, 'update' can be called directly for that property:
: (update '{2-;} 'nm) {2-;} nm "Edward" ...
https://picolisp.com/wiki/?tutsql
07jun12 | abu |