sql builder little enhancement and user table field rename
[brisk.git] / sql / builder.sh
index 0417a05..2c4bbfa 100755 (executable)
@@ -3,30 +3,37 @@
 #
 #  all this part is from mopshop and we will use it to construct the brisk database
 #
-
-if [ -f $HOME/.db.conf ]; then
+if [ -f $HOME/.brisk-spu-db.conf ]; then
+    source $HOME/.brisk-spu-db.conf
+elif [ -f $HOME/.db.conf ]; then
     source $HOME/.db.conf
 else
     DBHOST=127.0.0.1
     DBUSER=brisk
+    DBPORT=5432
     DBBASE=brisk
     DBPASS=briskpass
     PFX="bsk_"
 fi
 
-if [ -f $HOME/.brisk_install ]; then
-    source $HOME/.brisk_install
+if [ -f $HOME/.brisk_spu_install ]; then
+    source $HOME/.brisk_spu_install
 fi
 
-
+pg_args=""
+test "$DBHOST" != "" && pg_args="$pg_args -h $DBHOST"
+test "$DBUSER" != "" && pg_args="$pg_args -U $DBUSER"
+test "$DBPORT" != "" && pg_args="$pg_args -p $DBPORT"
+test "$DBBASE" != "" && pg_args="$pg_args $DBBASE"
+       
 sqlexe () {
     local sht
     sht=$1
     
     if [ $sht -eq 1 ];  then 
-        sed "s/#PFX#/$PFX/g" | psql -a -h $DBHOST -U $DBUSER $DBBASE 2>&1 | egrep 'ERROR|^-- MESG' 
+        sed "s/#PFX#/$PFX/g" | psql -a $pg_args 2>&1 | egrep 'ERROR|^-- MESG' 
     else
-        sed "s/#PFX#/$PFX/g" | psql -a -h $DBHOST -U $DBUSER $DBBASE
+        sed "s/#PFX#/$PFX/g" | psql -a $pg_args
     fi
 
     return 0
@@ -65,7 +72,11 @@ elif [ "$1" = "rebuild" ]; then
         echo "-- MESG: build start" ; one_or_all $2 | grep -iv '^drop' ; echo "-- MESG: build end" ;   ) \
         | sqlexe $sht
 elif [ "$1" = "psql" ]; then
-    psql -h $DBHOST -U $DBUSER $DBBASE
+   shift
+   psql $pg_args $@
+elif [ "$1" = "piped" ]; then
+   shift
+   psql $pg_args -t -q -A -F '|' $@
 elif [ "$1" = "dump" ]; then
     if [ $# -eq 1 ]; then
         pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE
@@ -79,7 +90,7 @@ elif [ "$1" = "dumpall" ]; then
         pg_dump -h $DBHOST -U $DBUSER $DBBASE > $2
     fi
 elif [ "$1" = "add" ]; then
-    cat "$2" | psql -h $DBHOST -U $DBUSER $DBBASE
+    cat "$2" | sqlexe $sht
 else
     echo " USAGE"
     echo "   ./builder create"
@@ -88,6 +99,7 @@ else
     echo "   ./builder build"
     echo "   ./builder rebuild"
     echo "   ./builder psql"
+    echo "   ./builder piped"
     echo "   ./builder add <filesql>"
     echo "   ./builder dump [dumpfile]"
     echo "   ./builder dumpall [dumpfile]"