vendredi 6 mai 2016

Add function to SQLite

I need to add function to SQLite library so that it will be possible to use it in my C# project in queries. I wrote this function in C in Console application in VS but it seems to be wrong desicion because it appears error that main() is required while building . What project should I create for writing this function and how add it in SQLite?

#include <sqlite3.h>
#include "tcl.h"

#ifdef SQLITE_ENABLE_RTREE

typedef struct Circle Circle; 
struct Circle {   
struct Box {
    double xmin;
    double xmax;
    double ymin;
    double ymax;   
} aBox[2];   
double centerx;   
double centery;   
double radius; 
};

static void circle_del(void *p){   
    sqlite3_free(p); 
}

static int circle_geom(   sqlite3_rtree_geometry *p,   int nCoord,
double *aCoord,    int *pRes ){   
int i;                             
Circle *pCircle;                   
double xmin, xmax;                 
double ymin, ymax;        

  if( p->pUser==0 ){

    if( nCoord!=4 ) return SQLITE_ERROR;

    if( p->nParam!=3 || p->aParam[2]<0.0 ) return SQLITE_ERROR;

    pCircle = (Circle *)(p->pUser = sqlite3_malloc(sizeof(Circle)));
    if( !pCircle ) return SQLITE_NOMEM;
    p->xDelUser = circle_del;

    pCircle->centerx = p->aParam[0];
    pCircle->centery = p->aParam[1];
    pCircle->radius = p->aParam[2];

    pCircle->aBox[0].xmin = pCircle->centerx;
    pCircle->aBox[0].xmax = pCircle->centerx;
    pCircle->aBox[0].ymin = pCircle->centery + pCircle->radius;
    pCircle->aBox[0].ymax = pCircle->centery - pCircle->radius;
    pCircle->aBox[1].xmin = pCircle->centerx + pCircle->radius;
    pCircle->aBox[1].xmax = pCircle->centerx - pCircle->radius;
    pCircle->aBox[1].ymin = pCircle->centery;
    pCircle->aBox[1].ymax = pCircle->centery;   
}

  pCircle = (Circle *)p->pUser;
  xmin = aCoord[0];
  xmax = aCoord[1];
  ymin = aCoord[2];
  ymax = aCoord[3];

  for(i=0; i<4; i++){
    double x = (i&0x01) ? xmax : xmin;
    double y = (i&0x02) ? ymax : ymin;
    double d2;

    d2  = (x-pCircle->centerx)*(x-pCircle->centerx);
    d2 += (y-pCircle->centery)*(y-pCircle->centery);
    if( d2<(pCircle->radius*pCircle->radius) ){
      *pRes = 1;
      return SQLITE_OK;
    }   
}

  for(i=0; i<2; i++){
    if( xmin<=pCircle->aBox[i].xmin 
     && xmax>=pCircle->aBox[i].xmax 
     && ymin<=pCircle->aBox[i].ymin 
     && ymax>=pCircle->aBox[i].ymax 
    ){
      *pRes = 1;
      return SQLITE_OK;
    }   
}

  *pRes = 0;   
return SQLITE_OK; 
}
#endif

static int register_circle_geom(   void * clientData,   Tcl_Interp
*interp, int objc,   Tcl_Obj *CONST objv[] ){

#ifdef SQLITE_ENABLE_RTREE

 extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);  
 extern const char *sqlite3TestErrorName(int);   
 sqlite3 *db;   
 int rc;

  if( objc!=2 ){
    Tcl_WrongNumArgs(interp, 1, objv, "DB");
    return TCL_ERROR;   
}   
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;

rc = sqlite3_rtree_geometry_callback(db,"circle", circle_geom, 0);
Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);

#endif   

return TCL_OK; 
}

Also should I recompile SQLite and if yes - in what way?

Aucun commentaire:

Enregistrer un commentaire