static int echoConstructor( sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVtab, char **pzErr ){ int rc; int i; echo_vtab *pVtab; pVtab = sqlite3MallocZero( sizeof(*pVtab) ); if( !pVtab ){ return SQLITE_NOMEM; } pVtab->interp = ((EchoModule *)pAux)->interp; pVtab->db = db; pVtab->zThis = sqlite3_mprintf("%s", argv[2]); if( !pVtab->zThis ){ echoDestructor((sqlite3_vtab *)pVtab); return SQLITE_NOMEM; } if( argc>3 ){ pVtab->zTableName = sqlite3_mprintf("%s", argv[3]); dequoteString(pVtab->zTableName); if( pVtab->zTableName && pVtab->zTableName[0]=='*' ){ char *z = sqlite3_mprintf("%s%s", argv[2], &(pVtab->zTableName[1])); sqlite3_free(pVtab->zTableName); pVtab->zTableName = z; pVtab->isPattern = 1; } if( !pVtab->zTableName ){ echoDestructor((sqlite3_vtab *)pVtab); return SQLITE_NOMEM; } } for(i=0; i<argc; i++){ appendToEchoModule(pVtab->interp, argv[i]); } rc = echoDeclareVtab(pVtab, db); if( rc!=SQLITE_OK ){ echoDestructor((sqlite3_vtab *)pVtab); return rc; } *ppVtab = &pVtab->base; return SQLITE_OK; }
/* ** This function is called to do the work of the xConnect() method - ** to allocate the required in-memory structures for a newly connected ** virtual table. */ static int echoConstructor( sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVtab, char **pzErr ){ int rc; int i; echo_vtab *pVtab; /* Allocate the sqlite3_vtab/echo_vtab structure itself */ pVtab = sqlite3MallocZero( sizeof(*pVtab) ); if( !pVtab ){ return SQLITE_NOMEM; } pVtab->interp = ((EchoModule *)pAux)->interp; pVtab->db = db; /* Allocate echo_vtab.zThis */ pVtab->zThis = sqlite3_mprintf("%s", argv[2]); if( !pVtab->zThis ){ echoDestructor((sqlite3_vtab *)pVtab); return SQLITE_NOMEM; } /* Allocate echo_vtab.zTableName */ if( argc>3 ){ pVtab->zTableName = sqlite3_mprintf("%s", argv[3]); dequoteString(pVtab->zTableName); if( pVtab->zTableName && pVtab->zTableName[0]=='*' ){ char *z = sqlite3_mprintf("%s%s", argv[2], &(pVtab->zTableName[1])); sqlite3_free(pVtab->zTableName); pVtab->zTableName = z; pVtab->isPattern = 1; } if( !pVtab->zTableName ){ echoDestructor((sqlite3_vtab *)pVtab); return SQLITE_NOMEM; } } /* Log the arguments to this function to Tcl var ::echo_module */ for(i=0; i<argc; i++){ appendToEchoModule(pVtab->interp, argv[i]); } /* Invoke sqlite3_declare_vtab and set up other members of the echo_vtab ** structure. If an error occurs, delete the sqlite3_vtab structure and ** return an error code. */ rc = echoDeclareVtab(pVtab, db); if( rc!=SQLITE_OK ){ echoDestructor((sqlite3_vtab *)pVtab); return rc; } /* Success. Set *ppVtab and return */ *ppVtab = &pVtab->base; return SQLITE_OK; }