In postgresql a query in the querylog gets something like this:
2009-02-05 00:12:27 CET LOG: duration: 3781.634 ms execute <unnamed>: SELECT QUERY ....
Is there a possibility to put something more usable into the "< unnamed >" placed like the url the query was requested from?
Are there any other possibilities to track the origin of a query in postgresql using jdbc from java?
Thanks
Short answer is "no"
The name can be set when preparing the statement, using the PREPARE command, but that requires rewriting all your SQL. There is no option to simply add a name parameter to your JDBC methods.
The JDBC driver makes use of both named and unnamed prepared statements. It will give them a name when it wishes to reuse them, which it will deem appropriate if the same PreparedStatement object is executed 5 times (though that is configurable through setting the prepareThreshold).
Documentation is here
More info can also be found by searching the PostgreSQL JDBC mailling list
Related
I use postgreSQL/pentaho CDE. I need to transmit parameter as schema prefix in my query. I need to run this query: select * from peredelkino_public.protocol
I used custom parameter in Pentaho (named 'selector_par'), in properties named 'Javascript code' i wrote 'peredelkino_public.protocol'.
Then I changed sql query in component 'sql over sqlJndi' like this: select * FROM (${selector_par})
But this query doesn't work! I get message 'Error processing component'. How I can transmit schema prefix in my query?
Check the Replace variables in script.
Note: if the parameter selector_par does not exists or is misspelled, you may get a hard to understand error message.
You cannot.
CDE can only use JDBC parameters and those cannot be used in the from clause.
If you absolutely need to do that, you should use a Kettle datasource in CDE, as PDI allows any variable to be used in any part of a SQL query.
But make sure you sanitize your inputs. Variable replacements such as that one are a gold mine for hackers.
3GLs provide mechanisms to prepare statements before executing them. E.g.
SELECT name
FROM people
WHERE age=:AGE
The same query can then be executed for different ages. But can such a statement also be prepared in a "plain" Oracle SQL client? Can the same be done in e.g. SQL Plus or dbForge Studio for Oracle as in Java or C# or any other programming language that supports prepared statements?
In dbForge Studio for Oracle, named parameters can be used, preceded by a colon :
SELECT *
FROM people
WHERE name=:name
The parameters can then be filled in with the "Edit parameters dialog box", available from the SQL toolbar.
I know you didn't ask about PostgreSQL but about Oracle. However, of note, PostgreSQL has this feature right in its SQL language.
The SQL standard includes a PREPARE statement, but it is only for use in embedded SQL. The PostgreSQL version of the PREPARE statement works like this:
PREPARE nameByAge(number) AS
SELECT name
FROM People
WHERE age=$1;
and you use it like this:
EXECUTE nameByAge(18);
EXECUTE nameByAge(50);
So unfortunately for Oracle SQLPlus the answer seems to be no, not bind variables. But SQLPlus has substitution variables, similar to shell scripts. You use them as &1, &2, &3, ... and they get their parameters from the way you call the SQLPlus script.
sqlplus user/password #script.sql 18
sqlplus user/password #script.sql 50
with the script.sql being
SELECT name
FROM People
WHERE age=&1;
this would work, even though it is not bind. But then, do you really care about the slight savings in repeat parse time? In fact Oracle hashes SQL statements and already replaces constants with bind variables to be able to better reuse query plans. So the savings you would get with PREPARE and BIND are really minuscule.
We have a database setup where we have a separate user for owners and users of database tables in an Oracle database. This means that in practice each query is prefixed like this: ownername.tablename
This works just fine if I just write the whole thing statically in Slick's SQLInterpolation.sql function:
(sql"select foo_owner.foo_sequence.nextval from dual").as[Long].first()
The problem is, that the owner prefix changes depending on test/prod. environment. What I'd like to do is this:
(sql"select $owner.foo_sequence.nextval from dual").as[Long].first()
But SQL interpolation doesn't work with it. I get this error Oracle:
An exception or error caused a run to abort: ORA-22806: not an object or REF
Any suggestions? I can of course fall back to the more verbose StaticQuery, but using sql/sqlu interpolation would be much more compact.
Using $foo inserts foo as a bind variable. You need to prefix it with # to insert a literal:
(sql"select #$owner.foo_sequence.nextval from dual").as[Long].first()
Is removing prefix acceptable? In your code you can run
ALTER SESSION SET CURRENT_SCHEMA=yourOwner
This way you do not need to prefix SQL. Just be sure that you do not have CREATE TABLE or any other DML as it will try to run it on owner's schema.
I'm looking for a way to programmatically execute arbitrary SQL commands against my DB.
(Hibernate, JPA, HSQL)
Query.createNativeQuery() doesn't work for things like CREATE TABLE.
Doing LOTS of searching, I thought I could use the Hibernate Session.doWork().
By using the deprecated Configuration.buildSesionFactory() seems to show that doWork won't work.
I get "use lacks privilege or object not found" for all the CREATE TABLE statements.
So, what other technique is there for executing arbitratry SQL statements?
There were some notes on using the underlying JDBC Statement, but I haven't figure out how to get a JDBC Connection object from Hibernate to try that.
Note that the hibernate.hbm2ddl.auto=create setting will NOT work for me, as I have ARRAY[] columns which it chokes on.
I don't think there is any problem executing a create table statement with a Hibernate native query. Just make sure to use Query.executeUpdate(), and not Query.list() or Query.uniqueResult().
If it doesn't work, please tell us what happens when you execute it, and join the full stack trace of the exception and the SQL query you're executing.
"use lacks privilege or object not found" in HSQL may mean anything, for example existence of a table with the same name. Error messages in HSQL are completely misleading. Try listing your tables using DatabaseMetadata - you have probably already created the table.
I have a problem with Oracle 'bind variables' and ActiveRecord.
How AR can use 'bind variables'?
I am trying to use oracle and oracle-enhanced adapters, set cursor_sharing directives to 'force' or 'similar', but result SQL query does not contain bind vars, only plain string.
I am using AR 2.0.2 and 3.1+.
And maybe console verbose does not show full SQL query?
With cursor_sharing = FORCE, it is unlikely that bind variables are not being used, but RoR will not know anything about them. Active Record will still send the queries to the database without binds, and then Oracle will force bind variables into the place of all literals. Therefore in the Rails console, you will just see the original SQL with no binds.
To confirm your SQL statements are using binds, query v$sql with something like:
select sql_fulltext
from v$sql
where upper(sql_fulltext) like '%TABLE IN YOUR APP%'
You should see your original queries in the output, but with bind variables in place of the values you had passed in.