Esempio n. 1
0
static void renameParentFunc(
  sqlite3_context *context,
  int NotUsed,
  sqlite3_value **argv
){
  sqlite3 *db = sqlite3_context_db_handle(context);
  char *zOutput = 0;
  char *zResult;
  unsigned char const *zInput = sqlite3_value_text(argv[0]);
  unsigned char const *zOld = sqlite3_value_text(argv[1]);
  unsigned char const *zNew = sqlite3_value_text(argv[2]);

  unsigned const char *z;         /* Pointer to token */
  int n;                          /* Length of token z */
  int token;                      /* Type of token */

  UNUSED_PARAMETER(NotUsed);
  for(z=zInput; *z; z=z+n){
    n = sqlite3GetToken(z, &token);
    if( token==TK_REFERENCES ){
      char *zParent;
      do {
        z += n;
        n = sqlite3GetToken(z, &token);
      }while( token==TK_SPACE );

      zParent = sqlite3DbStrNDup(db, (const char *)z, n);
      if( zParent==0 ) break;
      sqlite3Dequote(zParent);
      if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
        char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"", 
            (zOutput?zOutput:""), z-zInput, zInput, (const char *)zNew
        );
        sqlite3DbFree(db, zOutput);
        zOutput = zOut;
        zInput = &z[n];
      }
      sqlite3DbFree(db, zParent);
    }
  }

  zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput), 
  sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
  sqlite3DbFree(db, zOutput);
}
Esempio n. 2
0
SQLITE_EXTENSION_INIT1
#include <assert.h>
#include <string.h>

/*
** Implementation of the ieee754() function
*/
static void ieee754func(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  if( argc==1 ){
    sqlite3_int64 m, a;
    double r;
    int e;
    int isNeg;
    char zResult[100];
    assert( sizeof(m)==sizeof(r) );
    if( sqlite3_value_type(argv[0])!=SQLITE_FLOAT ) return;
    r = sqlite3_value_double(argv[0]);
    if( r<0.0 ){
      isNeg = 1;
      r = -r;
    }else{
      isNeg = 0;
    }
    memcpy(&a,&r,sizeof(a));
    if( a==0 ){
      e = 0;
      m = 0;
    }else{
      e = a>>52;
      m = a & ((((sqlite3_int64)1)<<52)-1);
      m |= ((sqlite3_int64)1)<<52;
      while( e<1075 && m>0 && (m&1)==0 ){
        m >>= 1;
        e++;
      }
      if( isNeg ) m = -m;
    }
    sqlite3_snprintf(sizeof(zResult), zResult, "ieee754(%lld,%d)",
                     m, e-1075);
    sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT);
  }else if( argc==2 ){
Esempio n. 3
0
static void
function_offsets (sqlite3_context *context,
                  int              argc,
                  sqlite3_value   *argv[])
{
	gchar *offsets, **names;
	gint offset_values[4];
	GString *result = NULL;
	gint i = 0;

	if (argc != 2) {
		sqlite3_result_error(context,
		                     "wrong number of arguments to function tracker_offsets()",
		                     -1);
		return;
	}

	offsets = sqlite3_value_text (argv[0]);
	names = (unsigned int *) sqlite3_value_blob (argv[1]);

	while (offsets && *offsets) {
		offset_values[i] = g_strtod (offsets, &offsets);

		/* All 4 values from the quartet have been gathered */
		if (i == 3) {
			if (!result) {
				result = g_string_new ("");
			} else {
				g_string_append_c (result, ',');
			}

			g_string_append_printf (result,
						"%s,%d",
						names[offset_values[0]],
						offset_values[2]);

		}

		i = (i + 1) % 4;
	}

	sqlite3_result_text (context,
			     (result) ? g_string_free (result, FALSE) : NULL,
			     -1, g_free);
}
Esempio n. 4
0
extern void pg_string_agg_final(sqlite3_context *context) {
  STRING_AGG_CONTEXT_T *ctx;

  ctx = (STRING_AGG_CONTEXT_T *)sqlite3_aggregate_context(context,0);
  if (ctx) {
    if (ctx->sz == 0) {
      sqlite3_result_null(context);
    } else {
      // SQLite3 will free what we have allocated
      sqlite3_result_text(context, (char *)ctx->aggr, ctx->len, sqlite3_free);
      ctx->aggr = (char *)NULL;
      ctx->sz = 0;
      ctx->len = 0;
    }
  } else {
    sqlite3_result_null(context);
  }
}
Esempio n. 5
0
SQLITE_EXTENSION_INIT1

static void hunupper(sqlite3_context *ctx, int argc, sqlite3_value **argv)
{
	const unsigned char *input;
	int length, pos = 0;
	unsigned char *result;

	if (argc != 1) {
		sqlite3_result_error(ctx, "invalid number of arguments", -1);
		return;
	}

	if (sqlite3_value_type(argv[0]) == SQLITE_NULL) return;
	input = (const unsigned char *) sqlite3_value_text(argv[0]);
	if (!input) {
		sqlite3_result_error(ctx, "no input specified", -1);
		return;
	}

	length = strlen((const char*) input);
	result = (unsigned char *)sqlite3_malloc(length);
	if (!result) {
		sqlite3_result_error(ctx, "cannot allocate result", -1);
		return;
	}

	while (pos < length) {
		result[pos] = input[pos] >= 'a' && input[pos] <= 'z' ? 0xDF & input[pos] : input[pos];
		switch (input[pos++]) {
			case 0xc3: /* á-a1>81 é-a9>89 í-ad>8d ó-b3>93 ö-b6>96 ú-ba>9a ü-bc>9c */
				result[pos] = input[pos] & 0xdf;
				pos++;
				break;
			case 0xc5: /* ő-91>90 ű-b1>b0 */
				result[pos] = input[pos] & 0xfe;
				pos++;
				break;
		}
	}

	sqlite3_result_text(ctx, (char *)result, length, sqlite3_free);
	return;
}
Esempio n. 6
0
/*
** This function is used by SQL generated to implement the 
** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
** CREATE INDEX command. The second is a table name. The table name in 
** the CREATE TABLE or CREATE INDEX statement is replaced with the third
** argument and the result returned. Examples:
**
** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
**     -> 'CREATE TABLE def(a, b, c)'
**
** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
**     -> 'CREATE INDEX i ON def(a, b, c)'
*/
static void renameTableFunc(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  unsigned char const *zSql = sqlite3_value_text(argv[0]);
  unsigned char const *zTableName = sqlite3_value_text(argv[1]);

  int token;
  Token tname;
  unsigned char const *zCsr = zSql;
  int len = 0;
  char *zRet;

  /* The principle used to locate the table name in the CREATE TABLE 
  ** statement is that the table name is the first token that is immediatedly
  ** followed by a left parenthesis - TK_LP - or "USING" TK_USING.
  */
  if( zSql ){
    do {
      if( !*zCsr ){
        /* Ran out of input before finding an opening bracket. Return NULL. */
        return;
      }

      /* Store the token that zCsr points to in tname. */
      tname.z = zCsr;
      tname.n = len;

      /* Advance zCsr to the next token. Store that token type in 'token',
      ** and it's length in 'len' (to be used next iteration of this loop).
      */
      do {
        zCsr += len;
        len = sqlite3GetToken(zCsr, &token);
      } while( token==TK_SPACE );
      assert( len>0 );
    } while( token!=TK_LP && token!=TK_USING );

    zRet = sqlite3MPrintf("%.*s%Q%s", tname.z - zSql, zSql, 
       zTableName, tname.z+tname.n);
    sqlite3_result_text(context, zRet, -1, sqlite3FreeX);
  }
}
Esempio n. 7
0
/*
**      hex_to_utf8(HEX)
**
** Convert the input string from HEX into binary.  Then return the
** result using sqlite3_result_text16le().
*/
static void testHexToUtf8(
  sqlite3_context *pCtx, 
  int nArg,
  sqlite3_value **argv
){
  int n;
  const char *zIn;
  char *zOut;
  assert( nArg==1 );
  n = sqlite3_value_bytes(argv[0]);
  zIn = (const char*)sqlite3_value_text(argv[0]);
  zOut = sqlite3_malloc( n/2 );
  if( zOut==0 ){
    sqlite3_result_error_nomem(pCtx);
  }else{
    testHexToBin(zIn, zOut);
    sqlite3_result_text(pCtx, zOut, n/2, sqlite3_free);
  }
}
Esempio n. 8
0
/*
 *   Doesn't exactly behaves as the PostgreSQL version as a \
 *   leaves sqlite completely cold. Serves, however, the
 *   same purpose, which is making data extracted from the
 *   database usable in a statement.
 */
extern void pg_quote_literal(sqlite3_context * context,
                             int               argc,
                             sqlite3_value  ** argv) {
  int   len;
  char *str;
  char *result = (char *)NULL;
  int   resultsz;
  int   i;
  int   j;

  _ksu_null_if_null_param(argc, argv);
  len = sqlite3_value_bytes(argv[0]);
  str = (char *)sqlite3_value_blob(argv[0]);
  if ((result = (char *)sqlite3_malloc(len + SAFETY_MARGIN + 1))
        == (char *)NULL) {
    sqlite3_result_error_nomem(context);
    return;
  }
  resultsz = len + SAFETY_MARGIN;
  *result = '\'';
  j = 1;
  for (i = 0; i < len; i++) {
    switch (str[i]) {
      case '\'':     // Escape quote
           result[j++] = '\'';
           break;
      default :
           break;
    }
    result[j++] = str[i];
    if (j >= resultsz - 2) { // Time to realloc
      if ((result = (char *)sqlite3_realloc(result,
                               resultsz + SAFETY_MARGIN + 1))
            == (char *)NULL) {
        sqlite3_result_error_nomem(context);
        return;
      }
      resultsz += SAFETY_MARGIN;
    }
  }
  result[j++] = '\'';
  sqlite3_result_text(context, result, j, sqlite3_free);
}
/**
 * Obtains the first UNICODE letter from the supplied string, normalizes and returns it.
 */
static void get_phonebook_index(
    sqlite3_context * context, int argc, sqlite3_value ** argv)
{
    if (argc != 2) {
      sqlite3_result_null(context);
      return;
    }

    char const * src = (char const *)sqlite3_value_text(argv[0]);
    char const * locale = (char const *)sqlite3_value_text(argv[1]);
    if (src == NULL || src[0] == 0 || locale == NULL) {
      sqlite3_result_null(context);
      return;
    }

    UCharIterator iter;
    uiter_setUTF8(&iter, src, -1);

    UBool isError = FALSE;
    UChar index[SMALL_BUFFER_SIZE];
    uint32_t len = android::GetPhonebookIndex(&iter, locale, index, sizeof(index), &isError);
    if (isError) {
      sqlite3_result_null(context);
      return;
    }

    uint32_t outlen = 0;
    uint8_t out[SMALL_BUFFER_SIZE];
    for (uint32_t i = 0; i < len; i++) {
      U8_APPEND(out, outlen, sizeof(out), index[i], isError);
      if (isError) {
        sqlite3_result_null(context);
        return;
      }
    }

    if (outlen == 0) {
      sqlite3_result_null(context);
      return;
    }

    sqlite3_result_text(context, (const char*)out, outlen, SQLITE_TRANSIENT);
}
static void SFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
  std::stringstream s;
  std::string d;
  double sum=0;
  char *buf=NULL;
  int i;

  s.str("");

  s << "(";    
  d="";
  for(i=0; i < argc; i++)
    {
  switch( sqlite3_value_type(argv[i]) ){
    case SQLITE_INTEGER: {
      sum+=(double) sqlite3_value_int64(argv[i]);
      s << d << sum;
      d=",";
      break;
    }
    case SQLITE_NULL: {
      s << d << "()";
      d=",";
      break;
    }
    default: {
      sum+=sqlite3_value_int64(argv[i]);
      s << d <<  sum;
      d=",";
      break;
     }
    }

    }

  s << ")";
  buf = (char *) malloc (sizeof(char)*(s.str().size()+2));
  if (buf == NULL)
    fprintf(stderr,"malloc error in SNFunc, buf\n");
  snprintf(buf,s.str().size()+1,"%s",s.str().c_str());
  sqlite3_result_text(context,buf,s.str().size()+1,free );

}                                                                                          
Esempio n. 11
0
static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
  char *z1;
  const char *z2;
  int i, n;
  UNUSED_PARAMETER(argc);
  z2 = (char*)sqlite3_value_text(argv[0]);
  n = sqlite3_value_bytes(argv[0]);
  /* Verify that the call to _bytes() does not invalidate the _text() pointer */
  assert( z2==(char*)sqlite3_value_text(argv[0]) );
  if( z2 ){
    z1 = contextMalloc(context, ((i64)n)+1);
    if( z1 ){
      for(i=0; i<n; i++){
        z1[i] = sqlite3Tolower(z2[i]);
      }
      sqlite3_result_text(context, z1, n, sqlite3_free);
    }
  }
}
Esempio n. 12
0
static void ST_GeometryType(sqlite3_context *context, int nbArgs, sqlite3_value **args) {
  spatialdb_t *spatialdb;
  FUNCTION_WKB_ARG(wkb);

  FUNCTION_START_STATIC(context, 256);
  spatialdb = (spatialdb_t *)sqlite3_user_data(context);
  FUNCTION_GET_WKB_ARG_UNSAFE(context, spatialdb, wkb, 0);

  const char *type_name;
  if (geom_type_name(wkb.geom_type, &type_name) == SQLITE_OK) {
    sqlite3_result_text(context, type_name, -1, SQLITE_STATIC);
  } else {
    error_append(FUNCTION_ERROR, "Unknown geometry type: %d", wkb.geom_type);
  }

  FUNCTION_END(context);

  FUNCTION_FREE_WKB_ARG(wkb);
}
Esempio n. 13
0
static
void OGRSQLITE_hstore_get_value(sqlite3_context* pContext,
                          int argc, sqlite3_value** argv)
{
    if( sqlite3_value_type (argv[0]) != SQLITE_TEXT ||
        sqlite3_value_type (argv[1]) != SQLITE_TEXT )
    {
        sqlite3_result_null (pContext);
        return;
    }

    const char* pszHStore = (const char*)sqlite3_value_text(argv[0]);
    const char* pszSearchedKey = (const char*)sqlite3_value_text(argv[1]);
    char* pszValue = OGRHStoreGetValue(pszHStore, pszSearchedKey);
    if( pszValue != NULL )
        sqlite3_result_text( pContext, pszValue, -1, CPLFree );
    else
        sqlite3_result_null( pContext );
}
static void phone_number_stripped_reversed(sqlite3_context * context, int argc,
      sqlite3_value ** argv)
{
    if (argc != 1) {
        sqlite3_result_int(context, 0);
        return;
    }

    char const * number = (char const *)sqlite3_value_text(argv[0]);
    if (number == NULL) {
        sqlite3_result_null(context);
        return;
    }

    char out[PHONE_NUMBER_BUFFER_SIZE];
    int outlen = 0;
    android::phone_number_stripped_reversed_inter(number, out, PHONE_NUMBER_BUFFER_SIZE, &outlen);
    sqlite3_result_text(context, (const char*)out, outlen, SQLITE_TRANSIENT);
}
Esempio n. 15
0
/*
** Implementation of highlight() function.
*/
static void fts5HighlightFunction(
  const Fts5ExtensionApi *pApi,   /* API offered by current FTS version */
  Fts5Context *pFts,              /* First arg to pass to pApi functions */
  sqlite3_context *pCtx,          /* Context for returning result/error */
  int nVal,                       /* Number of values in apVal[] array */
  sqlite3_value **apVal           /* Array of trailing arguments */
){
  HighlightContext ctx;
  int rc;
  int iCol;

  if( nVal!=3 ){
    const char *zErr = "wrong number of arguments to function highlight()";
    sqlite3_result_error(pCtx, zErr, -1);
    return;
  }

  iCol = sqlite3_value_int(apVal[0]);
  memset(&ctx, 0, sizeof(HighlightContext));
  ctx.zOpen = (const char*)sqlite3_value_text(apVal[1]);
  ctx.zClose = (const char*)sqlite3_value_text(apVal[2]);
  rc = pApi->xColumnText(pFts, iCol, &ctx.zIn, &ctx.nIn);

  if( ctx.zIn ){
    if( rc==SQLITE_OK ){
      rc = fts5CInstIterInit(pApi, pFts, iCol, &ctx.iter);
    }

    if( rc==SQLITE_OK ){
      rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
    }
    fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);

    if( rc==SQLITE_OK ){
      sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT);
    }
    sqlite3_free(ctx.zOut);
  }
  if( rc!=SQLITE_OK ){
    sqlite3_result_error_code(pCtx, rc);
  }
}
Esempio n. 16
0
static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
  char *z1;
  const char *z2;
  int i, n;
  if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
  z2 = (char*)sqlite3_value_text(argv[0]);
  n = sqlite3_value_bytes(argv[0]);
  /* Verify that the call to _bytes() does not invalidate the _text() pointer */
  assert( z2==(char*)sqlite3_value_text(argv[0]) );
  if( z2 ){
    z1 = sqlite3_malloc(n+1);
    if( z1 ){
      memcpy(z1, z2, n+1);
      for(i=0; z1[i]; i++){
        z1[i] = tolower(z1[i]);
      }
      sqlite3_result_text(context, z1, -1, sqlite3_free);
    }
  }
}
static void listFinalize(sqlite3_context *context){

  Ltx *p=NULL;

  char *buf=NULL;

  p = (Ltx *) sqlite3_aggregate_context(context, sizeof(*p));
  ssL[p->sscnt] << ")";
  
  buf = (char *) malloc (sizeof(char)*(ssL[p->sscnt].str().size()+2));
  if (buf == NULL)
    fprintf(stderr,"malloc error in listFinalize, buf\n");
  

  snprintf(buf,ssL[p->sscnt].str().size()+1,"%s",ssL[p->sscnt].str().c_str());
  sqlite3_result_text(context,buf,ssL[p->sscnt].str().size()+2,free );
  ssL[p->sscnt].clear();
  sscntL--;

}
static void FFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
  assert( argc==1 );
  char *buf=NULL;
  std::stringstream s;
  std::string ts;
  extract e;
  e.setdelims(", ");
  s.str("");

  s << sqlite3_value_text(argv[0]);
  e.strip(s.str());

  s.str(e.F());
  buf = (char *) malloc (sizeof(char)*(s.str().size()+2));
  if (buf == NULL)
    fprintf(stderr,"malloc error in SNFunc, buf\n");
  snprintf(buf,s.str().size()+1,"%s",s.str().c_str());
  sqlite3_result_text(context,buf,s.str().size()+1,free );

}
Esempio n. 19
0
/*
** Return values of columns for the row at which the vtablog_cursor
** is currently pointing.
*/
static int vtablogColumn(
  sqlite3_vtab_cursor *cur,   /* The cursor */
  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
  int i                       /* Which column to return */
){
  vtablog_cursor *pCur = (vtablog_cursor*)cur;
  vtablog_vtab *pTab = (vtablog_vtab*)cur->pVtab;
  char zVal[50];

  if( i<26 ){
    sqlite3_snprintf(sizeof(zVal),zVal,"%c%d", 
                     "abcdefghijklmnopqrstuvwyz"[i], pCur->iRowid);
  }else{
    sqlite3_snprintf(sizeof(zVal),zVal,"{%d}%d", i, pCur->iRowid);
  }
  printf("vtablogColumn(tab=%d, cursor=%d, i=%d): [%s]\n",
         pTab->iInst, pCur->iCursor, i, zVal);
  sqlite3_result_text(ctx, zVal, -1, SQLITE_TRANSIENT);
  return SQLITE_OK;
}
void qquncompress(sqlite3_context* context, int argc, sqlite3_value** argv)
{
#ifdef ZIP
    int len = sqlite3_value_bytes(argv[0]);
    unsigned char* msg = sqlite3_malloc(sizeof(unsigned char) * len);
    memcpy(msg, sqlite3_value_blob(argv[0]), len);

    unsigned long destlen;
    unsigned char* msg2 = qq_uncompress(msg, &destlen, len);
    sqlite3_free(msg);

    sqlite3_result_blob(context, msg2, destlen, sqlite3_free);
#else
    const char* msg = sqlite3_value_text(argv[0]);

    char* msg2 = base64_decode((char*) msg);

    sqlite3_result_text(context, msg2, strlen(msg2), sqlite3_free);
#endif
}
Esempio n. 21
0
/*
 * unzip --
 *  User defined Sqlite function to uncompress the FTS table.
 */
static void
unzip(sqlite3_context *pctx, int nval, sqlite3_value **apval)
{
	unsigned int rc;
	unsigned char *outbuf;
	z_stream stream;

	assert(nval == 1);
	stream.next_in = __UNCONST(sqlite3_value_blob(apval[0]));
	stream.avail_in = sqlite3_value_bytes(apval[0]);
	stream.avail_out = stream.avail_in * 2 + 100;
	stream.next_out = outbuf = malloc(stream.avail_out);
	assert(outbuf);
	stream.zalloc = NULL;
	stream.zfree = NULL;
	inflateInit(&stream);

	if (inflateInit(&stream) != Z_OK) {
		free(outbuf);
		return;
	}

	while ((rc = inflate(&stream, Z_SYNC_FLUSH)) != Z_STREAM_END) {
		if (rc != Z_OK ||
		    (stream.avail_out != 0 && stream.avail_in == 0)) {
			free(outbuf);
			return;
		}
		outbuf = realloc(outbuf, stream.total_out * 2);
		assert(outbuf);
		stream.next_out = outbuf + stream.total_out;
		stream.avail_out = stream.total_out;
	}
	if (inflateEnd(&stream) != Z_OK) {
		free(outbuf);
		return;
	}
	outbuf = realloc(outbuf, stream.total_out);
	assert(outbuf);
	sqlite3_result_text(pctx, outbuf, stream.total_out, free);
}
Esempio n. 22
0
/*
** The hex() function.  Interpret the argument as a blob.  Return
** a hexadecimal rendering as text.
*/
static void hexFunc(
    sqlite3_context *context,
    int argc,
    sqlite3_value **argv
) {
    int i, n;
    const unsigned char *pBlob;
    char *zHex, *z;
    assert( argc==1 );
    pBlob = sqlite3_value_blob(argv[0]);
    n = sqlite3_value_bytes(argv[0]);
    z = zHex = sqlite3_malloc(n*2 + 1);
    if( zHex==0 ) return;
    for(i=0; i<n; i++, pBlob++) {
        unsigned char c = *pBlob;
        *(z++) = hexdigits[(c>>4)&0xf];
        *(z++) = hexdigits[c&0xf];
    }
    *z = 0;
    sqlite3_result_text(context, zHex, n*2, sqlite3_free);
}
Esempio n. 23
0
void gbk(sqlite3_context *context,int argc,sqlite3_value **argv)
{
	if(argc == 1 && sqlite3_value_type(argv[0]) == SQLITE_TEXT)
	{ 
		const unsigned char* text = sqlite3_value_text(argv[0]);
		size_t textLen = sqlite3_value_bytes(argv[0]);

		size_t outLen = textLen * 4;
		char* out = (char*)sqlite3_malloc(outLen);
		char* pout = out;

		iconv_t handle = iconv_open("gbk","utf-8");
		memset(out,0,outLen);
		iconv(handle,(const char**)&text,&textLen,&pout,&outLen);
		iconv_close(handle);

		sqlite3_result_text(context,out,-1,SQLITE_TRANSIENT);

		sqlite3_free(out);
	}
}
Esempio n. 24
0
static void checkfreelist_function(
  sqlite3_context *pCtx,
  int nArg,
  sqlite3_value **apArg
){
  const char *zDb;
  int rc;
  char *zOut = 0;
  sqlite3 *db = sqlite3_context_db_handle(pCtx);

  assert( nArg==1 );
  zDb = (const char*)sqlite3_value_text(apArg[0]);
  rc = checkFreelist(db, zDb, &zOut);
  if( rc==SQLITE_OK ){
    sqlite3_result_text(pCtx, zOut?zOut:"ok", -1, SQLITE_TRANSIENT);
  }else{
    sqlite3_result_error_code(pCtx, rc);
  }

  sqlite3_free(zOut);
}
Esempio n. 25
0
/*
** Implementation of the substr() function
*/
static void substrFunc(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  const unsigned char *z;
  const unsigned char *z2;
  int i;
  int p1, p2, len;

  assert( argc==3 );
  z = sqlite3_value_text(argv[0]);
  if( z==0 ) return;
  p1 = sqlite3_value_int(argv[1]);
  p2 = sqlite3_value_int(argv[2]);
  for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
  if( p1<0 ){
    p1 += len;
    if( p1<0 ){
      p2 += p1;
      p1 = 0;
    }
  }else if( p1>0 ){
    p1--;
  }
  if( p1+p2>len ){
    p2 = len-p1;
  }
  for(i=0; i<p1 && z[i]; i++){
    if( (z[i]&0xc0)==0x80 ) p1++;
  }
  while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
  for(; i<p1+p2 && z[i]; i++){
    if( (z[i]&0xc0)==0x80 ) p2++;
  }
  while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
  if( p2<0 ) p2 = 0;
  sqlite3_result_text(context, (char*)&z[p1], p2, SQLITE_TRANSIENT);
}
Esempio n. 26
0
static void test_destructor(
  sqlite3_context *pCtx, 
  int nArg,
  sqlite3_value **argv
){
  char *zVal;
  int len;
  
  test_destructor_count_var++;
  assert( nArg==1 );
  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
  len = sqlite3_value_bytes(argv[0]); 
  zVal = testContextMalloc(pCtx, len+3);
  if( !zVal ){
    return;
  }
  zVal[len+1] = 0;
  zVal[len+2] = 0;
  zVal++;
  memcpy(zVal, sqlite3_value_text(argv[0]), len);
  sqlite3_result_text(pCtx, zVal, -1, destructor);
}
static void string_reverse_implementation(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
    const unsigned char* input = 0;
    int input_type;
    sqlite_uint64 input_length;
    char* result;
    int* decoded;

    if (argc < 1) {
        sqlite3_result_error(ctx, "not enough parameters", -1);
        return;
    }

    if (argc > 1) {
        sqlite3_result_error(ctx, "too many parameters", -1);
        return;
    }

    input_type = sqlite3_value_type(argv[0]);

    if (input_type != SQLITE_NULL) {
        input = sqlite3_value_text(argv[0]);
    }

    if (input == 0) {
        sqlite3_result_null(ctx);
        return;
    }

    input_length = strlen_utf8(input);
    result = sqlite3_malloc(4 * input_length + 1);
    decoded = sqlite3_malloc(sizeof(int) * input_length);

    decode_utf8(input, decoded);
    reverse_string(decoded, result, input_length);

    sqlite3_free(decoded);

    sqlite3_result_text(ctx, result, -1, result_string_destructor);
}
Esempio n. 28
0
static void ST_AsText(sqlite3_context *context, int nbArgs, sqlite3_value **args) {
  spatialdb_t *spatialdb;
  FUNCTION_GEOM_ARG(geomblob);

  FUNCTION_START_STATIC(context, 256);
  spatialdb = (spatialdb_t *)sqlite3_user_data(context);
  FUNCTION_GET_GEOM_ARG_UNSAFE(context, spatialdb, geomblob, 0);

  wkt_writer_t writer;
  wkt_writer_init(&writer);

  FUNCTION_RESULT = spatialdb->read_geometry(&FUNCTION_GEOM_ARG_STREAM(geomblob), wkt_writer_geom_consumer(&writer), FUNCTION_ERROR);

  if (FUNCTION_RESULT == SQLITE_OK) {
    sqlite3_result_text(context, wkt_writer_getwkt(&writer), (int) wkt_writer_length(&writer), SQLITE_TRANSIENT);
  }
  wkt_writer_destroy(&writer);

  FUNCTION_END(context);

  FUNCTION_FREE_GEOM_ARG(geomblob);
}
Esempio n. 29
0
void geo_type(sqlite3_context *context,int argc,sqlite3_value **argv)
{
	if(argc == 1 && sqlite3_value_type(argv[0]) == SQLITE_BLOB)
	{ 
		GEOSGeometry* geometry;
		char* type;
		const void* data = sqlite3_value_blob(argv[0]);
		size_t data_size = sqlite3_value_bytes(argv[0]);

		_init_geos();
		geometry = _geo_from_wkb((const unsigned char*)data,data_size);
		if(geometry != 0)
		{
			type = GEOSGeomType(geometry);
			sqlite3_result_text(context,type,-1,SQLITE_TRANSIENT);

			GEOSFree(type);
		}
		GEOSGeom_destroy(geometry);
		finishGEOS();
	}
}
Esempio n. 30
0
static void
convert_lua_value_to_sqlite(lua_State *L, sqlite3_context *ctx)
{
    switch(lua_type(L, -1)) {
        case LUA_TSTRING: {
            size_t length;
            const char *value;

            value = lua_tolstring(L, -1, &length);

            sqlite3_result_text(ctx, value, length, SQLITE_TRANSIENT);
            break;
        }
        case LUA_TNUMBER:
            sqlite3_result_double(ctx, lua_tonumber(L, -1));
            break;
        case LUA_TBOOLEAN:
            sqlite3_result_int(ctx, lua_toboolean(L, -1));
            break;
        case LUA_TNIL:
            sqlite3_result_null(ctx);
            break;

        case LUA_TTABLE:
        case LUA_TFUNCTION:
        case LUA_TTHREAD:
        case LUA_TUSERDATA: {
            char *error = NULL;

            error = sqlite3_mprintf("Invalid return type from lua(): %s", lua_typename(L, lua_type(L, -1)));

            sqlite3_result_error(ctx, error, -1);
            sqlite3_free(error);
        }
    }

    lua_pop(L, 1);
}