[ -n "$myname" ] || { echo Use this as library; exit 1; } _ct=_columns _tb=cards _def=$mydir/cards.def cards_stamp=db/_col.stamp # タイムスタンプファイル logfile=tmp/init.log # 初期化SQLのログファイル cards_initdb() { echo "---------- init-db at `date` ----------" >> $logfile grep -v '^#' "$1" | { cat<<-EOF DROP TABLE IF EXISTS $_ct; CREATE TABLE $_ct( attrname text, attr text PRIMARY KEY, attrmode text, vtype text, option text ); CREATE TABLE IF NOT EXISTS $_tb(id text PRIMARY KEY); CREATE TABLE IF NOT EXISTS ${_tb}_s ( id, key text, val, bin blob, mtime text, FOREIGN KEY(id) REFERENCES $_tb(id) ON DELETE CASCADE ON UPDATE CASCADE, UNIQUE(id, key) ); CREATE TABLE IF NOT EXISTS ${_tb}_m( id, key text, val, bin blob, mtime text, FOREIGN KEY(id) REFERENCES $_tb(id) ON DELETE CASCADE ON UPDATE CASCADE, UNIQUE(id, key, val) ); EOF while IFS=: read aname attr atmode vtype opt; do cat<<-EOF REPLACE INTO $_ct VALUES( '$aname', '$attr', '$atmode', '$vtype', '$opt' ); EOF done } | tee -a $logfile | query && touch $cards_stamp # 日付けを記録 exit # テーブル初期化と同時にタイムスタンプファイルを作成 } cards_update() ( if [ -s "$_def" ]; then # 属性定義ファイルが存在するなら if [ ! -e "$cards_stamp" -o "$_def" -nt "$cards_stamp" ]; then echo Updating $_ct using $_def... 1>&2 cards_initdb "$_def" # 定義ファイルが新しければ自動的に再構築 fi fi )