J
J
johnpion2012-09-13 11:32:34
SQL
johnpion, 2012-09-13 11:32:34

How to use one self-written function several times in a request?

Here is the module code for sqlite3:

#include "sqlite3ext.h"
#include <stdlib.h>
#include <stdio.h>
SQLITE_EXTENSION_INIT1

static void moneyFunc(
        sqlite3_context *context,
        int argc,
        sqlite3_value **argv
        ){
    char *result;

    int sum = sqlite3_value_int(argv[0]);
    int full = sum / 100;
    int dec = sum % 100;

    if (dec == 0){
        snprintf(result, 8, "%d,00", full);
    } else if (dec > 0 && dec < 10){
        snprintf(result, 8, "%d,0%d", full, dec);
    } else if (dec >= 10 && dec <= 99){
        snprintf(result, 8, "%d,%d", full, dec);
    } else return;

    sqlite3_result_text(context, result, 8, 0);
}

int sqlite3_extension_init(
        sqlite3 *db,
        char **pzErrMsg,
        const sqlite3_api_routines *pApi
        ){
    SQLITE_EXTENSION_INIT2(pApi)
            sqlite3_create_function(db, "money", 1, SQLITE_ANY, 0, moneyFunc, 0, 0);
    return 0;
}


View request
SELECT money(100)

returns 1.00. It's OK, BUT! View request

SELECT money(100), money(350)

Gives out 3.50 | 3.50 instead of 1.00 and 3.50. Those. the last value is used in the call. Where am I wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
johnpion, 2012-09-13
@johnpion

How to fix?

char *result = "бла бла бла"
?

S
Samuel_Leonardo, 2012-09-13
@Samuel_Leonardo

you don't seem to allocate memory for char *result;

J
jorikburlakov, 2012-09-13
@jorikburlakov

You can make two requests by combining them with Union

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question