/* * Copyright (C) 2012-2019 CypherCore * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ using System.Collections.Generic; using System.Threading.Tasks; namespace Framework.Database { public class SQLQueryHolder { public Dictionary m_queries = new Dictionary(); Dictionary _results = new Dictionary(); public void SetQuery(T index, string sql, params object[] args) { SetQuery(index, new PreparedStatement(string.Format(sql, args))); } public void SetQuery(T index, PreparedStatement stmt) { m_queries[index] = stmt; } public void SetResult(T index, SQLResult result) { _results[index] = result; } public SQLResult GetResult(T index) { if (!_results.ContainsKey(index)) return new SQLResult(); return _results[index]; } } class SQLQueryHolderTask : ISqlOperation { SQLQueryHolder m_holder; TaskCompletionSource> m_result; public SQLQueryHolderTask(SQLQueryHolder holder) { m_holder = holder; m_result = new TaskCompletionSource>(); } public bool Execute(MySqlBase mySqlBase) { if (m_holder == null) return false; // execute all queries in the holder and pass the results foreach (var pair in m_holder.m_queries) m_holder.SetResult(pair.Key, mySqlBase.Query(pair.Value)); return m_result.TrySetResult(m_holder); } public Task> GetFuture() { return m_result.Task; } } }