H2 memory DB test can't find table TAB - java

I use H2 memory DB to do my test integration and my real BD is oracle DB
jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;Mode=Oracle
When I want to use this SQL to check if my table exists
SELECT TNAME FROM TAB WHERE TNAME='myTableName'
But in the execution test, I got the error message "Table "TAB" not found".
What could I do to use this oracle table?

Create the table TAB in integration script sql and simule the fonctionnement Oracle ...

Related

Flyway h2 postgressql mode migration not working

I have to following migration working in postgres:
ALTER TABLE task_def
DROP COLUMN retry_count,
DROP COLUMN timeout_seconds;
(and running in prod)
but now i want to switch to h2 for my unit test but h2 doesnt seem to accept it
My database config in spring boot:
spring.datasource.url=jdbc:h2:./target/testdb;MODE=PostgreSQL
spring.datasource.username="sa"
spring.datasource.password=""
spring.jpa.hibernate.ddl-auto=none
spring.datasource.driver-class-name=org.postgresql.Driver
spring.flyway.url=jdbc:h2:./target/testdb;MODE=PostgreSQL
spring.flyway.user="sa"
spring.flyway.password=""
spring.flyway.schemas=
The error:
Migration V3__.....sql failed
---------------------------------------
SQL State : 42S22
Error Code : 42122
Message : Column "DROP" not found; SQL statement:
ALTER TABLE task_def
DROP COLUMN retry_count,
DROP COLUMN timeout_seconds [42122-200]
Location : db/migration/V3__.....sql
Line : 1
Statement : ALTER TABLE task_def
DROP COLUMN retry_count,
DROP COLUMN timeout_seconds
I haven't worked with H2 but it looks like it supports the following:
2 Statements instead of one in the same migration (flyway should run them in the same transaction anyway):
ALTER TABLE task_def DROP COLUMN retry_count;
ALTER TABLE task_def DROP COLUMN timeout_seconds;
Use different syntax:
ALTER TABLE task_def DROP COLUMN retry_count, timeout_seconds;
Of course if postgresql allows to do so as well.
All in all, I don't think that H2 will be able to cover all the features offered by postgres with its dialect, so failures like this can't be avoided.
So in my experience the following approach works much better:
Create a "test container" of postgres (see testcontainers project) and configure flyway / data source to run against it in tests. Depending on your tests infrastructure you can even not stop the container, but drop the database and run flyway before each test case. Or alternatively you can do like in spring tests - create an artificial transaction before running the test and "fail" it when the test finishes (even if it finishes successfully) so that the db won't be dirty for the next test.
There is no portable way to drop multiple columns at once, ALTER TABLE … DROP COLUMN is a standard command, but only for one column.
Some databases, however, including PostgreSQL and H2, support such non-standard feature, but their syntax is different. PostgreSQL expects
ALTER TABLE tableName DROP COLUMN columnName1, DROP COLUMN columnName2, …
https://www.postgresql.org/docs/12/sql-altertable.html
H2 expects
ALTER TABLE tableName DROP COLUMN columnName1, columnName2, …
https://h2database.com/html/commands.html#alter_table_drop_column
If you use different databases, you should avoid non-portable commands when possible.

Can't get existing table from embedded hsqldb

To use embedded database I have created hsqldb database connected with java by this tutorial.
In general it is about creating simple table with 3 records through hsqldb manager and connect to this database with java.
Database was created and after exit the manager and connect again I gained my tables. They are recorded in test.script file.
If I try to connect with java by
connection = DriverManager.getConnection("jdbc:hsqldb:file:/db/test;ifexists=true", "SA", "");
then I got connection and can read all tables from it by
resultSet = statement.executeQuery("SELECT TABLE_NAME, COLUMN_NAME, TYPE_NAME, COLUMN_SIZE, DECIMAL_DIGITS, IS_NULLABLE FROM INFORMATION_SCHEMA.SYSTEM_COLUMNS WHERE TABLE_NAME NOT LIKE 'SYSTEM_%'");
But I can't get previous created table in manager even if script file contains that table.
This snippet of create table is placed in test.script file:
CREATE MEMORY TABLE PUBLIC.SALARYDETAILS(EMPID VARCHAR(6) PRIMARY KEY,SALARY INTEGER NOT NULL,BONUS INTEGER NOT NULL,INCREMENT INTEGER NOT NULL)
I was confused by MEMORY command, but maybe it is not what I should to fix. After remove it manager add it there again.
----- update 1 -----
SHUTDOWN command didn't help.
I don't know how HSQLDB store data, but thought that when they are in script file, it is done.
Exception what is see in the command line when running Java is
user lacks privilege or object not found
----- update 2 -----
I have created a table in Java and record data into it and I was available to get these data, but can't see it in the manager. It seems to me like different database, but location is same.
Script file doesn't contain new table and data. I don't know where data are stored.
It seems the database file was not saved. Before you exit the manager, execute this SQL command:
SHUTDOWN

How I can Export ddl of all objects like table, index etc in SYBASE IQ/ SYBASE ASE without using any tools?

I have created a sample program in which I want to get ddl of all objects like table, trigger etc using get_ddl method. When I tried following queries in oracle it worked.
SELECT DBMS_METADATA.GET_DDL('TABLE', TABLE_NAME) FROM USER_TABLES;
SELECT DBMS_METADATA.GET_DDL('TRIGGER', TRIGGER_NAME) FROM USER_TRIGGERS;
SELECT DBMS_METADATA.GET_DDL('VIEW', VIEW_NAME) FROM USER_VIEWS;
SELECT DBMS_METADATA.GET_DDL('FUNCTION', OBJECT_NAME) FROM USER_PROCEDURES WHERE OBJECT_TYPE = 'FUNCTION';
SELECT DBMS_METADATA.GET_DDL('PROCEDURE', OBJECT_NAME) FROM USER_PROCEDURES WHERE OBJECT_TYPE = 'PROCEDURE';
SELECT DBMS_METADATA.GET_DDL('INDEX', INDEX_NAME) FROM USER_INDEXES ;
But when I try to create same sample for sybase to get ddl or script of all objects it doesn't work.because get_ddl not supported in sybase database. Can anyone help me to know that whether sybase Iq 15 supports get_ddl methods or there are any other method/way or queries for creating ddl/script of all objects.
I want to post it on SAP forums but all sites are unavailable can anyone suggest me link for post my problem.
Thanks in Advance!!
The ddl for triggers, stored procedures, and views can be pulled from sys.syssource. Unfortunately IQ doesn't store ddl for other objects
For ase,
Use sybsystemprocs
Go
sp_helptext (object)
Go
For views, stored procedures, and triggers

Universal query to check whether a table exist in all RDBMS

I have searched a lot but i could not find a query which will apply to any RDBMS to check whether a table exits. Some are ok with mysql and h2 but its not compatible with oracle. Any one has a solution for this.
This works for oracle but not h2 or mysql
select count(*) as tblCount from user_tables where table_name = 'ALERTS_HISTORY';
The java.sql.DatabaseMetaData object (which is obtainable from a Connection via .getMetaData() has a getTables(…) function, which does what you want; the driver will care about the SQL.

Hibernate multiple native SQL statements

I want to run a native SQL from a file using Hibernate. The SQL can contain several statements creating the database structure (i.e. tables, constraints but no insert/update/delete statements).
Example, very simple query is below (which contains the following two SQL statements)
CREATE DATABASE test;
CREATE TABLE test.testtbl( id int(5));
I am using MySQL db, and when I run the above query I am gettng syntax error returned. When I run them one by one, its ok.
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near
'CREATE TABLE test.testtbl( id int(5))' at line 1
The code to run the query is below (above statement is assigned to 'sql' variable):
session = sf.openSession();
session.beginTransaction();
Query qry = session.createSQLQuery(sql);
qry.executeUpdate();
session.getTransaction().commit();
Any help would be appreciated.
As others have explained
You must run these queries one by one.
The hibernate code gets translated into running one update statement on JDBC.
But you provided two update statements.
In addition,
I personally prefer to have the code that creates tables outside of the Java application, in some DB scripts.
The parameters of the method createSQLQuery is t-sql code;
t-sql code to ensure that in the mysql interface analyzer correctly.
You can try changed the sql :'CREATE TABLE testtbl(id int(5));'
by the way you can use JDBC Connection api (Don't recommend to do so)
Such as:
java.sql.Connection conn=session.connection();

Categories