home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
TP1.ZIP
/
TP1TABLE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-06-21
|
11KB
|
424 lines
/***************************************************************************/
/* Copyright (C) 1989 Microsoft Corporation */
/* Copyright (C) 1989 Sybase, Inc. */
/* Program: TP1TAB2.C */
/* Description: Loads rows into a TP1 table through DB-LIB bulk copy */
/* */
/* This version of TP1TABLE doesn't set select/into Bulkcopy to true. */
/* That allows you to create a large account table which is already */
/* Indexed. This makes it much easier and quicker to create a million */
/* row account table. This program also sets truncate log on checkpoint */
/* to true for the bulk copy and resets it afterward. */
/* */
/* Created: Trish Millines 2/27/89 */
/* Modified: Bob Muglia 4/8/89 */
/* */
/***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <types.h>
#include <stat.h>
#include <process.h>
#include <timeb.h>
#include <fcntl.h>
#include <share.h>
#include <io.h>
#include <sqlfront.h>
#include <sqldb.h>
#include "bench.h"
#define NUM_BRANCHES (long) 100
DBPROCESS far *dbproc,*bulk_proc;
LOGINREC far *login;
char server[31], table[31], database[31];
DBINT start_row, end_row;
main(argc,argv)
int argc;
char **argv;
{
int status;
char cmd[300],fmtfile[31],dbase[100];
long max;
/* */
/* Forward declaration of SQL Server error handlers */
/* */
int table_err_handler();
int table_msg_handler();
/* */
/* Call routine to parse the arguments */
/* */
status = Check_arguments(argc,argv);
if(status == FAIL)
exit(FAIL);
/* */
/* Install the SQL Server error handlers */
/* */
dberrhandle(table_err_handler);
dbmsghandle(table_msg_handler);
/* */
/* Make a connection to the server */
/* */
if((login = dblogin()) == NULL)
{
printf("\nTP1TABLE ERROR: Out of memory logging on to target server");
exit(FAIL);
}
DBSETLUSER(login,"sa");
DBSETLPWD(login,"");
DBSETLAPP(login,"tp1_table");
if((dbproc = dbopen(login,server)) == NULL)
{
printf("\nTP1TABLE ERROR: Couldn't connect to %s",server);
exit(FAIL);
}
if(dbuse(dbproc,database) == FAIL)
{
printf("\nTP1TABLE ERROR: Database %s not found",database);
dbexit();
exit(FAIL);
}
/* */
/* See if the start_row value is ok */
/* */
max = Get_max_value(dbproc,table,"number");
if(start_row <= max)
{
printf("\nERROR - The next available row is %ld",max + 1);
dbexit();
exit(FAIL);
}
printf("\nThere are %ld rows currently in the table",max);
/* */
/* Change Database option to "truncate log on checkpoint",true */
/* */
printf("\nChanging the DB option to truncate log");
if(dbuse(dbproc,"master") == FAIL)
{
printf("\nTP1TABLE ERROR: Can't use master database");
dbexit();
exit(FAIL);
}
sprintf(cmd,"sp_dboption %s,'truncate',true",database);
dbcmd(dbproc,(char DBDIST *)cmd);
if(dbsqlexec(dbproc) == FAIL)
{
printf("\nTP1TABLE ERROR: dbsqlexec failed");
dbexit();
exit(FAIL);
}
if((dbresults(dbproc)) == FAIL)
{
printf("\nTP1TABLE ERROR: dbresults failed");
dbexit();
exit(FAIL);
}
if(dbuse(dbproc,database) == FAIL)
{
printf("\nTP1TABLE ERROR: Can't use %s database",database);
dbexit();
exit(FAIL);
}
sprintf(cmd,"checkpoint");
dbcmd(dbproc,(char DBDIST *)cmd);
if(dbsqlexec(dbproc) == FAIL)
{
printf("\nTP1TABLE ERROR: dbsqlexec failed");
dbexit();
exit(FAIL);
}
if((dbresults(dbproc)) == FAIL)
{
printf("\nTP1TABLE ERROR: dbresults failed");
dbexit();
exit(FAIL);
}
/* */
/* Use the bulk copy routines to load in the data */
/* */
load_data();
/* */
/* Change Database option to "truncate log",false */
/* */
printf("\nChanging the DB option to no truncate log");
if(dbuse(dbproc,"master") == FAIL)
{
printf("\nTP1TABLE ERROR: Can't use master database");
dbexit();
exit(FAIL);
}
sprintf(cmd,"sp_dboption %s,'truncate',false",database);
dbcmd(dbproc,(char DBDIST *)cmd);
if(dbsqlexec(dbproc) == FAIL)
exit(FAIL);
if((dbresults(dbproc)) == FAIL)
exit(FAIL);
if(dbuse(dbproc,database) == FAIL)
{
printf("\nTP1TABLE ERROR: Can't use %s database",database);
dbexit();
exit(FAIL);
}
sprintf(cmd,"checkpoint");
dbcmd(dbproc,(char DBDIST *)cmd);
if(dbsqlexec(dbproc) == FAIL)
{
printf("\nTP1TABLE ERROR: dbsqlexec failed");
dbexit();
exit(FAIL);
}
if((dbresults(dbproc)) == FAIL)
{
printf("\nTP1TABLE ERROR: dbresults failed");
dbexit();
exit(FAIL);
}
/* */
/* Clean up and exit */
/* */
dbexit();
printf("\a\n\n\nDone");
exit(0);
}
/***************************************************************************/
/* FUNCTION TO PARSE THE ARGUMENTS */
/***************************************************************************/
int Check_arguments(argc,argv)
int argc;
char **argv;
{
/* */
/* See if all the necessary arguments are there */
/* */
if(argc != 6)
{
system("cls");
printf("\nUSAGE: tp1table Server Database Table Start End");
printf("\n\nWHERE:");
printf("\nServer = Name of the target server");
printf("\nDatabase = Name of the database tables are in");
printf("\nTable = Name of the table you're loading");
printf("\nStart = Starting row number");
printf("\nEnd = Ending row number");
printf("\n\nEXAMPLE: tp1table Rugger perform25 account 1 100000");
return(FAIL);
}
/* */
/* Initialize variables */
/* */
strcpy(server,argv[1]);
strcpy(database,argv[2]);
strcpy(table,argv[3]);
start_row = atol(argv[4]);
end_row = atol(argv[5]);
if(start_row > end_row)
{
printf("\nError - start row is less than end row");
return(FAIL);
}
printf("\nServer: %s Database: %s Table: %s Start: %ld End: %ld",server,\
database,table,start_row,end_row);
return(SUCCEED);
}
/***************************************************************************/
/* FUNCTION TO LOAD THE DATA */
/***************************************************************************/
int load_data()
{
DBCHAR name[21],altnumber[13];
DBCHAR field7[13],field8[11],field9[11],field10[11],field11[11];
long count;
DBINT number,branch,balance,joinkey;
/* */
/* Open a second connection for bulk copy */
/* */
BCP_SETL(login,TRUE);
if((bulk_proc = dbopen(login,server)) == NULL)
{
printf("\nTP1TABLE ERROR: Couldn't connect to %s for bulk copy",server);
exit(FAIL);
}
if(dbuse(bulk_proc,database) == FAIL)
{
printf("\nTP1TABLE ERROR: Database %s not found",database);
dbexit();
exit(FAIL);
}
if(bcp_init(bulk_proc,table,NULL,"TP1TABLE.ERR",DB_IN) == FAIL)
{
printf("\nTP1TABLE ERROR: Couldn't initialize bulk copy");
dbexit();
exit(FAIL);
}
/* */
/* Setup the static variables */
/* */
balance = 1000;
formdata(name,20);
formdata(field7,12);
formdata(field8,10);
formdata(field9,10);
formdata(field10,10);
formdata(field11,10);
/* */
/* Bind program variables to be copied */
/* */
bcp_bind(bulk_proc, (BYTE *) &number,0,(DBINT) -1, NULL,0,0,1);
bcp_bind(bulk_proc,name,0,(DBINT) -1,"",1,0,2);
bcp_bind(bulk_proc, (BYTE *) &branch,0,(DBINT) -1, NULL,0,0,3);
bcp_bind(bulk_proc, (BYTE *) &balance,0,(DBINT) -1, NULL,0,0,4);
bcp_bind(bulk_proc,altnumber,0,(DBINT) -1,"",1,0,5);
bcp_bind(bulk_proc, (BYTE *) &joinkey,0,(DBINT) -1,NULL,0,0,6);
bcp_bind(bulk_proc,field7,0,(DBINT) -1,"",1,0,7);
bcp_bind(bulk_proc,field8,0,(DBINT) -1,"",1,0,8);
bcp_bind(bulk_proc,field9,0,(DBINT) -1,"",1,0,9);
bcp_bind(bulk_proc,field10,0,(DBINT) -1,"",1,0,10);
bcp_bind(bulk_proc,field11,0,(DBINT) -1,"",1,0,11);
/* */
/* Copy in the data */
/* */
for(count=start_row; count <= end_row; count++)
{
number = count;
branch = get_random(NUM_BRANCHES);
ltoa(count,altnumber,10);
joinkey = count;
if (bcp_sendrow(bulk_proc) == FAIL)
{
printf("\nTP1TABLE ERROR: Couldn't send up a row");
/* dbexit();
exit(FAIL); */
}
if (count%1000 == 0) /* Checkpoint every 1000 rows */
{
if (bcp_batch(bulk_proc) == FAIL)
{
printf("\nTP1TABLE ERROR: Couldn't checkpoint");
dbexit();
exit(FAIL);
}
printf("\nCreated %ld rows in %s",count,table);
}
}
bcp_done(bulk_proc);
return(SUCCEED);
}
/***************************************************************************/
/* FUNCTION TO FORM STATIC CHARACTER STRINGS */
/***************************************************************************/
int formdata(cp, len)
char *cp;
int len;
{
char * pattern, * start;
pattern = start = "abcdefghijklmnopqrstuvwxyz";
while (--len >= 0)
{
/* If we have reached the end of the string, start again */
if (*pattern == '\0')
pattern = start;
*cp++ = *pattern++;
}
*cp = '\0';
}
/***************************************************************************/
/* DB-Library Error and Message handlers */
/***************************************************************************/
int table_err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
DBPROCESS *dbproc;
int severity;
int dberr;
int oserr;
char *dberrstr;
char *oserrstr;
{
printf("DB-LIB error:\n %d=%s\n\n",dberr,dberrstr);
printf("oserr=%d\n",oserr);
dbexit();
exit(FAIL);
}
int table_msg_handler(dbproc, msgno, msgstate, severity, msgtext)
DBPROCESS *dbproc;
DBINT msgno;
int msgstate;
int severity;
char *msgtext;
{
if (msgno != 5701 && msgno != 0)
{
printf("Server error:\n %ld=%s\n\n",msgno,msgtext);
dbexit();
exit(FAIL);
}
}