Please submit code to pastebin.com or similar.
2011/11/16 Matt Young :
> 200 lines of code with embedded c. (headers omitted)
> This code comes up and reads triples from a table view called self, reads
> triples one at a time. The triples are either a configure and load sql
> statement command, or the triple is executed, as a random sql procedure
> doing whatever, Rinse and repeat. The code uses a call back gfun.
>
> // G engine
>
> #include "stdafx.h"
> #include "sqlite3.h"
> #include "g.h"
> #include "exec.h"
>
> int counter = 0;
> M m;
> int debug(char * s) {
> char c;
> if(DEBUG) {
> printf("%s \n Top Op: %d Status: %d Sel Row: %d\n G: " ,
> s,m.top.link,m.status,m.self_rowid); c = getchar();
> if(c == 'q') exit(0);
> }
> return(0);
> }
> void gerror(char * c) {printf("%s error %d\n",m.status,c);}
> const VAR vars[] = {
> {_rowid ,2 , "self_row" } ,
> {_rowid ,2 , "other_row" } ,
> {_rowid ,2 , "result_row" }
> };
> OP operands[OPERMAX];
> void tableswap() {
> char buff[200];
> m.iself^=1;
> sprintf_s(buff,"%s\n",
> "alter table other_alias rename to temp;"
> "alter table self_alias rename to other_alias;"
> "alter table temp rename to self_alias");
> m.status = sqlite3_exec(m.db,buff,0,0,0);
> m.self_rowid = m.rowid[m.iself];
> m.other_rowid = m.rowid[m.iself ^1];
> }
>
> //triple bind, unbind
> void print_triple(TRIPLE t) {
> printf("Triple: %s %d %d\n",t.key,t.link,t.pointer);
> }
>
> int bind_triple(sqlite3_stmt *stmt,TRIPLE t) {
> m.status = sqlite3_bind_text(stmt,0,t.key,strlen(t.key),0);
> m.status = sqlite3_bind_int(stmt,1,t.link);
> m.status = sqlite3_bind_int(stmt,2,t.pointer);
> return m.status;
> }
> void unbind_triple(sqlite3_stmt *stmt,TRIPLE *t) {
> t->key = (const char *) sqlite3_column_text( stmt, 0);
> t->link= sqlite3_column_int(stmt, 1);
> t->pointer= sqlite3_column_int(stmt, 2);
> }
>
> // install key at installed operand position pointer
> int config(TRIPLE t) {
> int ivar;
> const char * ch = t.key;
> switch(t.pointer) {
> case 0: //opcode pointer
> m.variable = atoi(ch);
> if((OPERMAX < m.variable) && (m.variable < USERMIN))
> return(SQLITE_MISUSE);
> operands[m.variable].callback = 0;
> operands[m.variable].vp[0] = 0;
> //debug("Newop");
> break;
> case 1: // install user script
> m.status =
> sqlite3_prepare_v2(m.db,ch,strlen(ch)+1, [m.variable].stmt,0);
> break;
> case 2: //properties
> operands[m.variable].properties= atoi(t.key);
> break;
> default: // g parameters
> ivar = t.pointer -3;
> if(0 < ivar && ivar < OPERMAX)
> operands[m.variable].vp[ivar] = atoi(t.key);
> break;
> }
> if((m.status != SQLITE_OK) && (m.status != SQLITE_ROW))
> gerror("config");
> return m.status;
> }
> void gfunction(sqlite3_context* p,int n,sqlite3_value** v) {
>
> int op = sqlite3_value_int(v[0]);
> int x = sqlite3_value_int(v[1]);
> //printf("gfun: %d %d\n",x,m.self_rowid);
>
> switch(op) {
> case 0:
> sqlite3_result_int(p, m.self_rowid+1);
> break;
> case 1:
> sqlite3_result_int(p, m.other_rowid);
> break;
> case 2:
> sqlite3_result_int(p, m.result_rowid);
> break;
> }
> }
> // Something did a select and sent data to G
> int ghandler(TRIPLE t) { //links are system wih no sql
> // All jumps an some pointer arithmetic and config go here
> if(debug("Ghandle")) return(G_RESTART);
> if( (m.status != SQLITE_ROW) && (m.status != SQLITE_DONE) && (m.status ==
> SQLITE_OK) )
> gerror("ghandle");
> if(m.status == SQLITE_DONE) {
> if(m.rowcount > 0)
> m.self_rowid += m.rowcount;
> m.rowcount = 0;
> m.top = m.called;
> m.called = POP_TRIPLE;
> return(m.status);
> }
> TRIPLE node;
> unbind_triple(m.op->stmt,);
> if(m.top.link != G_POP) {
> printf("Foreign data\n");
> print_triple(node);
> } else {
> m.rowcount++;
> print_triple(node);
> if(node.link == G_CONFIG)
> m.status = config(node);
> else
> m.called = node;
> }
> return m.status;
> }
>
> void read_triples() {
> int i,j;
> void * p;
> m.op = [m.top.link];
>
> for(i=0; m.op->vp[i]; i++ ) {
> j = m.op->vp[i]-1;
> p = vars[j].location;
> if(vars[j].type == 1)
> m.status = sqlite3_bind_text(m.op->stmt,i+1,(char *) p,strlen((char *)
> p),0);
> else if(vars[j].type == 2)
> m.status = sqlite3_bind_int(m.op->stmt,i+1,*(int*) p);
> else if(vars[j].type == 3)
> m.status = bind_triple(m.op->stmt,*(TRIPLE *)p);
> }
> if(m.status == SQLITE_OK) {
> do {
>
> if(m.op->stmt)
> m.status = sqlite3_step(m.op->stmt );
> m.status = ghandler(m.top);
> if(m.status == G_RESTART) return;
> } while( m.status == SQLITE_ROW || (m.status == SQLITE_OK) );
> if(m.op->stmt)
> m.status = sqlite3_reset(m.op->stmt);
> }
> else gerror("bind \n");
> }
> void init_gbase() {
> m.status = sqlite3_open(GBASE,);
> debug("Gbase");
> m.status = sqlite3_create_function_v2(m.db,GFUN,2,SQLITE_UTF8
> ,0,gfunction,0,0,
> NULL);
> m.status = sqlite3_prepare_v2(m.db,POP_SQL, strlen(POP_SQL),
> [G_POP].stmt,0);
> m.status = sqlite3_prepare_v2(m.db,TEST_SQL, strlen(TEST_SQL),
> [G_TEST].stmt,0);
>
> }
>
> int _tmain(int argc, _TCHAR* argv[])