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);
}
Exemple #2
0
static void powerFunc(sqlite3_context* context,
                      int argc,
                      sqlite3_value** argv) {
  assert(argc == 2);

  if (sqlite3_value_type(argv[0]) == SQLITE_NULL ||
      sqlite3_value_type(argv[1]) == SQLITE_NULL) {
    sqlite3_result_null(context);
  } else {
    double r1 = sqlite3_value_double(argv[0]);
    double r2 = sqlite3_value_double(argv[1]);
    errno = 0;
    double val = pow(r1, r2);
    if (errno == 0) {
      sqlite3_result_double(context, val);
    } else {
      sqlite3_result_error(context, platformStrerr(errno).c_str(), errno);
    }
  }
}
Exemple #3
0
extern void     my_ceil(sqlite3_context * context, 
                        int               argc, 
                        sqlite3_value  ** argv) {
    double  val;
    int     typ;
        
    typ = sqlite3_value_type(argv[0]);
    if (typ == SQLITE_NULL) {
      sqlite3_result_null(context);
    } else {
      if ((typ == SQLITE_INTEGER)
          || (typ == SQLITE_FLOAT)) {
        val = sqlite3_value_double(argv[0]);
        sqlite3_result_int64(context, (long)ceil(val));
      } else {
        //Wrong input 
        sqlite3_result_int(context, 0);
      } 
    } 
} 
Exemple #4
0
/*
** largest integer value not greater than argument
*/
void myfloorFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
  double rVal=0.0;

  switch( sqlite3_value_type(argv[0]) ){
    case SQLITE_INTEGER: {
      i64 iVal = sqlite3_value_int64(argv[0]);
      sqlite3_result_int64(context, iVal);
      break;
    }
    case SQLITE_NULL: {
      sqlite3_result_null(context);
      break;
    }
    default: {
      rVal = sqlite3_value_double(argv[0]);
      sqlite3_result_int64(context, (i64) floor(rVal));
      break;
    }
  }
}
/*
** Implementation of the sign() function
*/
static void msignFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
  assert( argc==1 );
  switch( sqlite3_value_type(argv[0]) ){
    case SQLITE_INTEGER: {
      long long int iVal = sqlite3_value_int64(argv[0]);
      iVal = ( iVal > 0) ? 1 : ( iVal < 0 ) ? -1 : 0;
      sqlite3_result_int64(context, iVal);
      break;
    }
    case SQLITE_NULL: {
      sqlite3_result_null(context);
      break;
    }
    default: {
      double rVal = sqlite3_value_double(argv[0]);
      rVal = ( rVal > 0) ? 1 : ( rVal < 0 ) ? -1 : 0;
      sqlite3_result_double(context, rVal);
      break;
    }
  }
}
Exemple #6
0
/*
** Implementation of the abs() function
*/
static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
  assert( argc==1 );
  switch( sqlite3_value_type(argv[0]) ){
    case SQLITE_INTEGER: {
      i64 iVal = sqlite3_value_int64(argv[0]);
      if( iVal<0 ) iVal = iVal * -1;
      sqlite3_result_int64(context, iVal);
      break;
    }
    case SQLITE_NULL: {
      sqlite3_result_null(context);
      break;
    }
    default: {
      double rVal = sqlite3_value_double(argv[0]);
      if( rVal<0 ) rVal = rVal * -1.0;
      sqlite3_result_double(context, rVal);
      break;
    }
  }
}
Exemple #7
0
static void callCastedDoubleFunc(sqlite3_context* context,
                                 int argc,
                                 sqlite3_value** argv,
                                 DoubleDoubleFunction f) {
  double rVal = 0.0;
  assert(argc == 1);
  switch (sqlite3_value_type(argv[0])) {
  case SQLITE_INTEGER: {
    int64_t iVal = sqlite3_value_int64(argv[0]);
    sqlite3_result_int64(context, iVal);
    break;
  }
  case SQLITE_NULL:
    sqlite3_result_null(context);
    break;
  default:
    rVal = sqlite3_value_double(argv[0]);
    sqlite3_result_int64(context, (int64_t)f(rVal));
    break;
  }
}
/* wrapper for Jaro-Winkler string comparison */
void jarowinkler_wrapper(sqlite3_context *ctx, int n_values, sqlite3_value **value)
{
	// check for NULL values, return NULL if any of the input strings is NULL
	if(sqlite3_value_type(value[0]) == SQLITE_NULL || 
  	 sqlite3_value_type(value[1]) == SQLITE_NULL)
  {
		sqlite3_result_null(ctx);
		return;
	}
  const unsigned char *str1 = sqlite3_value_text(value[0]);
  const unsigned char *str2 = sqlite3_value_text(value[1]);
	#ifdef DEBUG
 	  Rprintf("String 1: %s\n", str1);
		Rprintf("String 2: %s\n", str2);
	#endif
  double result;
  result = jarowinkler_core(str1, str2, 1.0/3, 1.0/3, 1.0/3, 0.5);
	#ifdef DEBUG
  	Rprintf("Ergebnis des Stringvergleichs: %f\n", result);
  #endif
  sqlite3_result_double(ctx, result);
}
Exemple #9
0
/**
 * @brief Call a math function that takes a double and returns a double.
 */
static void callDoubleFunc(sqlite3_context* context,
                           int argc,
                           sqlite3_value** argv,
                           DoubleDoubleFunction f) {
  double rVal = 0.0, val;
  assert(argc == 1);
  switch (sqlite3_value_type(argv[0])) {
  case SQLITE_NULL:
    sqlite3_result_null(context);
    break;
  default:
    rVal = sqlite3_value_double(argv[0]);
    errno = 0;
    val = f(rVal);
    if (errno == 0) {
      sqlite3_result_double(context, val);
    } else {
      sqlite3_result_error(context, platformStrerr(errno).c_str(), errno);
    }
    break;
  }
}
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);
}
static void GPKG_CheckSpatialMetaData(sqlite3_context *context, int nbArgs, sqlite3_value **args) {
  spatialdb_t *spatialdb;
  FUNCTION_TEXT_ARG(db_name);
  FUNCTION_INT_ARG(check);
  FUNCTION_INT_ARG(type);
  FUNCTION_START(context);

  spatialdb = (spatialdb_t *)sqlite3_user_data(context);
  if (nbArgs == 0) {
    FUNCTION_SET_TEXT_ARG(db_name, "main");
    FUNCTION_SET_INT_ARG(check, 0);
  } else if (nbArgs == 1) {
    FUNCTION_GET_TYPE(type, 0);
    if (type == SQLITE_TEXT) {
      FUNCTION_GET_TEXT_ARG(context, db_name, 0);
    } else {
      FUNCTION_SET_TEXT_ARG(db_name, "main");
      FUNCTION_GET_INT_ARG(check, 0);
    }
  }  else {
    FUNCTION_GET_TEXT_ARG(context, db_name, 0);
    FUNCTION_GET_INT_ARG(check, 1);
  }

  if (check != 0) {
    check = SQL_CHECK_ALL;
  }

  FUNCTION_RESULT = spatialdb->check_meta(FUNCTION_DB_HANDLE, db_name, check, FUNCTION_ERROR);
  if (FUNCTION_RESULT == SQLITE_OK) {
    sqlite3_result_null(context);
  }

  FUNCTION_END(context);

  FUNCTION_FREE_TEXT_ARG(db_name);
  FUNCTION_FREE_INT_ARG(check);
  FUNCTION_FREE_INT_ARG(type);
}
static void GPKG_CreateTilesTable(sqlite3_context *context, int nbArgs, sqlite3_value **args) {
  spatialdb_t *spatialdb;
  FUNCTION_TEXT_ARG(db_name);
  FUNCTION_TEXT_ARG(table_name);
  FUNCTION_START(context);

  spatialdb = (spatialdb_t *)sqlite3_user_data(context);
  if (nbArgs == 2) {
    FUNCTION_GET_TEXT_ARG(context, db_name, 0);
    FUNCTION_GET_TEXT_ARG(context, table_name, 1);
  } else {
    FUNCTION_SET_TEXT_ARG(db_name, "main");
    FUNCTION_GET_TEXT_ARG(context, table_name, 0);
  }

  if (spatialdb->create_tiles_table == NULL) {
    error_append(FUNCTION_ERROR, "Tiles tables are not supported in %s mode", spatialdb->name);
    goto exit;
  }

  FUNCTION_START_TRANSACTION(__create_tiles_table);

  FUNCTION_RESULT = spatialdb->init_meta(FUNCTION_DB_HANDLE, db_name, FUNCTION_ERROR);
  if (FUNCTION_RESULT == SQLITE_OK) {
    FUNCTION_RESULT = spatialdb->create_tiles_table(FUNCTION_DB_HANDLE, db_name, table_name, FUNCTION_ERROR);
  }

  FUNCTION_END_TRANSACTION(__create_tiles_table);

  if (FUNCTION_RESULT == SQLITE_OK) {
    sqlite3_result_null(context);
  }

  FUNCTION_END(context);

  FUNCTION_FREE_TEXT_ARG(db_name);
  FUNCTION_FREE_TEXT_ARG(table_name);
}
Exemple #13
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);
}
static int set_geos_geom_result(sqlite3_context *context, const geos_context_t *geos_context, const GEOSGeometry *geom, errorstream_t *error) {
  int result = SQLITE_OK;

  if (geom == NULL) {
    sqlite3_result_null(context);
    return result;
  } else {
    geom_blob_writer_t writer;
    geos_context->spatialdb->writer_init_srid(&writer, GEOSGetSRID_r(geos_context->geos_handle, geom));

    result = geos_read_geometry(geos_context->geos_handle, geom, geom_blob_writer_geom_consumer(&writer), error);

    if (result == SQLITE_OK) {
      sqlite3_result_blob(context, geom_blob_writer_getdata(&writer), geom_blob_writer_length(&writer), sqlite3_free);
    } else {
      sqlite3_result_error(context, error_message(error), -1);
    }

    geos_context->spatialdb->writer_destroy(&writer, 0);

    return result;
  }
}
static void ST_Buffer(sqlite3_context *context, int nbArgs, sqlite3_value **args) {
  GEOS_START(context);
  GEOS_GET_GEOM(g1, args, 0);
  double distance = sqlite3_value_double(args[1]);
  if (g1 == NULL) {
    if (error_count(&error) > 0) {
      sqlite3_result_error(context, error_message(&error), -1);
    } else {
      sqlite3_result_null(context);
    }
    return;
  }

  GEOSGeometry *result = GEOSBuffer_r(GEOS_HANDLE, g1->geometry, distance, DEFAULT_QUADRANT_SEGMENTS);
  if (result != NULL) {
    set_geos_geom_result(context, GEOS_CONTEXT, result, &error);
    GEOSGeom_destroy_r( GEOS_HANDLE, result );
  } else {
    geom_geos_get_error(&error);
    sqlite3_result_error(context, error_message(&error), -1);
  }
  GEOS_FREE_GEOM( g1, 0 );
}
Exemple #16
0
static void
mm_cipher_key_func(sqlite3_context *db, int argc, sqlite3_value **argv)
{
    mm_cipher_context_t *ctx;

    // only accept 1 BLOB argument.
    if (argc != 1)
        goto error_misuse;
    if (sqlite3_value_type(argv[0]) != SQLITE_BLOB)
        goto error_misuse;
    if (sqlite3_value_bytes(argv[0]) != 16)
        goto error_misuse;

    ctx = (mm_cipher_context_t *) sqlite3_user_data(db);
    memcpy(ctx->key, sqlite3_value_blob(argv[0]), 16);

    sqlite3_result_null(db);
    return;

error_misuse:
    sqlite3_result_error_code(db, SQLITE_MISUSE);
    return;
}
Exemple #17
0
bool Command::DoReturn( void )
{
	if( mpExpr )
	{
		sqlite3_context* ctx = mpVM->mpContext;

		Value vl = mpExpr->get_Value();
		if( mpVM->mType != kUnknown && vl.mType != mpVM->mType )
			vl.Convert( mpVM->mType );

		switch( vl.mType )
		{
			case kDouble	: RESULT_DOUBLE( vl.ToDouble() ); break;
			case kInt		: RESULT_INT( vl.ToInt() ); break;
			case kText		: RESULT_WTEXT( vl.ToString() ); break;

			default:
				sqlite3_result_null( mpVM->mpContext );
		}
	}
	
	return false;
}
Exemple #18
0
static void sqlite3_do_callback(sqlite3_context *context,
                                const Variant& callback,
                                int argc,
                                sqlite3_value **argv,
                                bool is_agg) {
  Array params = Array::Create();
  php_sqlite3_agg_context *agg_context = nullptr;
  if (is_agg) {
    agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context
      (context, sizeof(php_sqlite3_agg_context));
    params.appendRef(agg_context->context);
    params.append(agg_context->row_count);
  }
  for (int i = 0; i < argc; i++) {
    params.append(get_value(argv[i]));
  }
  Variant ret = vm_call_user_func(callback, params);

  if (!is_agg || !argv) {
    /* only set the sqlite return value if we are a scalar function,
     * or if we are finalizing an aggregate */
    if (ret.isInteger()) {
      sqlite3_result_int(context, ret.toInt64());
    } else if (ret.isNull()) {
      sqlite3_result_null(context);
    } else if (ret.isDouble()) {
      sqlite3_result_double(context, ret.toDouble());
    } else {
      String sret = ret.toString();
      sqlite3_result_text(context, sret.data(), sret.size(), SQLITE_TRANSIENT);
    }
  } else {
    /* we're stepping in an aggregate; the return value goes into
     * the context */
    agg_context->context = ret;
  }
}
Exemple #19
0
/*
** Implementation of the abs() function.
**
** IMP: R-23979-26855 The abs(X) function returns the absolute value of
** the numeric argument X. 
*/
static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
  assert( argc==1 );
  UNUSED_PARAMETER(argc);
  switch( sqlite3_value_type(argv[0]) ){
    case SQLITE_INTEGER: {
      i64 iVal = sqlite3_value_int64(argv[0]);
      if( iVal<0 ){
        if( (iVal<<1)==0 ){
          /* IMP: R-35460-15084 If X is the integer -9223372036854775807 then
          ** abs(X) throws an integer overflow error since there is no
          ** equivalent positive 64-bit two complement value. */
          sqlite3_result_error(context, "integer overflow", -1);
          return;
        }
        iVal = -iVal;
      } 
      sqlite3_result_int64(context, iVal);
      break;
    }
    case SQLITE_NULL: {
      /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
      sqlite3_result_null(context);
      break;
    }
    default: {
      /* Because sqlite3_value_double() returns 0.0 if the argument is not
      ** something that can be converted into a number, we have:
      ** IMP: R-57326-31541 Abs(X) return 0.0 if X is a string or blob that
      ** cannot be converted to a numeric value. 
      */
      double rVal = sqlite3_value_double(argv[0]);
      if( rVal<0 ) rVal = -rVal;
      sqlite3_result_double(context, rVal);
      break;
    }
  }
}
static void ST_Relate(sqlite3_context *context, int nbArgs, sqlite3_value **args) {
  GEOS_START(context);
  GEOS_GET_GEOM(g1, args, 0);
  GEOS_GET_GEOM(g2, args, 1);
  const unsigned char *pattern = sqlite3_value_text(args[2]);
  if (g1 == NULL || g2 == NULL || pattern == NULL) {
    if (error_count(&error) > 0) {
      sqlite3_result_error(context, error_message(&error), -1);
    } else {
      sqlite3_result_null(context);
    }
    return;
  }

  char result = GEOSRelatePattern_r(GEOS_HANDLE, g1->geometry, g2->geometry, (const char *)pattern);
  if (result == 2) {
    geom_geos_get_error(&error);
    sqlite3_result_error(context, error_message(&error), -1);
  } else {
    sqlite3_result_int(context, result);
  }
  GEOS_FREE_GEOM(g1, 0);
  GEOS_FREE_GEOM(g2, 1);
}
static void GPKG_InitSpatialMetaData(sqlite3_context *context, int nbArgs, sqlite3_value **args) {
  spatialdb_t *spatialdb;
  FUNCTION_TEXT_ARG(db_name);

  FUNCTION_START(context);
  spatialdb = (spatialdb_t *)sqlite3_user_data(context);
  if (nbArgs == 0) {
    FUNCTION_SET_TEXT_ARG(db_name, "main");
  } else {
    FUNCTION_GET_TEXT_ARG(context, db_name, 0);
  }

  FUNCTION_START_TRANSACTION(__initspatialdb);
  FUNCTION_RESULT = spatialdb->init_meta(FUNCTION_DB_HANDLE, db_name, FUNCTION_ERROR);
  FUNCTION_END_TRANSACTION(__initspatialdb);

  if (FUNCTION_RESULT == SQLITE_OK) {
    sqlite3_result_null(context);
  }

  FUNCTION_END(context);

  FUNCTION_FREE_TEXT_ARG(db_name);
}
Exemple #22
0
GEOPACKAGE_DECLARE void
fnct_gpkgMakePointZMWithSRID (sqlite3_context * context, int argc UNUSED,
			      sqlite3_value ** argv)
{
/* SQL function:
/ gpkgMakePointZM(x, y, z, m, srid)
/
/ Creates a GeoPackage geometry POINT ZM
/
/ returns nothing on success, raises exception on error
*/
    unsigned int len;
    int int_value;
    unsigned char *p_result = NULL;
    double x;
    double y;
    double z;
    double m;
    int srid;
    GEOPACKAGE_UNUSED ();	/* LCOV_EXCL_LINE */
    if (sqlite3_value_type (argv[0]) == SQLITE_FLOAT)
      {
	  x = sqlite3_value_double (argv[0]);
      }
    else if (sqlite3_value_type (argv[0]) == SQLITE_INTEGER)
      {
	  int_value = sqlite3_value_int (argv[0]);
	  x = int_value;
      }
    else
      {
	  sqlite3_result_null (context);
	  return;
      }
    if (sqlite3_value_type (argv[1]) == SQLITE_FLOAT)
      {
	  y = sqlite3_value_double (argv[1]);
      }
    else if (sqlite3_value_type (argv[1]) == SQLITE_INTEGER)
      {
	  int_value = sqlite3_value_int (argv[1]);
	  y = int_value;
      }
    else
      {
	  sqlite3_result_null (context);
	  return;
      }
    if (sqlite3_value_type (argv[2]) == SQLITE_FLOAT)
      {
	  z = sqlite3_value_double (argv[2]);
      }
    else if (sqlite3_value_type (argv[2]) == SQLITE_INTEGER)
      {
	  int_value = sqlite3_value_int (argv[2]);
	  z = int_value;
      }
    else
      {
	  sqlite3_result_null (context);
	  return;
      }
    if (sqlite3_value_type (argv[3]) == SQLITE_FLOAT)
      {
	  m = sqlite3_value_double (argv[3]);
      }
    else if (sqlite3_value_type (argv[3]) == SQLITE_INTEGER)
      {
	  int_value = sqlite3_value_int (argv[3]);
	  m = int_value;
      }
    else
      {
	  sqlite3_result_null (context);
	  return;
      }
    if (sqlite3_value_type (argv[4]) != SQLITE_INTEGER)
      {
	  sqlite3_result_null (context);
	  return;
      }
    srid = sqlite3_value_int (argv[4]);

    gpkgMakePointZM (x, y, z, m, srid, &p_result, &len);
    if (!p_result)
      {
	  sqlite3_result_null (context);
      }
    else
      {
	  sqlite3_result_blob (context, p_result, len, free);
      }
}
Exemple #23
0
extern void pg_age(sqlite3_context  *context,
                   int argc,
                   sqlite3_value   **argv) {
   struct tm  age;
   struct tm  beg;
   struct tm  tmbuff;
   struct tm *bp;
   struct tm *bigger;
   struct tm *smaller;
   time_t     currTime;
   double     seconds;
   char      *rp;
   int        rplen;
   char       buff[BUFF_LEN];
   char      *arg1;
   char      *arg2;

   _ksu_check_arg_cnt(argc, 1, 2, "age");
    if (ksu_prm_ok(context, argc, argv,
                   "age", KSU_PRM_DATETIME, KSU_PRM_DATETIME)) {
      (void)memset(&age, 0, sizeof(struct tm));
      arg1 = (char *)sqlite3_value_text(argv[0]);
      if ((sscanf(arg1, "%d-%d-%d %d:%d:%d",
                  &age.tm_year, &age.tm_mon, &age.tm_mday,
                  &age.tm_hour, &age.tm_min, &age.tm_sec) < 3)) {
        ksu_err_msg(context, KSU_ERR_ARG_N_NOT_DATETIME,
                    1, "age");
        return;
      }
      age.tm_mon -= 1;
      age.tm_year -= 1900;
      if (argc == 1) {
        time(&currTime);
        if (currTime != -1) {
          bp = localtime_r(&currTime, &tmbuff);
          if (bp) {
            bp->tm_hour = 0;
            bp->tm_min = 0;
            bp->tm_sec = 0;
            bp->tm_gmtoff = 0;
            bp->tm_isdst = 0;
            seconds = currTime - mktime(&age);
            if (seconds > 0) {
              bigger = bp;
              smaller = &age;
            } else {
              bigger = &age;
              smaller = bp;
            }
          } else {
            // Not expected
            sqlite3_result_null(context);
            return;
          }
        } else {
          // Not expected
          sqlite3_result_null(context);
          return;
        }
      } else   {
        (void)memset(&beg, 0, sizeof(struct tm));
        arg2 = (char *)sqlite3_value_text(argv[1]);
        if ((sscanf(arg2, "%d-%d-%d %d:%d:%d",
            &beg.tm_year, &beg.tm_mon, &beg.tm_mday,
            &beg.tm_hour, &beg.tm_min, &beg.tm_sec) < 3)) {
          ksu_err_msg(context, KSU_ERR_ARG_N_NOT_DATETIME,
                      2, "age");
          return;
        }
        beg.tm_mon -= 1;
        beg.tm_year -= 1900;
        seconds = mktime(&age) - mktime(&beg);
        if (seconds > 0) {
          bigger = &age;
          smaller = &beg;
        } else   {
          bigger = &beg;
          smaller = &age;
        }
        bp = &age;
      }
      findDifference(bigger, smaller);
      if ((rp = (char *)sqlite3_malloc(AGE_LEN)) == NULL) {
        sqlite3_result_error_nomem(context);
        return;
      }
      rp[0] = '\0';
      rplen = 0;
      if (bigger->tm_year) {
        snprintf(buff, BUFF_LEN, "%s%d years",
                 (bigger == bp ? "" : "-"), bigger->tm_year);
        strncat(rp, buff, AGE_LEN - rplen);
        rplen = strlen(rp);
      }
      if (bigger->tm_mon) {
        snprintf(buff, BUFF_LEN, "%s%s%d mons",
                 (rplen ? " " : ""),
                 (bigger == bp ? "" : "-"), bigger->tm_mon);
        strncat(rp, buff, AGE_LEN - rplen);
        rplen = strlen(rp);
      }
      if (bigger->tm_mday) {
        snprintf(buff, BUFF_LEN, "%s%s%d days",
                 (rplen ? " " : ""),
                 (bigger == bp ? "" : "-"),  bigger->tm_mday);
        strncat(rp, buff, AGE_LEN - rplen);
      }
      sqlite3_result_text(context, rp, -1, sqlite3_free);
   }
}
 void context::result(null_type)
 {
   sqlite3_result_null(ctx_);
 }
Exemple #25
0
static void
result_or_bind(sqlite3_context *ctx, sqlite3_stmt *stmt, int idx,
	       char *data, int len, int type)
{
    char *endp;

    if (!data) {
	if (ctx) {
	    sqlite3_result_null(ctx);
	} else {
	    sqlite3_bind_null(stmt, idx);
	}
	return;
    }
    if (type == SQLITE_INTEGER) {
	sqlite_int64 val;
#if defined(_WIN32) || defined(_WIN64)
	char endc;

	if (sscanf(data, "%I64d%c", &val, &endc) == 1) {
	    if (ctx) {
		sqlite3_result_int64(ctx, val);
	    } else {
		sqlite3_bind_int64(stmt, idx, val);
	    }
	    return;
	}
#else
	endp = 0;
#ifdef __osf__
	val = strtol(data, &endp, 0);
#else
	val = strtoll(data, &endp, 0);
#endif
	if (endp && (endp != data) && !*endp) {
	    if (ctx) {
		sqlite3_result_int64(ctx, val);
	    } else {
		sqlite3_bind_int64(stmt, idx, val);
	    }
	    return;
	}
#endif
    } else if (type == SQLITE_FLOAT) {
	double val;

	endp = 0;
	val = strtod(data, &endp);
	if (endp && (endp != data) && !*endp) {
	    if (ctx) {
		sqlite3_result_double(ctx, val);
	    } else {
		sqlite3_bind_double(stmt, idx, val);
	    }
	    return;
	}
    }
    if (ctx) {
	sqlite3_result_text(ctx, data, len, SQLITE_TRANSIENT);
    } else {
	sqlite3_bind_text(stmt, idx, data, len, SQLITE_TRANSIENT);
    }
}
static
void OGR2SQLITE_ogr_geocode_reverse(sqlite3_context* pContext,
                                    int argc, sqlite3_value** argv)
{
    OGRSQLiteExtensionData* poModule =
        (OGRSQLiteExtensionData*) sqlite3_user_data(pContext);

    double dfLon = 0.0, dfLat = 0.0;
    int iAfterGeomIdx = 0;
    int bGotLon = FALSE, bGotLat = FALSE;

    if( argc >= 2 )
    {
        dfLon = OGR2SQLITE_GetValAsDouble(argv[0], &bGotLon);
        dfLat = OGR2SQLITE_GetValAsDouble(argv[1], &bGotLat);
    }

    if( argc >= 3 && bGotLon && bGotLat &&
            sqlite3_value_type (argv[2]) == SQLITE_TEXT )
    {
        iAfterGeomIdx = 2;
    }
    else if( argc >= 2 &&
             sqlite3_value_type (argv[0]) == SQLITE_BLOB &&
             sqlite3_value_type (argv[1]) == SQLITE_TEXT )
    {
        OGRGeometry* poGeom = OGR2SQLITE_GetGeom(pContext, argc, argv, NULL);
        if( poGeom != NULL && wkbFlatten(poGeom->getGeometryType()) == wkbPoint )
        {
            OGRPoint* poPoint = (OGRPoint*) poGeom;
            dfLon = poPoint->getX();
            dfLat = poPoint->getY();
            delete poGeom;
        }
        else
        {
            delete poGeom;
            sqlite3_result_null (pContext);
            return;
        }
        iAfterGeomIdx = 1;
    }
    else
    {
        sqlite3_result_null (pContext);
        return;
    }

    const char* pszField = (const char*)sqlite3_value_text(argv[iAfterGeomIdx]);

    int i;
    char** papszOptions = NULL;
    for(i = iAfterGeomIdx + 1; i < argc; i++)
    {
        if( sqlite3_value_type (argv[i]) == SQLITE_TEXT )
        {
            papszOptions = CSLAddString(papszOptions,
                                        (const char*)sqlite3_value_text(argv[i]));
        }
    }

    OGRGeocodingSessionH hSession = poModule->GetGeocodingSession();
    if( hSession == NULL )
    {
        hSession = OGRGeocodeCreateSession(papszOptions);
        if( hSession == NULL )
        {
            sqlite3_result_null (pContext);
            CSLDestroy(papszOptions);
            return;
        }
        poModule->SetGeocodingSession(hSession);
    }

    if( strcmp(pszField, "raw") == 0 )
        papszOptions = CSLAddString(papszOptions, "RAW_FEATURE=YES");

    OGRLayerH hLayer = OGRGeocodeReverse(hSession, dfLon, dfLat, papszOptions);

    OGR2SQLITE_ogr_geocode_set_result(pContext, hLayer, pszField);

    CSLDestroy(papszOptions);

    return;
}
Exemple #27
0
void context::result() {
	sqlite3_result_null(handle);
}
static int
vxpath_column (sqlite3_vtab_cursor * pCursor, sqlite3_context * pContext,
	       int column)
{
/* fetching value for the Nth column */
    VirtualXPathCursorPtr cursor = (VirtualXPathCursorPtr) pCursor;
    xmlNodeSetPtr nodeset = cursor->xpathObj->nodesetval;
    xmlNodePtr node = nodeset->nodeTab[cursor->xpathIdx];
    xmlNodePtr parent = node->parent;
    char *xParent = NULL;
    char *xNode = NULL;
    char *xAttribute = NULL;
    char *xValue = NULL;
    char *prefix;

    if (node->type == XML_ELEMENT_NODE)
      {
	  if (parent != NULL)
	    {
		if (parent->ns != NULL && parent->name != NULL)
		  {
		      prefix = (char *) (parent->ns->prefix);
		      if (prefix == NULL)
			  prefix = "dflt";
		      xParent = sqlite3_mprintf ("%s:%s", prefix, parent->name);
		  }
		else if (parent->name != NULL)
		    xParent = sqlite3_mprintf ("%s", parent->name);
	    }
	  if (node->ns != NULL && node->name != NULL)
	    {
		prefix = (char *) (node->ns->prefix);
		if (prefix == NULL)
		    prefix = "dflt";
		xNode = sqlite3_mprintf ("%s:%s", prefix, node->name);
	    }
	  else if (node->name != NULL)
	      xNode = sqlite3_mprintf ("%s", node->name);
      }
    else if (node->type == XML_ATTRIBUTE_NODE)
      {
	  if (parent != NULL)
	    {
		xmlNodePtr granpa = parent->parent;
		if (granpa != NULL)
		  {
		      if (granpa->ns != NULL && granpa->name != NULL)
			{
			    prefix = (char *) (granpa->ns->prefix);
			    if (prefix == NULL)
				prefix = "dflt";
			    xParent =
				sqlite3_mprintf ("%s:%s", prefix, granpa->name);
			}
		      else if (granpa->name != NULL)
			  xParent = sqlite3_mprintf ("%s", granpa->name);
		  }
		if (parent->ns != NULL && parent->name != NULL)
		  {
		      prefix = (char *) (parent->ns->prefix);
		      if (prefix == NULL)
			  prefix = "dflt";
		      xNode = sqlite3_mprintf ("%s:%s", prefix, parent->name);
		  }
		else if (parent->name != NULL)
		    xNode = sqlite3_mprintf ("%s", parent->name);
	    }
	  if (node->ns != NULL && node->name != NULL)
	    {
		prefix = (char *) (node->ns->prefix);
		if (prefix == NULL)
		    prefix = "dflt";
		xAttribute = sqlite3_mprintf ("%s:%s", prefix, node->name);
	    }
	  else if (node->name != NULL)
	      xAttribute = sqlite3_mprintf ("%s", node->name);
	  if (node->children != NULL)
	    {
		if (node->children->content != NULL)
		    xValue = sqlite3_mprintf ("%s", node->children->content);
	    }
      }
    else if (node->type == XML_TEXT_NODE)
      {
	  if (parent != NULL)
	    {
		xmlNodePtr granpa = parent->parent;
		if (granpa != NULL)
		  {
		      if (granpa->ns != NULL && granpa->name != NULL)
			{
			    prefix = (char *) (granpa->ns->prefix);
			    if (prefix == NULL)
				prefix = "dflt";
			    xParent =
				sqlite3_mprintf ("%s:%s", prefix, granpa->name);
			}
		      else if (granpa->name != NULL)
			  xParent = sqlite3_mprintf ("%s", granpa->name);
		  }
		if (parent->ns != NULL && parent->name != NULL)
		  {
		      prefix = (char *) (parent->ns->prefix);
		      if (prefix == NULL)
			  prefix = "dflt";
		      xNode = sqlite3_mprintf ("%s:%s", prefix, parent->name);
		  }
		else if (parent->name != NULL)
		    xNode = sqlite3_mprintf ("%s", parent->name);
	    }
	  if (node->content != NULL)
	      xValue = sqlite3_mprintf ("%s", node->content);
      }

    if (column == 0)
	sqlite3_result_int64 (pContext, cursor->current_row);
    else if (column == 1)
	sqlite3_result_int (pContext, cursor->xpathIdx);
    else if (column == 2)
      {
	  if (!xParent)
	      sqlite3_result_null (pContext);
	  else
	      sqlite3_result_text (pContext, xParent,
				   strlen (xParent), SQLITE_TRANSIENT);
      }
    else if (column == 3)
      {
	  if (!xNode)
	      sqlite3_result_null (pContext);
	  else
	      sqlite3_result_text (pContext, xNode,
				   strlen (xNode), SQLITE_TRANSIENT);
      }
    else if (column == 4)
      {
	  if (!xAttribute)
	      sqlite3_result_null (pContext);
	  else
	      sqlite3_result_text (pContext, xAttribute,
				   strlen (xAttribute), SQLITE_TRANSIENT);
      }
    else if (column == 5)
      {
	  if (!xValue)
	      sqlite3_result_null (pContext);
	  else
	      sqlite3_result_text (pContext, xValue,
				   strlen (xValue), SQLITE_TRANSIENT);
      }
    else if (column == 6)
	sqlite3_result_text (pContext, cursor->xpathExpr,
			     strlen (cursor->xpathExpr), SQLITE_STATIC);
    else
	sqlite3_result_null (pContext);
    if (xParent)
	sqlite3_free (xParent);
    if (xNode)
	sqlite3_free (xNode);
    if (xAttribute)
	sqlite3_free (xAttribute);
    if (xValue)
	sqlite3_free (xValue);
    return SQLITE_OK;
}
Exemple #29
0
static int do_callback(struct pdo_sqlite_fci *fc, zval *cb,
		int argc, sqlite3_value **argv, sqlite3_context *context,
		int is_agg)
{
	zval *zargs = NULL;
	zval retval;
	int i;
	int ret;
	int fake_argc;
	zend_reference *agg_context = NULL;

	if (is_agg) {
		is_agg = 2;
	}

	fake_argc = argc + is_agg;

	fc->fci.size = sizeof(fc->fci);
	ZVAL_COPY_VALUE(&fc->fci.function_name, cb);
	fc->fci.object = NULL;
	fc->fci.retval = &retval;
	fc->fci.param_count = fake_argc;

	/* build up the params */

	if (fake_argc) {
		zargs = safe_emalloc(fake_argc, sizeof(zval), 0);
	}

	if (is_agg) {
		agg_context = (zend_reference*)sqlite3_aggregate_context(context, sizeof(zend_reference));
		if (!agg_context) {
			ZVAL_NULL(&zargs[0]);
		} else {
			if (Z_ISUNDEF(agg_context->val)) {
				GC_REFCOUNT(agg_context) = 1;
				GC_TYPE_INFO(agg_context) = IS_REFERENCE;
				ZVAL_NULL(&agg_context->val);
			}
			ZVAL_REF(&zargs[0], agg_context);
		}
		ZVAL_LONG(&zargs[1], sqlite3_aggregate_count(context));
	}

	for (i = 0; i < argc; i++) {
		/* get the value */
		switch (sqlite3_value_type(argv[i])) {
			case SQLITE_INTEGER:
				ZVAL_LONG(&zargs[i + is_agg], sqlite3_value_int(argv[i]));
				break;

			case SQLITE_FLOAT:
				ZVAL_DOUBLE(&zargs[i + is_agg], sqlite3_value_double(argv[i]));
				break;

			case SQLITE_NULL:
				ZVAL_NULL(&zargs[i + is_agg]);
				break;

			case SQLITE_BLOB:
			case SQLITE3_TEXT:
			default:
				ZVAL_STRINGL(&zargs[i + is_agg], (char*)sqlite3_value_text(argv[i]), sqlite3_value_bytes(argv[i]));
				break;
		}
	}

	fc->fci.params = zargs;

	if ((ret = zend_call_function(&fc->fci, &fc->fcc)) == FAILURE) {
		php_error_docref(NULL, E_WARNING, "An error occurred while invoking the callback");
	}

	/* clean up the params */
	if (zargs) {
		for (i = is_agg; i < fake_argc; i++) {
			zval_ptr_dtor(&zargs[i]);
		}
		if (is_agg) {
			zval_ptr_dtor(&zargs[1]);
		}
		efree(zargs);
	}

	if (!is_agg || !argv) {
		/* only set the sqlite return value if we are a scalar function,
		 * or if we are finalizing an aggregate */
		if (!Z_ISUNDEF(retval)) {
			switch (Z_TYPE(retval)) {
				case IS_LONG:
					sqlite3_result_int(context, Z_LVAL(retval));
					break;

				case IS_NULL:
					sqlite3_result_null(context);
					break;

				case IS_DOUBLE:
					sqlite3_result_double(context, Z_DVAL(retval));
					break;

				default:
					convert_to_string_ex(&retval);
					sqlite3_result_text(context, Z_STRVAL(retval), Z_STRLEN(retval), SQLITE_TRANSIENT);
					break;
			}
		} else {
			sqlite3_result_error(context, "failed to invoke callback", 0);
		}

		if (agg_context) {
			zval_ptr_dtor(&agg_context->val);
		}
	} else {
		/* we're stepping in an aggregate; the return value goes into
		 * the context */
		if (agg_context) {
			zval_ptr_dtor(&agg_context->val);
		}
		if (!Z_ISUNDEF(retval)) {
			ZVAL_COPY_VALUE(&agg_context->val, &retval);
			ZVAL_UNDEF(&retval);
		} else {
			ZVAL_UNDEF(&agg_context->val);
		}
	}

	if (!Z_ISUNDEF(retval)) {
		zval_ptr_dtor(&retval);
	}

	return ret;
}
 void FunctionContext::SetNullResult()
 {
   sqlite3_result_null(context_);
 }