2008-06-04 14:27 UTC+0100 Viktor Szakats (harbour.01 syenar hu)
* contrib/hbsqlit2/hbsqlit2.c
* Further cleanup, optimizations.
! Converted to use ANSI comment only.
* contrib/hbsqlit3/hbsqlit3.c
% Minor optimization.
This commit is contained in:
@@ -8,6 +8,14 @@
|
||||
2008-12-31 13:59 UTC+0100 Foo Bar <foo.bar@foobar.org>
|
||||
*/
|
||||
|
||||
2008-06-04 14:27 UTC+0100 Viktor Szakats (harbour.01 syenar hu)
|
||||
* contrib/hbsqlit2/hbsqlit2.c
|
||||
* Further cleanup, optimizations.
|
||||
! Converted to use ANSI comment only.
|
||||
|
||||
* contrib/hbsqlit3/hbsqlit3.c
|
||||
% Minor optimization.
|
||||
|
||||
2008-06-04 14:02 UTC+0100 Viktor Szakats (harbour.01 syenar hu)
|
||||
* contrib/hbsqlit3/hbsqlit3.c
|
||||
% Removed __XHARBOUR__ guards.
|
||||
|
||||
@@ -46,11 +46,7 @@
|
||||
*
|
||||
*/
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// include and C code section
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
// NOTE: we need this to prevent base types redefinition
|
||||
/* NOTE: we need this to prevent base types redefinition */
|
||||
#define _CLIPDEFS_H
|
||||
|
||||
#include "hbapi.h"
|
||||
@@ -60,108 +56,109 @@
|
||||
#include "hbapiitm.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> // need it for exit() and BCC55
|
||||
#include <stdlib.h> /* need it for exit() and BCC55 */
|
||||
|
||||
#include "sqlite.h"
|
||||
#include "sqliteInt.h"
|
||||
|
||||
// PUBLIC VARS
|
||||
sqlite * hb_sqlite2_db; // public by ale
|
||||
/* Public vars */
|
||||
sqlite * hb_sqlite2_db; /* public by ale */
|
||||
char * hb_sqlite2_szErrMsg = NULL;
|
||||
int hb_sqlite2_iLastErrCode = 0; // for use in foreign languages (translate msg)
|
||||
// public (temporary) buffer till I can find a better approach
|
||||
int hb_sqlite2_iLastErrCode = 0; /* for use in foreign languages (translate msg) */
|
||||
/* Public temporary buffer till I can find a better approach */
|
||||
char * hb_sqlite2_aDataRows[ 1024 ];
|
||||
char * hb_sqlite2_aDataCols[ 255 ];
|
||||
int hb_sqlite2_iDataRows = 0; // records
|
||||
int hb_sqlite2_iDataCols = 0; // fields
|
||||
int hb_sqlite2_iDataRows = 0; /* records */
|
||||
int hb_sqlite2_iDataCols = 0; /* fields */
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
static int callback( void * NotUsed, int argc, char ** argv, char ** azColName )
|
||||
//--------------------------------------------------------------------------
|
||||
// INTERNAL DO NOT TOUCH !!!
|
||||
//---------------------------------------------------------------------------
|
||||
/* INTERNAL DO NOT TOUCH !!! */
|
||||
static int hb_sqlite2_callback( void * NotUsed, int argc, char ** argv, char ** azColName )
|
||||
{
|
||||
int i;
|
||||
|
||||
// in arg is the number of data fields for each row
|
||||
// printf("argc =>%d\n", argc);
|
||||
|
||||
HB_SYMBOL_UNUSED( NotUsed );
|
||||
HB_SYMBOL_UNUSED( argc );
|
||||
HB_SYMBOL_UNUSED( argv );
|
||||
HB_SYMBOL_UNUSED( azColName );
|
||||
|
||||
for( i = 0; i < argc; i++)
|
||||
#if 0
|
||||
{
|
||||
// printf(">>> %s = %s\n", azColName[ i ], argv[ i ] ? argv[ i ] : "NULL" );
|
||||
// hb_sqlite2_aDataCols[ i ] = azColName[ i ]; // en realidad copiar cadena a cadena
|
||||
// hb_sqlite2_aDataRows[ i ] = argv[ i ];
|
||||
int i;
|
||||
|
||||
/* in arg is the number of data fields for each row */
|
||||
printf("argc =>%d\n", argc);
|
||||
|
||||
for( i = 0; i < argc; i++)
|
||||
{
|
||||
printf(">>> %s = %s\n", azColName[ i ], argv[ i ] ? argv[ i ] : "NULL" );
|
||||
hb_sqlite2_aDataCols[ i ] = azColName[ i ]; /* en realidad copiar cadena a cadena */
|
||||
hb_sqlite2_aDataRows[ i ] = argv[ i ];
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
// printf("\n");
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//----------------------
|
||||
/* Returns a string explaining last error */
|
||||
HB_FUNC( SQLITE_ERROR )
|
||||
//--------------------------------------------------------------------------
|
||||
// Returns a string explaining last error
|
||||
//--------------------------------------------------------------------------
|
||||
{
|
||||
if( hb_sqlite2_szErrMsg )
|
||||
hb_retc( hb_sqlite2_szErrMsg );
|
||||
}
|
||||
|
||||
//----------------------
|
||||
HB_FUNC( SQLITE_OPEN ) // sqlite * = sqlite_open( argv[ 1 ], 0, &zErrMsg );
|
||||
//--------------------------------------------------------------------------
|
||||
// Open a database file (in SQLite format) and set a public structure
|
||||
//--------------------------------------------------------------------------
|
||||
/* Open a database file (in SQLite format) and set a public structure */
|
||||
HB_FUNC( SQLITE_OPEN )
|
||||
{
|
||||
char * szDB = hb_parc( 1 );
|
||||
int iMode = 0; // hb_parni( 2 );
|
||||
int iMode = 0; /* hb_parni( 2 ); */
|
||||
|
||||
HB_SYMBOL_UNUSED( iMode );
|
||||
|
||||
// hb_sqlite2_db is a public var (by ale)
|
||||
hb_sqlite2_db = ( sqlite * ) sqlite_open( szDB, 0, &hb_sqlite2_szErrMsg );
|
||||
|
||||
// It seems Borland BCC55 do not return a correct pointer to a sqlite
|
||||
// struc, thus I can't check if database was successfully open :(
|
||||
// ( sqlite * )
|
||||
// printf("\n %lu ", hb_sqlite2_db->flags, );
|
||||
/* It seems Borland BCC55 do not return a correct pointer to a sqlite
|
||||
/ struc, thus I can't check if database was successfully open :( */
|
||||
|
||||
#if 0
|
||||
( sqlite * )
|
||||
printf("\n %lu ", hb_sqlite2_db->flags, );
|
||||
#endif
|
||||
|
||||
hb_retni( hb_sqlite2_db == 0 ? 1 : 0 ); // error: 1
|
||||
}
|
||||
|
||||
//-----------------------
|
||||
/* Execute a query over the passed table */
|
||||
HB_FUNC( SQLITE_QUERY )
|
||||
//--------------------------------------------------------------------------
|
||||
// Execute a query over the passed table
|
||||
//--------------------------------------------------------------------------
|
||||
{
|
||||
int rc, iResRows = 0, iResCols = 0, i, iRec, iField;
|
||||
int rc;
|
||||
int i;
|
||||
char * szSQLcom = hb_parc( 1 );
|
||||
char * pErrmsg;
|
||||
char ** pResStr;
|
||||
PHB_ITEM paRows; // PHB_ITEM is the Harbour equivalent of Clipper ITEM
|
||||
PHB_ITEM paCols;
|
||||
|
||||
hb_sqlite2_iDataRows = 0; // reset global records
|
||||
hb_sqlite2_iDataCols = 0; // reset global fields
|
||||
|
||||
// reset temporary store
|
||||
for( i = 0; i < 255; i++)
|
||||
/* reset temporary store */
|
||||
for( i = 0; i < 255; i++ )
|
||||
hb_sqlite2_aDataCols[ i ] = 0L;
|
||||
|
||||
for( i = 0; i < 1024; i++)
|
||||
for( i = 0; i < 1024; i++ )
|
||||
hb_sqlite2_aDataRows[ i ] = 0L;
|
||||
|
||||
rc = sqlite_exec( hb_sqlite2_db, szSQLcom, callback, 0, &hb_sqlite2_szErrMsg );
|
||||
rc = sqlite_exec( hb_sqlite2_db, szSQLcom, hb_sqlite2_callback, 0, &hb_sqlite2_szErrMsg );
|
||||
|
||||
// Check the operation's result
|
||||
/* Check the operation's result */
|
||||
if( rc == SQLITE_OK )
|
||||
{
|
||||
// put here a routine to process results
|
||||
int iResRows = 0;
|
||||
int iResCols = 0;
|
||||
int iRec;
|
||||
int iField;
|
||||
char * pErrmsg;
|
||||
char ** pResStr;
|
||||
PHB_ITEM paRows;
|
||||
PHB_ITEM paCols;
|
||||
|
||||
/* put here a routine to process results */
|
||||
sqlite_get_table( hb_sqlite2_db, /* An open database */
|
||||
szSQLcom, /* SQL to be executed */
|
||||
&pResStr, /* Result written to a char *[] that this points to */
|
||||
@@ -170,15 +167,14 @@ HB_FUNC( SQLITE_QUERY )
|
||||
&pErrmsg /* Error msg written here */
|
||||
);
|
||||
|
||||
// global results
|
||||
hb_sqlite2_iDataRows = iResRows; // set rows from last operation
|
||||
hb_sqlite2_iDataCols = iResCols; // set cols from last operation
|
||||
/* global results */
|
||||
hb_sqlite2_iDataRows = iResRows; /* set rows from last operation */
|
||||
hb_sqlite2_iDataCols = iResCols; /* set cols from last operation */
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// quiero devolver un array bidimensional donde la cantidad de filas
|
||||
// es rows +1 (ó reccords +1 ) y las columnas los campos
|
||||
// la primer fila contiene los encabezados de los campos
|
||||
/*
|
||||
/* quiero devolver un array bidimensional donde la cantidad de filas
|
||||
es rows +1 (ó reccords +1 ) y las columnas los campos
|
||||
la primer fila contiene los encabezados de los campos */
|
||||
#if 0
|
||||
for( iRec = 0, i = 0; iRec < iResRows + 1; iRec++ )
|
||||
{
|
||||
for( iField = 0; iField < iResCols; iField++, i++ )
|
||||
@@ -186,143 +182,120 @@ HB_FUNC( SQLITE_QUERY )
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
*/
|
||||
// -------- dimension rows array -------------------------------
|
||||
#endif
|
||||
/* dimension rows array */
|
||||
paRows = hb_itemArrayNew( iResRows + 1 );
|
||||
i = 0;
|
||||
|
||||
for( iRec = 0; iRec < iResRows + 1; iRec++ )
|
||||
{
|
||||
// ---------- if it's a multidimensional array --------------------
|
||||
/* if it's a multidimensional array */
|
||||
if( iResCols > 1 )
|
||||
{
|
||||
paCols = hb_itemArrayNew( iResCols );
|
||||
|
||||
// for every field
|
||||
/* for every field */
|
||||
for( iField = 0; iField < iResCols; iField++ )
|
||||
{
|
||||
hb_arraySetC( paCols, iField + 1, ( pResStr )[ i ] ? ( pResStr )[ i ] : NULL );
|
||||
i++;
|
||||
}
|
||||
hb_arraySetC( paCols, iField + 1, pResStr[ i++ ] );
|
||||
|
||||
// put data onto subarray of records
|
||||
/* put data onto subarray of records */
|
||||
hb_itemArrayPut( paRows, iRec + 1, paCols);
|
||||
hb_itemRelease( paCols );
|
||||
}
|
||||
/* is an unidimensional array */
|
||||
else
|
||||
{
|
||||
// ---------- is an unidimensional array -----------------------------
|
||||
hb_arraySetC( paRows, iRec + 1, ( pResStr )[ i ] ? ( pResStr )[ i ] : NULL );
|
||||
i++;
|
||||
}
|
||||
hb_arraySetC( paRows, iRec + 1, pResStr[ i++ ] );
|
||||
}
|
||||
|
||||
// free memory allocated
|
||||
/* free memory allocated */
|
||||
sqlite_free_table( pResStr );
|
||||
|
||||
hb_itemReturn( paRows ); // final, return to harbour & release
|
||||
hb_itemReturn( paRows ); /* final, return to harbour & release */
|
||||
hb_itemRelease( paRows );
|
||||
}
|
||||
else
|
||||
{
|
||||
hb_sqlite2_iLastErrCode = rc; // set last error
|
||||
hb_retc_null(); // return last error also
|
||||
hb_sqlite2_iLastErrCode = rc; /* set last error */
|
||||
hb_reta( 0 );
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------
|
||||
HB_FUNC( SQLITE_CLOSE ) // void sqlite_close( sqlite * hb_sqlite2_db )
|
||||
//--------------------------------------------------------------------------
|
||||
// Close currently open database
|
||||
//--------------------------------------------------------------------------
|
||||
/* Close currently open database */
|
||||
HB_FUNC( SQLITE_CLOSE )
|
||||
{
|
||||
sqlite_close( hb_sqlite2_db );
|
||||
}
|
||||
|
||||
//----------------------
|
||||
/* Returns information about current SQLite package */
|
||||
HB_FUNC( SQLITE_INFO )
|
||||
//--------------------------------------------------------------------------
|
||||
// Returns information about current SQLite package
|
||||
//--------------------------------------------------------------------------
|
||||
{
|
||||
hb_reta( 3 );
|
||||
hb_storc( ( char * ) SQLITE_VERSION, -1, 1 );
|
||||
hb_storc( ( char * ) sqlite_libversion(), -1, 2 );
|
||||
hb_storc( ( char * ) SQLITE_VERSION , -1, 1 );
|
||||
hb_storc( ( char * ) sqlite_libversion() , -1, 2 );
|
||||
hb_storc( ( char * ) sqlite_libencoding(), -1, 3 );
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
/* Returns the number of rows resulting from last operation */
|
||||
HB_FUNC( SQLITE_GETROWS )
|
||||
//--------------------------------------------------------------------------
|
||||
// Returns the number of rows resulting from last operation
|
||||
//--------------------------------------------------------------------------
|
||||
{
|
||||
hb_retni( hb_sqlite2_iDataRows );
|
||||
}
|
||||
|
||||
//------------------------
|
||||
/* Returns the number of columns resulting from last operation */
|
||||
HB_FUNC( SQLITE_GETCOLS )
|
||||
//--------------------------------------------------------------------------
|
||||
// Returns the number of columns resulting from last operation
|
||||
//--------------------------------------------------------------------------
|
||||
{
|
||||
hb_retni( hb_sqlite2_iDataCols );
|
||||
}
|
||||
|
||||
//------------------------
|
||||
/* Execute a statment (not a query) over the database */
|
||||
HB_FUNC( SQLITE_EXECUTE )
|
||||
//--------------------------------------------------------------------------
|
||||
// Execute a statment (not a query) over the database
|
||||
//--------------------------------------------------------------------------
|
||||
{
|
||||
int rc, i; // , iRec, iField;
|
||||
int rc, i; /* , iRec, iField; */
|
||||
char * szSQLcom = hb_parc( 1 );
|
||||
|
||||
hb_sqlite2_iDataRows = 0; // reset global records
|
||||
hb_sqlite2_iDataCols = 0; // reset global fields
|
||||
hb_sqlite2_iDataRows = 0; /* reset global records */
|
||||
hb_sqlite2_iDataCols = 0; /* reset global fields */
|
||||
|
||||
// reset temporary store
|
||||
/* reset temporary store */
|
||||
for( i = 0; i < 255; i++ )
|
||||
hb_sqlite2_aDataCols[ i ] = 0L;
|
||||
|
||||
for( i = 0; i < 1024; i++ )
|
||||
hb_sqlite2_aDataRows[ i ] = 0L;
|
||||
|
||||
rc = sqlite_exec( hb_sqlite2_db, szSQLcom, callback, 0, &hb_sqlite2_szErrMsg );
|
||||
rc = sqlite_exec( hb_sqlite2_db, szSQLcom, hb_sqlite2_callback, 0, &hb_sqlite2_szErrMsg );
|
||||
|
||||
// Check the operation's result
|
||||
/* Check the operation's result */
|
||||
if( rc != SQLITE_OK )
|
||||
{
|
||||
hb_sqlite2_iLastErrCode = rc; // set last error
|
||||
hb_retni( rc ); // return last error also
|
||||
hb_sqlite2_iLastErrCode = rc; /* set last error */
|
||||
hb_retni( rc ); /* return last error also */
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------
|
||||
/* Returns an unidimensional array with FIELD NAMES only */
|
||||
HB_FUNC( SQLITE_SYSCOLUMNS )
|
||||
//--------------------------------------------------------------------------
|
||||
// Returns an unidimensional array with FIELD NAMES only
|
||||
//--------------------------------------------------------------------------
|
||||
{
|
||||
struct Table * pTable = ( struct Table * ) sqliteFindTable( hb_sqlite2_db, ( const char * ) hb_parc( 1 ), 0 );
|
||||
|
||||
if( pTable )
|
||||
{
|
||||
int iField;
|
||||
PHB_ITEM paRows; // PHB_ITEM is the Harbour equivalent of Clipper ITEM
|
||||
PHB_ITEM paRows;
|
||||
PHB_ITEM paCols;
|
||||
|
||||
// -------- dimension rows array -------------------------------
|
||||
paRows = hb_itemArrayNew( 2 + pTable->nCol ); // 1 is table name
|
||||
// 2 is field number
|
||||
// 3 to n cols data
|
||||
// the Table structure itself
|
||||
hb_arraySetC( paRows, 1, pTable->zName ); // save name of table
|
||||
hb_arraySetNL( paRows, 2, pTable->nCol ); // save number of cols/fields
|
||||
/* dimension rows array:
|
||||
1 is table name
|
||||
2 is field number
|
||||
3 to n cols data */
|
||||
|
||||
// hb_reta( pTable->nCol );
|
||||
paRows = hb_itemArrayNew( 2 + pTable->nCol );
|
||||
|
||||
/* the Table structure itself */
|
||||
hb_arraySetC( paRows, 1, pTable->zName ); /* save name of table */
|
||||
hb_arraySetNL( paRows, 2, pTable->nCol ); /* save number of cols/fields */
|
||||
|
||||
/*
|
||||
#if 0
|
||||
printf("\n\nName= %s", pTable->zName );
|
||||
printf("\n\nCols = %i", pTable->nCol );
|
||||
|
||||
@@ -333,12 +306,12 @@ HB_FUNC( SQLITE_SYSCOLUMNS )
|
||||
printf("\n Type= %s", pTable->aCol[ i ].zType );
|
||||
printf("\nPrimKey= %i", pTable->aCol[ i ].isPrimKey );
|
||||
}
|
||||
*/
|
||||
#endif
|
||||
|
||||
for( iField = 0; iField < pTable->nCol; iField++ )
|
||||
{
|
||||
// ---------- it's a multidimensional array --------------------
|
||||
// four data columns name, default, type, isprimarykey per field
|
||||
/* it's a multidimensional array */
|
||||
/* four data columns name, default, type, isprimarykey per field */
|
||||
|
||||
paCols = hb_itemArrayNew( 4 );
|
||||
|
||||
@@ -347,7 +320,7 @@ HB_FUNC( SQLITE_SYSCOLUMNS )
|
||||
hb_arraySetC( paCols, 3, pTable->aCol[ iField ].zType );
|
||||
hb_arraySetL( paCols, 4, pTable->aCol[ iField ].isPrimKey );
|
||||
|
||||
// put data onto subarray of records
|
||||
/* put data onto subarray of records */
|
||||
hb_itemArrayPut( paRows, 3 + iField, paCols );
|
||||
|
||||
hb_itemRelease( paCols );
|
||||
@@ -359,11 +332,8 @@ HB_FUNC( SQLITE_SYSCOLUMNS )
|
||||
hb_reta( 0 );
|
||||
}
|
||||
|
||||
//-----------------------
|
||||
/* Returns an unidimensional array with field names only */
|
||||
HB_FUNC( SQLITE_FIELDS )
|
||||
//--------------------------------------------------------------------------
|
||||
// Returns an unidimensional array with field names only
|
||||
//--------------------------------------------------------------------------
|
||||
{
|
||||
struct Table * pTable = ( struct Table * ) sqliteFindTable( hb_sqlite2_db, ( const char * ) hb_parc( 1 ), 0 );
|
||||
|
||||
@@ -379,11 +349,8 @@ HB_FUNC( SQLITE_FIELDS )
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------
|
||||
/* Returns number of tables inside current open database (not table) */
|
||||
HB_FUNC( SQLITE_NUMOFTABLES )
|
||||
//--------------------------------------------------------------------------
|
||||
// Returns number of tables inside current open database (not table)
|
||||
//--------------------------------------------------------------------------
|
||||
{
|
||||
hb_retni( hb_sqlite2_db ? hb_sqlite2_db->nTable - 2 : 0 );
|
||||
}
|
||||
|
||||
@@ -837,7 +837,7 @@ HB_FUNC( SQLITE3_GET_TABLE )
|
||||
PHB_ITEM pArray = hb_itemArrayNew( iCol );
|
||||
|
||||
for( j = 1; j <= iCol; j++, k++ )
|
||||
hb_arraySetC( pArray, j, pResult[ k ] ? pResult[ k ] : NULL );
|
||||
hb_arraySetC( pArray, j, pResult[ k ] );
|
||||
|
||||
hb_arrayAddForward( pResultList, pArray );
|
||||
hb_itemRelease( pArray );
|
||||
|
||||
Reference in New Issue
Block a user