All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cds.jdbc.sqlite.SqliteSessionVariableSetter Maven / Gradle / Ivy

There is a newer version: 3.4.0
Show newest version
/*******************************************************************
 * © 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();
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy