How to prevent Sqlite unbound parameters interpreted as NULL - sqlite

As the Sqlite docs specify, unbound parameters in prepared statements are interpreted as NULL - my question is this then:
Is there a way to have Sqlite ensure that all parameters have been bound at least once, thereby ensuring none were missed by accident?
It is better to get an error and require calls to sqlite3_bind_null(statement_, col); then to get a subtle error because I forgot to call sqlite3_bind_* on the where clause of an update statement!

It is not possible to differentiate unbound parameters from parameters set to NULL using the current SQLite libraries.
If you have a look at the C source code for sqlite3_bind_null(), you will find that it simply calls the internal SQLite function that unbinds a parameter. Therefore there is no way to tell the two cases apart.
The only solution for this would be to wrap the SQLite C API functions with your own functions that will do a bit of book-keeping. You can bundle a bitmap with each sqlite3_stmt structure, with each bit set to true if and only if the corresponding parameter is bound. You would have to create wrappers for at least the sqlite3_bind_*() functions and sqlite3_clear_bindings().

Related

Having trouble understanding FLWOR expressions combined with inserts and return statements [duplicate]

I recognized that (insert/delete)-XQueries executed with the BaseX client always returning an empty string. I find this very confusing or unintuitive.
Is there a way to find out if the query was "successful" without querying the database again (and using potentially buggy "transitive" logic like "if I deleted a node, there must be 'oldNodeCount-1' nodes in the XML")?
XQuery Update statements do not return anything -- that's how they are defined. But you're not the only one who does not like those restrictions, and BaseX added two ways around this limitation:
Returning Results
By default, it is not possible to mix different types of expressions
in a query result. The outermost expression of a query must either be
a collection of updating or non-updating expressions. But there are
two ways out:
The BaseX-specific update:output() function bridges this gap: it caches the results of its arguments at runtime and returns them after
all updates have been processed. The following example performs an
update and returns a success message:
update:output("Update successful."), insert node <c/> into doc('factbook')/mondial
With the MIXUPDATES option, all updating constraints will be turned off. Returned nodes will be copied before they are modified by
updating expressions. An error is raised if items are returned within
a transform expression.
If you want to modify nodes in main memory, you can use the transform
expression.
The transform expression will not help you, as you seem to modify the data on disk. Enabling MIXUPDATES allows you to both update the document and return something at the same time, for example running something like
let $node := <c/>
return ($node, insert node $node into doc('factbook')/mondial)
MIXUPDATES allows you to return something which can be further processed. Results are copied before being returned, if you run multiple updates operations and do not get the expected results, make sure you got the concept of the pending update list.
The db:output() function intentionally breaks its interface contract: it is defined to be an updating function (not having any output), but at the same time it prints some information to the query info. You cannot further process these results, but the output can help you debugging some issues.
Pending Update List
Both ways, you will not be able to have an immediate result from the update, you have to add something on your own -- and be aware updates are not visible until the pending update list is applied, ie. after the query finished.
Compatibility
Obviously, these options are BaseX-specific. If you strongly require compatible and standard XQuery, you cannot use these expressions.

How to return results together with update operations in BaseX?

I recognized that (insert/delete)-XQueries executed with the BaseX client always returning an empty string. I find this very confusing or unintuitive.
Is there a way to find out if the query was "successful" without querying the database again (and using potentially buggy "transitive" logic like "if I deleted a node, there must be 'oldNodeCount-1' nodes in the XML")?
XQuery Update statements do not return anything -- that's how they are defined. But you're not the only one who does not like those restrictions, and BaseX added two ways around this limitation:
Returning Results
By default, it is not possible to mix different types of expressions
in a query result. The outermost expression of a query must either be
a collection of updating or non-updating expressions. But there are
two ways out:
The BaseX-specific update:output() function bridges this gap: it caches the results of its arguments at runtime and returns them after
all updates have been processed. The following example performs an
update and returns a success message:
update:output("Update successful."), insert node <c/> into doc('factbook')/mondial
With the MIXUPDATES option, all updating constraints will be turned off. Returned nodes will be copied before they are modified by
updating expressions. An error is raised if items are returned within
a transform expression.
If you want to modify nodes in main memory, you can use the transform
expression.
The transform expression will not help you, as you seem to modify the data on disk. Enabling MIXUPDATES allows you to both update the document and return something at the same time, for example running something like
let $node := <c/>
return ($node, insert node $node into doc('factbook')/mondial)
MIXUPDATES allows you to return something which can be further processed. Results are copied before being returned, if you run multiple updates operations and do not get the expected results, make sure you got the concept of the pending update list.
The db:output() function intentionally breaks its interface contract: it is defined to be an updating function (not having any output), but at the same time it prints some information to the query info. You cannot further process these results, but the output can help you debugging some issues.
Pending Update List
Both ways, you will not be able to have an immediate result from the update, you have to add something on your own -- and be aware updates are not visible until the pending update list is applied, ie. after the query finished.
Compatibility
Obviously, these options are BaseX-specific. If you strongly require compatible and standard XQuery, you cannot use these expressions.

Checking for Null Statements in SQLite3

Does anyone know how to check if a statement has been prepared or not? I have searched through the SQLite documentation for statements and searched SO, DreamInCode, and DaniWeb but have found no references to my exact question.
I have a statement that I only call in specific circumstances and I only want to attempt to finalize it if it has been used since program crashes are happening by trying to finalize a statement that has not been used. I would rather use whatever SQLite has built-in instead of extra code but I will do just that if there is no null-checking for statements.
The documentation says:
Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
So just ensure that your sqlite3_stmt* variables are initialized with NULL, and are reset to NULL when finalized.

PSQL: Purpose of Trigger Function

How is trigger function different from 'regular' functions? And is it absolutely necessary in creating a trigger?
For example, in this case:
-- Trigger function
CREATE FUNCTION update_record_trigger_function() RETURNS trigger
LANGUAGE plpgsql
AS $update_record_trigger_function$
BEGIN
PERFORM update_record(NEW.oid); -- helper function ...
RETURN NEW;
END
$update_record_trigger_function$;
-- Trigger for updating latest clicks for posts
CREATE TRIGGER update_latest_record
AFTER INSERT OR UPDATE ON record
FOR EACH ROW
EXECUTE PROCEDURE update_record_trigger_function();
Would it not be simpler to (or is it possible to do):
-- Trigger for updating latest clicks for posts
CREATE TRIGGER update_latest_record
AFTER INSERT OR UPDATE ON record
FOR EACH ROW
PERFORM update_record(NEW.oid); -- syntactically not right but along this idea
I wasn't able to find any document with examples of 'skipping trigger function' or one that explaining how trigger function is special and necessary for triggers?
PostgreSQL separates the "trigger function" from the trigger definition for quite a few reasons:
Trigger functions can be written as generic functions that can work on many different tables, so you can add the same trigger function as a trigger to many different tables. This eases maintenance.
Trigger functions can take parameters that allow them to adapt to specific column names or other characteristics of the table you apply them to. This makes it practical to write generic, re-usable trigger functions where you otherwise could not do so, so you often don't need to repeat the code in multiple places. See EXECUTE ... USING and the format function with its %L and %I format-specifiers.
PostgreSQL tries very hard not care what programming language your functions are written in . They could be PL/PgSQL, Python, Perl, C, or MyWackyPluginLanguage. If it didn't separate trigger function from trigger definition this would be harder to achieve.
The shorthand you describe is not supported in PostgreSQL, though some other database systems provide something very much like what you've shown. You essentially want to be able to define a trigger function as an inline expression. This isn't supported. There's never been a pressing need for it so nobody's implemented it.
If such a feature were added in future - and I'm not aware of any plans to do so - it would probably be done using a PL/PgSQL DO block that was contextually interpreted as a trigger, something like (not legal syntax):
CREATE TRIGGER update_latest_record
AFTER INSERT OR UPDATE ON record
FOR EACH ROW DO $$ PERFORM update_record(NEW.oid); RETURN NEW; $$;
That way the parser wouldn't need to understand PL/PgSQL constructs, we'd just have to teach it to understand FOR EACH ROW DO [string literal body of function] as an alternative to FOR EACH ROW EXECUTE PROCEDURE proc_name(args) and then invoke the PL/PgSQL subsystem to process the trigger literal.
In practice Pg would probably just generate an ordinary trigger function and add a reference to it from the trigger so it's dropped if the trigger was dropped, making this a just convenience macro, just like SERIAL is a convenience macro for an INTEGER field with a DEFAULT nextval(...) and a generated SEQUENCE owned by the table with the SERIAL field. In particular, pg_dump would probably output it as if you'd defined a separate trigger function and trigger, rather than used the shorthand.
Honestly, I doubt anything like this will ever be added, but if you can provide sufficiently convincing use cases you might if you're really lucky interest someone in it. Most likely you'll only get anywhere with such a proposal ("allow DO blocks to be used as trigger procedures") if you're willing to back it with funding for the time required to implement the feature.

Convenient way to use LIKE case sensivity in sqlite

As I see LIKE operator can optimize query if I switch PRAGMA case_sensitive_like=ON. I measured, it really worked, queries "LIKE someth%" becomes ten times faster on a compartively large binary indexed tables.
But the problem is that my library implemented as an add-on to my application, it maintains its own tables with any db it is connected. So the problems are
I can not read case_sensitive_like since it is only supported to be set, not read. So I can not temporarily read the state and return it after the query,
As an addon that should obey the main functionality of the db, I should not change the setting to my need for good since it can affect other routines.
As I see there's no Like(case-sensitive) internal equivalent for me to call optimizid query directly. For example use LIKECASESENSITIVE instead of LIKE
I can call sqlite3_create_function, but I don't know whether or not I can call LIKE(CASE SENSITIVE) internally.
I can not read case_sensitive_like
since it is only supported to be set,
not read. So I can not temporarily
read the state and return it after the
query
You can get the state of case_sensitive_like with a query like this one:
select case when 'a' like 'A' then 0 else 1 end
which will return 1 if case_sensitive_like = ON and 0 if it's OFF.

Resources