Locks added in MySQLConnector

main
Inga 🏳‍🌈 15 years ago
parent 024f4c5c4e
commit b401ed57f6
  1. 115
      MySQLConnector/Connection.cs

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

Loading…
Cancel
Save