@ -12,13 +12,16 @@ namespace MySQLConnector {
internal readonly IDBTraits traits ;
internal readonly IDBTraits traits ;
internal readonly ILogger logger ;
// private DbConnection connection;
// private DbConnection connection;
private string connectionString ;
private string connectionString ;
private HashSet < Transaction > transactions ;
private HashSet < Transaction > transactions ;
public Connection ( string connectionString , IDBTraits traits ) {
public Connection ( string connectionString , IDBTraits traits , ILogger logger ) {
this . traits = traits ;
this . traits = traits ;
this . logger = logger ;
this . connectionString = connectionString ;
this . connectionString = connectionString ;
using ( DbConnection connection = this . createConnection ( ) ) {
using ( DbConnection connection = this . createConnection ( ) ) {
//just testing we can open a connection
//just testing we can open a connection
@ -26,6 +29,10 @@ namespace MySQLConnector {
this . transactions = new HashSet < Transaction > ( ) ;
this . transactions = new HashSet < Transaction > ( ) ;
}
}
private CommandExecutionLogger CreateCommandExecutionLogger ( ) {
return new CommandExecutionLogger ( this . logger ) ;
}
internal DbConnection createConnection ( ) {
internal DbConnection createConnection ( ) {
DbConnection connection = this . traits . createConnection ( this . connectionString ) ;
DbConnection connection = this . traits . createConnection ( this . connectionString ) ;
connection . Open ( ) ;
connection . Open ( ) ;
@ -33,6 +40,8 @@ namespace MySQLConnector {
}
}
private List < Dictionary < string , string > > _L oadByIds ( DbCommand command , ITableSpec table , List < string > ids , bool forUpdate ) {
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 ;
command . CommandType = System . Data . CommandType . Text ;
ParamsHolder paramsHolder = new ParamsHolder ( ) ;
ParamsHolder paramsHolder = new ParamsHolder ( ) ;
@ -41,7 +50,7 @@ namespace MySQLConnector {
placeholder . Add ( this . traits . markParam ( paramsHolder . Add ( id ) ) ) ;
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();
//command.Prepare();
foreach ( KeyValuePair < string , string > kvp in paramsHolder . data ) {
foreach ( KeyValuePair < string , string > kvp in paramsHolder . data ) {
command . AddParameter ( kvp . Key , kvp . Value ) ;
command . AddParameter ( kvp . Key , kvp . Value ) ;
@ -76,6 +85,7 @@ namespace MySQLConnector {
}
}
return result ;
return result ;
}
}
}
public List < Dictionary < string , string > > LoadByIds ( ITableSpec table , List < string > ids ) {
public List < Dictionary < string , string > > LoadByIds ( ITableSpec table , List < string > ids ) {
using ( DbConnection connection = this . createConnection ( ) ) {
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 ) {
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 ;
command . CommandType = System . Data . CommandType . Text ;
var conditionsCompiled = ConditionCompiler . Compile ( conditions , this . traits ) ;
var conditionsCompiled = ConditionCompiler . Compile ( conditions , this . traits ) ;
@ -126,7 +138,7 @@ namespace MySQLConnector {
command . AddParameter ( kvp . Key , kvp . Value ) ;
command . AddParameter ( kvp . Key , kvp . Value ) ;
}
}
command . CommandText = "SELECT COUNT(*) " + queryMain ;
command . CommandText = logger . c ommandText = "SELECT COUNT(*) " + queryMain ;
object rawCount ;
object rawCount ;
//try {
//try {
rawCount = command . ExecuteScalar ( ) ;
rawCount = command . ExecuteScalar ( ) ;
@ -157,6 +169,7 @@ namespace MySQLConnector {
return result ;
return result ;
}
}
}
}
}
public List < string > LoadIdsByConditions ( ITableSpec table , Web . Core . DB . conditions . AbstractCondition conditions , Diapasone diapasone , JoinSpec [ ] joins , SortSpec [ ] sorts , bool allowHugeLists ) {
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 ( ) ) {
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 ) {
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 ( DbConnection connection = this . createConnection ( ) ) {
using ( DbCommand command = connection . CreateCommand ( ) ) {
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 ) {
foreach ( KeyValuePair < string , string > kvp in paramsHolder . data ) {
command . AddParameter ( kvp . Key , kvp . Value ) ;
command . AddParameter ( kvp . Key , kvp . Value ) ;
}
}
@ -195,6 +209,7 @@ namespace MySQLConnector {
}
}
}
}
}
}
}
public Web . Core . DB . Transaction beginTransaction ( System . Data . IsolationLevel iso ) {
public Web . Core . DB . Transaction beginTransaction ( System . Data . IsolationLevel iso ) {
lock ( this ) {
lock ( this ) {
@ -210,29 +225,33 @@ namespace MySQLConnector {
}
}
public void lockTable ( Web . Core . DB . Transaction _ transaction , ITableSpec table ) {
public void lockTable ( Web . Core . DB . Transaction _ transaction , ITableSpec table ) {
using ( var logger = this . CreateCommandExecutionLogger ( ) ) {
Transaction transaction = ( Transaction ) _ transaction ;
Transaction transaction = ( Transaction ) _ transaction ;
lock ( transaction ) {
lock ( transaction ) {
using ( DbCommand command = transaction . sqlconnection . CreateCommand ( ) ) {
using ( DbCommand command = transaction . sqlconnection . CreateCommand ( ) ) {
command . Transaction = transaction . sqltransaction ;
command . Transaction = transaction . sqltransaction ;
command . CommandType = System . Data . CommandType . Text ;
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 ( ) ;
command . ExecuteNonQuery ( ) ;
}
}
}
}
}
}
}
public void lockRow ( Web . Core . DB . Transaction _ transaction , ITableSpec table , string id ) {
public void lockRow ( Web . Core . DB . Transaction _ transaction , ITableSpec table , string id ) {
using ( var logger = this . CreateCommandExecutionLogger ( ) ) {
Transaction transaction = ( Transaction ) _ transaction ;
Transaction transaction = ( Transaction ) _ transaction ;
lock ( transaction ) {
lock ( transaction ) {
using ( DbCommand command = transaction . sqlconnection . CreateCommand ( ) ) {
using ( DbCommand command = transaction . sqlconnection . CreateCommand ( ) ) {
command . Transaction = transaction . sqltransaction ;
command . Transaction = transaction . sqltransaction ;
command . CommandType = System . Data . CommandType . Text ;
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 . AddParameter ( "id" , id ) ;
command . ExecuteNonQuery ( ) ;
command . ExecuteNonQuery ( ) ;
}
}
}
}
}
}
}
public List < Dictionary < string , string > > LoadByIds ( Web . Core . DB . Transaction _ transaction , ITableSpec table , List < string > ids ) {
public List < Dictionary < string , string > > LoadByIds ( Web . Core . DB . Transaction _ transaction , ITableSpec table , List < string > ids ) {
Transaction transaction = ( Transaction ) _ transaction ;
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 ) {
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 ;
Transaction transaction = ( Transaction ) _ transaction ;
lock ( transaction ) {
lock ( transaction ) {
using ( DbCommand command = transaction . sqlconnection . CreateCommand ( ) ) {
using ( DbCommand command = transaction . sqlconnection . CreateCommand ( ) ) {
@ -266,7 +286,7 @@ namespace MySQLConnector {
command . Transaction = transaction . sqltransaction ;
command . Transaction = transaction . sqltransaction ;
command . CommandType = System . Data . CommandType . Text ;
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 ) ;
command . AddParameter ( "id" , id ) ;
foreach ( KeyValuePair < string , string > kvp in paramsholder . data ) {
foreach ( KeyValuePair < string , string > kvp in paramsholder . data ) {
command . AddParameter ( kvp . Key , kvp . Value ) ;
command . AddParameter ( kvp . Key , kvp . Value ) ;
@ -276,8 +296,10 @@ namespace MySQLConnector {
}
}
}
}
}
}
}
public string insert ( Web . Core . DB . Transaction _ transaction , ITableSpec table , Dictionary < string , string > data ) {
public string insert ( Web . Core . DB . Transaction _ transaction , ITableSpec table , Dictionary < string , string > data ) {
using ( var logger = this . CreateCommandExecutionLogger ( ) ) {
Transaction transaction = ( Transaction ) _ transaction ;
Transaction transaction = ( Transaction ) _ transaction ;
lock ( transaction ) {
lock ( transaction ) {
using ( DbCommand command = transaction . sqlconnection . CreateCommand ( ) ) {
using ( DbCommand command = transaction . sqlconnection . CreateCommand ( ) ) {
@ -291,7 +313,7 @@ namespace MySQLConnector {
command . Transaction = transaction . sqltransaction ;
command . Transaction = transaction . sqltransaction ;
command . CommandType = System . Data . CommandType . Text ;
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 ) {
foreach ( KeyValuePair < string , string > kvp in paramsholder . data ) {
command . AddParameter ( kvp . Key , kvp . Value ) ;
command . AddParameter ( kvp . Key , kvp . Value ) ;
}
}
@ -301,19 +323,22 @@ namespace MySQLConnector {
}
}
}
}
}
}
}
public void delete ( Web . Core . DB . Transaction _ transaction , ITableSpec table , string id ) {
public void delete ( Web . Core . DB . Transaction _ transaction , ITableSpec table , string id ) {
using ( var logger = this . CreateCommandExecutionLogger ( ) ) {
Transaction transaction = ( Transaction ) _ transaction ;
Transaction transaction = ( Transaction ) _ transaction ;
lock ( transaction ) {
lock ( transaction ) {
using ( DbCommand command = transaction . sqlconnection . CreateCommand ( ) ) {
using ( DbCommand command = transaction . sqlconnection . CreateCommand ( ) ) {
command . Transaction = transaction . sqltransaction ;
command . Transaction = transaction . sqltransaction ;
command . CommandType = System . Data . CommandType . Text ;
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 . AddParameter ( "id" , id ) ;
command . ExecuteNonQuery ( ) ;
command . ExecuteNonQuery ( ) ;
}
}
}
}
}
}
}
internal void RemoveTransaction ( Transaction transaction ) {
internal void RemoveTransaction ( Transaction transaction ) {
lock ( this ) {
lock ( this ) {