From 0a13b1aba2bcb5dd3878bb293957ed91d79e9f34 Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Mon, 14 Jun 2010 10:55:42 +0000 Subject: [PATCH] LoadByIds while in transaction implemented in DBConnection and SQLConnector --- Core/DB/IDBConnection.cs | 2 + MySQLConnector/Connection.cs | 86 +++++++++++++++++++++--------------- 2 files changed, 52 insertions(+), 36 deletions(-) diff --git a/Core/DB/IDBConnection.cs b/Core/DB/IDBConnection.cs index 277699d..d2a45b5 100644 --- a/Core/DB/IDBConnection.cs +++ b/Core/DB/IDBConnection.cs @@ -18,6 +18,8 @@ namespace FLocal.Core.DB { void lockRow(Transaction transaction, ITableSpec table, string id); + List> LoadByIds(Transaction transaction, ITableSpec table, List ids); + void update(Transaction transaction, ITableSpec table, string id, Dictionary data); string insert(Transaction transaction, ITableSpec table, Dictionary data); diff --git a/MySQLConnector/Connection.cs b/MySQLConnector/Connection.cs index 332be19..d293a6d 100644 --- a/MySQLConnector/Connection.cs +++ b/MySQLConnector/Connection.cs @@ -31,49 +31,53 @@ namespace FLocal.MySQLConnector { return connection; } - public List> LoadByIds(ITableSpec table, List ids) { - lock(this) { - using(DbCommand command = this.connection.CreateCommand()) { - command.CommandType = System.Data.CommandType.Text; + private List> _LoadByIds(DbCommand command, ITableSpec table, List ids, bool forUpdate) { + command.CommandType = System.Data.CommandType.Text; - ParamsHolder paramsHolder = new ParamsHolder(); - List placeholder = new List(); - foreach(string id in ids) { - placeholder.Add(this.traits.markParam(paramsHolder.Add(id))); - } + ParamsHolder paramsHolder = new ParamsHolder(); + List placeholder = new List(); + foreach(string id in ids) { + placeholder.Add(this.traits.markParam(paramsHolder.Add(id))); + } - command.CommandText = "SELECT * FROM " + table.compile(this.traits) + " WHERE " + table.getIdSpec().compile(this.traits) + " IN (" + string.Join(", ", placeholder.ToArray()) + ")"; - //command.Prepare(); - foreach(KeyValuePair kvp in paramsHolder.data) { - command.AddParameter(kvp.Key, kvp.Value); - } + command.CommandText = "SELECT * FROM " + table.compile(this.traits) + " WHERE " + table.getIdSpec().compile(this.traits) + " IN (" + string.Join(", ", placeholder.ToArray()) + ")" + (forUpdate ? " FOR UPDATE" : ""); + //command.Prepare(); + foreach(KeyValuePair kvp in paramsHolder.data) { + command.AddParameter(kvp.Key, kvp.Value); + } - Dictionary> rawResult = new Dictionary>(); - using(DbDataReader reader = command.ExecuteReader()) { - while(reader.Read()) { - Dictionary row = new Dictionary(); - for(int i=0; i> rawResult = new Dictionary>(); + using(DbDataReader 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]); - } - } - return result; + List> result = new List>(); + foreach(string id in ids) { + if(rawResult.ContainsKey(id)) { + result.Add(rawResult[id]); + } + } + return result; + } + + public List> LoadByIds(ITableSpec table, List ids) { + lock(this) { + using(DbCommand command = this.connection.CreateCommand()) { + return this._LoadByIds(command, table, ids, false); } } } @@ -209,6 +213,16 @@ namespace FLocal.MySQLConnector { } } + public List> LoadByIds(FLocal.Core.DB.Transaction _transaction, ITableSpec table, List ids) { + Transaction transaction = (Transaction)_transaction; + lock(transaction) { + using(DbCommand command = transaction.sqlconnection.CreateCommand()) { + command.Transaction = transaction.sqltransaction; + return this._LoadByIds(command, table, ids, true); + } + } + } + public void update(FLocal.Core.DB.Transaction _transaction, ITableSpec table, string id, Dictionary data) { Transaction transaction = (Transaction)_transaction; lock(transaction) {