home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / database / crde10.zip / SAMPLE.C < prev    next >
Text File  |  1991-01-04  |  4KB  |  118 lines

  1. /* sample.c */
  2.  
  3. /**********************************************************************/
  4. /*                                                                    */
  5. /* Sample Application for The C Relational Database Engine.           */
  6. /* Copyright (c) 1990, Mark Lang.                                     */
  7. /*                                                                    */
  8. /* This sample program demostrates the power and ease of use of The   */
  9. /* C Relational Database Engine.  Although only 100 or so lines long, */
  10. /* this program generates a suppliers table with 1000 random records  */
  11. /* and performs several complex queries upon them.                    */
  12. /*                                      */
  13. /* Many of a the queries shown which call a single function call in   */
  14. /* CRDE would require hundreds or even thousands of lines of code in  */
  15. /* other database toolkits.  And this power and ease of use is not    */
  16. /* comprimised by lack of performance.  In fact, CRDE will and has    */
  17. /* outperformed professional databases products such as Paradox and   */
  18. /* R:Base, making it more than a match when compared to other C       */
  19. /* database libraries.                                                */
  20. /*                                                                    */
  21. /**********************************************************************/
  22.  
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include "crde.h"
  27.  
  28.  
  29. typedef struct {
  30.   int s;
  31.   char sname[26];
  32.   char city[26];
  33.   int status;
  34. } sup;
  35.  
  36.  
  37. int tbuffers = 128;
  38.  
  39. char *cities[10] = { "Atlanta", "Boston", "Detroit", "Hong Kong", "New York",
  40.   "London", "San Francisco", "Denver", "Charlotte", "Chicago" };
  41.  
  42. char supname[26];
  43.  
  44.  
  45. void showtable(TABLE *t, char *prompt)
  46. {
  47.   gotoxy(3,1); clreol(); puts(prompt);
  48.   gotoxy(3,2); clreol(); puts("Press [Enter] or [Esc] to continue.");
  49.   tview(t, NULL, NULL, 3, 3, 78, 23, 0x07, 0x07, 0x07, NULL);
  50. }
  51.  
  52. int calccount(void *in, void *out, int first)
  53. {
  54.   typedef struct {
  55.     char city[26];
  56.     int count;
  57.   } outs;
  58.  
  59.   switch (first) {
  60.     case 1 :
  61.       strncpy(((outs *)out)->city, ((sup *)in)->city, 25);
  62.       ((outs *)out)->count = 0;
  63.     case 0 :
  64.       ((outs *)out)->count++;
  65.   }
  66.   return 1;
  67. }
  68.  
  69.  
  70. int main()
  71. {
  72.   TABLE *t, *answer;
  73.   int i;
  74.  
  75.   clrscr();
  76.   puts("  Generating supplier table with 1000 random suppliers...");
  77.  
  78.   strcpy(supname, "Supplier    ");
  79.  
  80.   /* create sups table */
  81.   t = tcreat("sups", "*s# i, sname c26, city c26, status i");
  82.  
  83.   twritec(t);
  84.   for (i = 1; i <= 1000; i++) {
  85.     itoa(i, &supname[8], 10);
  86.     tload(t, i, supname, cities[random(10)], random(30) + 1);
  87.   }
  88.   tnormal(t);
  89.  
  90.   /* display table */
  91.   showtable(t, "View supplier table.");
  92.  
  93.   /* display supplier #100 */
  94.   answer = tselect(TEMP, t, "s#", EQ, 100, NULL);
  95.   showtable(answer, "Selected supplier #100 from supplier table.");
  96.  
  97.   answer = tselect(TEMP, t, "status", GT, 25, NULL);
  98.   showtable(answer, "Selected all suppliers with a status > 25.");
  99.  
  100.   answer = tselect(TEMP, t, "status", GT, 25, "city", EQ, "Atlanta", NULL);
  101.   showtable(answer, "Selected all suppliers from Atlanta with a status > 25.");
  102.  
  103.   /* select distinct cities in sorted order */
  104.   answer = tsort(TEMP, tproject(TEMP, t, "*city", NULL), "city");
  105.   showtable(answer, "Selected distinct cities from suppliers, sorted by city.");
  106.  
  107.   /* calculate the average status per city */
  108.   answer = tsort(TEMP, tgroup(TEMP, t, "city", "city c26, count i", calccount), "city");
  109.   showtable(answer, "Grouped supplier table by city calculating the number of suppliers per city.");
  110.  
  111.   clrscr();
  112.   puts("   End of CRDE demostration.");
  113.  
  114.   tclose(t);
  115. }
  116.  
  117.  
  118. /* end of sample.c */