Simple database: Difference between revisions
Content added Content deleted
(→{{header|Wren}}: Added a second version based on new Wren-table module.) |
No edit summary |
||
Line 2,756: | Line 2,756: | ||
item5, 2018-03-23 16:49:46, cat3 |
item5, 2018-03-23 16:49:46, cat3 |
||
</pre> |
</pre> |
||
=={{header|M2000 Interpreter}}== |
|||
Write the code on a UTF-8 text file as tool.gsb |
|||
Assign gsb files to open with M2000.exe (the M2000 environment) |
|||
Open the folder where exist tool.gsb in a cmd window |
|||
the follow the > lines in the out session |
|||
The format of file is xml, Utf8 with a BOM. |
|||
The M2000 envirnoment not used here, all the output redirect to console. (Although the environment is a Window program, we can attach the console). |
|||
<syntaxhighlight lang="m2000 interpreter"> |
|||
MODULE GLOBAL interpret { |
|||
global filename$="base1.xml" |
|||
module latest { |
|||
PrintConsoleLn("The latest entry is:") |
|||
if exist(filename$) else exit |
|||
declare xml xmlData |
|||
with xml, "xml" as doc$, "beautify" as beautify |
|||
doc$=string$(eval$(buffer(filename$)) as UTF8dec) |
|||
with xml, "lastchild" set child |
|||
with child,"attr" as attr$() |
|||
PrintConsoleLn(attr$("name")+","+attr$("tag")+","+attr$("date")) |
|||
declare xml nothing |
|||
} |
|||
module latestForEachTag { |
|||
PrintConsoleLn("latest entry for each tag:") |
|||
if exist(filename$) else exit |
|||
declare xml xmlData |
|||
with xml, "xml" as doc$, "beautify" as beautify |
|||
doc$=string$(eval$(buffer("base1.xml")) as UTF8dec) |
|||
with xml, "firstchild" as firstchild |
|||
child=firstchild |
|||
with child,"attr" as attr$() |
|||
inventory alfa |
|||
do |
|||
if not exist(alfa, attr$("tag")) then |
|||
append alfa, attr$("tag"):=child |
|||
else |
|||
return alfa, attr$("tag"):=child |
|||
end if |
|||
Method xml, "EndOffChilds", &child as ok |
|||
when ok |
|||
sort alfa |
|||
k=each(alfa) |
|||
while k |
|||
child=eval(k) |
|||
PrintConsoleLn(attr$("name")+","+attr$("tag")+","+attr$("date")) |
|||
end while |
|||
declare xml nothing |
|||
} |
|||
module All { |
|||
PrintConsoleLn("All entries sorted by date:") |
|||
if exist(filename$) else exit |
|||
declare xml xmlData |
|||
with xml, "xml" as doc$, "beautify" as beautify |
|||
doc$=string$(eval$(buffer("base1.xml")) as UTF8dec) |
|||
with xml, "firstchild" as firstchild |
|||
child=firstchild |
|||
with child,"attr" as attr$() |
|||
inventory alfa |
|||
i=0 |
|||
do |
|||
// prevent same keys using a unique patch key |
|||
append alfa, attr$("date")+str$(i,"000000"):=child |
|||
i++ |
|||
Method xml, "EndOffChilds", &child as ok |
|||
when ok |
|||
sort alfa |
|||
k=each(alfa) |
|||
while k |
|||
child=eval(k) |
|||
PrintConsoleLn(attr$("name")+","+attr$("tag")+","+attr$("date")) |
|||
end while |
|||
declare xml nothing |
|||
} |
|||
module add (line$) { |
|||
line$=trim$(line$) |
|||
if line$="" then exit |
|||
declare xml xmlData |
|||
with xml, "xml" as doc$, "beautify" as beautify |
|||
bom$=str$(format$("\uef\ubb\ubf")) |
|||
// len(bom$)=1.5 (1.5*2=3 bytes) |
|||
k=0 |
|||
if exist(filename$) then try {k=filelen(filename$)} |
|||
if k<10 then |
|||
method xml, "PrepareNodeSimple", "xml" as ProcessInstructions |
|||
method xml, "PlaceAttributeToNode", ProcessInstructions, "version", "1.0" |
|||
method xml, "PlaceAttributeToNode", ProcessInstructions, "encoding", "utf-8-sig" |
|||
method xml, "PlaceProcessingInstructions", ProcessInstructions |
|||
method xml, "PrepareNode", "MyFile" as Node |
|||
method xml, "InsertNode", Node |
|||
else |
|||
doc$=string$(eval$(buffer(filename$)) as UTF8dec) |
|||
end if |
|||
a$=""""+line$ |
|||
def name$, tag$,date$ |
|||
do |
|||
a$=rightpart$(a$, """") : what$=lcase$(trim$(leftpart$(a$, """"))) |
|||
if what$="" then exit else loop |
|||
a$=rightpart$(a$, """") :par$=leftpart$(a$, """") |
|||
select case what$ |
|||
case "name" |
|||
name$=par$ |
|||
case "tag" |
|||
tag$=par$ |
|||
case "date" |
|||
date$=par$ |
|||
end select |
|||
always |
|||
if name$<>"" and date$<>"" then |
|||
method xml, "PrepareNode", "Row", "" as Node1 |
|||
method xml, "PlaceAttributeToNode", Node1, "name", name$ |
|||
if tag$<>"" then method xml, "PlaceAttributeToNode", Node1, "tag", tag$ |
|||
method xml, "PlaceAttributeToNode", Node1, "date", date$ |
|||
method xml, "AppendChild", Node1 |
|||
open filename$ for wide output as #f |
|||
print #f, bom$;string$(doc$ as UTF8enc); |
|||
close #f |
|||
beautify=-4 |
|||
PrintConsoleLn(doc$) |
|||
end if |
|||
declare xml nothing |
|||
} |
|||
declare FreeConsole lib "Kernel32.FreeConsole" |
|||
declare GetStdHandle lib "Kernel32.GetStdHandle" {long a} |
|||
declare AttachConsole lib "Kernel32.AttachConsole" {long a} |
|||
declare CloseHandle lib "Kernel32.CloseHandle" {long a} |
|||
declare global WriteCons Lib "Kernel32.WriteConsoleW" {long cons, a$, long n, Long p, long u} |
|||
long STD_OUTPUT_HANDLE=-11 |
|||
global retvalue |
|||
buffer clear retvalue as long |
|||
ret=AttachConsole(-1) |
|||
global m=GetStdHandle(STD_OUTPUT_HANDLE) |
|||
if ret=0 then beep: exit |
|||
if not islet then |
|||
try { |
|||
open "tool.bat" for output as #f |
|||
print #f, {@}+appdir$+{m2000.exe data {%*}: dir %cd%:load tool |
|||
} |
|||
close #f |
|||
} |
|||
PrintConsoleLn("") |
|||
dos "tool.bat" |
|||
else |
|||
read cmd$ |
|||
cmd$=trim$(cmd$)+" " |
|||
select case lcase$(leftpart$(cmd$, " ")) |
|||
case "add" |
|||
add rightpart$(cmd$," ") |
|||
case "latest" |
|||
latest |
|||
case "latest-per-tag" |
|||
latestForEachTag |
|||
case "all-entries" |
|||
all |
|||
case else |
|||
help() |
|||
end select |
|||
end if |
|||
call void closehandle(m) |
|||
call void freeconsole() |
|||
Sub PrintConsole(a$) |
|||
Call Void WriteCons(m, a$, Len(a$), retvalue(0), 0) |
|||
End Sub |
|||
Sub PrintConsoleLn(a$) |
|||
a$+={ |
|||
} |
|||
Call Void WriteCons(m, a$, Len(a$), retvalue(0), 0) |
|||
End Sub |
|||
Sub Help() |
|||
h$={Commands: |
|||
tool add name "anyname" tag "tagtext" date "YYYY.MM.DD.HH:MM" |
|||
tool latest |
|||
tool latest-per-tag |
|||
tool all-entries |
|||
} |
|||
PrintConsole(h$) |
|||
End Sub |
|||
} |
|||
module interpret1 { |
|||
try {interpret} |
|||
} |
|||
interpret1: end |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
first time the program make a tool.bat |
|||
>tool.gsb |
|||
So now we can call the tool bat. |
|||
>tool |
|||
Commands: |
|||
tool add name "anyname" tag "tagtext" date "YYYY.MM.DD.HH:MM" |
|||
tool latest |
|||
tool latest-per-tag |
|||
tool all-entries |
|||
>tool add name "BOB" tag "EAT A LOT" date "2022.10.21.04:00" |
|||
>tool add name "JOHN" tag "DRINK WATER" "date 2022.10.15.12:00" |
|||
>tool add name "PAUL" tag "EAT A LOT" date "2022.10.13.22:00" |
|||
>tool add name "SUZAN" tag "DRINK WATER" date "2022.10.13.12:00" |
|||
>tool add name "PHILIP" tag "DRINK WATER" date "2022.10.13.10:00" |
|||
>tool add name "MONDY" tag "EAT A LOT" date "2022.10.10.12:00" |
|||
>tool add name "MARY" tag "PIZZA FRIENDLY" date "2022.10.10.12:00" |
|||
>tool add name "DONALD" tag "PIZZA FRIENDLY" date "2022.10.10.02:00" |
|||
This is the last print from last tool addd: |
|||
<?xml version="1.0" encoding="utf-8-sig"?> |
|||
<MyFile> |
|||
<Row name="BOB" tag="EAT A LOT" date="2022.10.21.04:00"></Row> |
|||
<Row name="PAUL" tag="EAT A LOT" date="2022.10.13.22:00"></Row> |
|||
<Row name="SUZAN" tag="DRINK WATER" date="2022.10.13.12:00"></Row> |
|||
<Row name="PHILIP" tag="DRINK WATER" date="2022.10.13.10:00"></Row> |
|||
<Row name="MONDY" tag="EAT A LOT" date="2022.10.10.12:00"></Row> |
|||
<Row name="MARY" tag="PIZZA FRIENDLY" date="2022.10.10.12:00"></Row> |
|||
<Row name="DONALD" tag="PIZZA FRIENDLY" date="2022.10.10.02:00"></Row> |
|||
</MyFile> |
|||
>tool latest |
|||
The latest entry is: |
|||
DONALD,PIZZA FRIENDLY,2022.10.10.02:00 |
|||
>tool latest-per-tag |
|||
latest entry for each tag: |
|||
PHILIP,DRINK WATER,2022.10.13.10:00 |
|||
MONDY,EAT A LOT,2022.10.10.12:00 |
|||
DONALD,PIZZA FRIENDLY,2022.10.10.02:00 |
|||
>tool all-entries |
|||
All entries sorted by date: |
|||
DONALD,PIZZA FRIENDLY,2022.10.10.02:00 |
|||
MONDY,EAT A LOT,2022.10.10.12:00 |
|||
MARY,PIZZA FRIENDLY,2022.10.10.12:00 |
|||
PHILIP,DRINK WATER,2022.10.13.10:00 |
|||
SUZAN,DRINK WATER,2022.10.13.12:00 |
|||
PAUL,EAT A LOT,2022.10.13.22:00 |
|||
BOB,EAT A LOT,2022.10.21.04:00 |
|||
</pre> |
|||
=={{header|Nim}}== |
=={{header|Nim}}== |