private static void LoadBroadcastTextLocale() { // 0 1 2 3 4 var query = new StringBuilder(string.Format("SELECT Id, locale, MaleText_lang, FemaleText_lang, VerifiedBuild FROM {0}.broadcast_text_locale;", Settings.HotfixesDatabase)); using (var reader = SQLConnector.ExecuteQuery(query.ToString())) { if (reader == null) { return; } while (reader.Read()) { var broadcastTextLocale = new BroadcastTextLocale(); var id = (uint)reader.GetValue(0); var locale = (string)reader.GetValue(1); broadcastTextLocale.MaleText_lang = (string)reader.GetValue(2); broadcastTextLocale.FemaleText_lang = (string)reader.GetValue(3); broadcastTextLocale.VerifiedBuild = Convert.ToInt16(reader.GetValue(4)); BroadcastTextLocaleStores.Add(Tuple.Create(id, locale), broadcastTextLocale); } } }
private static void LoadPointsOfinterest() { string query = "SELECT ID, PositionX, PositionY, Icon, Flags, Importance, Name " + $"FROM {Settings.TDBDatabase}.points_of_interest ORDER BY ID;"; using (var reader = SQLConnector.ExecuteQuery(query)) { if (reader == null) { return; } while (reader.Read()) { var poiData = new POIData() { ID = Convert.ToUInt32(reader["ID"]), PositionX = Convert.ToSingle(reader["PositionX"]), PositionY = Convert.ToSingle(reader["PositionY"]), Icon = Convert.ToUInt32(reader["Icon"]), Flags = Convert.ToUInt32(reader["Flags"]), Importance = Convert.ToUInt32(reader["Importance"]), Name = Convert.ToString(reader["Name"]) }; POIs.Add(poiData); } } }
private static void LoadQuestObjectivesLocale() { // 0 1 2 3 4 5 var query = new StringBuilder(string.Format("SELECT Id, locale, QuestId, StorageIndex, Description, VerifiedBuild FROM {0}.quest_objectives_locale;", Settings.TDBDatabase)); using (var reader = SQLConnector.ExecuteQuery(query.ToString())) { if (reader == null) { return; } while (reader.Read()) { var localesQuestObjectives = new LocalesQuestObjectives(); var id = (uint)reader.GetValue(0); var locale = (string)reader.GetValue(1); localesQuestObjectives.QuestId = (uint)reader.GetValue(2); localesQuestObjectives.StorageIndex = Convert.ToInt16(reader.GetValue(3)); localesQuestObjectives.Description = (string)reader.GetValue(4); localesQuestObjectives.VerifiedBuild = Convert.ToInt16(reader.GetValue(5)); LocalesQuestObjectiveStores.Add(Tuple.Create(id, locale), localesQuestObjectives); } } }
private static void LoadCreatureDifficulty() { // 0 1 2 3 4 5 6 7 8 9 10 var query = new StringBuilder(string.Format("SELECT ID, CreatureID, FactionID, Expansion, MinLevel, MaxLevel, Flags1, Flags2, Flags3, Flags4, Flags5 FROM {0}.creature_difficulty;", Settings.HotfixesDatabase)); using (var reader = SQLConnector.ExecuteQuery(query.ToString())) { if (reader == null) { return; } while (reader.Read()) { var creatureDifficulty = new CreatureDifficulty(); uint id = (uint)reader.GetValue(0); creatureDifficulty.CreatureID = (uint)reader.GetValue(1); creatureDifficulty.FactionID = (uint)reader.GetValue(2); creatureDifficulty.Expansion = (int)reader.GetValue(3); creatureDifficulty.MinLevel = (int)reader.GetValue(4); creatureDifficulty.MaxLevel = (int)reader.GetValue(5); creatureDifficulty.Flags = new uint[5]; for (int i = 0; i < 5; i++) { creatureDifficulty.Flags[i] = (uint)reader.GetValue(i + 6); } CreatureDifficultyStores.Add(id, creatureDifficulty); } } }
/// <summary> /// Loads the broadcast texts form the database. /// </summary> private static void LoadBroadcastText() { string query = $"SELECT ID, MaleText, FemaleText FROM {Settings.HotfixesDatabase}.broadcast_text;"; using (var reader = SQLConnector.ExecuteQuery(query)) { if (reader == null) { return; } while (reader.Read()) { var id = Convert.ToInt32(reader["Id"]); var maleText = Convert.ToString(reader["MaleText"]); var femaleText = Convert.ToString(reader["FemaleText"]); if (!BroadcastMaleTexts.ContainsKey(maleText)) { BroadcastMaleTexts[maleText] = new List <int>(); } BroadcastMaleTexts[maleText].Add(id); if (!BroadcastFemaleTexts.ContainsKey(femaleText)) { BroadcastFemaleTexts[femaleText] = new List <int>(); } BroadcastFemaleTexts[femaleText].Add(id); } } }
private static void LoadWorldQuestInfo() { string query = "SELECT ID, QuestInfoID " + $"FROM {Settings.TDBDatabase}.quest_template "; using (var reader = SQLConnector.ExecuteQuery(query)) { if (reader == null) { return; } while (reader.Read()) { var questid = Convert.ToInt32(reader["ID"]); var questinfoid = Convert.ToInt32(reader["QuestInfoID"]); if (!WorldQuestInfos.ContainsKey(questid)) { WorldQuestInfos[questid] = questinfoid; } } } }
// Returns a dictionary from a DB query with any number of parameters // TODO: Drop this and use the GetDict<T, TK> method below public static Dictionary <T, dynamic> GetDict <T>(string query) { using (var reader = SQLConnector.ExecuteQuery(query)) { if (reader == null) { return(null); } var dict = new Dictionary <T, dynamic>(); while (reader.Read()) { var pk = (T)reader.GetValue(0); var objs = new object[30]; var fieldCount = reader.GetValues(objs); var obj = objs.ToTuple(fieldCount); dict.Add(pk, obj); } return(dict); } }
// Returns a dictionary from a DB query with two parameters (e.g <creature_entry, creature_name>) private static Dictionary <T, TK> GetDict <T, TK>(string query) { using (var reader = SQLConnector.ExecuteQuery(query)) { if (reader == null) // Not sure why I am doing this. { return(null); // Probably because I don't know } // a better alternative return(reader.GetDictionary <T, TK>()); } }
public static void LoadSQL() { if (!SQLConnector.Connected()) { throw new DataException("Cannot get DB data without an active DB connection."); } var startTime = DateTime.Now; var query = new StringBuilder(string.Format("SELECT ID, Language, MaleText, FemaleText, EmoteID0, EmoteID1, EmoteID2, EmoteDelay0, EmoteDelay1, EmoteDelay2, SoundId, Unk1, Unk2 FROM {0}.broadcast_text;", Settings.TDBDatabase)); using (var reader = SQLConnector.ExecuteQuery(query.ToString())) { if (reader == null) { return; } while (reader.Read()) { var broadcastText = new BroadcastText(); uint Id = Convert.ToUInt32(reader["Id"]); broadcastText.language = Convert.ToUInt32(reader["Language"]); broadcastText.MaleText = Convert.ToString(reader["MaleText"]); broadcastText.FemaleText = Convert.ToString(reader["FemaleText"]); broadcastText.emoteID0 = Convert.ToUInt32(reader["EmoteID0"]); broadcastText.emoteID1 = Convert.ToUInt32(reader["EmoteID1"]); broadcastText.emoteID2 = Convert.ToUInt32(reader["EmoteID2"]); broadcastText.emoteDelay0 = Convert.ToUInt32(reader["EmoteDelay0"]); broadcastText.emoteDelay1 = Convert.ToUInt32(reader["EmoteDelay1"]); broadcastText.emoteDelay2 = Convert.ToUInt32(reader["EmoteDelay2"]); broadcastText.soundId = Convert.ToUInt32(reader["SoundId"]); broadcastText.unk1 = Convert.ToUInt32(reader["Unk1"]); broadcastText.unk2 = Convert.ToUInt32(reader["Unk2"]); var tuple = Tuple.Create(Id, broadcastText); BroadcastTextStores.Add(tuple); } } var endTime = DateTime.Now; var span = endTime.Subtract(startTime); Trace.WriteLine(String.Format("SQL loaded in {0}.", span.ToFormattedString())); }
// Returns a dictionary from a DB query with two parameters (e.g <creature_entry, creature_name>) // TODO: Drop this and use the GetDict<T, TK> method below private static Dictionary <T, TK> GetDict <T, TK>(string query) { using (var reader = SQLConnector.ExecuteQuery(query)) { if (reader == null) { return(null); } var dict = new Dictionary <T, TK>(); while (reader.Read()) { dict.Add((T)reader.GetValue(0), (TK)reader.GetValue(1)); } return(dict); } }
/// <summary> /// Loads the broadcast texts form the database. /// </summary> private static void LoadBroadcastText() { string query = $"SELECT ID, Language, MaleText, FemaleText, EmoteID1, EmoteID2, EmoteID3, EmoteDelay1, EmoteDelay2, EmoteDelay3, SoundId, UnkEmoteID, Type FROM {Settings.HotfixesDatabase}.broadcast_text;"; using (MySqlDataReader reader = SQLConnector.ExecuteQuery(query)) { if (reader == null) { return; } while (reader.Read()) { BroadcastText broadcastText = new BroadcastText(); uint id = Convert.ToUInt32(reader["Id"]); broadcastText.Language = Convert.ToInt32(reader["Language"]); broadcastText.MaleText = Convert.ToString(reader["MaleText"]); broadcastText.FemaleText = Convert.ToString(reader["FemaleText"]); broadcastText.EmoteID = new uint?[3]; broadcastText.EmoteID[0] = Convert.ToUInt32(reader["EmoteID1"]); broadcastText.EmoteID[1] = Convert.ToUInt32(reader["EmoteID2"]); broadcastText.EmoteID[2] = Convert.ToUInt32(reader["EmoteID3"]); broadcastText.EmoteDelay = new uint?[3]; broadcastText.EmoteDelay[0] = Convert.ToUInt32(reader["EmoteDelay1"]); broadcastText.EmoteDelay[1] = Convert.ToUInt32(reader["EmoteDelay2"]); broadcastText.EmoteDelay[2] = Convert.ToUInt32(reader["EmoteDelay3"]); broadcastText.SoundId = Convert.ToUInt32(reader["SoundId"]); broadcastText.UnkEmoteId = Convert.ToUInt32(reader["UnkEmoteID"]); broadcastText.Type = Convert.ToUInt32(reader["Type"]); var tuple = Tuple.Create(id, broadcastText); BroadcastTextStores.Add(tuple); } } }
private static void LoadBroadcastText() { var query = new StringBuilder(string.Format("SELECT ID, Language, MaleText, FemaleText, EmoteID1, EmoteID2, EmoteID3, EmoteDelay1, EmoteDelay2, EmoteDelay3, SoundId, UnkEmoteID, Type FROM {0}.broadcast_text;", Settings.HotfixesDatabase)); using (var reader = SQLConnector.ExecuteQuery(query.ToString())) { if (reader == null) { return; } while (reader.Read()) { var broadcastText = new BroadcastText(); uint id = Convert.ToUInt32(reader["Id"]); broadcastText.language = Convert.ToInt32(reader["Language"]); broadcastText.MaleText = Convert.ToString(reader["MaleText"]); broadcastText.FemaleText = Convert.ToString(reader["FemaleText"]); broadcastText.emoteID0 = Convert.ToUInt32(reader["EmoteID1"]); broadcastText.emoteID1 = Convert.ToUInt32(reader["EmoteID2"]); broadcastText.emoteID2 = Convert.ToUInt32(reader["EmoteID3"]); broadcastText.emoteDelay0 = Convert.ToUInt32(reader["EmoteDelay1"]); broadcastText.emoteDelay1 = Convert.ToUInt32(reader["EmoteDelay2"]); broadcastText.emoteDelay2 = Convert.ToUInt32(reader["EmoteDelay3"]); broadcastText.soundId = Convert.ToUInt32(reader["SoundId"]); broadcastText.unk1 = Convert.ToUInt32(reader["UnkEmoteID"]); broadcastText.unk2 = Convert.ToUInt32(reader["Type"]); var tuple = Tuple.Create(id, broadcastText); BroadcastTextStores.Add(tuple); } } }
private static void LoadMapDifficulty() { // 0 1 2 var query = new StringBuilder(string.Format("SELECT ID, MapID, DifficultyID FROM {0}.map_difficulty;", Settings.WPPDatabase)); using (var reader = SQLConnector.ExecuteQuery(query.ToString())) { if (reader == null) { return; } while (reader.Read()) { var id = (int)reader.GetValue(0); var mapId = (int)reader.GetValue(1); var difficultyID = (int)reader.GetValue(2); MapDifficultyStores.Add(Tuple.Create(id, mapId), (1 << difficultyID)); } } if (MapDifficultyStores != null) { foreach (var mapDifficulty in MapDifficultyStores) { if (MapSpawnMaskStores.ContainsKey(mapDifficulty.Key.Item2)) { MapSpawnMaskStores[mapDifficulty.Key.Item2] |= mapDifficulty.Value; } else { MapSpawnMaskStores.Add(mapDifficulty.Key.Item2, mapDifficulty.Value); } } } }
private static void LoadQuestTemplateLocale() { // 0 1 var query = new StringBuilder(string.Format("SELECT Id, locale, " + // 2 3 4 5 6 7 8 9 10 11 "LogTitle, LogDescription, QuestDescription, AreaDescription, PortraitGiverText, PortraitGiverName, PortraitTurnInText, PortraitTurnInName, QuestCompletionLog, VerifiedBuild" + " FROM {0}.quest_template_locale;", Settings.TDBDatabase)); using (var reader = SQLConnector.ExecuteQuery(query.ToString())) { if (reader == null) { return; } while (reader.Read()) { var localesQuest = new LocalesQuest(); var id = (uint)reader.GetValue(0); var locale = (string)reader.GetValue(1); localesQuest.LogTitle = (string)reader.GetValue(2); localesQuest.LogDescription = (string)reader.GetValue(3); localesQuest.QuestDescription = (string)reader.GetValue(4); localesQuest.AreaDescription = (string)reader.GetValue(5); localesQuest.PortraitGiverText = (string)reader.GetValue(6); localesQuest.PortraitGiverName = (string)reader.GetValue(7); localesQuest.PortraitTurnInText = (string)reader.GetValue(8); localesQuest.PortraitTurnInName = (string)reader.GetValue(9); localesQuest.QuestCompletionLog = (string)reader.GetValue(10); localesQuest.VerifiedBuild = Convert.ToInt16(reader.GetValue(11)); LocalesQuestStores.Add(Tuple.Create(id, locale), localesQuest); } } }
/// <summary> /// Gets from `world` database a dictionary of the given struct/class. /// Structs fields type must match the type of the DB columns. /// DB columns names are set by using DBFieldNameAttribute. /// </summary> /// <typeparam name="T">Type of the elements of the list of entries (usually uint)</typeparam> /// <typeparam name="TK">Type of the struct</typeparam> /// <param name="entries">List of entries to select from DB</param> /// <param name="primaryKeyName"> </param> /// <returns>Dictionary of structs of type TK</returns> public static StoreDictionary <T, TK> GetDict <T, TK>(List <T> entries, string primaryKeyName = "entry") { if (entries.Count == 0) { return(null); } // TODO: Add new config option "Verify data against DB" if (!SQLConnector.Enabled) { return(null); } var tableAttrs = (DBTableNameAttribute[])typeof(TK).GetCustomAttributes(typeof(DBTableNameAttribute), false); if (tableAttrs.Length <= 0) { return(null); } var tableName = tableAttrs[0].Name; var fields = Utilities.GetFieldsAndAttribute <TK, DBFieldNameAttribute>(); fields.RemoveAll(field => field.Item2.Name == null); var fieldCount = 1; var fieldNames = new StringBuilder(); fieldNames.Append(primaryKeyName + ","); foreach (var field in fields) { fieldNames.Append(field.Item2); fieldNames.Append(","); fieldCount += field.Item2.Count; } var query = string.Format("SELECT {0} FROM {1}.{2} WHERE {3} IN ({4})", fieldNames.ToString().TrimEnd(','), Settings.TDBDatabase, tableName, primaryKeyName, String.Join(",", entries)); var dict = new Dictionary <T, TK>(entries.Count); using (var reader = SQLConnector.ExecuteQuery(query)) { if (reader == null) { return(null); } while (reader.Read()) { var instance = (TK)Activator.CreateInstance(typeof(TK)); var values = new object[fieldCount]; var count = reader.GetValues(values); if (count != fieldCount) { throw new InvalidConstraintException( "Number of fields from DB is different of the number of fields with DBFieldName attribute"); } var i = 1; foreach (var field in fields) { #if __MonoCS__ // Mono does not support __makeref (only added in the upcoming 2.12 version) if (values[i] is DBNull && field.Item1.FieldType == typeof(string)) { field.Item1.SetValue(instance, string.Empty); } else if (field.Item1.FieldType.BaseType == typeof(Enum)) { field.Item1.SetValue(instance, Enum.Parse(field.Item1.FieldType, values[i].ToString())); } else if (field.Item1.FieldType.BaseType == typeof(Array)) { var arr = Array.CreateInstance(field.Item1.FieldType.GetElementType(), field.Item2.Count); for (var j = 0; j < arr.Length; j++) { var elemType = arr.GetType().GetElementType(); var val = elemType.IsEnum ? Enum.Parse(elemType, values[i + j].ToString()) : Convert.ChangeType(values[i + j], elemType); arr.SetValue(val, j); } field.Item1.SetValue(instance, arr); } else if (field.Item1.FieldType == typeof(bool)) { field.Item1.SetValue(instance, Convert.ToBoolean(values[i])); } else { field.Item1.SetValue(instance, values[i]); } #else if (values[i] is DBNull && field.Item1.FieldType == typeof(string)) { field.Item1.SetValueDirect(__makeref(instance), string.Empty); } else if (field.Item1.FieldType.BaseType == typeof(Enum)) { field.Item1.SetValueDirect(__makeref(instance), Enum.Parse(field.Item1.FieldType, values[i].ToString())); } else if (field.Item1.FieldType.BaseType == typeof(Array)) { var arr = Array.CreateInstance(field.Item1.FieldType.GetElementType(), field.Item2.Count); for (var j = 0; j < arr.Length; j++) { var elemType = arr.GetType().GetElementType(); var val = elemType.IsEnum ? Enum.Parse(elemType, values[i + j].ToString()) : Convert.ChangeType(values[i + j], elemType); arr.SetValue(val, j); } field.Item1.SetValueDirect(__makeref(instance), arr); } else if (field.Item1.FieldType == typeof(bool)) { field.Item1.SetValueDirect(__makeref(instance), Convert.ToBoolean(values[i])); } else { field.Item1.SetValueDirect(__makeref(instance), values[i]); } #endif i += field.Item2.Count; } T key = (T)values[0]; if (!dict.ContainsKey(key)) { dict.Add(key, instance); } } } return(new StoreDictionary <T, TK>(dict)); }
/// <summary> /// Gets from `world` database a dictionary of the given struct/class. /// Structs fields type must match the type of the DB columns. /// DB columns names are set by using DBFieldNameAttribute. /// </summary> /// <typeparam name="T">Type of the first element of the list of entries (usually uint)</typeparam> /// <typeparam name="TG">Type of the second element of the list of entries (usually uint)</typeparam> /// <typeparam name="TK">Type of the struct</typeparam> /// <typeparam name="TH"></typeparam> /// <param name="entries">List of entries to select from DB</param> /// <param name="primaryKeyName1">Name of the first primary key</param> /// <param name="primaryKeyName2">Name of the second primary key</param> /// <returns>Dictionary of structs of type TK</returns> public static StoreDictionary <Tuple <T, TG, TH>, TK> GetDict <T, TG, TH, TK>(List <Tuple <T, TG, TH> > entries, string primaryKeyName1, string primaryKeyName2, string primaryKeyName3, string database = null) where T : struct where TG : struct where TH : struct { if (entries.Count == 0) { return(null); } // TODO: Add new config option "Verify data against DB" if (!SQLConnector.Enabled) { return(null); } var tableAttrs = (DBTableNameAttribute[])typeof(TK).GetCustomAttributes(typeof(DBTableNameAttribute), false); if (tableAttrs.Length <= 0) { return(null); } var tableName = tableAttrs[0].Name; var fields = Utilities.GetFieldsAndAttribute <TK, DBFieldNameAttribute>(); fields.RemoveAll(field => field.Item2.Name == null); var fieldCount = 3; var fieldNames = new StringBuilder(); fieldNames.Append(primaryKeyName1 + ","); fieldNames.Append(primaryKeyName2 + ","); fieldNames.Append(primaryKeyName3 + ","); foreach (var field in fields) { fieldNames.Append(field.Item2); fieldNames.Append(","); fieldCount += field.Item2.Count; } // WHERE (a = x1 AND b = y1) OR (a = x2 AND b = y2) OR ... var whereClause = new StringBuilder(); var ji = 0; foreach (var tuple in entries) { ji += 1; whereClause.Append("(") .Append(primaryKeyName1) .Append(" = ") .Append(Convert.ToInt64(tuple.Item1)) .Append(" AND ") .Append(primaryKeyName2) .Append(" = ") .Append(Convert.ToInt64(tuple.Item2)) .Append(" AND ") .Append(primaryKeyName3) .Append(" = ") .Append(Convert.ToInt64(tuple.Item3)) .Append(")"); if (ji != entries.Count) { whereClause.Append(" OR "); } } var query = string.Format("SELECT {0} FROM {1}.{2} WHERE {3}", fieldNames.ToString().TrimEnd(','), database ?? Settings.TDBDatabase, tableName, whereClause); Trace.WriteLine(query); var dict = new Dictionary <Tuple <T, TG, TH>, TK>(entries.Count); using (var reader = SQLConnector.ExecuteQuery(query)) { if (reader == null) { return(null); } while (reader.Read()) { var instance = (TK)Activator.CreateInstance(typeof(TK)); var values = new object[fieldCount]; var count = reader.GetValues(values); if (count != fieldCount) { throw new InvalidConstraintException( "Number of fields from DB is different of the number of fields with DBFieldName attribute"); } var i = 3; foreach (var field in fields) { if (values[i] is DBNull && field.Item1.FieldType == typeof(string)) { field.Item1.SetValueDirect(__makeref(instance), string.Empty); } else if (field.Item1.FieldType.BaseType == typeof(Enum)) { field.Item1.SetValueDirect(__makeref(instance), Enum.Parse(field.Item1.FieldType, values[i].ToString())); } else if (field.Item1.FieldType.BaseType == typeof(Array)) { var arr = Array.CreateInstance(field.Item1.FieldType.GetElementType(), field.Item2.Count); for (var j = 0; j < arr.Length; j++) { var elemType = arr.GetType().GetElementType(); var val = elemType.IsEnum ? Enum.Parse(elemType, values[i + j].ToString()) : Convert.ChangeType(values[i + j], elemType); arr.SetValue(val, j); } field.Item1.SetValueDirect(__makeref(instance), arr); } else if (field.Item1.FieldType == typeof(bool)) { field.Item1.SetValueDirect(__makeref(instance), Convert.ToBoolean(values[i])); } else { field.Item1.SetValueDirect(__makeref(instance), values[i]); } i += field.Item2.Count; } T key1; TG key2; TH key3; if (typeof(T).IsEnum) { key1 = (T)Enum.ToObject(typeof(T), values[0]); } else { key1 = (T)values[0]; } if (typeof(TG).IsEnum) { key2 = (TG)Enum.ToObject(typeof(TG), values[1]); } else { key2 = (TG)values[1]; } if (typeof(TH).IsEnum) { key3 = (TH)Enum.ToObject(typeof(TH), values[2]); } else { key3 = (TH)values[2]; } var key = Tuple.Create(key1, key2, key3); if (!dict.ContainsKey(key)) { dict.Add(key, instance); } } } return(new StoreDictionary <Tuple <T, TG, TH>, TK>(dict)); }
public static StoreMulti <Tuple <T, TG>, TK> GetDictMulti <T, TG, TK>(List <Tuple <T, TG> > entries, string primaryKeyName1, string primaryKeyName2) { if (entries.Count == 0) { return(null); } // TODO: Add new config option "Verify data against DB" if (!SQLConnector.Enabled) { return(null); } var tableAttrs = (DBTableNameAttribute[])typeof(TK).GetCustomAttributes(typeof(DBTableNameAttribute), false); if (tableAttrs.Length <= 0) { return(null); } var tableName = tableAttrs[0].Name; var fields = Utilities.GetFieldsAndAttribute <TK, DBFieldNameAttribute>(); var fieldCount = 2; var fieldNames = new StringBuilder(); fieldNames.Append(primaryKeyName1 + ","); fieldNames.Append(primaryKeyName2 + ","); foreach (var field in fields) { fieldNames.Append(field.Item2.ToString()); fieldNames.Append(","); fieldCount += field.Item2.Count; } // WHERE (a = x1 AND b = y1) OR (a = x2 AND b = y2) OR ... var whereClause = new StringBuilder(); var ji = 0; foreach (var tuple in entries) { ji += 1; whereClause.Append("(") .Append(primaryKeyName1) .Append(" = ") .Append(tuple.Item1) .Append(" AND ") .Append(primaryKeyName2) .Append(" = ") .Append(tuple.Item2) .Append(")"); if (ji != entries.Count) { whereClause.Append(" OR "); } } var query = string.Format("SELECT {0} FROM {1}.{2} WHERE {3}", fieldNames.ToString().TrimEnd(','), Settings.TDBDatabase, tableName, whereClause); var dict = new MultiDictionary <Tuple <T, TG>, TK>(true); using (var reader = SQLConnector.ExecuteQuery(query)) { if (reader == null) { return(null); } while (reader.Read()) { var instance = (TK)Activator.CreateInstance(typeof(TK)); var values = new object[fieldCount]; var count = reader.GetValues(values); if (count != fieldCount) { throw new InvalidConstraintException( "Number of fields from DB is different of the number of fields with DBFieldName attribute"); } var i = 2; foreach (var field in fields) { #if __MonoCS__ // Mono does not support __makeref (only added in the upcoming 2.12 version) if (values[i] is DBNull && field.Item1.FieldType == typeof(string)) { field.Item1.SetValue(instance, string.Empty); } else if (field.Item1.FieldType.BaseType == typeof(Enum)) { field.Item1.SetValue(instance, Enum.Parse(field.Item1.FieldType, values[i].ToString())); } else if (field.Item1.FieldType.BaseType == typeof(Array)) { var arr = Array.CreateInstance(field.Item1.FieldType.GetElementType(), field.Item2.Count); for (var j = 0; j < arr.Length; j++) { var elemType = arr.GetType().GetElementType(); var val = Convert.ChangeType(values[i + j], elemType); arr.SetValue(val, j); } field.Item1.SetValue(instance, arr); } else if (field.Item1.FieldType == typeof(bool)) { field.Item1.SetValue(instance, Convert.ToBoolean(values[i])); } else { field.Item1.SetValue(instance, values[i]); } #else if (values[i] is DBNull && field.Item1.FieldType == typeof(string)) { field.Item1.SetValueDirect(__makeref(instance), string.Empty); } else if (field.Item1.FieldType.BaseType == typeof(Enum)) { field.Item1.SetValueDirect(__makeref(instance), Enum.Parse(field.Item1.FieldType, values[i].ToString())); } else if (field.Item1.FieldType.BaseType == typeof(Array)) { var arr = Array.CreateInstance(field.Item1.FieldType.GetElementType(), field.Item2.Count); for (var j = 0; j < arr.Length; j++) { var elemType = arr.GetType().GetElementType(); var val = Convert.ChangeType(values[i + j], elemType); arr.SetValue(val, j); } field.Item1.SetValueDirect(__makeref(instance), arr); } else if (field.Item1.FieldType == typeof(bool)) { field.Item1.SetValueDirect(__makeref(instance), Convert.ToBoolean(values[i])); } else { field.Item1.SetValueDirect(__makeref(instance), values[i]); } #endif i += field.Item2.Count; } dict.Add(Tuple.Create((T)values[0], (TG)values[1]), instance); } } return(new StoreMulti <Tuple <T, TG>, TK>(dict)); }
/// <summary> /// Loads the broadcast texts form the database. /// </summary> private static void LoadBroadcastText() { string query = "SELECT ID, MaleText, FemaleText, EmoteID1, EmoteID2, EmoteID3, EmoteDelay1, EmoteDelay2, EmoteDelay3, UnkEmoteID, Language, Type, SoundID1, SoundID2, PlayerConditionID " + $"FROM {Settings.HotfixesDatabase}.broadcast_text;"; using (var reader = SQLConnector.ExecuteQuery(query)) { if (reader == null) { return; } while (reader.Read()) { var id = Convert.ToInt32(reader["Id"]); var maleText = Convert.ToString(reader["MaleText"]); var femaleText = Convert.ToString(reader["FemaleText"]); if (!BroadcastMaleTexts.ContainsKey(maleText)) { BroadcastMaleTexts[maleText] = new List <int>(); } BroadcastMaleTexts[maleText].Add(id); if (!BroadcastFemaleTexts.ContainsKey(femaleText)) { BroadcastFemaleTexts[femaleText] = new List <int>(); } BroadcastFemaleTexts[femaleText].Add(id); if (!Settings.UseDBC) { continue; } var broadcastText = new BroadcastTextEntry() { MaleText = maleText, FemaleText = femaleText, }; broadcastText.EmoteID = new ushort[3]; broadcastText.EmoteID[0] = Convert.ToUInt16(reader["EmoteID1"]); broadcastText.EmoteID[1] = Convert.ToUInt16(reader["EmoteID2"]); broadcastText.EmoteID[2] = Convert.ToUInt16(reader["EmoteID3"]); broadcastText.EmoteDelay = new ushort[3]; broadcastText.EmoteDelay[0] = Convert.ToUInt16(reader["EmoteDelay1"]); broadcastText.EmoteDelay[1] = Convert.ToUInt16(reader["EmoteDelay2"]); broadcastText.EmoteDelay[2] = Convert.ToUInt16(reader["EmoteDelay3"]); broadcastText.UnkEmoteID = Convert.ToUInt16(reader["UnkEmoteID"]); broadcastText.Language = Convert.ToByte(reader["Language"]); broadcastText.Type = Convert.ToByte(reader["Type"]); broadcastText.SoundID = new uint[2]; broadcastText.SoundID[0] = Convert.ToUInt32(reader["SoundID1"]); broadcastText.SoundID[1] = Convert.ToUInt32(reader["SoundID2"]); broadcastText.PlayerConditionID = Convert.ToUInt32(reader["PlayerConditionID"]); if (!DBC.DBC.BroadcastText.ContainsKey(id)) { DBC.DBC.BroadcastText.Add(id, broadcastText); } else { DBC.DBC.BroadcastText[id] = broadcastText; } } } }
public static RowList <T> Get <T>(RowList <T> rowList = null, string database = null) where T : IDataModel, new() { // TODO: Add new config option "Verify data against DB" if (!SQLConnector.Enabled) { return(null); } var result = new RowList <T>(); using (var reader = SQLConnector.ExecuteQuery(new SQLSelect <T>(rowList, database).Build())) { if (reader == null) { return(null); } var fields = SQLUtil.GetFields <T>(); var fieldsCount = fields.Select(f => f.Item3.First().Count).Sum(); while (reader.Read()) { var instance = (T)Activator.CreateInstance(typeof(T)); var values = GetValues(reader, fieldsCount); var i = 0; foreach (var field in fields) { if (values[i] is DBNull && field.Item2.FieldType == typeof(string)) { field.Item2.SetValue(instance, string.Empty); } else if (field.Item2.FieldType.BaseType == typeof(Enum)) { field.Item2.SetValue(instance, Enum.Parse(field.Item2.FieldType, values[i].ToString())); } else if (field.Item2.FieldType.BaseType == typeof(Array)) { var arr = Array.CreateInstance(field.Item2.FieldType.GetElementType(), field.Item3.First().Count); for (var j = 0; j < arr.Length; j++) { var elemType = arr.GetType().GetElementType(); if (elemType.IsEnum) { arr.SetValue(Enum.Parse(elemType, values[i + j].ToString()), j); } else if (Nullable.GetUnderlyingType(elemType) != null) //is nullable { arr.SetValue(Convert.ChangeType(values[i + j], Nullable.GetUnderlyingType(elemType)), j); } else { arr.SetValue(Convert.ChangeType(values[i + j], elemType), j); } } field.Item2.SetValue(instance, arr); } else if (field.Item2.FieldType == typeof(bool)) { field.Item2.SetValue(instance, Convert.ToBoolean(values[i])); } else if (Nullable.GetUnderlyingType(field.Item2.FieldType) != null) // is nullable { var uType = Nullable.GetUnderlyingType(field.Item2.FieldType); field.Item2.SetValue(instance, uType.IsEnum ? Enum.Parse(uType, values[i].ToString()) : Convert.ChangeType(values[i], Nullable.GetUnderlyingType(field.Item2.FieldType))); } else { field.Item2.SetValue(instance, values[i]); } i += field.Item3.First().Count; } result.Add(instance); } } return(result); }
/// <summary> /// Loads the broadcast texts form the database. /// </summary> private static void LoadBroadcastText() { string query = "SELECT ID, Text, Text1, EmoteID1, EmoteID2, EmoteID3, EmoteDelay1, EmoteDelay2, EmoteDelay3, EmotesID, LanguageID, Flags, SoundEntriesID1, SoundEntriesID2, ConditionID " + $"FROM {Settings.HotfixesDatabase}.broadcast_text " + $"UNION ALL " + $"SELECT a.ID, a.Text_lang, a.Text1_lang, b.EmoteID1, b.EmoteID2, b.EmoteID3, b.EmoteDelay1, b.EmoteDelay2, b.EmoteDelay3, b.EmotesID, b.LanguageID, b.Flags, b.SoundEntriesID1, b.SoundEntriesID2, b.ConditionID " + $"FROM {Settings.HotfixesDatabase}.broadcast_text_locale a LEFT JOIN {Settings.HotfixesDatabase}.broadcast_text b ON ( (a.ID= b.ID) AND ((a.locale='ruRU') OR (a.locale='zhCN') ) ); "; using (var reader = SQLConnector.ExecuteQuery(query)) { if (reader == null) { return; } while (reader.Read()) { var id = Convert.ToInt32(reader["Id"]); var text = Convert.ToString(reader["Text"]); var text1 = Convert.ToString(reader["Text1"]); if (!BroadcastTexts.ContainsKey(text)) { BroadcastTexts[text] = new List <int>(); } BroadcastTexts[text].Add(id); if (!BroadcastText1s.ContainsKey(text1)) { BroadcastText1s[text1] = new List <int>(); } BroadcastText1s[text1].Add(id); if (!Settings.UseDBC) { continue; } var broadcastText = new BroadcastTextEntry() { Text = text, Text1 = text1, }; broadcastText.EmoteID = new ushort[3]; broadcastText.EmoteID[0] = Convert.ToUInt16(reader["EmoteID1"]); broadcastText.EmoteID[1] = Convert.ToUInt16(reader["EmoteID2"]); broadcastText.EmoteID[2] = Convert.ToUInt16(reader["EmoteID3"]); broadcastText.EmoteDelay = new ushort[3]; broadcastText.EmoteDelay[0] = Convert.ToUInt16(reader["EmoteDelay1"]); broadcastText.EmoteDelay[1] = Convert.ToUInt16(reader["EmoteDelay2"]); broadcastText.EmoteDelay[2] = Convert.ToUInt16(reader["EmoteDelay3"]); broadcastText.EmotesID = Convert.ToUInt16(reader["EmotesID"]); broadcastText.LanguageID = Convert.ToByte(reader["LanguageID"]); broadcastText.Flags = Convert.ToByte(reader["Flags"]); broadcastText.ConditionID = Convert.ToUInt32(reader["ConditionID"]); broadcastText.SoundEntriesID = new uint[2]; broadcastText.SoundEntriesID[0] = Convert.ToUInt32(reader["SoundEntriesID1"]); broadcastText.SoundEntriesID[1] = Convert.ToUInt32(reader["SoundEntriesID2"]); if (!DBC.DBC.BroadcastText.ContainsKey(id)) { DBC.DBC.BroadcastText.TryAdd(id, broadcastText); } else { DBC.DBC.BroadcastText[id] = broadcastText; } } } }