com.sap.cds.jdbc.sqlite.SqliteSessionVariableSetter Maven / Gradle / Ivy
/*******************************************************************
* © 2023 SAP SE or an SAP affiliate company. All rights reserved. *
*******************************************************************/
package com.sap.cds.jdbc.sqlite;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.Map;
import org.sqlite.Function;
import org.sqlite.SQLiteConnection;
import com.sap.cds.jdbc.spi.SessionVariableSetter;
public class SqliteSessionVariableSetter implements SessionVariableSetter {
@Override
public void set(Connection conn, String key, String value) throws SQLException {
set(conn, Collections.singletonMap(key, value));
}
@Override
public void set(Connection conn, Map contextVariables) throws SQLException {
SQLiteConnection sqliteCon = conn.unwrap(SQLiteConnection.class);
Function.create(sqliteCon, "session_context", new SessionContextFunction(contextVariables));
}
private static class SessionContextFunction extends Function {
private Map contextVariables;
public SessionContextFunction(Map contextVariables) {
this.contextVariables = contextVariables;
}
@Override
protected void xFunc() throws SQLException {
String key = value_text(0);
switch (key) {
case "$user.id":
result(contextVariables.get("APPLICATIONUSER"));
break;
case "$user.locale":
result(contextVariables.get("LOCALE"));
break;
case "$valid.from":
case "$at.from":
result(sqlTimestampToIso8601(contextVariables.get("VALID-FROM")));
break;
case "$valid.to":
case "$at.to":
result(sqlTimestampToIso8601(contextVariables.get("VALID-TO")));
break;
default:
result();
}
}
private static String sqlTimestampToIso8601(String iso9075) {
return Timestamp.valueOf(iso9075).toInstant().toString();
}
}
}