You have a bug (missing break) between line 80 and 81 which will lead to a
memory leak every time a text value is stored ...
switch (pval->t) {
case SQLITE_INTEGER:
pval->n=0;
pval->i=sqlite3_value_int64(arg);
break;
case SQLITE_TEXT:
pval->n=sqlite3_value_bytes(arg);
if (!pval->n) {
pval->z="";
} else {
pval->z=sqlite3_malloc(pval->n);
assert (pval->z); /* TODO: SQLITE_NOMEM*/
memcpy(pval->z,sqlite3_value_text(arg),pval->n);
}
break; /* <<<<<<<<<< MISSING break <<<<<<<<<< */
case SQLITE_BLOB: /* sqlite3_value_blob instead of _text */
pval->n=sqlite3_value_bytes(arg);
if (!pval->n) {
pval->z="";
} else {
pval->z=sqlite3_malloc(pval->n);
assert (pval->z); /* TODO: SQLITE_NOMEM*/
memcpy(pval->z,sqlite3_value_blob(arg),pval->n);
}
break;
case SQLITE_FLOAT:
pval->n=0;
pval->r=sqlite3_value_double(arg);
break;
}
}
> -----Original Message-----
> From: sqlite-users-bounces at mailinglists.sqlite.org [mailto:sqlite-users-
> bounces at mailinglists.sqlite.org] On Behalf Of E.Pasma
> Sent: Monday, 22 February, 2016 07:49
> To: SQLite mailing list
> Subject: Re: [sqlite] User-defined SQL functions
>
> 21 feb 2016, Igor Tandetnik:
>
> > On 2/21/2016 12:09 PM, Olivier Mascia wrote:
> >> Is it possible to implement a SQL function
> (https://www.sqlite.org/c3ref/create_function.html
> >> ), which implementation would be able to return the same value for
> >> the duration of the current transaction?
> >>
> >> In other words, assume I'd like to implement some function which I
> >> could name 'now()' which would return the current_timestamp as
> >> julian day, but would return exactly the same value for any call
> >> made during the same transaction?
> >
> > When I needed a stable version of now(), this is what I did. I had a
> > global (well, actually, per-connection) time_t variable. I passed
> > its address to sqlite3_create_function, to be retrieved within the
> > custom function's implementation by means of sqlite3_user_data.
> >
> > The custom function would check this variable. If it's zero, it
> > would obtain the current timestamp save it in that variable, and
> > return it as its result; otherwise, it would return the previously-
> > cached value.
> >
> > Whenever I started a new transaction, I reset the variable to zero.
> > Luckily, I had a single spot issuing BEGIN statements (a C++ class
> > wrapping SQLite connection handle; the time_t variable was also part
> > of that class).
> > --
> > Igor Tandetnik
>
> Hello Oliver, I have a tool that is purely within SQLite. I once named
> it sqlite_mprint. It is far from the requirement but may be a start.
>
> sqlite> .load sqlite_mprint
> sqlite> select mprint (datetime('now')); --set time
> 2016-02-22 14:30:03
> sqlite> select mprint() as t0; --get old time
> 2016-02-22 14:30:03
>
> The code is temporarily at
> http://h1972688.stratoserver.net/sqlite_mprint/160222
>
> Best regards, E. Pasma
>
>
> _______________________________________________
> sqlite-users mailing list
> sqlite-users at mailinglists.sqlite.org
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users