#ifndef _SQLITE3_H_
#include "sqlite3.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <unistd.h>
static int getProcessId(void){
#if SQLITE_OS_WIN
return (int)_getpid();
#else
return (int)getpid();
#endif
}
#define ENVIRONMENT_VARIABLE1_NAME "SQLITE_SQLLOG_DIR"
#define ENVIRONMENT_VARIABLE2_NAME "SQLITE_SQLLOG_REUSE_FILES"
#define ENVIRONMENT_VARIABLE3_NAME "SQLITE_SQLLOG_CONDITIONAL"
#define SQLLOG_NAMESZ 512
#define MAX_CONNECTIONS 256
struct SLConn {
int isErr;
sqlite3 *db;
int iLog;
FILE *fd;
};
static struct SLGlobal {
sqlite3_mutex *mutex;
int nConn;
int bConditional;
int bReuse;
char zPrefix[SQLLOG_NAMESZ];
char zIdx[SQLLOG_NAMESZ];
int iNextLog;
int iNextDb;
int bRec;
int iClock;
struct SLConn aConn[MAX_CONNECTIONS];
} sqllogglobal;
static int sqllog_isspace(char c){
return (c==' ' || c=='\t' || c=='\n' || c=='\v' || c=='\f' || c=='\r');
}
static void sqllogTokenize(const char *z, const char **pz, int *pn){
const char *p = z;
int n;
while( sqllog_isspace(*p) ){
p++;
}
*pz = p;
n = 0;
while( (p[n]>='a' && p[n]<='z') || (p[n]>='A' && p[n]<='Z') ) n++;
*pn = n;
}
static char *sqllogFindFile(const char *zFile){
char *zRet = 0;
FILE *fd = 0;
fd = fopen(sqllogglobal.zIdx, "r");
if( fd==0 ){
sqlite3_log(SQLITE_IOERR, "sqllogFindFile(): error in fopen()");
return 0;
}
while( feof(fd)==0 ){
char zLine[SQLLOG_NAMESZ*2+5];
if( fgets(zLine, sizeof(zLine), fd) ){
int n;
char *z;
zLine[sizeof(zLine)-1] = '\0';
z = zLine;
while( *z>='0' && *z<='9' ) z++;
while( *z==' ' ) z++;
n = strlen(z);
while( n>0 && sqllog_isspace(z[n-1]) ) n--;
if( n==strlen(zFile) && 0==memcmp(zFile, z, n) ){
char zBuf[16];
memset(zBuf, 0, sizeof(zBuf));
z = zLine;
while( *z>='0' && *z<='9' ){
zBuf[z-zLine] = *z;
z++;
}
zRet = sqlite3_mprintf("%s_%s.db", sqllogglobal.zPrefix, zBuf);
break;
}
}
}
if( ferror(fd) ){
sqlite3_log(SQLITE_IOERR, "sqllogFindFile(): error reading index file");
}
fclose(fd);
return zRet;
}
static int sqllogFindAttached(
sqlite3 *db,
const char *zSearch,
char *zName,
char *zFile
){
sqlite3_stmt *pStmt;
int rc;
assert( sqllogglobal.bRec==0 );
sqllogglobal.bRec = 1;
rc = sqlite3_prepare_v2(db, "PRAGMA database_list", -1, &pStmt, 0);
if( rc==SQLITE_OK ){
while( SQLITE_ROW==sqlite3_step(pStmt) ){
const char *zVal1; int nVal1;
const char *zVal2; int nVal2;
zVal1 = (const char*)sqlite3_column_text(pStmt, 1);
nVal1 = sqlite3_column_bytes(pStmt, 1);
if( zName ){
memcpy(zName, zVal1, nVal1+1);
}
zVal2 = (const char*)sqlite3_column_text(pStmt, 2);
nVal2 = sqlite3_column_bytes(pStmt, 2);
memcpy(zFile, zVal2, nVal2+1);
if( zSearch && strlen(zSearch)==nVal1
&& 0==sqlite3_strnicmp(zSearch, zVal1, nVal1)
){
break;
}
}
rc = sqlite3_finalize(pStmt);
}
sqllogglobal.bRec = 0;
if( rc!=SQLITE_OK ){
sqlite3_log(rc, "sqllogFindAttached(): error in \"PRAGMA database_list\"");
}
return rc;
}
static void sqllogCopydb(struct SLConn *p, const char *zSearch, int bLog){
char zName[SQLLOG_NAMESZ];
char zFile[SQLLOG_NAMESZ];
char *zFree;
char *zInit = 0;
int rc;
rc = sqllogFindAttached(p->db, zSearch, zName, zFile);
if( rc!=SQLITE_OK ) return;
if( zFile[0]=='\0' ){
zInit = sqlite3_mprintf("");
}else{
if( sqllogglobal.bReuse ){
zInit = sqllogFindFile(zFile);
}else{
zInit = 0;
}
if( zInit==0 ){
int rc;
sqlite3 *copy = 0;
int iDb;
iDb = sqllogglobal.iNextDb++;
zInit = sqlite3_mprintf("%s_%02d.db", sqllogglobal.zPrefix, iDb);
assert( sqllogglobal.bRec==0 );
sqllogglobal.bRec = 1;
rc = sqlite3_open(zInit, ©);
if( rc==SQLITE_OK ){
sqlite3_backup *pBak;
sqlite3_exec(copy, "PRAGMA synchronous = 0", 0, 0, 0);
pBak = sqlite3_backup_init(copy, "main", p->db, zName);
if( pBak ){
sqlite3_backup_step(pBak, -1);
rc = sqlite3_backup_finish(pBak);
}else{
rc = sqlite3_errcode(copy);
}
sqlite3_close(copy);
}
sqllogglobal.bRec = 0;
if( rc==SQLITE_OK ){
FILE *fd = fopen(sqllogglobal.zIdx, "a");
if( fd ){
fprintf(fd, "%d %s\n", iDb, zFile);
fclose(fd);
}
}else{
sqlite3_log(rc, "sqllogCopydb(): error backing up database");
}
}
}
if( bLog ){
zFree = sqlite3_mprintf("ATTACH '%q' AS '%q'; -- clock=%d\n",
zInit, zName, sqllogglobal.iClock++
);
}else{
zFree = sqlite3_mprintf("-- Main database is '%q'\n", zInit);
}
fprintf(p->fd, "%s", zFree);
sqlite3_free(zFree);
sqlite3_free(zInit);
}
static void sqllogOpenlog(struct SLConn *p){
if( p->fd==0 ){
char *zLog;
if( sqllogglobal.zPrefix[0]==0 ){
FILE *fd;
char *zVar = getenv(ENVIRONMENT_VARIABLE1_NAME);
if( zVar==0 || strlen(zVar)+10>=(sizeof(sqllogglobal.zPrefix)) ) return;
sqlite3_snprintf(sizeof(sqllogglobal.zPrefix), sqllogglobal.zPrefix,
"%s/sqllog_%05d", zVar, getProcessId());
sqlite3_snprintf(sizeof(sqllogglobal.zIdx), sqllogglobal.zIdx,
"%s.idx", sqllogglobal.zPrefix);
if( getenv(ENVIRONMENT_VARIABLE2_NAME) ){
sqllogglobal.bReuse = atoi(getenv(ENVIRONMENT_VARIABLE2_NAME));
}
fd = fopen(sqllogglobal.zIdx, "w");
if( fd ) fclose(fd);
}
zLog = sqlite3_mprintf("%s_%05d.sql", sqllogglobal.zPrefix, p->iLog);
p->fd = fopen(zLog, "w");
sqlite3_free(zLog);
if( p->fd==0 ){
sqlite3_log(SQLITE_IOERR, "sqllogOpenlog(): Failed to open log file");
}
}
}
static void testSqllogStmt(struct SLConn *p, const char *zSql){
const char *zFirst;
int nFirst;
sqllogTokenize(zSql, &zFirst, &nFirst);
if( nFirst!=6 || 0!=sqlite3_strnicmp("ATTACH", zFirst, 6) ){
fprintf(p->fd, "%s; -- clock=%d\n", zSql, sqllogglobal.iClock++);
}else{
sqllogCopydb(p, 0, 1);
}
}
static int sqllogTraceDb(sqlite3 *db){
int bRet = 1;
if( sqllogglobal.bConditional ){
char zFile[SQLLOG_NAMESZ];
int rc = sqllogFindAttached(db, "main", 0, zFile);
if( rc==SQLITE_OK ){
int nFile = strlen(zFile);
if( (SQLLOG_NAMESZ-nFile)<8 ){
sqlite3_log(SQLITE_IOERR,
"sqllogTraceDb(): database name too long (%d bytes)", nFile
);
bRet = 0;
}else{
memcpy(&zFile[nFile], "-sqllog", 8);
bRet = !access(zFile, F_OK);
}
}
}
return bRet;
}
static void testSqllog(void *pCtx, sqlite3 *db, const char *zSql, int eType){
struct SLConn *p = 0;
sqlite3_mutex *mainmtx = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MAIN);
assert( eType==0 || eType==1 || eType==2 );
assert( (eType==2)==(zSql==0) );
if( eType==0 ){
sqlite3_mutex_enter(mainmtx);
if( sqllogglobal.mutex==0 ){
sqllogglobal.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
}
sqlite3_mutex_leave(mainmtx);
sqlite3_mutex_enter(sqllogglobal.mutex);
if( sqllogglobal.bRec==0 && sqllogTraceDb(db) ){
sqlite3_mutex_enter(mainmtx);
p = &sqllogglobal.aConn[sqllogglobal.nConn++];
p->fd = 0;
p->db = db;
p->iLog = sqllogglobal.iNextLog++;
sqlite3_mutex_leave(mainmtx);
sqllogOpenlog(p);
if( p->fd ) sqllogCopydb(p, "main", 0);
}
sqlite3_mutex_leave(sqllogglobal.mutex);
}
else{
int i;
for(i=0; i<sqllogglobal.nConn; i++){
p = &sqllogglobal.aConn[i];
if( p->db==db ) break;
}
if( eType==2 ){
sqlite3_mutex_enter(mainmtx);
if( i<sqllogglobal.nConn ){
if( p->fd ) fclose(p->fd);
p->db = 0;
p->fd = 0;
sqllogglobal.nConn--;
}
if( sqllogglobal.nConn==0 ){
sqlite3_mutex_free(sqllogglobal.mutex);
sqllogglobal.mutex = 0;
}else if( i<sqllogglobal.nConn ){
int nShift = &sqllogglobal.aConn[sqllogglobal.nConn] - p;
if( nShift>0 ){
memmove(p, &p[1], nShift*sizeof(struct SLConn));
}
}
sqlite3_mutex_leave(mainmtx);
}else if( i<sqllogglobal.nConn && p->fd ){
sqlite3_mutex_enter(sqllogglobal.mutex);
if( sqllogglobal.bRec==0 ){
testSqllogStmt(p, zSql);
}
sqlite3_mutex_leave(sqllogglobal.mutex);
}
}
}
void sqlite3_init_sqllog(void){
if( getenv(ENVIRONMENT_VARIABLE1_NAME) ){
if( SQLITE_OK==sqlite3_config(SQLITE_CONFIG_SQLLOG, testSqllog, 0) ){
memset(&sqllogglobal, 0, sizeof(sqllogglobal));
sqllogglobal.bReuse = 1;
if( getenv(ENVIRONMENT_VARIABLE3_NAME) ){
sqllogglobal.bConditional = 1;
}
}
}
}