Querying available (global) variables in MonetDB - global-variables

Is there a way to query all the available global variables, such as current_schema, current_user, in MonetDB?

You can do: select * from sys.var();

Related

Getting environment variables with SQLite

Is there a way to get system/environment variables with SQLite?
I know that by using sqlite3's command line I can do something like:
sqlite> .shell echo $USER
But I want to know if I can implement this in a SQL trigger.
We have multiple users using a shared sqlite database and I want to create an automatic log that records who made that changes to specific tables.
SQLite does not know anything about environment variables. (Many systems on which it runs do not even have them.)
The easiest way to get a user name into a trigger would be to create a user-defined function, but then the trigger would fail if used outside a program that has registered this function.

How to use dynamic values while executing SQL scripts in R

My R workflow now involves dealing with a lot of queries (RPostgreSQL library). I really want to make code easy to maintain and manage in the future.
I started loading large queries from separate .SQL files (this helped) and it worked great.
Then I started using interpolated values (that helped) which means that I can write
SELECT * FROM table WHERE value = ?my_value;
and (after loading it into R) interpolate it using sqlInterpolate(ANSI(), query, value = "stackoverflow").
What happens now is I want to use something like this
SELECT count(*) FROM ?my_table;
but how can I make it work? sqlInterpolate() only interpolates safely by default. Is there a workaround?
Thanks
In ?DBI::SQL, you can read:
By default, any user supplied input to a query should be escaped using
either dbQuoteIdentifier() or dbQuoteString() depending on whether it
refers to a table or variable name, or is a literal string.
Also, on this page:
You may also need dbQuoteIdentifier() if you are creating tables or
relying on user input to choose which column to filter on.
So you can use:
sqlInterpolate(ANSI(),
"SELECT count(*) FROM ?my_table",
my_table = dbQuoteIdentifier(ANSI(), "table_name"))
# <SQL> SELECT count(*) FROM "table_name"
sqlInterpolate() is for substituting values only, not other components like table names. You could use other templating frameworks such as brew or whisker.

Use SOUNDEX function in Websql

I'm trying to include this function in a websql query, something like
"select * from products where filter like '%pname%' order by soundex(filter);"
But according to this: https://www.sqlite.org/lang_corefunc.html
Soundex function is only available if sqlite was compiled with SQLITE_SOUNDEX argument, which I don't think is the case for chrome as is throwing
could not prepare statement (1 no such function: soundex)
So, my question is, is there a way to use soundex function, or at least some other similar function?
Edit 1:
For now I'm just using
order by coalesce(like('pname', filter), 0)
which is not the final solution, but better than a simple order by a specific column.
WebSQL does not specify which SQL functions are available, and has no mechanism to install user-defined functions.

How to prepare a statement from the CLI interpreter?

How does one prepare a statement from the SQLite CLI? I have found the page Compiling An SQL Statement but it is geared more towards the ODBC interface, not the CLI interpreter. I'm hopinpg for something akin to the following:
sqlite> pq = prepare(SELECT * FROM Users WHERE username=?)
sqlite> run(pq, 'jeffatwood')
0 | jeffatwood | hunter2 | admin
sqlite>
Does the SQLite CLI have any such functionality? Note that I am not referring to the Bash CLI but rather SQLite's CLI interpreter or the excellent LiteCLI alternative.
Perhaps SQL Parameters using named parameters would do the trick
sqlite> .param set :user 'jeffatwood'
sqlite> select * from Users where username = :user
should return the desired row.
CLI was not designed for such. For this you must use an SQLite API on an available programming language.
You may also write a batch/shell file to handle CLI call.
E.g., in Windows a file named User.bat like following:
#SQLITE3.EXE some.db "SELECT * FROM Users WHERE username='%~1'"
May be called like this:
User "jeffatwood"
will perform desired result.
EDIT:
About prepared/compiled statements: with those you can bind parameters, fetch queries row by row and repeat same command in a faster manner.
sqlite3 CLI tool wouldn't take any advantage on those:
all parameters must be typed in SQL statement, making binding useless;
all query rows are returned at once, no need to fetch row by row;
repeated commands must be retyped - small speed improvement would result in using precompiled statements.

Common table expression functionality in SQLite

I need to apply two successive aggregate functions to a dataset (the sum of a series of averages), something that is easily and routinely done with common table expressions in SQL Server or another DBMS that supports CTEs. Unfortunately, I am currently stuck with SQLite which does not support CTEs. Is there an alternative or workaround for achieving the same result in SQLite without performing two queries and rolling up the results in code?
To add a few more details, I don't think it could be easily done with views because the first set of aggregate values need to be retrieved based on a WHERE clause with several parameters. E.g.,
SELECT avg(elapsedTime)
FROM statisticsTable
WHERE connectionId in ([lots of values]) AND
updateTime > [startTime] AND
updateTime < [endTime]
GROUP BY connectionId
And then I need the sum of those averages.
Now that we are in THE FUTURE, let me note here that SQLite now does support Common Table Expressions, as of version 3.8.3 of 2014-02-03.
http://www.sqlite.org/lang_with.html
Would this work?
SELECT SUM(t.time) as sum_of_series_of_averages
FROM
(
SELECT avg(elapsedTime) as time
FROM statisticsTable
WHERE connectionId in ([lots of values]) AND
updateTime > [startTime] AND
updateTime < [endTime]
GROUP BY connectionId
) as t
By converting your averages into an inline view, you can SUM() the averages.
Is this what you are looking for?
As you've mentioned, SQLite doesn't support CTEs, window functions, or any of the like.
You can, however, write your own user functions that you can call inside SQLite by registering them to the database with the SQLite API using sqlite_create_function(). You register them with the database, and then you can use them in your own application code. You can make an aggregate function that would perform the sum of a series of averages based on the individual column values. For each value, a step-type callback function is called that allows you to perform some calculation on the data, and a pointer for holding state data is also available.
In your SQL, then, you could register a custom function called sum_of_series_of_averages and have:
SELECT sum_of_series_of_averages(columnA,columnB)
FROM table
WHERE ...
For some good examples on how those work, you should check out the SQLite source code, and also check out this tutorial (search for Defining SQLite User Functions).

Resources