OpenVMS Source-Code Demos

mysql_demo03

//===============================================================================================================================
// title     : mysql_demo03.c
// author    : Neil Rieck ( http://neilrieck.net MAILTO:n.rieck@sympatico.ca )
//           : Waterloo, Ontario, Canada.
// created   : 2014-02-22
// purpose   : real work demo for MariaDB (an alternative fork of MySQL)
//	       read an indexed file created by VMS-BASIC then use it to populate a MySQL table
// caveats   : 1) This program is not complete. When I realized that I would need to hand-code 33 fields for our profiledb
//	       program then repeat the whole process another 100 times if we ever used this tool as a starting point for a
//	       conversion to MySQl, I decided to start work on a more general solution (see: MYSQL_DEMO4_10x.C) to import data
//	       from a tab-delimited text file. We will keep this program in case we ever want to do a direct import from RMS
//	       (which would only happen if we ever lost the source code and were unable to create a data export utility)
//	       2) You probably should not import data into MySQL this way unless you are developing additional programming
//	       skills. See this link for a better way: http://dev.mysql.com/doc/refman/5.1/en/load-data.html
// target    : for MariaDB on OpenVMS-8.4
// vms-build : @mysql_demo.com mysql_demo3.c (see mysql_demo.com for details)
// references: http://zetcode.com/db/mysqlc/	(MySQL C API programming tutorial)
//             http://www.yolinux.com/TUTORIALS/MySQL-Programming-C-API.html
//             http://code-reference.com/c/mysql/mysql.h/mysql_real_connect
// vms-refs:   your-vms-system::sys$sysroot:[syshlp.examples.crtl]
//             http://h71000.www7.hp.com/doc/732final/5763/5763pro_006.html (c-rtl)
//==============================================================================================================================
//
//	ANSI headers
//
#include <stdio.h>					//
#include <stdlib>					//
#include <string.h>					//

//
//	program declarations
//
/*-------------------------------------------------------------------------------------------------
	!
	!	VMS-BASIC record map for Profiledb_employee_92.rec (for reference purposes only)
	!
	declare string constant										&
	    k_fs_pro_empl$	= "csmis$dat:profiledb_employee_92.dat"
	!=======================================================================
	map (my_disk)	string	d91_whole_record	= 300	,	!				&
				d91_align		= 0		!
	map (my_disk)	string	d91_whole_data		= 274	,	!				&
				d91_filler$		=  26	,	! room to grow ...		&
				d91_align		=   0		!
	map (my_disk)	string	d91_group		=   3,		! csm group name		&
		    		d91_csmis_id		=  12,		! csmis id			&
				d91_last_name		=  20,		! given name			&
				d91_first_name		=  15,		! surname			&
				d91_init		=   3,		! initials			&
				d91_inactive		=   1,		! inactive flag			&
				d91_pin			=   7,		! pin				&
				d91_labour		=   5,		! labour rate (now: S.O.T.)	&
				d91_job_code		=   3,		! job code from nomas		&
				d91_pref_prog		=   3,		! what program to run		&
				d91_alerts		=   5,		! alert option bits		&
				d91_work_phone		=  10,		! work phone 10 digits		&
				d91_home_phone		=  10,		! home phone 10 digits		&
				d91_cell_phone		=  10,		! cell phone 10 digits		&
				d91_pager_prefix	=   2,		! for future pager fix		&
				d91_pager_num		=   8,		! pager last 8 digits		&
				d91_pager_type		=   1,		! type of pager a/n/t		&
                        	d91_ardis_num   	=   8,		! mobile term num (T.O.D.)	&
				d91_ardis_type		=   1,		! hand held term (NOMAS Flag)	&
				d91_org_code		=   8,		! org code			&
				d91_title		=   3,		! csmis title			&
				d91_report_centre	=  12,		! home office			&
				d91_safety		=   1,		! auto safety check		&
				d91_printer		=   3,		! default printer		&
				d91_priv		=   5,		! application privs		&
				d91_language		=   1,		! default language		&
				d91_mail_service	=   3,		! service of choice		&
				d91_mail_id		=  39,		! service id			&
				d91_system		=  20,		! system			&
				d91_skill		=   1,		! skills			&
				d91_callout		=   1,		! callout a.....z		&
				d91_trained		=   8,		! date last trained		&
				d91_notes		=  42,		! notes				&
				d91_filler$		=  26,		! room to grow ...		&
				d91_align		=   0		!
-------------------------------------------------------------------------
	KAWC99::Neil> directory/full profiledb_employee_92.DAT;

	Directory CSMIS$ROOT4:[DAT]

	profiledb_employee_92.DAT;1383            File ID:  (19927,424,0)
	Size:          721/736        Owner:    [SYSTEM]
	Created:    11-APR-2011 07:00:01.79
	Revised:    22-FEB-2014 12:45:42.71 (45475)
	Expires:    <None specified>
	Backup:     <No backup recorded>
	Effective:  <None specified>
	Recording:  <None specified>
	Accessed:   22-FEB-2014 12:45:25.47
	Attributes: 22-FEB-2014 12:45:42.71
	Modified:   22-FEB-2014 12:45:25.47
	Linkcount:  1
	File organization:  Indexed, Prolog: 3, Using 6 keys
	Shelved state:      Online
	Caching attribute:  Writethrough
	File attributes:    Allocation: 736, Extend: 0, Maximum bucket size: 2, Global buffer count: 0, No version limit
	Record format:      Variable length, maximum 300 bytes, longest 0 bytes
	Record attributes:  Carriage return carriage control
		RMS attributes:     None
	Journaling enabled: None
	File protection:    System:RWED, Owner:RWED, Group:RWE, World:RWE
	Access Cntrl List:  None
	Client attributes:  None

	KAWC99::Neil>
-------------------------------------------------------------------------------------------------*/
//
//	c source code resumes...
//
struct my_profile{
char d91_group		[   3];
char d91_csmis_id	[  12];
char d91_last_name	[  20];
char d91_first_name	[  15];
char d91_init		[   3];
char d91_inactive	[   1];
char d91_pin		[   7];
char d91_labour		[   5];
char d91_job_code	[   3];
char d91_pref_prog	[   3];
char d91_alerts		[   5];
char d91_work_phone	[  10];
char d91_home_phone	[  10];
char d91_cell_phone	[  10];
char d91_pager_prefix	[   2];
char d91_pager_num	[   8];
char d91_pager_type	[   1];
char d91_ardis_num   	[   8];
char d91_ardis_type	[   1];
char d91_org_code	[   8];
char d91_title		[   3];
char d91_report_centre	[  12];
char d91_safety		[   1];
char d91_printer	[   3];
char d91_priv		[   5];
char d91_language	[   1];
char d91_mail_service	[   3];
char d91_mail_id	[  39];
char d91_system		[  20];
char d91_skill		[   1];
char d91_callout	[   1];
char d91_trained	[   8];
char d91_notes		[  42];
char d91_filler		[  26];
};
struct my_disk{
char buffer[300];					// same size as map above
char safety[30];					// 10% more in case RMS adds something
};

union twoway{
struct my_profile p;					// p is a tag meaning "profile"
struct my_disk    d; 					// d is a tag meaning "disk"
};

union twoway v;						// variable declaration
//
//	this is necessary because the strings we are pulling out of the
//	record are not null terminated. In fact, there is no room for the null at all.
//
void extract(char* dst, char* src,long size){
    dst[0] = '\0';					//
    strncpy(dst, src, size);
    dst[size] = '\0';
}

//========================================================================================
//	Main
//========================================================================================
int main( int *argc, char **argv) {
    FILE *fp;						// file pointer
    long sts;						// status
    long count;						//
    char tmp[132];					//
    char fs[] = "csmis$dat:profiledb_employee_92.dat";
    //
    printf("pgm: MYSQL_DEMO3\n");
    printf("opening file: %s\n", fs);
    fp = fopen(	fs,					// file spec (directory + name)
		"r",					// access: read
	//	"rfm=fix",				// record format: fixed		(from the example)
		"rfm=vfc",				// record format: var		(directory/full says we need this)
		"mrs=300",				// maxiumum record size: 300
	//	"rat=none",				// record attributes: none	(from the example)
		"rat=cr",				// record attributes: none	(directory/full says we need this)
		"ctx=bin"				// context: binary		(preserve record alignment)
	);
    if (fp == NULL) {
        perror("-e-could not open file");
        exit(EXIT_FAILURE);
    }
    count = 0;						// init
    tmp[0] = '\0';					// init
    rewind(fp);						// probably not necessary but do it anyway
    do {
        sts = fread(v.d.buffer, 300, 1, fp);		// retreive 300 single-byte elements
	printf("-i-fread status: %ld\n", sts);
        if (sts != 1){
            perror("fread error");
	}
	else{
	    count++;					//
	    printf("-i-rec-num: %ld\n", count);		//
            printf("-i-dat-rec: %.70s\n", v.d.buffer);	// only display first 75 characters
	    //
	    extract(tmp, v.p.d91_group,sizeof(v.p.d91_group));
	    printf("-i-group     : %s\n", tmp);
	    //
	    extract(tmp, v.p.d91_csmis_id,sizeof(v.p.d91_csmis_id));
	    printf("-i-csmis id  : %s\n", tmp);
	    //
	    extract(tmp, v.p.d91_last_name,sizeof(v.p.d91_last_name));
	    printf("-i-last name : %s\n", tmp);
	    //
	    extract(tmp, v.p.d91_first_name,sizeof(v.p.d91_first_name));
	    printf("-i-first name: %s\n", tmp);
	    //
	    extract(tmp, v.p.d91_init,sizeof(v.p.d91_init));
	    printf("-i-init      : %s\n", tmp);
	    //
	    extract(tmp, v.p.d91_inactive,sizeof(v.p.d91_inactive));
	    printf("-i-inactive  : %s\n", tmp);
	    //
	    extract(tmp, v.p.d91_pin,sizeof(v.p.d91_pin));
	    printf("-i-inactive  : %s\n", tmp);
	    //
	    extract(tmp, v.p.d91_labour,sizeof(v.p.d91_labour));
	    printf("-i-SOT       : %s\n", tmp);
	    //
	    extract(tmp, v.p.d91_job_code,sizeof(v.p.d91_job_code));
	    printf("-i-Job Code  : %s\n", tmp);
	    //
	}
    } while (sts == 1);
    //
    return(1);						// exit with VMS-S (success)
}

left hand Back to OpenVMS
left hand Back to OpenVMS Demo Index
home Back to Home
Neil Rieck
Kitchener - Waterloo - Cambridge, Ontario, Canada.