home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / TP1.ZIP / TP1TABLE.C < prev    next >
C/C++ Source or Header  |  1989-06-21  |  11KB  |  424 lines

  1. /***************************************************************************/
  2. /* Copyright (C) 1989 Microsoft Corporation                                */
  3. /* Copyright (C) 1989 Sybase, Inc.                                         */
  4. /* Program: TP1TAB2.C                                                      */
  5. /* Description: Loads rows into a TP1 table through DB-LIB bulk copy       */
  6. /*                                                                         */
  7. /* This version of TP1TABLE doesn't set select/into Bulkcopy to true.      */
  8. /* That allows you to create a large account table which is already        */
  9. /* Indexed.  This makes it much easier and quicker to create a million     */
  10. /* row account table.  This program also sets truncate log on checkpoint   */
  11. /* to true for the bulk copy and resets it afterward.                      */
  12. /*                                                                         */
  13. /* Created:  Trish Millines 2/27/89                                        */
  14. /* Modified: Bob Muglia 4/8/89                                             */
  15. /*                                                                         */
  16. /***************************************************************************/
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <time.h>
  20. #include <math.h>
  21. #include <types.h>
  22. #include <stat.h>
  23. #include <process.h>
  24. #include <timeb.h>
  25. #include <fcntl.h>
  26. #include <share.h>
  27. #include <io.h>
  28. #include <sqlfront.h>
  29. #include <sqldb.h>
  30. #include "bench.h"
  31.  
  32. #define NUM_BRANCHES (long) 100
  33.  
  34. DBPROCESS far *dbproc,*bulk_proc;
  35.  LOGINREC far *login;
  36. char server[31], table[31], database[31];
  37. DBINT start_row, end_row;
  38.  
  39. main(argc,argv)
  40. int argc;
  41. char **argv;
  42. {
  43.  int status;
  44.  char cmd[300],fmtfile[31],dbase[100];
  45.  long max;
  46.  
  47. /* */
  48. /* Forward declaration of SQL Server error handlers */
  49. /* */
  50.  
  51.  int table_err_handler();
  52.  int table_msg_handler();
  53.  
  54. /* */
  55. /* Call routine to parse the arguments */
  56. /* */
  57.  
  58.  status = Check_arguments(argc,argv);
  59.  if(status == FAIL)
  60.    exit(FAIL);
  61.  
  62. /* */
  63. /* Install the SQL Server error handlers */
  64. /* */
  65.  
  66.  dberrhandle(table_err_handler);
  67.  dbmsghandle(table_msg_handler);
  68.  
  69. /* */
  70. /* Make a connection to the server */
  71. /* */
  72.  
  73.  if((login = dblogin()) == NULL)
  74.   {
  75.    printf("\nTP1TABLE ERROR: Out of memory logging on to target server");
  76.    exit(FAIL);
  77.   }
  78.  
  79.  DBSETLUSER(login,"sa");
  80.  DBSETLPWD(login,"");
  81.  DBSETLAPP(login,"tp1_table");
  82.  
  83.  if((dbproc = dbopen(login,server)) == NULL)
  84.   {
  85.    printf("\nTP1TABLE ERROR: Couldn't connect to %s",server);
  86.    exit(FAIL);
  87.   }
  88.  
  89.  if(dbuse(dbproc,database) == FAIL)
  90.   {
  91.    printf("\nTP1TABLE ERROR: Database %s not found",database);
  92.    dbexit();
  93.    exit(FAIL);
  94.   }
  95.  
  96. /* */
  97. /* See if the start_row value is ok */
  98. /* */
  99.  
  100.  max = Get_max_value(dbproc,table,"number");
  101.  if(start_row <= max)
  102.   {
  103.    printf("\nERROR - The next available row is %ld",max + 1);
  104.    dbexit();
  105.    exit(FAIL);
  106.   }
  107.  printf("\nThere are %ld rows currently in the table",max);
  108.  
  109. /* */
  110. /* Change Database option to "truncate log on checkpoint",true */
  111. /* */
  112.  
  113.  printf("\nChanging the DB option to truncate log");
  114.  if(dbuse(dbproc,"master") == FAIL)
  115.   {
  116.    printf("\nTP1TABLE ERROR: Can't use master database");
  117.    dbexit();
  118.    exit(FAIL);
  119.   }
  120.  
  121.  sprintf(cmd,"sp_dboption %s,'truncate',true",database);
  122.  dbcmd(dbproc,(char DBDIST *)cmd);
  123.  if(dbsqlexec(dbproc) == FAIL)
  124.   {
  125.    printf("\nTP1TABLE ERROR: dbsqlexec failed");
  126.    dbexit();
  127.    exit(FAIL);
  128.   }
  129.  
  130.  if((dbresults(dbproc)) == FAIL)
  131.   {
  132.    printf("\nTP1TABLE ERROR: dbresults failed");
  133.    dbexit();
  134.    exit(FAIL);
  135.   }
  136.  
  137.  if(dbuse(dbproc,database) == FAIL)
  138.   {
  139.    printf("\nTP1TABLE ERROR: Can't use %s database",database);
  140.    dbexit();
  141.    exit(FAIL);
  142.   }
  143.  
  144.  sprintf(cmd,"checkpoint");
  145.  dbcmd(dbproc,(char DBDIST *)cmd);
  146.  if(dbsqlexec(dbproc) == FAIL)
  147.   {
  148.    printf("\nTP1TABLE ERROR: dbsqlexec failed");
  149.    dbexit();
  150.    exit(FAIL);
  151.   }
  152.  
  153.  if((dbresults(dbproc)) == FAIL)
  154.   {
  155.    printf("\nTP1TABLE ERROR: dbresults failed");
  156.    dbexit();
  157.    exit(FAIL);
  158.   }
  159.  
  160. /* */
  161. /* Use the bulk copy routines to load in the data */
  162. /* */
  163.  
  164.  load_data();
  165.  
  166. /* */
  167. /* Change Database option to "truncate log",false */
  168. /* */
  169.  
  170.  printf("\nChanging the DB option to no truncate log");
  171.  if(dbuse(dbproc,"master") == FAIL)
  172.   {
  173.    printf("\nTP1TABLE ERROR: Can't use master database");
  174.    dbexit();
  175.    exit(FAIL);
  176.   }
  177.  
  178.  sprintf(cmd,"sp_dboption %s,'truncate',false",database);
  179.  dbcmd(dbproc,(char DBDIST *)cmd);
  180.  if(dbsqlexec(dbproc) == FAIL)
  181.    exit(FAIL);
  182.  
  183.  if((dbresults(dbproc)) == FAIL)
  184.    exit(FAIL);
  185.  
  186.  if(dbuse(dbproc,database) == FAIL)
  187.   {
  188.    printf("\nTP1TABLE ERROR: Can't use %s database",database);
  189.    dbexit();
  190.    exit(FAIL);
  191.   }
  192.  
  193.  sprintf(cmd,"checkpoint");
  194.  dbcmd(dbproc,(char DBDIST *)cmd);
  195.  if(dbsqlexec(dbproc) == FAIL)
  196.   {
  197.    printf("\nTP1TABLE ERROR: dbsqlexec failed");
  198.    dbexit();
  199.    exit(FAIL);
  200.   }
  201.  
  202.  if((dbresults(dbproc)) == FAIL)
  203.   {
  204.    printf("\nTP1TABLE ERROR: dbresults failed");
  205.    dbexit();
  206.    exit(FAIL);
  207.   }
  208.  
  209. /* */
  210. /* Clean up and exit */
  211. /* */
  212.  
  213.  dbexit();
  214.  printf("\a\n\n\nDone");
  215.  exit(0);
  216. }
  217.  
  218. /***************************************************************************/
  219. /*                      FUNCTION TO PARSE THE ARGUMENTS                    */
  220. /***************************************************************************/
  221. int Check_arguments(argc,argv)
  222. int argc;
  223. char **argv;
  224. {
  225.  
  226. /* */
  227. /* See if all the necessary arguments are there */
  228. /* */
  229.  
  230.  if(argc != 6)
  231.   {
  232.    system("cls");
  233.    printf("\nUSAGE: tp1table Server Database Table Start End");
  234.    printf("\n\nWHERE:");
  235.    printf("\nServer = Name of the target server");
  236.    printf("\nDatabase = Name of the database tables are in");
  237.    printf("\nTable  = Name of the table you're loading");
  238.    printf("\nStart  = Starting row number");
  239.    printf("\nEnd    = Ending row number");
  240.    printf("\n\nEXAMPLE: tp1table Rugger perform25 account 1 100000");
  241.    return(FAIL);
  242.   }
  243.  
  244. /* */
  245. /* Initialize variables */
  246. /* */
  247.  
  248.  strcpy(server,argv[1]);
  249.  strcpy(database,argv[2]);
  250.  strcpy(table,argv[3]);
  251.  start_row = atol(argv[4]);
  252.  end_row = atol(argv[5]);
  253.  if(start_row > end_row)
  254.   {
  255.    printf("\nError - start row is less than end row");
  256.    return(FAIL);
  257.   }
  258.  printf("\nServer: %s Database: %s Table: %s Start: %ld End: %ld",server,\
  259.         database,table,start_row,end_row);
  260.  return(SUCCEED);
  261. }
  262.  
  263.  
  264. /***************************************************************************/
  265. /*                    FUNCTION TO LOAD THE DATA                            */
  266. /***************************************************************************/
  267. int load_data()
  268. {
  269.  DBCHAR name[21],altnumber[13];
  270.  DBCHAR field7[13],field8[11],field9[11],field10[11],field11[11];
  271.  long count;
  272.  DBINT number,branch,balance,joinkey;
  273.  
  274. /* */
  275. /* Open a second connection for bulk copy */
  276. /* */
  277.  
  278.  BCP_SETL(login,TRUE);
  279.  if((bulk_proc = dbopen(login,server)) == NULL)
  280.   {
  281.    printf("\nTP1TABLE ERROR: Couldn't connect to %s for bulk copy",server);
  282.    exit(FAIL);
  283.   }
  284.  
  285.  if(dbuse(bulk_proc,database) == FAIL)
  286.   {
  287.    printf("\nTP1TABLE ERROR: Database %s not found",database);
  288.    dbexit();
  289.    exit(FAIL);
  290.   }
  291.  
  292.   if(bcp_init(bulk_proc,table,NULL,"TP1TABLE.ERR",DB_IN) == FAIL)
  293.   {
  294.    printf("\nTP1TABLE ERROR: Couldn't initialize bulk copy");
  295.    dbexit();
  296.    exit(FAIL);
  297.   }
  298.  
  299. /* */
  300. /* Setup the static variables */
  301. /* */
  302.  
  303.   balance = 1000;
  304.   formdata(name,20);
  305.   formdata(field7,12);
  306.   formdata(field8,10);
  307.   formdata(field9,10);
  308.   formdata(field10,10);
  309.   formdata(field11,10);
  310.  
  311. /* */
  312. /* Bind program variables to be copied */
  313. /* */
  314.  
  315.   bcp_bind(bulk_proc, (BYTE *) &number,0,(DBINT) -1, NULL,0,0,1);
  316.   bcp_bind(bulk_proc,name,0,(DBINT) -1,"",1,0,2);
  317.   bcp_bind(bulk_proc, (BYTE *) &branch,0,(DBINT) -1, NULL,0,0,3);
  318.   bcp_bind(bulk_proc, (BYTE *) &balance,0,(DBINT) -1, NULL,0,0,4);
  319.   bcp_bind(bulk_proc,altnumber,0,(DBINT) -1,"",1,0,5);
  320.   bcp_bind(bulk_proc, (BYTE *) &joinkey,0,(DBINT) -1,NULL,0,0,6);
  321.   bcp_bind(bulk_proc,field7,0,(DBINT) -1,"",1,0,7);
  322.   bcp_bind(bulk_proc,field8,0,(DBINT) -1,"",1,0,8);
  323.   bcp_bind(bulk_proc,field9,0,(DBINT) -1,"",1,0,9);
  324.   bcp_bind(bulk_proc,field10,0,(DBINT) -1,"",1,0,10);
  325.   bcp_bind(bulk_proc,field11,0,(DBINT) -1,"",1,0,11);
  326.  
  327.  
  328. /* */
  329. /* Copy in the data */
  330. /* */
  331.  
  332.   for(count=start_row; count <= end_row; count++)
  333.   {
  334.  
  335.    number = count;
  336.    branch = get_random(NUM_BRANCHES);
  337.    ltoa(count,altnumber,10);
  338.    joinkey = count;
  339.  
  340.    if (bcp_sendrow(bulk_proc) == FAIL)                          
  341.    {
  342.     printf("\nTP1TABLE ERROR: Couldn't send up a row");
  343. /*  dbexit();
  344.     exit(FAIL); */
  345.    }
  346.    
  347.    if (count%1000 == 0)         /* Checkpoint every 1000 rows */
  348.    {
  349.     if (bcp_batch(bulk_proc) == FAIL)
  350.      {
  351.       printf("\nTP1TABLE ERROR: Couldn't checkpoint");
  352.       dbexit();
  353.       exit(FAIL);
  354.      }
  355.     printf("\nCreated %ld rows in %s",count,table);
  356.    }
  357.   }
  358.                              
  359.   bcp_done(bulk_proc);
  360.   return(SUCCEED);
  361. }
  362.  
  363. /***************************************************************************/
  364. /*                 FUNCTION TO FORM STATIC CHARACTER STRINGS               */
  365. /***************************************************************************/
  366.    
  367. int formdata(cp, len)
  368. char *cp;
  369. int len;
  370. {
  371.   char * pattern, * start;
  372.   
  373.   pattern = start = "abcdefghijklmnopqrstuvwxyz";
  374.   
  375.   while (--len >= 0)
  376.   {   
  377.      /* If we have reached the end of the string, start again */
  378.      
  379.      if (*pattern == '\0')
  380.        pattern = start;
  381.  
  382.      *cp++ = *pattern++;
  383.   }
  384.   *cp = '\0';
  385. }
  386.  
  387.  
  388. /***************************************************************************/
  389. /* DB-Library Error and Message handlers                                   */
  390. /***************************************************************************/
  391.  
  392.  
  393. int table_err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  394. DBPROCESS       *dbproc;
  395. int             severity;
  396. int             dberr;
  397. int             oserr;
  398. char            *dberrstr;
  399. char            *oserrstr;
  400. {
  401.  printf("DB-LIB error:\n   %d=%s\n\n",dberr,dberrstr);
  402.  printf("oserr=%d\n",oserr);
  403.  dbexit();
  404.  exit(FAIL);
  405. }
  406.  
  407.  
  408. int table_msg_handler(dbproc, msgno, msgstate, severity, msgtext)
  409. DBPROCESS        *dbproc;
  410. DBINT            msgno;
  411. int              msgstate;
  412. int              severity;
  413. char             *msgtext;
  414. {                             
  415.  if (msgno != 5701 && msgno != 0)
  416.  {
  417.    printf("Server error:\n    %ld=%s\n\n",msgno,msgtext);
  418.    dbexit();
  419.    exit(FAIL);
  420.  }
  421. }
  422.  
  423.  
  424.