using System; using System.Collections.Generic; using System.Runtime.InteropServices; namespace PlDotNET.Common { /// /// Loads and provides access to runtime settings for PL/.NET from PostgreSQL's configuration, /// using lazy initialization and interop with the PostgreSQL backend. /// public partial class PlDotNETSettings { /// /// Stores lazy-loaded PostgreSQL configuration settings by key. /// private readonly Dictionary> settings; /// /// Initializes a new instance of the class. /// Preloads known configuration keys with deferred access via Lazy evaluation. /// public PlDotNETSettings() { settings = []; var settingsKeys = new[] { "pldotnet.always_nullable", "pldotnet.print_source_code", "pldotnet.save_source_code", "pldotnet.compile_fsharp_with_fcs", "pldotnet.verbose_level", "pldotnet.path_to_save_source_code", "pldotnet.path_to_temporary_files", }; foreach (var key in settingsKeys) { settings[key] = new Lazy(() => GetPostgreSetting(key)); } } /// /// Gets a value indicating whether all database values should be treated as nullable by default. /// public bool AlwaysNullable => ConvertToBoolean(settings["pldotnet.always_nullable"].Value); /// /// Gets a value indicating whether the generated source code should be printed to the PostgreSQL log. /// public bool PrintSourceCode => ConvertToBoolean(settings["pldotnet.print_source_code"].Value); /// /// Gets a value indicating whether the generated source code should be saved to disk. /// public bool SaveSourceCode => ConvertToBoolean(settings["pldotnet.save_source_code"].Value); /// /// Gets a value indicating whether F# compilation should use the FCS (F# Compiler Services) backend. /// public bool CompileFSharpWithFCS => ConvertToBoolean(settings["pldotnet.compile_fsharp_with_fcs"].Value); /// /// Gets the configured verbosity level for diagnostics or logging output. /// public int VerboseLevel => int.Parse(settings["pldotnet.verbose_level"].Value); /// /// Gets the file system path where generated source code should be saved. /// public string PathToSaveSourceCode => settings["pldotnet.path_to_save_source_code"].Value; /// /// Gets the directory path used to store temporary files created during compilation or execution. /// public string PathToTemporaryFiles => settings["pldotnet.path_to_temporary_files"].Value; /// /// Calls the PostgreSQL backend to retrieve the value of a server configuration setting by name. /// /// The name of the PostgreSQL configuration setting to retrieve. /// /// A pointer to an ANSI string representing the setting's value, or if not found. /// [LibraryImport("@PKG_LIBDIR/pldotnet.so", StringMarshalling = StringMarshalling.Utf8)] private static partial IntPtr pldotnet_GetPostgresSetting(string settingName); /// /// Converts a PostgreSQL "on"/"off" setting value to a boolean. /// /// The setting value, expected to be "on" or "off" (case-insensitive). /// true if the value is "on"; otherwise, false. private static bool ConvertToBoolean(string value) { return value?.ToLower() == "on"; } /// /// Retrieves a PostgreSQL configuration setting by name via interop. /// /// The name of the PostgreSQL configuration setting. /// /// The string representation of the setting's value, or null if the value is not found. /// private static string GetPostgreSetting(string settingName) { IntPtr resultPtr = pldotnet_GetPostgresSetting(settingName); if (resultPtr == IntPtr.Zero) { return null; } return Marshal.PtrToStringAnsi(resultPtr); } } }