Simple database: Difference between revisions

Content added Content deleted
(→‎{{header|REXX}}: add output)
(Added Bracmat example)
Line 24: Line 24:


See also [[Take notes on the command line]] for a related task.
See also [[Take notes on the command line]] for a related task.

=={{header|Bracmat}}==
This is a rather minimal solution. The program is run from the command line of the operating system, in this example the Windows command prompt. The program is stored in a file called 'sdb':
<lang bracmat> whl
' ( arg$:?command
& ( get'db
| (db=1)&lst$(db,db,NEW)
)
& !command
: ( add
& :?name:?tag:?date
& whl
' ( arg$:?argmnt
& arg$:?value
& (!argmnt.!value)
: ( (title|name.?name)
| (category|tag.?tag)
| (date.?date)
)
)
& ( !name:~
& !tag:~
& !date:~
& ( !db:?*!tag^(?+(!date.!name)+?)*?
& out$"This record already exists"
| !tag^(!date.!name)*!db:?db
& lst$(db,db,NEW)
)
| out$"invalid data"
)
| latest
& :?date
& nothing found:?latest
& ( !db
: ?
* ?tag
^ ( ?
+ ( (>!date:?date.?name)
& (!name,!tag,!date):?latest
& ~
)
+ ?
)
* ?
| out$!latest
)
| latest/category
& :?date:?latests:?latest
& ( !db
: ?
* ( ?tag
& !latests !latest:?latests
& :?latest:?date
)
^ ( ?
+ ( (>!date:?date.?name)
& (!name,!tag,!date):?latest
& ~
)
+ ?
)
* ?
| !latests !latest:?latests&out$!latests
)
| sorted
& 0:?sorted
& ( !db
: ?
* ?tag
^ ( ?
+ ( (?date.?name)
& (!date.!name,!tag,!date)+!sorted:?sorted
& ~
)
+ ?
)
* ?
| whl
' (!sorted:(?.?row)+?sorted&out$!row)
)
)
);
</lang>
First we add some records, a some ships that arrived at the harbour in Rotterdam today.
<pre>bracmat "get$sdb" add name "CORONA BULKER" tag "BULK CARRIER" date "2014.10.21.04:00"
bracmat "get$sdb" add name "FPMC 21" tag "CHEMICAL TANKER" date "2014.10.15.12:00"
bracmat "get$sdb" add name "CHINA PROGRESS" tag "BULK CARRIER" date "2014.10.13.22:00"
bracmat "get$sdb" add name "FAIRCHEM YUKA" tag "CHEMICAL TANKER" date "2014.10.13.12:00"
bracmat "get$sdb" add name "NAVE COSMOS" tag "CHEMICAL TANKER" date "2014.10.13.10:00"
bracmat "get$sdb" add name "GOLDEN ICE" tag "BULK CARRIER" date "2014.10.10.12:00"
bracmat "get$sdb" add name "GHAZAL" tag "CRUDE OIL" date "2014.10.10.12:00"
bracmat "get$sdb" add name "HS MEDEA" tag "CRUDE OIL" date "2014.10.10.02:00"</pre>
Instead of 'name' you can use 'title' and instead of 'tag' you can use 'category'. The date has to be year first and day last, followed by time information if you like. No attempt is made to validate the date/time. Now the queries:
<pre>prompt> bracmat "get$sdb" latest
CORONA BULKER,BULK CARRIER,2014.10.21.04:00

prompt> bracmat "get$sdb" latest/category
(CORONA BULKER,BULK CARRIER,2014.10.21.04:00)
(FPMC 21,CHEMICAL TANKER,2014.10.15.12:00)
(GHAZAL,CRUDE OIL,2014.10.10.12:00)

prompt> bracmat "get$sdb" sorted
HS MEDEA,CRUDE OIL,2014.10.10.02:00
GHAZAL,CRUDE OIL,2014.10.10.12:00
GOLDEN ICE,BULK CARRIER,2014.10.10.12:00
NAVE COSMOS,CHEMICAL TANKER,2014.10.13.10:00
FAIRCHEM YUKA,CHEMICAL TANKER,2014.10.13.12:00
CHINA PROGRESS,BULK CARRIER,2014.10.13.22:00
FPMC 21,CHEMICAL TANKER,2014.10.15.12:00
CORONA BULKER,BULK CARRIER,2014.10.21.04:00</pre>

The database file 'db' looks like this:
<pre>db= "BULK CARRIER"
^ ( ("2014.10.10.12:00"."GOLDEN ICE")
+ ("2014.10.13.22:00"."CHINA PROGRESS")
+ ("2014.10.21.04:00"."CORONA BULKER")
)
* "CHEMICAL TANKER"
^ ( ("2014.10.13.10:00"."NAVE COSMOS")
+ ("2014.10.13.12:00"."FAIRCHEM YUKA")
+ ("2014.10.15.12:00"."FPMC 21")
)
* "CRUDE OIL"^(("2014.10.10.02:00"."HS MEDEA")+("2014.10.10.12:00".GHAZAL));</pre>

Use is made of Bracmat's automatic normalization of algebraic formula to turn the data into a hierarchical structure, with the tag as the top level and the date/time immediately below that level.


=={{header|C}}==
=={{header|C}}==