libdatabase/utils/testEntrColumns.c
2006-02-28 23:28:21 +00:00

190 lines
6.9 KiB
C

/*---------------------------------------------------------------------------------*/
/* $RCSfile: testEntrColumns.c,v $ */
/*---------------------------------------------------------------------------------*/
/* $Revision: 1.1 $ */
/* $Name: $ */
/* $Date: 2006/02/28 23:28:21 $ */
/* $Author: agibert $ */
/*---------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------*/
/* This file is part of LibDataBase */
/* */
/* LibDataBase is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU Lesser General Public Licence as published by */
/* the Free Software Foundation; either version 2.1 of the License, or */
/* (at your option) any later version. */
/* */
/* LibDataBase is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU Lesser General Public License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with LibDataBase; if not, write to the Free Software */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/*---------------------------------------------------------------------------------*/
#include "../lib/database.h"
#include "testEntr.h"
#include "testEntrColumns.h"
/* Globals */
extern int columnsCount; /* cf. testEntr.c */
DBT_HostVar hv[MAX_COLUMNS]; /* One hostvar for each column */
DBT_Indicator *indic[MAX_COLUMNS]; /* One array of indicator for each column */
const char *columnsNames[] = {
"CT_PERSONNE_PHYSIQUE", "NUM_PP", "DAT_DER_MAJ_BDM", "DAT_CRE_PP_BDM", "CT_CG_RDP",
"NUM_IDENT_CG_RDP", "DAT_CRE_CG_BDM_RDP", "CD_BQUE_RDP", "CD_GUI_CLI_RDP",
"CD_ACT_ECO", "CD_PAYS_NAIS", "CD_SOGETYPE", "CD_SIT_FAM", "CD_CMN_INSEE_NAIS",
"CD_PAYS_RM", "CD_PAYS_RF", "CD_DEPT_NAIS", "CD_ST_PROF", "NOM_PAT", "NOM_MAR",
"PNOM_OFF", "PNOM_SUPP", "RAISON_SOC", "CT_FOYER_MKT", "CD_CAT_PROF", "CT_ADRESSE",
"CD_SEXE", "CD_ST_INT_CHQ", "CD_ST_DECEDE", "CD_ST_COM_PP", "CD_ETA_PP", "CD_ST_TECH",
"CD_IND_PP_ANO", "CD_CAP_JUR", "CD_UNIC_PP", "ADR_EMAIL", "DAT_DER_MAJ_BLPP", "CD_SEG_MAR"
};
int columnsSizes[] = {
INTEGER_SIZE, 8, DATE_SIZE, DATE_SIZE, INTEGER_SIZE, INTEGER_SIZE, DATE_SIZE, 6, 6, 3, 3,
INTEGER_SIZE, 3, 3, 3, 3, 3, 3, 33, 33, 33, 33, 41, INTEGER_SIZE, 3, INTEGER_SIZE, 3,
INTEGER_SIZE, 3, INTEGER_SIZE, INTEGER_SIZE, INTEGER_SIZE, 2, INTEGER_SIZE, INTEGER_SIZE,
81, DATE_SIZE, 6
};
int columnsTypes[] = {
DBD_INTEGER, DBD_STRING, DBD_STRING, DBD_STRING, DBD_INTEGER, DBD_INTEGER, DBD_STRING,
DBD_STRING, DBD_STRING, DBD_STRING, DBD_STRING, DBD_INTEGER, DBD_STRING, DBD_STRING,
DBD_STRING, DBD_STRING, DBD_STRING, DBD_STRING, DBD_STRING, DBD_STRING,
DBD_STRING, DBD_STRING, DBD_STRING, DBD_INTEGER, DBD_STRING, DBD_INTEGER, DBD_STRING,
DBD_INTEGER, DBD_STRING, DBD_INTEGER, DBD_INTEGER, DBD_INTEGER, DBD_STRING, DBD_INTEGER,
DBD_INTEGER, DBD_STRING, DBD_STRING, DBD_STRING
};
void *columnsValues[MAX_COLUMNS];
/**
* Initializes host variables.
* Associates an 'hostvar' with a type, size, array of values and array of indicators
* for each column.
*/
int setupHostVars(void) {
int i = 0;
DBT_Status rc;
for (i = 0; i < columnsCount; ++i) {
rc = DB_HostVar_Setup(&hv[i], columnsTypes[i], columnsSizes[i], columnsValues[i], indic[i]);
if (rc != DBS_OK) {
say("Error: %s\n", DB_Error_Message_Get());
return FALSE;
}
}
return TRUE;
}
/**
* Setup arrays.
* n (IN): Rows count to allocate for each array.
*/
void setupData(int n) {
int i;
for (i = 0; i < columnsCount; ++i) {
/* Allocate array of indicators for each column */
indic[i] = malloc(n * sizeof(int));
/* Allocate arrays of values for each column */
columnsValues[i] = malloc((unsigned) n * columnsSizes[i]);
}
}
/**
* Dynamically creates the SELECT request according to the number of columns involved.
*/
char *createSelectRequest(int columns) {
int i;
char *s = (char *) malloc(2000);
strcpy(s, "SELECT ");
for (i = 0; i < columns; ++i) {
strcat(s, columnsNames[i]);
if (i < columns -1)
strcat(s, ", ");
}
strcat(s, " FROM tb_personne_physique");
return s;
}
/**
* Dynamically creates the INSERT request according to the number of columns involved.
*/
char *createInsertRequest(int columns) {
int i;
char val[8];
char *s = (char *) malloc(2000);
strcpy(s, "INSERT INTO test_ens (");
for (i = 0; i < columns; ++i) {
strcat(s, columnsNames[i]);
if (i < columns -1)
strcat(s, ", ");
}
strcat(s, ") VALUES (");
for (i = 0; i < columns; ++i) {
sprintf(val, ":%d", i+1);
strcat(s, val);
if (i < columns -1)
strcat(s, ", ");
}
strcat(s, ")");
return s;
}
/**
* Dynamically displays values of each column according to their type.
*/
void displayValues(int rowsCount) {
int row;
int col;
say("Columns %d to %d:\n", 1, columnsCount);
for (row = 0; row < rowsCount; row++) {
say("#%d\t", row);
for (col = 0; col < columnsCount; col++) {
if (indic[col][row] == -1) {
printf("<NULL> ");
}
else {
if (columnsTypes[col] == DBD_INTEGER) {
int v = ((int *)(columnsValues[col])) [row];
printf("%d ", v);
/* indicator at -2 or > 0 means data have been truncated */
if (indic[col][row] != 0)
printf("<trunc> ");
}
else if (columnsTypes[col] == DBD_STRING) {
char *v = & ((char *)(columnsValues[col])) [row * columnsSizes[col]];
printf("%s ", v);
if (indic[col][row] != 0)
printf("<trunc> ");
}
else {
printf("<UNKNOWN TYPE> ");
}
}
}
printf("\n");
}
}