diff --git a/NTDLS.Katzebase.Engine/Interactions/Management/EnvironmentManager.cs b/NTDLS.Katzebase.Engine/Interactions/Management/EnvironmentManager.cs index 7892b403..7dc9f243 100644 --- a/NTDLS.Katzebase.Engine/Interactions/Management/EnvironmentManager.cs +++ b/NTDLS.Katzebase.Engine/Interactions/Management/EnvironmentManager.cs @@ -1,10 +1,9 @@ -using NTDLS.Katzebase.Api.Exceptions; +using Newtonsoft.Json; +using NTDLS.Katzebase.Api.Exceptions; using NTDLS.Katzebase.Engine.Atomicity; using NTDLS.Katzebase.Engine.Interactions.APIHandlers; using NTDLS.Katzebase.Engine.Interactions.QueryHandlers; -using NTDLS.Katzebase.Shared; -using System.Text.Json; -using static NTDLS.Katzebase.Parsers.Query.SupportingTypes.PreparedQuery; +using NTDLS.Katzebase.Parsers.Query.SupportingTypes; namespace NTDLS.Katzebase.Engine.Interactions.Management { @@ -34,67 +33,30 @@ internal EnvironmentManager(EngineCore core) } } - static void UpdateSettingProperty(T obj, string propertyName, object newValue) - { - Type type = typeof(T); - var property = type.GetProperty(propertyName); - if (property != null && property.CanWrite) - { - property.SetValue(obj, Convert.ChangeType(newValue, property.PropertyType)); - } - } - - internal void Alter(Transaction transaction, IReadOnlyDictionary attributes) + internal void Alter(Transaction transaction, IReadOnlyDictionary configuration) { try { var appSettingsPath = Path.Combine(AppContext.BaseDirectory, "appsettings.json"); - if (!File.Exists(appSettingsPath)) { throw new KbEngineException($"Could not locate configuration file: [{appSettingsPath}[."); } - string json = File.ReadAllText(appSettingsPath); + var settingsType = _core.Settings.GetType(); - // Parse the JSON into a JsonDocument - using JsonDocument document = JsonDocument.Parse(json); - var root = document.RootElement; - //var settingsElement = root.GetProperty("Settings"); - var settings = JsonSerializer.Deserialize(root.ToString()); - - foreach (var settingElement in root.EnumerateObject()) + foreach (var item in configuration) { - if (Enum.TryParse(settingElement.Name, true, out QueryAttribute optionType)) + //Update the running engine setting. + var property = settingsType.GetProperty(item.Key); + if (property != null && property.CanWrite) { - if (attributes.TryGetValue(optionType, out var value)) - { - UpdateSettingProperty(settings, settingElement.Name, value); //Save the value in the JSON settings file. - UpdateSettingProperty(_core.Settings, settingElement.Name, value); //Save the setting in the live core. - } + property.SetValue(_core.Settings, Convert.ChangeType(item.Value.Value, property.PropertyType)); } } - string updatedSettingsJson = JsonSerializer.Serialize(settings); - using var file = File.Create(appSettingsPath); - using (var writer = new Utf8JsonWriter(file, new JsonWriterOptions { Indented = true })) - { - writer.WriteStartObject(); - foreach (var property in root.EnumerateObject()) - { - if (property.Name == "Settings") - { - writer.WritePropertyName(property.Name); - writer.WriteRawValue(updatedSettingsJson); - } - else - { - property.WriteTo(writer); - } - } - writer.WriteEndObject(); - } - file.Close(); + //Save the new settings to file. + File.WriteAllText(appSettingsPath, JsonConvert.SerializeObject(_core.Settings, Formatting.Indented)); } catch (Exception ex) { @@ -102,6 +64,5 @@ internal void Alter(Transaction transaction, IReadOnlyDictionary(PreparedQuery.QueryAttribute.TargetSchemaName); + var targetSchema = preparedQuery.GetAttribute(PreparedQuery.Attribute.TargetSchemaName); var physicalTargetSchema = _core.Schemas.AcquireVirtual(transactionReference.Transaction, targetSchema.EnsureNotNull(), LockOperation.Write, LockOperation.Read); if (physicalTargetSchema.Exists == false) @@ -194,7 +194,7 @@ internal KbActionResponse ExecuteUpdate(SessionState session, PreparedQuery prep { using var transactionReference = _core.Transactions.APIAcquire(session); - var targetSchemaAlias = preparedQuery.GetAttribute(PreparedQuery.QueryAttribute.TargetSchemaAlias); + var targetSchemaAlias = preparedQuery.GetAttribute(PreparedQuery.Attribute.TargetSchemaAlias); var targetSchema = preparedQuery.Schemas.Where(o => o.Alias.Is(targetSchemaAlias)).Single(); var physicalSchema = _core.Schemas.Acquire(transactionReference.Transaction, targetSchema.Name, LockOperation.Read); @@ -332,7 +332,7 @@ internal KbActionResponse ExecuteDelete(SessionState session, PreparedQuery prep { using var transactionReference = _core.Transactions.APIAcquire(session); - var targetSchemaAlias = preparedQuery.GetAttribute(PreparedQuery.QueryAttribute.TargetSchemaAlias); + var targetSchemaAlias = preparedQuery.GetAttribute(PreparedQuery.Attribute.TargetSchemaAlias); var firstSchema = preparedQuery.Schemas.Where(o => o.Alias.Is(targetSchemaAlias)).Single(); var physicalSchema = _core.Schemas.Acquire(transactionReference.Transaction, firstSchema.Name, LockOperation.Delete); diff --git a/NTDLS.Katzebase.Engine/Interactions/QueryHandlers/IndexQueryHandlers.cs b/NTDLS.Katzebase.Engine/Interactions/QueryHandlers/IndexQueryHandlers.cs index f9a6c19d..dbc2ac36 100644 --- a/NTDLS.Katzebase.Engine/Interactions/QueryHandlers/IndexQueryHandlers.cs +++ b/NTDLS.Katzebase.Engine/Interactions/QueryHandlers/IndexQueryHandlers.cs @@ -38,7 +38,7 @@ internal KbActionResponse ExecuteDrop(SessionState session, PreparedQuery prepar if (preparedQuery.SubQueryType == SubQueryType.Index || preparedQuery.SubQueryType == SubQueryType.UniqueKey) { _core.Indexes.DropIndex(transactionReference.Transaction, schemaName, - preparedQuery.GetAttribute(PreparedQuery.QueryAttribute.IndexName)); + preparedQuery.GetAttribute(PreparedQuery.Attribute.IndexName)); } else { @@ -61,8 +61,8 @@ internal KbQueryDocumentListResult ExecuteAnalyze(SessionState session, Prepared using var transactionReference = _core.Transactions.APIAcquire(session); var analysis = _core.Indexes.AnalyzeIndex(transactionReference.Transaction, - preparedQuery.GetAttribute(PreparedQuery.QueryAttribute.Schema), - preparedQuery.GetAttribute(PreparedQuery.QueryAttribute.IndexName)); + preparedQuery.GetAttribute(PreparedQuery.Attribute.Schema), + preparedQuery.GetAttribute(PreparedQuery.Attribute.IndexName)); transactionReference.Transaction.AddMessage(analysis, KbMessageType.Verbose); @@ -83,8 +83,8 @@ internal KbActionResponse ExecuteRebuild(SessionState session, PreparedQuery pre using var transactionReference = _core.Transactions.APIAcquire(session); string schemaName = preparedQuery.Schemas.First().Name; - var indexName = preparedQuery.GetAttribute(PreparedQuery.QueryAttribute.IndexName); - var indexPartitions = preparedQuery.GetAttribute(PreparedQuery.QueryAttribute.Partitions, _core.Settings.DefaultIndexPartitions); + var indexName = preparedQuery.GetAttribute(PreparedQuery.Attribute.IndexName); + var indexPartitions = preparedQuery.GetAttribute(PreparedQuery.Attribute.Partitions, _core.Settings.DefaultIndexPartitions); _core.Indexes.RebuildIndex(transactionReference.Transaction, schemaName, indexName, indexPartitions); return transactionReference.CommitAndApplyMetricsThenReturnResults(); @@ -105,12 +105,12 @@ internal KbActionResponse ExecuteCreate(SessionState session, PreparedQuery prep if (preparedQuery.SubQueryType == SubQueryType.Index || preparedQuery.SubQueryType == SubQueryType.UniqueKey) { var indexPartitions = preparedQuery.GetAttribute( - PreparedQuery.QueryAttribute.Partitions, _core.Settings.DefaultIndexPartitions); + PreparedQuery.Attribute.Partitions, _core.Settings.DefaultIndexPartitions); var index = new KbIndex { - Name = preparedQuery.GetAttribute(PreparedQuery.QueryAttribute.IndexName), - IsUnique = preparedQuery.GetAttribute(PreparedQuery.QueryAttribute.IsUnique), + Name = preparedQuery.GetAttribute(PreparedQuery.Attribute.IndexName), + IsUnique = preparedQuery.GetAttribute(PreparedQuery.Attribute.IsUnique), Partitions = indexPartitions }; diff --git a/NTDLS.Katzebase.Engine/Interactions/QueryHandlers/ProcedureQueryHandlers.cs b/NTDLS.Katzebase.Engine/Interactions/QueryHandlers/ProcedureQueryHandlers.cs index 0b4561f7..356c7e4f 100644 --- a/NTDLS.Katzebase.Engine/Interactions/QueryHandlers/ProcedureQueryHandlers.cs +++ b/NTDLS.Katzebase.Engine/Interactions/QueryHandlers/ProcedureQueryHandlers.cs @@ -39,10 +39,10 @@ internal KbActionResponse ExecuteCreate(SessionState session, PreparedQuery prep if (preparedQuery.SubQueryType == SubQueryType.Procedure) { - var objectName = preparedQuery.GetAttribute(PreparedQuery.QueryAttribute.ObjectName); - var objectSchema = preparedQuery.GetAttribute(PreparedQuery.QueryAttribute.Schema); - var parameters = preparedQuery.GetAttribute>(PreparedQuery.QueryAttribute.Parameters); - var Batches = preparedQuery.GetAttribute>(PreparedQuery.QueryAttribute.Batches); + var objectName = preparedQuery.GetAttribute(PreparedQuery.Attribute.ObjectName); + var objectSchema = preparedQuery.GetAttribute(PreparedQuery.Attribute.Schema); + var parameters = preparedQuery.GetAttribute>(PreparedQuery.Attribute.Parameters); + var Batches = preparedQuery.GetAttribute>(PreparedQuery.Attribute.Batches); _core.Procedures.CreateCustomProcedure(transactionReference.Transaction, objectSchema, objectName, parameters, Batches); } @@ -64,8 +64,8 @@ internal KbQueryResultCollection ExecuteExec(SessionState session, PreparedQuery { try { - var schemaName = preparedQuery.GetAttribute(PreparedQuery.QueryAttribute.Schema); - var objectName = preparedQuery.GetAttribute(PreparedQuery.QueryAttribute.ObjectName); + var schemaName = preparedQuery.GetAttribute(PreparedQuery.Attribute.Schema); + var objectName = preparedQuery.GetAttribute(PreparedQuery.Attribute.ObjectName); using var transactionReference = _core.Transactions.APIAcquire(session); diff --git a/NTDLS.Katzebase.Engine/Interactions/QueryHandlers/SchemaQueryHandlers.cs b/NTDLS.Katzebase.Engine/Interactions/QueryHandlers/SchemaQueryHandlers.cs index 0514cf60..bbd22a6f 100644 --- a/NTDLS.Katzebase.Engine/Interactions/QueryHandlers/SchemaQueryHandlers.cs +++ b/NTDLS.Katzebase.Engine/Interactions/QueryHandlers/SchemaQueryHandlers.cs @@ -39,7 +39,7 @@ internal KbQueryDocumentListResult ExecuteAnalyze(SessionState session, Prepared if (preparedQuery.SubQueryType == SubQueryType.Schema) { - var includePhysicalPages = preparedQuery.GetAttribute(PreparedQuery.QueryAttribute.IncludePhysicalPages, false); + var includePhysicalPages = preparedQuery.GetAttribute(PreparedQuery.Attribute.IncludePhysicalPages, false); result = _core.Schemas.AnalyzePages(transactionReference.Transaction, schemaName, includePhysicalPages); } else @@ -89,7 +89,7 @@ internal KbActionResponse ExecuteAlter(SessionState session, PreparedQuery prepa if (preparedQuery.SubQueryType == SubQueryType.Schema) { - var pageSize = preparedQuery.GetAttribute(PreparedQuery.QueryAttribute.PageSize, _core.Settings.DefaultDocumentPageSize); + var pageSize = preparedQuery.GetAttribute(PreparedQuery.Attribute.PageSize, _core.Settings.DefaultDocumentPageSize); string schemaName = preparedQuery.Schemas.Single().Name; _core.Schemas.Alter(transactionReference.Transaction, schemaName, pageSize); } @@ -115,7 +115,7 @@ internal KbActionResponse ExecuteCreate(SessionState session, PreparedQuery prep if (preparedQuery.SubQueryType == SubQueryType.Schema) { - var pageSize = preparedQuery.GetAttribute(PreparedQuery.QueryAttribute.PageSize, _core.Settings.DefaultDocumentPageSize); + var pageSize = preparedQuery.GetAttribute(PreparedQuery.Attribute.PageSize, _core.Settings.DefaultDocumentPageSize); string schemaName = preparedQuery.Schemas.Single().Name; _core.Schemas.CreateSingleSchema(transactionReference.Transaction, schemaName, pageSize); } diff --git a/NTDLS.Katzebase.Engine/Interactions/QueryHandlers/SessionQueryHandlers.cs b/NTDLS.Katzebase.Engine/Interactions/QueryHandlers/SessionQueryHandlers.cs index 5e2db0d9..7d7863d9 100644 --- a/NTDLS.Katzebase.Engine/Interactions/QueryHandlers/SessionQueryHandlers.cs +++ b/NTDLS.Katzebase.Engine/Interactions/QueryHandlers/SessionQueryHandlers.cs @@ -32,7 +32,7 @@ internal KbActionResponse ExecuteKillProcess(SessionState session, PreparedQuery try { using var transactionReference = _core.Transactions.APIAcquire(session); - var referencedProcessId = preparedQuery.GetAttribute(PreparedQuery.QueryAttribute.ProcessId); + var referencedProcessId = preparedQuery.GetAttribute(PreparedQuery.Attribute.ProcessId); _core.Sessions.CloseByProcessId(referencedProcessId); return transactionReference.CommitAndApplyMetricsThenReturnResults(); diff --git a/NTDLS.Katzebase.Management/Classes/Editor/TextMarkerService.cs b/NTDLS.Katzebase.Management/Classes/Editor/TextMarkerService.cs index f9404a0a..48bd2c75 100644 --- a/NTDLS.Katzebase.Management/Classes/Editor/TextMarkerService.cs +++ b/NTDLS.Katzebase.Management/Classes/Editor/TextMarkerService.cs @@ -65,6 +65,11 @@ private void TextArea_MouseMove(object sender, System.Windows.Input.MouseEventAr _tooltipTimer.Stop(); } } + else + { + _toolTip.IsOpen = false; // Hide tooltip if no marker + _tooltipTimer.Stop(); + } } catch { } } diff --git a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserAlterConfiguration.cs b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserAlterConfiguration.cs index a4ae7ea6..ec481368 100644 --- a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserAlterConfiguration.cs +++ b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserAlterConfiguration.cs @@ -1,5 +1,4 @@ -using NTDLS.Katzebase.Parsers.Query.Specific.WithOptions; -using NTDLS.Katzebase.Parsers.Query.SupportingTypes; +using NTDLS.Katzebase.Parsers.Query.SupportingTypes; using NTDLS.Katzebase.Parsers.Tokens; using NTDLS.Katzebase.Shared; using System.Reflection; @@ -18,7 +17,7 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) tokenizer.EatIfNext("with"); - var options = new ExpectedWithOptions(); + var options = new ExpectedQueryAttributes(); var properties = typeof(KatzebaseSettings).GetProperties(BindingFlags.Public | BindingFlags.Instance); @@ -27,7 +26,7 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) options.Add(property.Name, property.PropertyType); } - query.AddAttributes(StaticParserWithOptions.Parse(tokenizer, options)); + query.AddAttributes(StaticParserAttributes.Parse(tokenizer, options)); return query; } diff --git a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserAlterSchema.cs b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserAlterSchema.cs index 16a20bb7..54ee4adc 100644 --- a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserAlterSchema.cs +++ b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserAlterSchema.cs @@ -1,5 +1,4 @@ using NTDLS.Katzebase.Api.Exceptions; -using NTDLS.Katzebase.Parsers.Query.Specific.WithOptions; using NTDLS.Katzebase.Parsers.Query.SupportingTypes; using NTDLS.Katzebase.Parsers.Tokens; using static NTDLS.Katzebase.Parsers.Constants; @@ -24,11 +23,11 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) tokenizer.EatIfNext("with"); { - var options = new ExpectedWithOptions + var options = new ExpectedQueryAttributes { {"pagesize", typeof(uint) } }; - query.AddAttributes(StaticParserWithOptions.Parse(tokenizer, options)); + query.AddAttributes(StaticParserAttributes.Parse(tokenizer, options)); } return query; diff --git a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserAnalyzeIndex.cs b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserAnalyzeIndex.cs index 6b2d6770..81cc81ca 100644 --- a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserAnalyzeIndex.cs +++ b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserAnalyzeIndex.cs @@ -19,7 +19,7 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) { throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Expected index name, found: [{indexName}]."); } - query.AddAttribute(PreparedQuery.QueryAttribute.IndexName, indexName); + query.AddAttribute(PreparedQuery.Attribute.IndexName, indexName); tokenizer.EatIfNext("on"); @@ -28,7 +28,7 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Expected schema name, found: [{schemaName}]."); } query.Schemas.Add(new QuerySchema(tokenizer.GetCurrentLineNumber(), schemaName, QuerySchemaUsageType.Primary)); - query.AddAttribute(PreparedQuery.QueryAttribute.Schema, schemaName); + query.AddAttribute(PreparedQuery.Attribute.Schema, schemaName); return query; } } diff --git a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserAnalyzeSchema.cs b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserAnalyzeSchema.cs index ddab0593..d82a4b3d 100644 --- a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserAnalyzeSchema.cs +++ b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserAnalyzeSchema.cs @@ -1,5 +1,4 @@ using NTDLS.Katzebase.Api.Exceptions; -using NTDLS.Katzebase.Parsers.Query.Specific.WithOptions; using NTDLS.Katzebase.Parsers.Query.SupportingTypes; using NTDLS.Katzebase.Parsers.Tokens; using static NTDLS.Katzebase.Parsers.Constants; @@ -26,11 +25,11 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) if (tokenizer.TryEatIfNext("with")) { - var options = new ExpectedWithOptions + var options = new ExpectedQueryAttributes { {"includephysicalpages", typeof(bool) } }; - query.AddAttributes(StaticParserWithOptions.Parse(tokenizer, options)); + query.AddAttributes(StaticParserAttributes.Parse(tokenizer, options)); } return query; diff --git a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserWithOptions.cs b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserAttributes.cs similarity index 57% rename from NTDLS.Katzebase.Parsers/Query/Specific/StaticParserWithOptions.cs rename to NTDLS.Katzebase.Parsers/Query/Specific/StaticParserAttributes.cs index f3d706e0..ebca3d41 100644 --- a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserWithOptions.cs +++ b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserAttributes.cs @@ -1,18 +1,18 @@ using NTDLS.Katzebase.Api.Exceptions; -using NTDLS.Katzebase.Parsers.Query.Specific.WithOptions; +using NTDLS.Katzebase.Api.Types; +using NTDLS.Katzebase.Parsers.Query.SupportingTypes; using NTDLS.Katzebase.Parsers.Tokens; -using static NTDLS.Katzebase.Parsers.Query.SupportingTypes.PreparedQuery; namespace NTDLS.Katzebase.Parsers.Query.Specific { - public static class StaticParserWithOptions + public static class StaticParserAttributes { /// /// Parses "with options" and returns the dictionary of values that can be added to a prepared query. /// - internal static Dictionary Parse(Tokenizer tokenizer, ExpectedWithOptions expectedOptions) + internal static KbInsensitiveDictionary Parse(Tokenizer tokenizer, ExpectedQueryAttributes expectedOptions) { - var results = new Dictionary(); + var results = new KbInsensitiveDictionary(); if (tokenizer.TryIsNextCharacter('(') == false) { @@ -22,36 +22,29 @@ internal static Dictionary Parse(Tokenizer tokenizer, Ex while (!tokenizer.IsExhausted()) { - string name = tokenizer.EatGetNext().ToLowerInvariant(); + string attributeName = tokenizer.EatGetNext(); if (tokenizer.TryIsNextCharacter('=') == false) { throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Expected [=], found: [{tokenizer.NextCharacter}]."); } tokenizer.EatNextCharacter(); - string? tokenValue = tokenizer.EatGetNext().ToLowerInvariant(); + string? attributeValue = tokenizer.EatGetNext(); - if (expectedOptions.ContainsKey(name) == false) + if (expectedOptions.TryGetValue(attributeName, out var matchedOptionType) == false) { throw new KbParserException(tokenizer.GetCurrentLineNumber(), - $"Expected [{string.Join("],[", expectedOptions.Select(o => o.Key))}], found: [{tokenizer.ResolveLiteral(name)}]"); + $"Expected [{string.Join("],[", expectedOptions.Select(o => o.Key))}], found: [{tokenizer.ResolveLiteral(attributeName)}]"); } - if (tokenizer.Literals.TryGetValue(tokenValue, out var literal)) + if (tokenizer.Literals.TryGetValue(attributeValue, out var literal)) { - tokenValue = literal.Value; + attributeValue = literal.Value; } - var convertedValue = expectedOptions.ValidateAndConvert(tokenizer, name, tokenValue); - - var option = new WithOption(name, convertedValue, convertedValue.GetType()); - if (Enum.TryParse(option.Name, true, out QueryAttribute optionType) == false) - { - throw new KbParserException(tokenizer.GetCurrentLineNumber(), - $"Expected [{string.Join("],[", expectedOptions.Select(o => o.Key))}], found: [{option.Name}]."); - } - - results.Add(optionType, option.Value); + var convertedValue = expectedOptions.ValidateAndConvert(tokenizer, attributeName, attributeValue); + var option = new QueryAttribute(attributeName, convertedValue, convertedValue.GetType()); + results.Add(attributeName, option); if (tokenizer.TryEatIfNextCharacter(',') == false) { diff --git a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserCreateIndex.cs b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserCreateIndex.cs index b8984f60..968f4964 100644 --- a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserCreateIndex.cs +++ b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserCreateIndex.cs @@ -1,5 +1,4 @@ using NTDLS.Katzebase.Api.Exceptions; -using NTDLS.Katzebase.Parsers.Query.Specific.WithOptions; using NTDLS.Katzebase.Parsers.Query.SupportingTypes; using NTDLS.Katzebase.Parsers.Tokens; using static NTDLS.Katzebase.Parsers.Constants; @@ -21,8 +20,8 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Expected index name, found: [{indexName}]."); } - query.AddAttribute(PreparedQuery.QueryAttribute.IndexName, indexName); - query.AddAttribute(PreparedQuery.QueryAttribute.IsUnique, false); + query.AddAttribute(PreparedQuery.Attribute.IndexName, indexName); + query.AddAttribute(PreparedQuery.Attribute.IsUnique, false); if (string.IsNullOrEmpty(tokenizer.MatchingScope(out var endOfScope))) { @@ -50,16 +49,16 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Expected schema name, found: [{schemaName}]."); } query.Schemas.Add(new QuerySchema(tokenizer.GetCurrentLineNumber(), schemaName, QuerySchemaUsageType.Primary)); - query.AddAttribute(PreparedQuery.QueryAttribute.Schema, schemaName); + query.AddAttribute(PreparedQuery.Attribute.Schema, schemaName); if (tokenizer.TryEatIfNext("with")) { - var options = new ExpectedWithOptions + var options = new ExpectedQueryAttributes { {"partitions", typeof(uint) } }; - query.AddAttributes(StaticParserWithOptions.Parse(tokenizer, options)); + query.AddAttributes(StaticParserAttributes.Parse(tokenizer, options)); } return query; diff --git a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserCreateSchema.cs b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserCreateSchema.cs index 210aeec7..f79b7af7 100644 --- a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserCreateSchema.cs +++ b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserCreateSchema.cs @@ -1,5 +1,4 @@ using NTDLS.Katzebase.Api.Exceptions; -using NTDLS.Katzebase.Parsers.Query.Specific.WithOptions; using NTDLS.Katzebase.Parsers.Query.SupportingTypes; using NTDLS.Katzebase.Parsers.Tokens; using static NTDLS.Katzebase.Parsers.Constants; @@ -21,16 +20,16 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Expected schema name, found: [ {schemaName} ]."); } query.Schemas.Add(new QuerySchema(tokenizer.GetCurrentLineNumber(), schemaName, QuerySchemaUsageType.Primary)); - query.AddAttribute(PreparedQuery.QueryAttribute.Schema, schemaName); + query.AddAttribute(PreparedQuery.Attribute.Schema, schemaName); if (tokenizer.TryEatIfNext("with")) { - var options = new ExpectedWithOptions + var options = new ExpectedQueryAttributes { {"pagesize", typeof(uint) } }; - query.AddAttributes(StaticParserWithOptions.Parse(tokenizer, options)); + query.AddAttributes(StaticParserAttributes.Parse(tokenizer, options)); } return query; diff --git a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserCreateUniqueKey.cs b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserCreateUniqueKey.cs index a35f82ca..ee9d52cb 100644 --- a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserCreateUniqueKey.cs +++ b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserCreateUniqueKey.cs @@ -1,5 +1,4 @@ using NTDLS.Katzebase.Api.Exceptions; -using NTDLS.Katzebase.Parsers.Query.Specific.WithOptions; using NTDLS.Katzebase.Parsers.Query.SupportingTypes; using NTDLS.Katzebase.Parsers.Tokens; using static NTDLS.Katzebase.Parsers.Constants; @@ -21,8 +20,8 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Expected index name, found: [{indexName}]."); } - query.AddAttribute(PreparedQuery.QueryAttribute.IndexName, indexName); - query.AddAttribute(PreparedQuery.QueryAttribute.IsUnique, true); + query.AddAttribute(PreparedQuery.Attribute.IndexName, indexName); + query.AddAttribute(PreparedQuery.Attribute.IsUnique, true); tokenizer.IsNext('('); @@ -36,15 +35,15 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Expected schema name, found: [{schemaName}]."); } query.Schemas.Add(new QuerySchema(tokenizer.GetCurrentLineNumber(), schemaName, QuerySchemaUsageType.Primary)); - query.AddAttribute(PreparedQuery.QueryAttribute.Schema, schemaName); + query.AddAttribute(PreparedQuery.Attribute.Schema, schemaName); if (tokenizer.TryEatIfNext("with")) { - var options = new ExpectedWithOptions + var options = new ExpectedQueryAttributes { {"partitions", typeof(uint) } }; - query.AddAttributes(StaticParserWithOptions.Parse(tokenizer, options)); + query.AddAttributes(StaticParserAttributes.Parse(tokenizer, options)); } return query; diff --git a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserDelete.cs b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserDelete.cs index 26491c72..29d7edf8 100644 --- a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserDelete.cs +++ b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserDelete.cs @@ -22,7 +22,7 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) } query.Schemas.Add(new QuerySchema(tokenizer.GetCurrentLineNumber(), schemaName.ToLowerInvariant(), QuerySchemaUsageType.Primary)); - query.AddAttribute(PreparedQuery.QueryAttribute.TargetSchemaAlias, string.Empty); + query.AddAttribute(PreparedQuery.Attribute.TargetSchemaAlias, string.Empty); } else { @@ -32,7 +32,7 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) { throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Expected schema name, found: [{targetAlias}]."); } - query.AddAttribute(PreparedQuery.QueryAttribute.TargetSchemaAlias, targetAlias.ToLowerInvariant()); + query.AddAttribute(PreparedQuery.Attribute.TargetSchemaAlias, targetAlias.ToLowerInvariant()); tokenizer.EatIfNext("from"); diff --git a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserDropIndex.cs b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserDropIndex.cs index 4d2cdbb8..0a358f24 100644 --- a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserDropIndex.cs +++ b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserDropIndex.cs @@ -20,8 +20,8 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Expected index name, found: [{indexName}]."); } - query.AddAttribute(PreparedQuery.QueryAttribute.IndexName, indexName); - query.AddAttribute(PreparedQuery.QueryAttribute.IsUnique, false); + query.AddAttribute(PreparedQuery.Attribute.IndexName, indexName); + query.AddAttribute(PreparedQuery.Attribute.IsUnique, false); tokenizer.EatIfNext("on"); @@ -30,7 +30,7 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Expected schema name, found: [{schemaName}]."); } query.Schemas.Add(new QuerySchema(tokenizer.GetCurrentLineNumber(), schemaName, QuerySchemaUsageType.Primary)); - query.AddAttribute(PreparedQuery.QueryAttribute.Schema, schemaName); + query.AddAttribute(PreparedQuery.Attribute.Schema, schemaName); return query; } diff --git a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserDropSchema.cs b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserDropSchema.cs index cd76c7cd..4232ba36 100644 --- a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserDropSchema.cs +++ b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserDropSchema.cs @@ -20,7 +20,7 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Expected schema name, found: [{schemaName}]."); } query.Schemas.Add(new QuerySchema(tokenizer.GetCurrentLineNumber(), schemaName, QuerySchemaUsageType.Primary)); - query.AddAttribute(PreparedQuery.QueryAttribute.Schema, schemaName); + query.AddAttribute(PreparedQuery.Attribute.Schema, schemaName); return query; } diff --git a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserDropUniqueKey.cs b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserDropUniqueKey.cs index 63ad2545..25764015 100644 --- a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserDropUniqueKey.cs +++ b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserDropUniqueKey.cs @@ -20,8 +20,8 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Expected index name, found: [{indexName}]."); } - query.AddAttribute(PreparedQuery.QueryAttribute.IndexName, indexName); - query.AddAttribute(PreparedQuery.QueryAttribute.IsUnique, true); + query.AddAttribute(PreparedQuery.Attribute.IndexName, indexName); + query.AddAttribute(PreparedQuery.Attribute.IsUnique, true); tokenizer.IsNext('('); @@ -35,7 +35,7 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Expected schema name, found: [{schemaName}]."); } query.Schemas.Add(new QuerySchema(tokenizer.GetCurrentLineNumber(), schemaName, QuerySchemaUsageType.Primary)); - query.AddAttribute(PreparedQuery.QueryAttribute.Schema, schemaName); + query.AddAttribute(PreparedQuery.Attribute.Schema, schemaName); return query; } diff --git a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserExec.cs b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserExec.cs index c86f26ca..fe7a5bad 100644 --- a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserExec.cs +++ b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserExec.cs @@ -22,14 +22,14 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) var parts = procedureName.Split(':'); if (parts.Length == 1) { - query.AddAttribute(PreparedQuery.QueryAttribute.Schema, ":"); - query.AddAttribute(PreparedQuery.QueryAttribute.ObjectName, procedureName); + query.AddAttribute(PreparedQuery.Attribute.Schema, ":"); + query.AddAttribute(PreparedQuery.Attribute.ObjectName, procedureName); } else { var schemaName = string.Join(':', parts.Take(parts.Length - 1)); - query.AddAttribute(PreparedQuery.QueryAttribute.Schema, schemaName); - query.AddAttribute(PreparedQuery.QueryAttribute.ObjectName, parts.Last()); + query.AddAttribute(PreparedQuery.Attribute.Schema, schemaName); + query.AddAttribute(PreparedQuery.Attribute.ObjectName, parts.Last()); } if (tokenizer.TryCompareNext(o => o.StartsWith('$'))) diff --git a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserKill.cs b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserKill.cs index 73d7bd49..7f79f912 100644 --- a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserKill.cs +++ b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserKill.cs @@ -14,7 +14,7 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) var referencedProcessId = tokenizer.EatGetNextEvaluated(); try { - query.AddAttribute(PreparedQuery.QueryAttribute.ProcessId, referencedProcessId); + query.AddAttribute(PreparedQuery.Attribute.ProcessId, referencedProcessId); } catch { diff --git a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserRebuildIndex.cs b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserRebuildIndex.cs index 788bfb52..dd0dc219 100644 --- a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserRebuildIndex.cs +++ b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserRebuildIndex.cs @@ -1,5 +1,4 @@ using NTDLS.Katzebase.Api.Exceptions; -using NTDLS.Katzebase.Parsers.Query.Specific.WithOptions; using NTDLS.Katzebase.Parsers.Query.SupportingTypes; using NTDLS.Katzebase.Parsers.Tokens; using static NTDLS.Katzebase.Parsers.Constants; @@ -21,7 +20,7 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Expected index name, found: [{indexName}]."); } - query.AddAttribute(PreparedQuery.QueryAttribute.IndexName, indexName); + query.AddAttribute(PreparedQuery.Attribute.IndexName, indexName); tokenizer.EatIfNext("on"); @@ -33,11 +32,11 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) if (tokenizer.TryEatIfNext("with")) { - var options = new ExpectedWithOptions + var options = new ExpectedQueryAttributes { {"partitions", typeof(uint) } }; - query.AddAttributes(StaticParserWithOptions.Parse(tokenizer, options)); + query.AddAttributes(StaticParserAttributes.Parse(tokenizer, options)); } return query; diff --git a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserRebuildUniqueKey.cs b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserRebuildUniqueKey.cs index a7d2650e..56fd458e 100644 --- a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserRebuildUniqueKey.cs +++ b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserRebuildUniqueKey.cs @@ -1,5 +1,4 @@ using NTDLS.Katzebase.Api.Exceptions; -using NTDLS.Katzebase.Parsers.Query.Specific.WithOptions; using NTDLS.Katzebase.Parsers.Query.SupportingTypes; using NTDLS.Katzebase.Parsers.Tokens; using static NTDLS.Katzebase.Parsers.Constants; @@ -20,7 +19,7 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) { throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Expected unique key name, found: [{tokenizer.ResolveLiteral(indexName)}]."); } - query.AddAttribute(PreparedQuery.QueryAttribute.IndexName, indexName); + query.AddAttribute(PreparedQuery.Attribute.IndexName, indexName); tokenizer.EatIfNext("on"); @@ -32,11 +31,11 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) if (tokenizer.TryEatIfNext("with")) { - var options = new ExpectedWithOptions + var options = new ExpectedQueryAttributes { {"partitions", typeof(uint) } }; - query.AddAttributes(StaticParserWithOptions.Parse(tokenizer, options)); + query.AddAttributes(StaticParserAttributes.Parse(tokenizer, options)); } return query; diff --git a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserSelect.cs b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserSelect.cs index 834a5763..7add1eed 100644 --- a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserSelect.cs +++ b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserSelect.cs @@ -45,7 +45,7 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Expected schema name, found: [{tokenizer.ResolveLiteral(selectIntoSchema)}]."); } - query.AddAttribute(PreparedQuery.QueryAttribute.TargetSchemaName, selectIntoSchema); + query.AddAttribute(PreparedQuery.Attribute.TargetSchemaName, selectIntoSchema); query.QueryType = QueryType.SelectInto; } diff --git a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserUpdate.cs b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserUpdate.cs index eff7389c..fe44fde1 100644 --- a/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserUpdate.cs +++ b/NTDLS.Katzebase.Parsers/Query/Specific/StaticParserUpdate.cs @@ -105,13 +105,13 @@ internal static PreparedQuery Parse(QueryBatch queryBatch, Tokenizer tokenizer) var targetSchema = query.Schemas.Where(o => o.Alias.Is(updateSchemaNameOrAlias)).FirstOrDefault() ?? throw new KbParserException(query.ScriptLine, $"Update schema now found in query: [{updateSchemaNameOrAlias}]."); - query.AddAttribute(PreparedQuery.QueryAttribute.TargetSchemaAlias, targetSchema.Alias); + query.AddAttribute(PreparedQuery.Attribute.TargetSchemaAlias, targetSchema.Alias); } else { //The query did not have a from, so the schema specified on the UPDATE line is the schema name. query.Schemas.Add(new QuerySchema(tokenizer.GetCurrentLineNumber(), updateSchemaNameOrAlias.ToLowerInvariant(), QuerySchemaUsageType.Primary)); - query.AddAttribute(PreparedQuery.QueryAttribute.TargetSchemaAlias, string.Empty); + query.AddAttribute(PreparedQuery.Attribute.TargetSchemaAlias, string.Empty); } if (tokenizer.TryEatIfNext("where")) diff --git a/NTDLS.Katzebase.Parsers/Query/Specific/WithOptions/ExpectedWithOptions.cs b/NTDLS.Katzebase.Parsers/Query/SupportingTypes/ExpectedQueryAttributes.cs similarity index 74% rename from NTDLS.Katzebase.Parsers/Query/Specific/WithOptions/ExpectedWithOptions.cs rename to NTDLS.Katzebase.Parsers/Query/SupportingTypes/ExpectedQueryAttributes.cs index 924715f8..62810d64 100644 --- a/NTDLS.Katzebase.Parsers/Query/Specific/WithOptions/ExpectedWithOptions.cs +++ b/NTDLS.Katzebase.Parsers/Query/SupportingTypes/ExpectedQueryAttributes.cs @@ -3,11 +3,14 @@ using NTDLS.Katzebase.Api.Types; using NTDLS.Katzebase.Parsers.Tokens; -namespace NTDLS.Katzebase.Parsers.Query.Specific.WithOptions +namespace NTDLS.Katzebase.Parsers.Query.SupportingTypes { - public class ExpectedWithOptions : KbInsensitiveDictionary + /// + /// Contains the name and type of expected query attributes. Used for validating while parsing + /// + public class ExpectedQueryAttributes : KbInsensitiveDictionary { - public ExpectedWithOptions() + public ExpectedQueryAttributes() { } @@ -33,7 +36,14 @@ public object ValidateAndConvert(Tokenizer tokenizer, string name, string? value return boolValue != 0; } - return value?.Is("true") == true; + if (value?.Is("true") == true) + { + return true; + } + else if (value?.Is("false") == true) + { + return false; + } } var resultingValue = Convert.ChangeType(value, resultType); @@ -43,7 +53,7 @@ public object ValidateAndConvert(Tokenizer tokenizer, string name, string? value } catch { - throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Failed to convert [{resultType.Name}] option, found: [{name}]."); + throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Failed to convert [{resultType.Name}] option for [{name}], found: [{value}]."); } } throw new KbParserException(tokenizer.GetCurrentLineNumber(), $"Unexpected with option, found: [{name}]."); diff --git a/NTDLS.Katzebase.Parsers/Query/SupportingTypes/PreparedQuery.cs b/NTDLS.Katzebase.Parsers/Query/SupportingTypes/PreparedQuery.cs index 45e42b84..8dfacf44 100644 --- a/NTDLS.Katzebase.Parsers/Query/SupportingTypes/PreparedQuery.cs +++ b/NTDLS.Katzebase.Parsers/Query/SupportingTypes/PreparedQuery.cs @@ -1,4 +1,5 @@ -using NTDLS.Katzebase.Parsers.Query.WhereAndJoinConditions; +using NTDLS.Katzebase.Api.Types; +using NTDLS.Katzebase.Parsers.Query.WhereAndJoinConditions; using System.Diagnostics.CodeAnalysis; using static NTDLS.Katzebase.Parsers.Constants; @@ -9,7 +10,7 @@ namespace NTDLS.Katzebase.Parsers.Query.SupportingTypes /// public class PreparedQuery(QueryBatch queryBatch, Constants.QueryType queryType, int? fileLine) { - public enum QueryAttribute + public enum Attribute { IndexName, IsUnique, @@ -23,39 +24,15 @@ public enum QueryAttribute Batches, Partitions, PageSize, - - //----------Configuration (BEGIN) ---------- - BaseAddress, - DataRootPath, - TransactionDataPath, - LogDirectory, - FlushLog, - DefaultDocumentPageSize, - UseCompression, - HealthMonitoringEnabled, - HealthMonitoringCheckpointSeconds, - HealthMonitoringInstanceLevelEnabled, - HealthMonitoringInstanceLevelTimeToLiveSeconds, - MaxIdleConnectionSeconds, - DefaultIndexPartitions, - DeferredIOEnabled, - WriteTraceData, - CacheEnabled, - CacheMaxMemory, - CacheScavengeInterval, - CachePartitions, - CacheSeconds - //----------Configuration (END)---------- } - private readonly Dictionary _attributes = new(); - public IReadOnlyDictionary Attributes => _attributes; - /// /// The line that the query started on. /// public int? ScriptLine { get; set; } = fileLine; + public List> VariableValues { get; set; } = new(); + /// /// Contains the hash of the whole query text with all constants and variables removed. /// @@ -120,61 +97,65 @@ public enum QueryAttribute #endregion - public List> VariableValues { get; set; } = new(); + #region Add/Get Attributes. + + private readonly KbInsensitiveDictionary _attributes = new(); + public IReadOnlyDictionary Attributes => _attributes; - public bool TryGetAttribute(QueryAttribute attribute, out T outValue, T defaultValue) + public bool TryGetAttribute(string attribute, out T outValue, T defaultValue) { - if (_attributes.TryGetValue(attribute, out object? value)) + if (_attributes.TryGetValue(attribute, out var option)) { - outValue = (T)value; + outValue = (T)option.Value; return true; } outValue = defaultValue; return false; } - public bool TryGetAttribute(QueryAttribute attribute, [NotNullWhen(true)] out T? outValue) + public bool TryGetAttribute(string attribute, [NotNullWhen(true)] out T? outValue) { - if (_attributes.TryGetValue(attribute, out object? value)) + if (_attributes.TryGetValue(attribute, out var option)) { - outValue = (T)value; + outValue = (T)option.Value; return true; } outValue = default; return false; } - public T GetAttribute(QueryAttribute attribute, T defaultValue) - { - if (_attributes.TryGetValue(attribute, out object? value)) - { - return (T)value; - } - return defaultValue; - } + public T GetAttribute(string attribute, T defaultValue) + => _attributes.TryGetValue(attribute, out var option) ? (T)option.Value : defaultValue; - public T GetAttribute(QueryAttribute attribute) - { - return (T)_attributes[attribute]; - } + public T GetAttribute(string attribute) + => (T)_attributes[attribute].Value; - public void AddAttribute(QueryAttribute key, object value) - { - if (!_attributes.TryAdd(key, value)) - { - _attributes[key] = value; - } - } + public void AddAttribute(string key, T value) where T : notnull + => _attributes[key] = new QueryAttribute(key, value, value.GetType()); + + public bool TryGetAttribute(Attribute attribute, out T outValue, T defaultValue) + => TryGetAttribute(attribute.ToString(), out outValue, defaultValue); + + public bool TryGetAttribute(Attribute attribute, [NotNullWhen(true)] out T? outValue) + => TryGetAttribute(attribute.ToString(), out outValue); - public void AddAttributes(Dictionary attributes) + public T GetAttribute(Attribute attribute, T defaultValue) + => GetAttribute(attribute.ToString(), defaultValue); + + public T GetAttribute(Attribute attribute) + => GetAttribute(attribute.ToString()); + + public void AddAttribute(Attribute key, T value) where T : notnull + => AddAttribute(key.ToString(), value); + + public void AddAttributes(KbInsensitiveDictionary attributes) { foreach (var attribute in attributes) { - if (!_attributes.TryAdd(attribute.Key, attribute.Value)) - { - _attributes[attribute.Key] = attribute.Value; - } + _attributes.Add(attribute.Key, attribute.Value); } } + + #endregion } } diff --git a/NTDLS.Katzebase.Parsers/Query/Specific/WithOptions/WithOption.cs b/NTDLS.Katzebase.Parsers/Query/SupportingTypes/QueryAttribute.cs similarity index 59% rename from NTDLS.Katzebase.Parsers/Query/Specific/WithOptions/WithOption.cs rename to NTDLS.Katzebase.Parsers/Query/SupportingTypes/QueryAttribute.cs index 90622d49..40c2020e 100644 --- a/NTDLS.Katzebase.Parsers/Query/Specific/WithOptions/WithOption.cs +++ b/NTDLS.Katzebase.Parsers/Query/SupportingTypes/QueryAttribute.cs @@ -1,6 +1,6 @@ -namespace NTDLS.Katzebase.Parsers.Query.Specific.WithOptions +namespace NTDLS.Katzebase.Parsers.Query.SupportingTypes { - public class WithOption(string name, object value, Type valueType) + public class QueryAttribute(string name, object value, Type valueType) { public string Name { get; private set; } = name; public object Value { get; private set; } = value; diff --git a/NTDLS.Katzebase.Parsers/Query/Validation/QueryValidation.cs b/NTDLS.Katzebase.Parsers/Query/Validation/QueryValidation.cs index f62663fe..383eb1f6 100644 --- a/NTDLS.Katzebase.Parsers/Query/Validation/QueryValidation.cs +++ b/NTDLS.Katzebase.Parsers/Query/Validation/QueryValidation.cs @@ -11,7 +11,7 @@ public static void Validate(Tokenizer tokenizer, PreparedQuery query) { var exceptions = new List(); - if (query.TryGetAttribute(PreparedQuery.QueryAttribute.TargetSchemaAlias, out var schemaAliasAttribute)) + if (query.TryGetAttribute(PreparedQuery.Attribute.TargetSchemaAlias, out var schemaAliasAttribute)) { if (query.Schemas.Any(o => o.Alias.Is(schemaAliasAttribute)) == false) {