spring-data-jdbc how to specify schema name - spring-data

When using spring-data QueryDslJdbcTemplate to query, can I specify table schema name - different to the jdbc username in the datasource? Thanks.
(the generated qBean from querydsl-maven-plugin used the correct schema name, however, when I'm querying, the template always use jdbc username from the datasource, and the generated query itself doesn't have schema name prefix, resulting in java.sql.SQLException: ORA-00942: table or view does not exist)

By default the schema is not printed, but you can enable it via
OracleTemplates.builder().printSchema().build()

Related

Default schema for native SQL queries (spring-boot + hibernate + postgresql + postgis)

I am introducing spring to the existing application (hibernate has already been there) and encountered a problem with native SQL queries.
A sample query:
SELECT ST_MAKEPOINT(cast(longitude as float), cast(latitude as float)) FROM
OUR_TABLE;
OUR_TABLE is in OUR_SCHEMA.
When we connect to the db to OUR_SCHEMA:
spring.datasource.url: jdbc:postgresql://host:port/db_name?currentSchema=OUR_SCHEMA
the query fails because function ST_MAKEPOINT is not found - the function is located in schema: PUBLIC.
When we connect to the db without specifying the schema, ST_MAKEPOINT is found and runs correctly, though schema name needs to be added to the table name in the query.
As we are talking about thousands of such queries and all the tables are located in OUR_SCHEMA, is there a chance to anyhow specify the default schema, so still functions from PUBLIC schema were visible?
So far, I have tried the following springboot properties - with no success:
spring.jpa.properties.hibernate.default_schema: OUR_SCHEMA
spring.datasource.tomcat.initSQL: ALTER SESSION SET CURRENT_SCHEMA=OUR_SCHEMA
spring.datasource.initSQL: ALTER SESSION SET CURRENT_SCHEMA=OUR_SCHEMA
Also, it worked before switching to springboot config - specifying hibernate.default-schema = OUR_SCHEMA in persistence.xml was enough.
Stack:
spring-boot: 2.0.6
hibernate: 5.3.1.Final
postgresql: 42.2.5
postgis: 2.2.1
You're probably looking for the PostgreSQL search_path variable, which controls which schemas are checked when trying to resolve database object names. The path accepts several schema names, which are checked in order. So you can use the following
SET search_path=our_schema,public;
This will make PostgreSQL look for your tables (and functions!) first in our_schema, and then in public. Your JDBC driver may or may not support multiple schemas in its current_schema parameter.
Another option is to install the PostGIS extension (which provides the make_point() function) in the our_schema schema:
CREATE EXTENSION postgis SCHEMA our_schema;
This way you only have to have one schema in your search path.
JDBC param currentSchema explicitly allows specifying several schemas separating them by commas:
jdbc:postgresql://postgres-cert:5432/db?currentSchema=my,public&connectTimeout=4&ApplicationName=my-app
From https://jdbc.postgresql.org/documentation/head/connect.html
currentSchema = String
Specify the schema (or several schema separated by commas) to be set in the search-path. This schema will be used to resolve unqualified object names used in statements over this connection.
Note you probably need Postgres 9.6 or better for currentSchema support.
PS Probably better solution is to set search_path per user:
ALTER USER myuser SET search_path TO mydb,pg_catalog;
if you use hibernate.default_schema, then for native queries, you need to provide the {h-schema} placeholder, something like that
SELECT ST_MAKEPOINT(cast(longitude as float), cast(latitude as float)) FROM {h-schema}OUR_TABLE;

Accessing table without using schema name

I am new with DB2.
I am not able to get data from a table without using the schema name. If I use a schema name with table name, I am able to fetch data.
Example:
SELECT * FROM TABLE_NAME;
It is giving me error, while
SELECT FROM SCHEMA_NAME.TABLE_NAME;
is fetching a result.
What do I have to set up not to always have to use the schema name?
By default, your username is used as the schema name for unqualified object names. You can see the current schema with, e.g. VALUES CURRENT SCHEMA. You can change the current schema for you current session with SET SCHEMA new_schema_name, or via e.g. a JDBC connection parameter. Most query tools also have a place to specify/change the current schema.
See the manual page for SET SCHEMA https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.1.0/com.ibm.db2.luw.sql.ref.doc/doc/r0001016.html
The full rules for the qualification of unqualified objects is here https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.1.0/com.ibm.db2.luw.sql.ref.doc/doc/r0000720.html#r0000720__unq-alias
E.g.
Unqualified alias, index, package, sequence, table, trigger, and view names are implicitly qualified by the default schema.
However, you can create a public alias for a table, module or sequence if you wish to be able to reference it regardless of your CURRENT SCHEMA value.
https://www.ibm.com/support/knowledgecenter/SSEPGG_11.5.0/com.ibm.db2.luw.sql.ref.doc/doc/r0000910.html
(P.S. all the above assumes you are using Db2 LUW)
Try using SET SCHEMA to set the default schema to be used in the session:
SET SCHEMA SCHEMA_NAME;
SELECT * FROM TABLE_NAME;
When using DBeaver - right click on connection > Connection Settings > Initialization, and select your default DB and default Schema:
After that, open your SQL Script and select Active DB:

AWS Data Pipeline; RedshiftLoadCopy failed; schema error

I have created my schema in Redshift and want to use Data pipeline to populate my table with csv file in S3.
Under parameters for the field: myRedshiftTableName:
If I only use my tableName without specifying the Schema, then error is:
output table named 'public.myTable' doesn't exist and no
createTableSql was provided
If I also specify the Schema, then the error is:
output table named 'public.mySchema.myTable' doesn't exist and no
createTableSql was provided
If I drop the table and specify the Schema in myRedshiftCreateTableSql field, then the error is:
ERROR: schema "mySchema" does not exist
How to use my own defined schema?
Go to edit pipelines > data nodes > click on "Add an optional field" and then specify your schema name.

IBM Data Studio Table name issue

When I create a new SQL script , for example
select * from table
When I execute this the sql that gets ran is
SELECT * FROM MYNAME.TABLE
I understand DB2 does this , but I want the schema name to be concatenated onto the table names, not my username.
SELECT * FROM SCHEMANAME.TABLE
It did this for me before, but I dont know what changed that it now only puts the database username at the end of the tables.
Does anyone know how to set it so the schema name gets added on?
Best way in my eyes for Data Stuio is setting the special register "Current schema" on the Special Registers tab of the SQL Editor. Here is a screenshot
Schema name defaults to your authorisation ID (user name in most cases). To use a different name to qualify unqualified object names, set the special register CURRENT_SCHEMA appropriately:
SET SCHEMA whatever
Could you try this;
In Data Project Explorer,
right clicked, properties
Driver properties
type your schema in Default schema
disconnect and reconnect database;
and try without SCHEMANAME.

Birt/Set Schema and Set Path

I'm currently using a schema that will be changed to another schema in a couple of months. I do not not want to have to remove the SCHEMA_NAME multiple times from each report. All the reports so far have this format:
SELECT
COLUMN_NAME1,
SCHEMA_NAME.USER_DEFINED_FUNCTION(COLUMN_NAME2),
COLUMN_NAME3
FROM
SCHEMA_NAME.TABLE_NAME
I want to remove the schema name from the query so it looks like this:
SET SCHEMA LROUIM;
SET PATH LROUIM;
SELECT
COLUMN_NAME1,
USER_DEFINED_FUNCTION(COLUMN_NAME2),
COLUMN_NAME3
FROM
TABLE_NAME
I need the "SET SCHEMA" for the table name and "SET PATH" for the User Defined Function. This code works in Squirrel SQL, but if I insert this code into a Birt data set of type SQL SELECT QUERY, I get an error because of
SET SCHEMA LROUIM;
SET PATH LROUIM;
How do I implement SET SCHEMA and SET PATH in a SQL Select Query in Birt?
I think BIRT uses a JDBC connexion, and you can modify the connection parameters by specifying a default schema.