Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration of the database session is updated to use a datastore #829

Merged
merged 4 commits into from
Jun 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using GeneXus.Application;
using GeneXus.Configuration;
using GeneXus.Data;
using GeneXus.Data.ADO;
using GeneXus.Encryption;
using GxClasses.Helpers;
using log4net;
Expand All @@ -11,7 +14,6 @@ public class GXSessionServiceFactory
private static readonly ILog log = log4net.LogManager.GetLogger(typeof(GXSessionServiceFactory));
static string REDIS = "REDIS";
static string DATABASE = "DATABASE";

public static ISessionService GetProvider()
{
ISessionService sessionService = null;
Expand All @@ -32,7 +34,6 @@ public static ISessionService GetProvider()
}
else
{

GXLogging.Debug(log, "Loading Session provider:", className);
#if !NETCORE
type = Type.GetType(className, true, true);
Expand All @@ -53,16 +54,15 @@ public static ISessionService GetProvider()
}
}
return null;

}
}
public class GxRedisSession : ISessionService
{
private static readonly ILog log = log4net.LogManager.GetLogger(typeof(GxRedisSession));
internal static string SESSION_ADDRESS = "SESSION_PROVIDER_ADDRESS";
internal static string SESSION_INSTANCE = "SESSION_PROVIDER_INSTANCE_NAME";
internal static string SESSION_PASSWORD = "SESSION_PROVIDER_PASSWORD";
static string SESSION_TIMEOUT = "SESSION_PROVIDER_SESSION_TIMEOUT";

public GxRedisSession(GXService serviceProvider)
{
string password = serviceProvider.Properties.Get(SESSION_PASSWORD);
Expand All @@ -85,6 +85,8 @@ public GxRedisSession(GXService serviceProvider)
int.TryParse(sessionTimeoutStrCompatibility, out sessionTimeoutMinutes);

SessionTimeout = sessionTimeoutMinutes;
GXLogging.Debug(log, "Redis Host:", host, ", InstanceName:", instanceName);
GXLogging.Debug(log, "Redis sessionTimeoutMinutes:", sessionTimeoutMinutes.ToString());
}
public GxRedisSession(string host, string password, string instanceName, int sessionTimeout)
{
Expand All @@ -95,63 +97,69 @@ public GxRedisSession(string host, string password, string instanceName, int ses
}
InstanceName = instanceName;
SessionTimeout = sessionTimeout;
GXLogging.Debug(log, "Redis Host:", host, ", InstanceName:", instanceName);
GXLogging.Debug(log, "Redis sessionTimeoutMinutes:", sessionTimeout.ToString());
}
public string ConnectionString { get; }
public string InstanceName { get; }
public int SessionTimeout { get; }

public string Schema => throw new NotImplementedException();

public string TableName => throw new NotImplementedException();
}
public class GxDatabaseSession : ISessionService
{
private static readonly ILog log = log4net.LogManager.GetLogger(typeof(GxDatabaseSession));
internal static string SESSION_ADDRESS = "SESSION_PROVIDER_ADDRESS";
internal static string SESSION_PASSWORD = "SESSION_PROVIDER_PASSWORD";
internal static string SESSION_SCHEMA = "SESSION_PROVIDER_SCHEMA";
internal static string SESSION_TABLE_NAME = "SESSION_PROVIDER_TABLE_NAME";
internal static string SESSION_PROVIDER_SERVER = "SESSION_PROVIDER_SERVER";
internal static string SESSION_PROVIDER_DATABASE = "SESSION_PROVIDER_DATABASE";
internal static string SESSION_PROVIDER_USER = "SESSION_PROVIDER_USER";

internal static string SESSION_DATASTORE = "SESSION_PROVIDER_DATASTORE";
const string DEFAULT_SQLSERVER_SCHEMA = "dbo";
public GxDatabaseSession(GXService serviceProvider)
{
string password = serviceProvider.Properties.Get(SESSION_PASSWORD);
if (!string.IsNullOrEmpty(password))
{
password = CryptoImpl.Decrypt(password);
}
string serverName = serviceProvider.Properties.Get(SESSION_PROVIDER_SERVER);
string userName = serviceProvider.Properties.Get(SESSION_PROVIDER_USER);
string database = serviceProvider.Properties.Get(SESSION_PROVIDER_DATABASE);
string schema = serviceProvider.Properties.Get(SESSION_SCHEMA);
string tableName = serviceProvider.Properties.Get(SESSION_TABLE_NAME);

string sessionAddresCompatibility = serviceProvider.Properties.Get(GxDatabaseSession.SESSION_ADDRESS);
if (!string.IsNullOrEmpty(sessionAddresCompatibility))
{
ConnectionString = sessionAddresCompatibility;
}

if (!string.IsNullOrEmpty(serverName))
string datastoreName = serviceProvider.Properties.Get(SESSION_DATASTORE);
if (!string.IsNullOrEmpty(datastoreName))
{
ConnectionString += $"Data Source={serverName};";
}
if (!string.IsNullOrEmpty(database))
{
ConnectionString += $"Initial Catalog={database}";
}
if (!string.IsNullOrEmpty(password))
{
ConnectionString += $";password={password}";
GxContext context = GxContext.CreateDefaultInstance();
IGxDataStore datastore = context.GetDataStore(datastoreName);
string schema = datastore.Connection.CurrentSchema;
if (string.IsNullOrEmpty(schema))
schema = DEFAULT_SQLSERVER_SCHEMA;
string tableName = serviceProvider.Properties.Get(SESSION_TABLE_NAME);
GxConnection conn = datastore.Connection as GxConnection;
Schema = schema;
TableName = tableName;
context.CloseConnections();
GxDataRecord dr = datastore.Db as GxDataRecord;
if (dr != null && conn != null)
{
ConnectionString = dr.BuildConnectionStringImpl(conn.DataSourceName, conn.InternalUserId, conn.UserPassword, conn.DatabaseName, conn.Port, conn.CurrentSchema, conn.Data);
GXLogging.Debug(log, "Database ConnectionString:", dr.ConnectionStringForLog());
}
}
if (!string.IsNullOrEmpty(userName))
else //Backward compatibility configuration
{
ConnectionString += $";user={userName}";
string password = serviceProvider.Properties.Get(SESSION_PASSWORD);
if (!string.IsNullOrEmpty(password))
{
password = CryptoImpl.Decrypt(password);
}
string schema = serviceProvider.Properties.Get(SESSION_SCHEMA);
string tableName = serviceProvider.Properties.Get(SESSION_TABLE_NAME);
string sessionAddresCompatibility = serviceProvider.Properties.Get(SESSION_ADDRESS);
if (!string.IsNullOrEmpty(sessionAddresCompatibility))
{
ConnectionString = sessionAddresCompatibility;
}
if (!string.IsNullOrEmpty(password))
{
ConnectionString += $";password={password}";
}
Schema = schema;
TableName = tableName;
}
Schema = schema;
TableName = tableName;
SessionTimeout = Preferences.SessionTimeout;
GXLogging.Debug(log, "Database sessionTimeoutMinutes:", SessionTimeout.ToString());
}
public GxDatabaseSession(string host, string password, string schema, string tableName)
{
Expand All @@ -164,16 +172,11 @@ public GxDatabaseSession(string host, string password, string schema, string tab
TableName = tableName;
}
public string ConnectionString { get; }

public string Schema { get; }

public string TableName { get; }

public string InstanceName => throw new NotImplementedException();

public int SessionTimeout { get; }
}

public interface ISessionService
{
string ConnectionString { get; }
Expand Down