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

  1. Newsgroups: comp.databases.informix
  2. Path: sparky!uunet!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!uwm.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: Tue, 21 Jul 1992 17:33:59 GMT
  7. Message-ID: <1992Jul21.173359.1868@cbnewsh.cb.att.com>
  8. Summary: using awk instead
  9. References: <1992Jul20.190339.13147@twg.com>
  10. Lines: 96
  11.  
  12. AS some other readers pointed out, setting columns/COLUMNS does
  13. not appear to work in many cases.
  14.  
  15. As an alternative, I whipped up a awk script to do this.
  16. I've given some limitted testing to it, but it appears to do
  17. basically what is desired here.  Note that ALL fields from
  18. the file are selected - if you want a subset, then the extension 
  19. to this script is left as an excersise for the user.  Also,
  20. feel free to re-write in perl, etc.
  21.  
  22. Also note that if the normal isql works, this script attempts to
  23. detect that - but I haven't tested the boundary conditions around
  24. 80, so be prepared.
  25.  
  26. Also, I've only typed some of the obvious types of data - others
  27. will probably need to be added for total correctnes [e.g. float
  28. and date types].
  29.  
  30. Standard disclaimers apply - and no support is promised!!!
  31. ENJOY.
  32. Ihor Kinal
  33. att!trumpet!ijk
  34. ------------------------
  35.  
  36. # /bin/ksh
  37. # dump_flat: script to give you column headings and fields
  38. # $1 is dbname, $2 is file name.  This script attempts to insure alignment.
  39. # Note that it only works for ALL the columns in a table - if you want
  40. # a subset, some more work is needed.
  41.  
  42. if [ $# -ne 2 ]; then
  43.     echo "USAGE: $0 dbname filename"
  44.     exit 1
  45. fi
  46. export DB=$1
  47. export FILE=$2
  48. cd /tmp
  49.  
  50. (
  51. isql $DB - <<!
  52. info tables columns for $FILE;
  53. !
  54. ) | tail +5 | egrep -v "^$" > cols
  55.  
  56. # use the columns info to create awk scripts to format  first the column 
  57. # names, and then actual data.  Assume smallints take 5 spaces, and integers
  58. # 10 spaces.
  59.  
  60. cat cols | tr ")" " " | tr "(" " " |  egrep -v "^$" | nawk '
  61.         { len=length($1); if ($2=="smallint" && len < 5)
  62.                             len=5
  63.                         else if ($2=="integer" && len < 10)
  64.                             len=10
  65.                         else if ($2=="char" && len < $3)
  66.                             len=$3
  67.             printf "{ if (NR==%d) printf \"%%%ds \",$1} \n",NR,len;
  68.             tot_len = tot_len + len +1;
  69.         }
  70.  
  71.     END { if (tot_len <= 80) 
  72.             printf "TOTAL length too short - %d\n",tot_len
  73.         }
  74.  
  75.     ' > t1.awk
  76.  
  77. # if the length of the fields is <= 80, a normal dump will suffice.
  78. grep TOTAL t1.awk > /dev/null
  79. if [ $? -eq 0 ]; then
  80.     isql $DB - <<!
  81.     select * from $FILE;
  82. !
  83. else
  84.  
  85.     nawk -f t1.awk cols
  86.     echo "\n"
  87.     cat cols | tr ")" " " | tr "(" " " |  egrep -v "^$" | nawk '
  88.         { len=length($1); if ($2=="smallint" && len < 5)
  89.                             len=5
  90.                         else if ($2=="integer" && len < 10)
  91.                             len=10
  92.                         else if ($2=="char" && len < $3)
  93.                             len=$3
  94.             printf "{ if (NR==%d) printf \"%%%ds \",$2} \n",NR,len}
  95.     ' > t2.awk
  96.  
  97.     (
  98.     2> /dev/null isql $DB - <<!
  99.     select * from $FILE;
  100. !
  101.     ) | tail +4 |nawk -f t2.awk
  102.  
  103. fi
  104.  
  105. # /bin/rm -f t[12].awk cols
  106. exit 0
  107.  
  108.