home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / database / informix / 1585 < prev    next >
Encoding:
Text File  |  1992-07-22  |  2.3 KB  |  89 lines

  1. Newsgroups: comp.databases.informix
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!cbnewsh!ijk
  3. From: ijk@cbnewsh.cb.att.com (ihor.j.kinal)
  4. Subject: Re: A solution to SQL reformatting problem
  5. Organization: AT&T
  6. Date: Wed, 22 Jul 1992 12:01:57 GMT
  7. Message-ID: <1992Jul22.120157.29353@cbnewsh.cb.att.com>
  8. Summary: bug in previous listing
  9. References: <1992Jul20.190339.13147@twg.com> <1992Jul21.173359.1868@cbnewsh.cb.att.com>
  10. Lines: 77
  11.  
  12. It occurred to me after I posted that my prgram would only work for
  13. one record.  Here's the modified program works for more.  Other
  14. disclaimers still apply.
  15. Ihor Kinal
  16. -------------------------------------
  17. # /bin/ksh
  18. # script to give you column headings and fields
  19. # $1 is dbname, $2 is file name.  This script attempts to insure alignment.
  20. # Note that it only works for ALL the columns in a table - if you want
  21. # a subset, some more work is needed.
  22.  
  23. if [ $# -ne 2 ]; then
  24.     echo "USAGE: $0 dbname filename"
  25.     exit 1
  26. fi
  27. export DB=$1
  28. export FILE=$2
  29. cd /tmp
  30.  
  31. (
  32. isql $DB - <<!
  33. info tables columns for $FILE;
  34. !
  35. ) | tail +5 | egrep -v "^$" > cols
  36.  
  37. # use the columns info to create awk scripts to format  first the column 
  38. # names, and then actual data.  Assume smallints take 5 spaces, and integers
  39. # 10 spaces.
  40.  
  41. cat cols | tr ")" " " | tr "(" " " |  egrep -v "^$" | nawk '
  42.         { len=length($1); if ($2=="smallint" && len < 5)
  43.                             len=5
  44.                         else if ($2=="integer" && len < 10)
  45.                             len=10
  46.                         else if ($2=="char" && len < $3)
  47.                             len=$3
  48.             printf "{ if (NR==%d) printf \"%%%ds \",$1} \n",NR,len;
  49.             tot_len = tot_len + len +1;
  50.         }
  51.  
  52.     END { if (tot_len <= 80) 
  53.             printf "TOTAL length too short - %d\n",tot_len
  54.         }
  55.  
  56.     ' > t1.awk
  57.  
  58. # if the length of the fields is <= 80, a normal dump will suffice.
  59. grep TOTAL t1.awk > /dev/null
  60. if [ $? -eq 0 ]; then
  61.     isql $DB - <<!
  62.     select * from $FILE;
  63. !
  64. else
  65.  
  66.     nawk -f t1.awk cols
  67.     echo "\n"
  68.     cat cols | tr ")" " " | tr "(" " " |  egrep -v "^$" | nawk '
  69.         BEGIN { printf "{ if (NF==0) {cnt =0; printf \"\\n\"} else cnt++}\n" }
  70.         { len=length($1); if ($2=="smallint" && len < 5)
  71.                             len=5
  72.                         else if ($2=="integer" && len < 10)
  73.                             len=10
  74.                         else if ($2=="char" && len < $3)
  75.                             len=$3
  76.             printf "{ if (cnt==%d) printf \"%%%ds \",$2} \n",NR,len}
  77.     ' > t2.awk
  78.  
  79.     (
  80.     2> /dev/null isql $DB - <<!
  81.     select * from $FILE;
  82. !
  83.     ) | tail +4 |nawk -f t2.awk
  84.  
  85. fi
  86.  
  87. /bin/rm -f t[12].awk cols
  88. exit 0
  89.