Simple database: Difference between revisions

Content added Content deleted
m (moved Simple Database to Simple database: Conform to RC naming rules)
(file format, schmile format)
Line 22: Line 22:


The fileformat on disk should be human readable, but it need not be standardized. A natively available format that doesn't need an external library is preferred. Avoid developing your own format however if you can use an already existing one. If there is no existing format available pick one of: [[JSON]], [[S-Expressions]], YAML, or [[wp:Comparison_of_data_serialization_formats|others]].
The fileformat on disk should be human readable, but it need not be standardized. A natively available format that doesn't need an external library is preferred. Avoid developing your own format however if you can use an already existing one. If there is no existing format available pick one of: [[JSON]], [[S-Expressions]], YAML, or [[wp:Comparison_of_data_serialization_formats|others]].

=={{header|Unix Shell}}==
This format is guaranteed to be human readable: if you can type it, you can read it.
<lang bash>#!/bin/sh

db_create() {
mkdir ./"$1" && mkdir "./$1/.tag" && echo "Create DB \`$1'"
}

db_delete() {
rm -r ./"$1" && echo "Delete DB \`$1'"
}

db_show() {
if [ -z "$2" ]; then show_help; fi
for x in "./$1/$2/"*; do
echo "$x:" | sed "s/.*\///"
cat "$x" | sed "s/^/ /"
echo
done

printf "Tags: "
ls "./$1/$2/.tag"
}

db_tag() {
local db="$1" item="$2"
shift
shift
for tag in $@; do
mkdir "./$db/.tag/$tag"
ln -s "$PWD/$db/$item" "./$db/.tag/$tag/"
touch "./$db/$item/.tag/$tag"
done
}

show_help() {
echo "Usage: $0 command [args]"
echo "Commands:"
cat $0 | grep ") ##" | grep -v grep | sed 's/) ## /:\t/'
exit
}

if [ -z "$1" ]; then show_help; fi

action=$1 it=database
shift
case $action in
create) ## db -- create $it
db_create "$@" ;;

drop) ## db -- delete $it
db_delete "$@" ;;

add) ## db item -- add new item to $it
mkdir -p "./$1/$2/.tag" && touch "./$1/$2/Description" ;;

rem) ## db item -- delete item from $it
rm -r "./$1/$2"
rm "./$1/.tag/"*"/$2"
;;

show) ## db item -- show item
db_show "$@" ;;

newtag) ## db new-tag-name -- create new tag name
mkdir "./$1/.tag/$2" ;;

prop) ## db item property-name property-content -- add property to item
echo "$4" > "./$1/$2/$3" ;;

tag) ## db item tag [more-tags...] -- mark item with tags
db_tag "$@" ;;

last) ## db -- show latest item
ls "$1" --sort=time | tail -n 1
;;

list) ## db -- list all items
ls "$1" -1 --sort=time
;;

last-all) ## db -- list items in each category
for x in "$1/.tag/"*; do
echo "$x" | sed 's/.*\//Tag: /'
printf " "
ls "$x" --sort=time | tail -n 1
echo
done
;;

help) ## this message
show_help
;;

*) echo Bad DB command: $1
show_help
;;
esac</lang>
Sample usage (assuming script is named "sdb"):<lang>$ sdb create CDs
Create DB `CDs'
$ sdb create CDs
Create DB `CDs'
$ sdb add CDs Bookends
$ sdb prop CDs Bookends artists "Simon & Garfunkel"
$ sdb add CDs "Ode to joy"
$ sdb prop CDs "Ode to joy" artist "Beethoven"
$ sdb tag CDs Bookends rock folk # I'm not sure about this
$ sdb tag CDs "Ode to joy" classical
$ sdb show CDs Bookends
Description:

artists:
Simon & Garfunkel

Tags: folk rock
$ sdb prop CDs "Ode to joy" Description "Sym. No. 9"
$ sdb show CDs "Ode to joy"
Description:
Sym. No. 9

artist:
Beethoven

Tags: classical
$ sdb last-all CDs
Tag: classical
Ode to joy

Tag: folk
Bookends

Tag: rock
Bookends

$ sdb drop CDs
Delete DB `CDs'
$</lang>