H2 DB identity() function not working in embedded mode - h2

I am using the H2 database. I have a table with an identity column. I perform an insert and then try and call the identity() function to determine the id assigned to the newly created row.
here is the code snippet I use to call the identity function:
CallableStatement cs = dbConnection.prepareCall("{ ? = call IDENTITY()}");
cs.registerOutParameter(1, Types.BIGINT);
cs.execute();
id = cs.getLong(1);
cs.close();
The problem I run into is that for some reason this function works in server mode, but gives me this error when I call the function in embedded mode:
org.h2.jdbc.JdbcSQLException: Parameter "#1" is not set; SQL statement:
? = call IDENTITY() [90012-160]

Related

How to create a dummy function in H2 embbeded db for integration test

I have a spring boot application that connects to an oracle database. The project contains a service class (userService) that calls the function VALIDATEUSER(USERNAME IN VARCHAR2,PASSWD IN VARCHAR2) in oracle and return 1 if user is valid and 0 invalid.
I need to create the same function in h2 db that always return true for my integration test.
Basically I wanted to created the function in sql script and load it during integration test as follows:
#Test
#Sql(scripts={"classpath:/sql/createFunction.sql"})
public void testUserInfo() throws Exception {
// userService calls VALIDATEUSER function
userService.isUserValid("testdb", "testdb");
}
How to create the function VALIDATEUSER in h2?
Thanks in advance.
You can execute the following SQL in H2 to create a function that accepts two VARCHAR parameters and returns an INTEGER result 1.
CREATE ALIAS VALIDATEUSER AS $$int validateUser(String name, String password) { return 1; }$$
Try this
CREATE ALIAS functionName AS 'int methodName(String name, String password) { return 1; }'
in Java you can use like this
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:h2:mem:", "sa", "");
Statement stat = conn.createStatement();
// Using a custom Java function
stat.execute("CREATE ALIAS functionName AS 'int methodName(String name, String password) { return 1; }' ");
stat.close();
conn.close();

Problem in updating a data column in spring

I have a Database table called ProgramData. their i have a data column called Id and executed. id set to be as auto increment.
Table structure is like this.
What i want is according to id executed column need to be updated. following is my code segment.
public void saveDtvProgDataExecuted()
{
ProgramData programeData = new ProgramData();
String SQL = "UPDATE program_data SET executed=1 WHERE programeData.id = ?";
this.jdbcTemplate.update(SQL);
}
If i run this code this gives me error like bad SQL grammar [UPDATE program_data SET executed=1 WHERE programeData.id = ?]; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?' at line 1
Problem is you’re not passing the ID value to the jdbctemplate.
You should use
this.jdbctemplate.update(SQL, id);
Where id is the id of the record you’re updating.
Please refer to the documentation for more information:
http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#jdbc-updates
TRY THIS statement while you are passing ? in your sql query it need to be set while execution.
String SQL = "UPDATE program_data SET executed=1 WHERE programeData.id = ?";
this.jdbcTemplate.update(SQL,new PreparedStatementCallback<Boolean>(){
#Override
public Boolean doInPreparedStatement(PreparedStatement ps)
throws SQLException, DataAccessException {
ps.setInt(1,"here you need to pass value of programeData.id);
return ps.execute();
}
});

To execute a dynamic query which using DB function

Requirement :
Having a query stored in DB with in a query there is a where condition in that its calling a database function.
Using spring MVC I need to get the query, pass the parameter and get the return value.
This is the query:
SELECT COUNT(*)
FROM IncidentHdr ih, IncidentUser iu
WHERE ih.incidentId = iu.incidentHdr.incidentId
AND get_response_team_access (ih.incidentId, :perscode)
Here get_response_team_access is a DB function which returns an integer. Query works fine as we tested in DB using dummy data.
What I tried So far :
#PersistenceContext
private EntityManager em;
#Override
public Long getAlertCount(String queryString, long persCode) throws DataAccessException {
Query q = em.createQuery(queryString);
q.setParameter("perscode", persCode);
return (long) q.getSingleResult();
}
Throws Exception:
ERROR org.hibernate.hql.internal.ast.ErrorCounter - <AST>:1:293: unexpected AST node: (
antlr.NoViableAltException: unexpected AST node: (
To call DB function from JPQL you have to use FUNCTION keyword.
SELECT COUNT(*) FROM IncidentHdr ih,IncidentUser iu
WHERE ih.incidentId = iu.incidentHdr.incidentId
AND FUNCTION('get_response_team_access',ih.incidentId, :perscode)
Use FUNCTION (formerly FUNC) to call database specific functions from
JPQL
Usage:
You can use FUNCTION to call database functions that are not supported
directly in JPQL and to call user or library specific functions.
Source: http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/j_func.htm

How can I persist Alter Session Set variables for a certain database only?

I want to change the NLS_SORT and NLS_COMP parameters for every query to this particular schema, but there are other users on this system as well that want to keep the original values, so using ALTER SYSTEM SET is a no-no in this case. I also don't want to change every query to this particular schema.
Is there a way to either put these values in an initialization file that is particular to only this schema or can I somehow add a trigger that sets these values on the session whenever a session is started to this schema?
I am running Oracle Express 11G R2 and the solution does not need to be backwards compatible.
My goal is to not have to run the ALTER SESSION SET rows before running the SELECT-LIKE-statement and having it produce two results rather than one. Here is the Java sample code that I've used to examine what values I actually want the NLS_COMP and NLS_SORT values to have:
public class OracleCaseTest {
public static void main(String[] args) throws SQLException {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setUrl("jdbc:oracle:thin:#localhost:1521:xe");
dataSource.setUsername("CASETEST");
dataSource.setPassword("casetest");
Connection conn = null;
PreparedStatement createStatement = null;
PreparedStatement populateStatement = null;
PreparedStatement comparisonAlterSessionStatement = null;
PreparedStatement sortAlterSessionStatement = null;
PreparedStatement queryStatement = null;
PreparedStatement deleteStatement = null;
ResultSet rs = null;
conn = dataSource.getConnection();
createStatement = conn
.prepareStatement("CREATE TABLE CollationTestTable ( Name varchar(255) )");
createStatement.execute();
try {
// comparisonAlterSessionStatement = conn
// .prepareStatement("ALTER SESSION SET NLS_COMP=LINGUISTIC");
// comparisonAlterSessionStatement.execute();
//
// sortAlterSessionStatement = conn.prepareStatement("ALTER SESSION SET NLS_SORT=BINARY_CI");
// sortAlterSessionStatement.execute();
String[] names = { "pepe", "pépé", "PEPE", "MEME", "mémé", "meme" };
for (String name : names) {
populateStatement = conn
.prepareStatement("INSERT INTO CollationTestTable VALUES (?)");
populateStatement.setString(1, name);
populateStatement.execute();
}
queryStatement = conn
.prepareStatement("SELECT Name FROM CollationTestTable WHERE NAME LIKE 'pe%'");
rs = queryStatement.executeQuery();
while (rs.next()) {
System.out.println(rs.getString(1));
}
} finally {
deleteStatement = conn.prepareStatement("DROP TABLE CollationTestTable");
deleteStatement.execute();
}
}
}
I'm aware of the problem with full table scans without linguistic indexes that this might create, but ignore that for this question.
UPDATE: This is the statement I used to create my trigger from the SQL command-line interface (after connecting and logging in with my user):
create or replace trigger nls_settings
after logon on schema
begin
EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_COMP=LINGUISTIC';
EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_SORT=BINARY_CI';
end nls_settings;
/
Also changed the original question to indicate that by "database", in the Oracle world I really meant "schema"/"user".
You can create trigger on
database startup or instance shutdown
user logon or logoff
http://download.oracle.com/docs/cd/B14117_01/server.101/b10743/triggers.htm#i6061
First off, can you clarify what you mean by "database"? In Oracle terminology, a database is the set of schemas that would be affected by an ALTER SYSTEM call. It is possible to have multiple databases on a single server but you can only have one XE database on a machine. If you are coming from a SQL Server background, what SQL Server calls a "database" is more similar to what Oracle calls a "schema".
Assuming that you really mean schema and not database, and assuming that the Oracle CASETEST user only interacts with tables in the one schema, I would second bpgergo's suggestion of a login trigger in the CASETEST schema.

Why am I getting a MissingMethodException with querying the TableServiceContext?

I am trying to query a Azure Table Storage. For that I use the following two methods:
TableServiceContext:
public IQueryable<T> QueryEntities<T>(string tableName) where T : TableServiceEntity
{
this.ResolveType = (unused) => typeof(T);
return this.CreateQuery<T>(tableName);
}
Code that uses the method above:
CloudStorageAccount account = AzureConnector.GetCloudStorageAccount(AppSettingsVariables.TableStorageConnection);
AzureTableStorageContext context = new AzureTableStorageContext(account.TableEndpoint.ToString(), account.Credentials);
// Checks if the setting already exists in the Azure table storage. Returns null if not exists.
var existsQuery = from e in context.QueryEntities<ServiceSettingEntity>(TableName)
where e.ServiceName.Equals(this.ServiceName) && e.SettingName.Equals(settingName)
select e;
ServiceSettingEntity existingSettginEntity = existsQuery.FirstOrDefault();
The LINQ query above generates the following request url:
http://127.0.0.1:10002/devstoreaccount1/PublicSpaceNotificationSettingsTable()?$filter=(ServiceName eq 'PublicSpaceNotification') and (SettingName eq 'expectiss')
The code in the class generates the following MissingMethodException:
I have looked at the supported LINQ Queries for the Table API;
Looked at several working stackoverflow solutions;
Tried IgnoreResourceNotFoundException on the TableServiceContext (usercomments of QueryOperators);
Tried to convert the linq query with ToList() before calling first or default (usercomments of QueryOperators).
but I can't get this to work.
Make sure you have parameterless constructor for the class "ServerSettingEntity". The ‘DTO’ that inherits TableServiceEntity needs a constructor with no parameters.

Resources