public async Task InstallScriptFor(int entryOrGuid, SmartScriptType type, IEnumerable <ISmartScriptLine> script) { using var writeLock = await MySqlSingleWriteLock.WriteLock(); await using var model = new TrinityDatabase(); await model.BeginTransactionAsync(IsolationLevel.ReadCommitted); await model.SmartScript.Where(x => x.EntryOrGuid == entryOrGuid && x.ScriptSourceType == (int)type).DeleteAsync(); switch (type) { case SmartScriptType.Creature: await model.CreatureTemplate.Where(p => p.Entry == (uint)entryOrGuid) .Set(p => p.AIName, "SmartAI") .Set(p => p.ScriptName, "") .UpdateAsync(); break; case SmartScriptType.GameObject: await model.GameObjectTemplate.Where(p => p.Entry == (uint)entryOrGuid) .Set(p => p.AIName, "SmartAI") .Set(p => p.ScriptName, "") .UpdateAsync(); break; case SmartScriptType.AreaTrigger: await model.AreaTriggerScript.Where(p => p.Id == entryOrGuid).DeleteAsync(); await model.AreaTriggerScript.InsertAsync(() => new MySqlAreaTriggerScript() { Id = entryOrGuid, ScriptName = "SmartTrigger" }); break; case SmartScriptType.AreaTriggerEntity: await model.AreaTriggerTemplate.Where(p => p.Id == (uint)entryOrGuid && p.IsServerSide == false) .Set(p => p.ScriptName, "SmartAreaTriggerAI") .UpdateAsync(); break; case SmartScriptType.AreaTriggerEntityServerSide: await model.AreaTriggerTemplate.Where(p => p.Id == (uint)entryOrGuid && p.IsServerSide == true) .Set(p => p.ScriptName, "SmartAreaTriggerAI") .UpdateAsync(); break; } await model.SmartScript.BulkCopyAsync(script.Select(l => new MySqlSmartScriptLine(l))); await model.CommitTransactionAsync(); }
public async Task InstallConditions(IEnumerable <IConditionLine> conditionLines, IDatabaseProvider.ConditionKeyMask keyMask, IDatabaseProvider.ConditionKey?manualKey = null) { using var writeLock = await MySqlSingleWriteLock.WriteLock(); await using var model = new TrinityDatabase(); var conditions = conditionLines?.ToList() ?? new List <IConditionLine>(); List <(int SourceType, int?SourceGroup, int?SourceEntry, int?SourceId)> keys = conditions.Select(c => (c.SourceType, keyMask.HasFlag(IDatabaseProvider.ConditionKeyMask.SourceGroup) ? (int?)c.SourceGroup : null, keyMask.HasFlag(IDatabaseProvider.ConditionKeyMask.SourceEntry) ? (int?)c.SourceEntry : null, keyMask.HasFlag(IDatabaseProvider.ConditionKeyMask.SourceId) ? (int?)c.SourceId : null)) .Union(manualKey.HasValue ? new[]
public async Task ExecuteSql(string query) { using var writeLock = await MySqlSingleWriteLock.WriteLock(); string connStr = settings.ConnectionStrings.First().ConnectionString; MySqlConnection conn = new(connStr); MySqlTransaction transaction; try { conn.Open(); transaction = await conn.BeginTransactionAsync(); } catch (Exception e) { throw new IMySqlExecutor.CannotConnectToDatabaseException(e); } try { MySqlCommand cmd = new(query, conn, transaction); await cmd.ExecuteNonQueryAsync(); await transaction.CommitAsync(); } catch (Exception ex) { await transaction.RollbackAsync(); await conn.CloseAsync(); throw new IMySqlExecutor.QueryFailedDatabaseException(ex); } await conn.CloseAsync(); }