@ -12,13 +12,16 @@ namespace MySQLConnector {
internal readonly IDBTraits traits ;
internal readonly ILogger logger ;
// private DbConnection connection;
private string connectionString ;
private HashSet < Transaction > transactions ;
public Connection ( string connectionString , IDBTraits traits ) {
public Connection ( string connectionString , IDBTraits traits , ILogger logger ) {
this . traits = traits ;
this . logger = logger ;
this . connectionString = connectionString ;
using ( DbConnection connection = this . createConnection ( ) ) {
//just testing we can open a connection
@ -26,6 +29,10 @@ namespace MySQLConnector {
this . transactions = new HashSet < Transaction > ( ) ;
}
private CommandExecutionLogger CreateCommandExecutionLogger ( ) {
return new CommandExecutionLogger ( this . logger ) ;
}
internal DbConnection createConnection ( ) {
DbConnection connection = this . traits . createConnection ( this . connectionString ) ;
connection . Open ( ) ;
@ -33,6 +40,8 @@ namespace MySQLConnector {
}
private List < Dictionary < string , string > > _L oadByIds ( DbCommand command , ITableSpec table , List < string > ids , bool forUpdate ) {
using ( var logger = this . CreateCommandExecutionLogger ( ) ) {
command . CommandType = System . Data . CommandType . Text ;
ParamsHolder paramsHolder = new ParamsHolder ( ) ;
@ -41,7 +50,7 @@ namespace MySQLConnector {
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 ( ) ) + ")" + ( forUpdate ? " FOR UPDATE" : "" ) ;
command . CommandText = logger . c ommandText = "SELECT * FROM " + table . compile ( this . traits ) + " WHERE " + table . getIdSpec ( ) . compile ( this . traits ) + " IN (" + string . Join ( ", " , placeholder . ToArray ( ) ) + ")" + ( forUpdate ? " FOR UPDATE" : "" ) ;
//command.Prepare();
foreach ( KeyValuePair < string , string > kvp in paramsHolder . data ) {
command . AddParameter ( kvp . Key , kvp . Value ) ;
@ -50,9 +59,9 @@ namespace MySQLConnector {
Dictionary < string , Dictionary < string , string > > rawResult = new Dictionary < string , Dictionary < string , string > > ( ) ;
using ( DbDataReader reader = command . ExecuteReader ( ) ) {
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 + + ) {
// throw new CriticalException("Name: " + reader.GetName(i));
//throw new CriticalException("Name: " + reader.GetName(i));
object value = reader . GetValue ( i ) ;
string sValue ;
if ( value is DateTime ) {
@ -68,7 +77,7 @@ namespace MySQLConnector {
}
}
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 ) {
if ( rawResult . ContainsKey ( id ) ) {
result . Add ( rawResult [ id ] ) ;
@ -76,6 +85,7 @@ namespace MySQLConnector {
}
return result ;
}
}
public List < Dictionary < string , string > > LoadByIds ( ITableSpec table , List < string > ids ) {
using ( DbConnection connection = this . createConnection ( ) ) {
@ -86,6 +96,8 @@ namespace MySQLConnector {
}
private List < string > _L oadIdsByConditions ( DbCommand command , ITableSpec table , Web . Core . DB . conditions . AbstractCondition conditions , Diapasone diapasone , JoinSpec [ ] joins , SortSpec [ ] sorts , bool allowHugeLists ) {
using ( var logger = this . CreateCommandExecutionLogger ( ) ) {
command . CommandType = System . Data . CommandType . Text ;
var conditionsCompiled = ConditionCompiler . Compile ( conditions , this . traits ) ;
@ -126,7 +138,7 @@ namespace MySQLConnector {
command . AddParameter ( kvp . Key , kvp . Value ) ;
}
command . CommandText = "SELECT COUNT(*) " + queryMain ;
command . CommandText = logger . c ommandText = "SELECT COUNT(*) " + queryMain ;
object rawCount ;
//try {
rawCount = command . ExecuteScalar ( ) ;
@ -157,6 +169,7 @@ namespace MySQLConnector {
return result ;
}
}
}
public List < string > LoadIdsByConditions ( ITableSpec table , Web . Core . DB . conditions . AbstractCondition conditions , Diapasone diapasone , JoinSpec [ ] joins , SortSpec [ ] sorts , bool allowHugeLists ) {
using ( DbConnection connection = this . createConnection ( ) ) {
@ -167,6 +180,7 @@ namespace MySQLConnector {
}
public long GetCountByConditions ( ITableSpec table , Web . Core . DB . conditions . AbstractCondition conditions , params JoinSpec [ ] joins ) {
using ( var logger = this . CreateCommandExecutionLogger ( ) ) {
using ( DbConnection connection = this . createConnection ( ) ) {
using ( DbCommand command = connection . CreateCommand ( ) ) {
@ -185,7 +199,7 @@ namespace MySQLConnector {
}
command . CommandText = "SELECT COUNT(*) " + "FROM " + table . compile ( this . traits ) + " " + queryJoins + " " + queryConditions ;
command . CommandText = logger . c ommandText = "SELECT COUNT(*) " + "FROM " + table . compile ( this . traits ) + " " + queryJoins + " " + queryConditions ;
foreach ( KeyValuePair < string , string > kvp in paramsHolder . data ) {
command . AddParameter ( kvp . Key , kvp . Value ) ;
}
@ -195,6 +209,7 @@ namespace MySQLConnector {
}
}
}
}
public Web . Core . DB . Transaction beginTransaction ( System . Data . IsolationLevel iso ) {
lock ( this ) {
@ -210,29 +225,33 @@ namespace MySQLConnector {
}
public void lockTable ( Web . Core . DB . Transaction _ transaction , ITableSpec table ) {
using ( var logger = this . CreateCommandExecutionLogger ( ) ) {
Transaction transaction = ( Transaction ) _ transaction ;
lock ( transaction ) {
using ( DbCommand command = transaction . sqlconnection . CreateCommand ( ) ) {
command . Transaction = transaction . sqltransaction ;
command . CommandType = System . Data . CommandType . Text ;
command . CommandText = "LOCK TABLE " + table . compile ( this . traits ) ;
command . CommandText = logger . c ommandText = "LOCK TABLE " + table . compile ( this . traits ) ;
command . ExecuteNonQuery ( ) ;
}
}
}
}
public void lockRow ( Web . Core . DB . Transaction _ transaction , ITableSpec table , string id ) {
using ( var logger = this . CreateCommandExecutionLogger ( ) ) {
Transaction transaction = ( Transaction ) _ transaction ;
lock ( transaction ) {
using ( DbCommand command = transaction . sqlconnection . CreateCommand ( ) ) {
command . Transaction = transaction . sqltransaction ;
command . CommandType = System . Data . CommandType . Text ;
command . CommandText = "SELECT * FROM " + table . compile ( this . traits ) + " where " + table . getIdSpec ( ) . compile ( this . traits ) + " = " + this . traits . markParam ( "id" ) + " FOR UPDATE" ;
command . CommandText = logger . c ommandText = "SELECT * FROM " + table . compile ( this . traits ) + " where " + table . getIdSpec ( ) . compile ( this . traits ) + " = " + this . traits . markParam ( "id" ) + " FOR UPDATE" ;
command . AddParameter ( "id" , id ) ;
command . ExecuteNonQuery ( ) ;
}
}
}
}
public List < Dictionary < string , string > > LoadByIds ( Web . Core . DB . Transaction _ transaction , ITableSpec table , List < string > ids ) {
Transaction transaction = ( Transaction ) _ transaction ;
@ -255,6 +274,7 @@ namespace MySQLConnector {
}
public void update ( Web . Core . DB . Transaction _ transaction , ITableSpec table , string id , Dictionary < string , string > data ) {
using ( var logger = this . CreateCommandExecutionLogger ( ) ) {
Transaction transaction = ( Transaction ) _ transaction ;
lock ( transaction ) {
using ( DbCommand command = transaction . sqlconnection . CreateCommand ( ) ) {
@ -266,18 +286,20 @@ namespace MySQLConnector {
command . Transaction = transaction . sqltransaction ;
command . CommandType = System . Data . CommandType . Text ;
command . CommandText = "UPDATE " + table . compile ( traits ) + " set " + String . Join ( ", " , updates . ToArray ( ) ) + " where " + table . getIdSpec ( ) . compile ( this . traits ) + " = " + this . traits . markParam ( "id" ) ;
command . CommandText = logger . c ommandText = "UPDATE " + table . compile ( traits ) + " set " + String . Join ( ", " , updates . ToArray ( ) ) + " where " + table . getIdSpec ( ) . compile ( this . traits ) + " = " + this . traits . markParam ( "id" ) ;
command . AddParameter ( "id" , id ) ;
foreach ( KeyValuePair < string , string > kvp in paramsholder . data ) {
command . AddParameter ( kvp . Key , kvp . Value ) ;
}
// throw new CriticalException(command.CommandText + "; parameters: " + string.Join(", ", (from DbParameter parameter in command.Parameters select parameter.ParameterName + "='" + parameter.Value.ToString() + "'").ToArray()));
// throw new CriticalException(command.CommandText + "; parameters: " + string.Join(", ", (from DbParameter parameter in command.Parameters select parameter.ParameterName + "='" + parameter.Value.ToString() + "'").ToArray()));
command . ExecuteNonQuery ( ) ;
}
}
}
}
public string insert ( Web . Core . DB . Transaction _ transaction , ITableSpec table , Dictionary < string , string > data ) {
using ( var logger = this . CreateCommandExecutionLogger ( ) ) {
Transaction transaction = ( Transaction ) _ transaction ;
lock ( transaction ) {
using ( DbCommand command = transaction . sqlconnection . CreateCommand ( ) ) {
@ -291,7 +313,7 @@ namespace MySQLConnector {
command . Transaction = transaction . sqltransaction ;
command . CommandType = System . Data . CommandType . Text ;
command . CommandText = "INSERT INTO " + table . compile ( this . traits ) + " (" + String . Join ( ", " , updates . ToArray ( ) ) + ") VALUES (" + String . Join ( ", " , updatesPlaceholders . ToArray ( ) ) + ")" ;
command . CommandText = logger . c ommandText = "INSERT INTO " + table . compile ( this . traits ) + " (" + String . Join ( ", " , updates . ToArray ( ) ) + ") VALUES (" + String . Join ( ", " , updatesPlaceholders . ToArray ( ) ) + ")" ;
foreach ( KeyValuePair < string , string > kvp in paramsholder . data ) {
command . AddParameter ( kvp . Key , kvp . Value ) ;
}
@ -301,19 +323,22 @@ namespace MySQLConnector {
}
}
}
}
public void delete ( Web . Core . DB . Transaction _ transaction , ITableSpec table , string id ) {
using ( var logger = this . CreateCommandExecutionLogger ( ) ) {
Transaction transaction = ( Transaction ) _ transaction ;
lock ( transaction ) {
using ( DbCommand command = transaction . sqlconnection . CreateCommand ( ) ) {
command . Transaction = transaction . sqltransaction ;
command . CommandType = System . Data . CommandType . Text ;
command . CommandText = "DELETE FROM " + table . compile ( traits ) + " where " + table . getIdSpec ( ) . compile ( this . traits ) + " = " + this . traits . markParam ( "id" ) ;
command . CommandText = logger . c ommandText = "DELETE FROM " + table . compile ( traits ) + " where " + table . getIdSpec ( ) . compile ( this . traits ) + " = " + this . traits . markParam ( "id" ) ;
command . AddParameter ( "id" , id ) ;
command . ExecuteNonQuery ( ) ;
}
}
}
}
internal void RemoveTransaction ( Transaction transaction ) {
lock ( this ) {