diff --git a/MySQLConnector/Connection.cs b/MySQLConnector/Connection.cs index f23cbf7..1af3975 100644 --- a/MySQLConnector/Connection.cs +++ b/MySQLConnector/Connection.cs @@ -27,84 +27,87 @@ namespace FLocal.MySQLConnector { } public List> LoadByIds(ITableSpec table, List ids) { - using(MySqlCommand command = this.connection.CreateCommand()) { - command.CommandType = System.Data.CommandType.Text; + lock(this) { + using(MySqlCommand command = this.connection.CreateCommand()) { + command.CommandType = System.Data.CommandType.Text; - ParamsHolder paramsHolder = new ParamsHolder(); - List placeholder = new List(); - foreach(string id in ids) { - placeholder.Add("@" + paramsHolder.Add(id)); - } + ParamsHolder paramsHolder = new ParamsHolder(); + List placeholder = new List(); + foreach(string id in ids) { + placeholder.Add("@" + paramsHolder.Add(id)); + } - command.CommandText = "SELECT * FROM " + table.compile() + " WHERE " + table.getIdSpec().compile() + " IN (" + string.Join(", ", placeholder.ToArray()) + ")"; - foreach(KeyValuePair kvp in paramsHolder.data) { - command.Parameters.AddWithValue(kvp.Key, kvp.Value); - } + command.CommandText = "SELECT * FROM " + table.compile() + " WHERE " + table.getIdSpec().compile() + " IN (" + string.Join(", ", placeholder.ToArray()) + ")"; + foreach(KeyValuePair kvp in paramsHolder.data) { + command.Parameters.AddWithValue(kvp.Key, kvp.Value); + } - Dictionary> rawResult = new Dictionary>(); - using(MySqlDataReader reader = command.ExecuteReader()) { - while(reader.Read()) { - Dictionary row = new Dictionary(); - for(int i=0; i> rawResult = new Dictionary>(); + using(MySqlDataReader reader = command.ExecuteReader()) { + while(reader.Read()) { + Dictionary row = new Dictionary(); + for(int i=0; i> result = new List>(); - foreach(string id in ids) { - if(rawResult.ContainsKey(id)) { - result.Add(rawResult[id]); + List> result = new List>(); + foreach(string id in ids) { + if(rawResult.ContainsKey(id)) { + result.Add(rawResult[id]); + } } + return result; } - return result; } } public List LoadIdsByConditions(ITableSpec table, FLocal.Core.DB.conditions.AbstractCondition conditions, Diapasone diapasone, JoinSpec[] joins, SortSpec[] sorts) { + lock(this) { + using(MySqlCommand command = this.connection.CreateCommand()) { - using(MySqlCommand command = this.connection.CreateCommand()) { - - command.CommandType = System.Data.CommandType.Text; - - var conditionsCompiled = ConditionCompiler.Compile(conditions); - string queryConditions = conditionsCompiled.Key; - ParamsHolder paramsHolder = conditionsCompiled.Value; + command.CommandType = System.Data.CommandType.Text; - string queryJoins = ""; + var conditionsCompiled = ConditionCompiler.Compile(conditions); + string queryConditions = conditionsCompiled.Key; + ParamsHolder paramsHolder = conditionsCompiled.Value; - string querySorts = ""; - { - } + string queryJoins = ""; - string queryMain = "FROM " + table.compile() + " " + queryJoins + " WHERE " + queryConditions; + string querySorts = ""; + { + } - foreach(KeyValuePair kvp in paramsHolder.data) { - command.Parameters.AddWithValue(kvp.Key, kvp.Value); - } + string queryMain = "FROM " + table.compile() + " " + queryJoins + " WHERE " + queryConditions; - command.CommandText = "SELECT COUNT(*) " + queryMain; - object rawCount = command.ExecuteScalar(); - int count = (int)rawCount; - if(count < 1) { - diapasone.total = 0; - return new List(); - } else { - diapasone.total = count; - string queryLimits = ""; - if(diapasone.count >= 0) { - queryLimits = "LIMIT " + diapasone.count + " OFFSET " + diapasone.start; + foreach(KeyValuePair kvp in paramsHolder.data) { + command.Parameters.AddWithValue(kvp.Key, kvp.Value); } - command.CommandText = "SELECT " + table.compile() + ".* " + queryMain + " " + querySorts + " " + queryLimits; - List result = new List(); - using(MySqlDataReader reader = command.ExecuteReader()) { - while(reader.Read()) { - result.Add(reader.GetString(0)); + command.CommandText = "SELECT COUNT(*) " + queryMain; + object rawCount = command.ExecuteScalar(); + int count = (int)rawCount; + if(count < 1) { + diapasone.total = 0; + return new List(); + } else { + diapasone.total = count; + string queryLimits = ""; + if(diapasone.count >= 0) { + queryLimits = "LIMIT " + diapasone.count + " OFFSET " + diapasone.start; } + command.CommandText = "SELECT " + table.compile() + ".* " + queryMain + " " + querySorts + " " + queryLimits; + + List result = new List(); + using(MySqlDataReader reader = command.ExecuteReader()) { + while(reader.Read()) { + result.Add(reader.GetString(0)); + } + } + return result; } - return result; } } }