using System; using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; using ACE.Entity.Enum.Properties; namespace ACE.Database.SQLFormatters { public abstract class SQLWriter { protected static readonly Regex IllegalInFileName = new Regex($"[{Regex.Escape(new string(Path.GetInvalidFileNameChars()))}]", RegexOptions.Compiled); /// /// Set this to enable auto commenting when creating SQL statements. /// public Dictionary WeenieClassNames; /// /// Set this to enable auto commenting when creating SQL statements. /// If a weenie id is found in the dictionary, the name will be added in the form of a /* Friendly Weenie Name */ /// public Dictionary WeenieNames; /// /// Set this to enable auto commenting when creating SQL statements. /// If a spell id is found in the dictionary, the name will be added in the form of a /* Friendly Spell Name */ /// public Dictionary SpellNames; /// /// Set this to enable auto commenting when creating SQL statements. /// If a opcode is found in the dictionary, the name will be added in the form of a /* Friendly Opcode Name */ /// public Dictionary PacketOpCodes; /// /// lineGenerator should generate the entire line after the first (. It should include the trailing ) and any comments after. /// It should consist of only a single line. /// This will automatically call FixNullFields on the output created by lineGenerator() /// protected static void ValuesWriter(int count, Func lineGenerator, StreamWriter writer) { for (int i = 0; i < count; i++) { string output; if (i == 0) output = "VALUES ("; else output = " , ("; output += lineGenerator(i); if (i == count - 1) output += ";"; output = FixNullFields(output); writer.WriteLine(output); } } /// /// If input is null, NULL will be returned. /// If input is not null, a string surrounded in ' will be returned, and any ' found within the string will be replaced wtih '' /// protected static string GetSQLString(string input) { if (input == null) return null; return "'" + input.Replace("'", "''") + "'"; } /// /// This will find values that were not output to a values line, for example, if a property is a (int?), and it has no value, you might see ", ," in the sql. /// This function will replace that ", ," with ", NULL,". /// It also removes empty comments like the folliwng: " /* */" /// protected static string FixNullFields(string input) { input = input.Replace(", ,", ", NULL,"); input = input.Replace(", ,", ", NULL,"); // Fix cases where the last field might be null input = input.Replace(", )", ", NULL)"); // Remove empty comments input = input.Replace(" /* */", ""); return input; } protected string GetValueEnumName(PropertyInt property, int value) { switch (property) { case PropertyInt.ActivationCreateClass: if (WeenieNames != null) { WeenieNames.TryGetValue((uint) value, out var propertyValueDescription); return propertyValueDescription; } break; } return property.GetValueEnumName(value); } protected string GetValueEnumName(PropertyDataId property, uint value) { switch (property) { case PropertyDataId.AlternateCurrency: case PropertyDataId.AugmentationCreateItem: case PropertyDataId.LastPortal: case PropertyDataId.LinkedPortalOne: case PropertyDataId.LinkedPortalTwo: case PropertyDataId.OriginalPortal: case PropertyDataId.UseCreateItem: case PropertyDataId.VendorsClassId: if (WeenieNames != null) { WeenieNames.TryGetValue(value, out var propertyValueDescription); return propertyValueDescription; } break; case PropertyDataId.BlueSurgeSpell: case PropertyDataId.DeathSpell: case PropertyDataId.ProcSpell: case PropertyDataId.RedSurgeSpell: case PropertyDataId.Spell: case PropertyDataId.YellowSurgeSpell: if (SpellNames != null) { SpellNames.TryGetValue(value, out var propertyValueDescription); return propertyValueDescription; } break; } return property.GetValueEnumName(value); } } }