@ -32,46 +32,48 @@ namespace FLocal.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 ) {
command . CommandType = System . Data . CommandType . Text ;
lock ( this ) {
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 ( 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 = "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 ) ;
}
}
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 ( DbDataReader reader = command . ExecuteReader ( ) ) {
using ( DbDataReader 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 + + ) {
// throw new CriticalException("Name: " + reader.GetName(i));
// throw new CriticalException("Name: " + reader.GetName(i));
object value = reader . GetValue ( i ) ;
object value = reader . GetValue ( i ) ;
string sValue ;
string sValue ;
if ( value is DateTime ) {
if ( value is DateTime ) {
sValue = ( ( DateTime ) value ) . Ticks . ToString ( ) ;
sValue = ( ( DateTime ) value ) . Ticks . ToString ( ) ;
} else {
} else {
sValue = value . ToString ( ) ;
sValue = value . ToString ( ) ;
}
row . Add ( reader . GetName ( i ) , sValue ) ;
}
}
row . Add ( reader . GetName ( i ) , sValue ) ;
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 < Dictionary < string , string > > LoadByIds ( ITableSpec table , List < string > ids ) {
public List < Dictionary < string , string > > LoadByIds ( ITableSpec table , List < string > ids ) {
@ -82,74 +84,77 @@ namespace FLocal.MySQLConnector {
}
}
}
}
public List < string > LoadIdsByConditions ( ITableSpec table , FLocal . Core . DB . conditions . AbstractCondition conditions , Diapasone diapasone , JoinSpec [ ] joins , SortSpec [ ] sorts , bool allowHugeLists ) {
private List < string > _L oadIdsByConditions ( DbCommand command , ITableSpec table , FLocal . Core . DB . conditions . AbstractCondition conditions , Diapasone diapasone , JoinSpec [ ] joins , SortSpec [ ] sorts , bool allowHugeLists ) {
lock ( this ) {
command . CommandType = System . Data . CommandType . Text ;
using ( DbCommand command = this . connection . CreateCommand ( ) ) {
command . CommandType = System . Data . CommandType . Text ;
var conditionsCompiled = ConditionCompiler . Compile ( conditions , this . traits ) ;
var conditionsCompiled = ConditionCompiler . Compile ( conditions , this . traits ) ;
string queryConditions = "" ;
string queryConditions = "" ;
if ( conditionsCompiled . Key ! = "" ) queryConditions = "WHERE " + conditionsCompiled . Key ;
if ( conditionsCompiled . Key ! = "" ) queryConditions = "WHERE " + conditionsCompiled . Key ;
ParamsHolder paramsHolder = conditionsCompiled . Value ;
ParamsHolder paramsHolder = conditionsCompiled . Value ;
string queryJoins = "" ;
string queryJoins = "" ;
{
{
if ( joins . Length > 0 ) {
if ( joins . Length > 0 ) {
throw new NotImplementedException ( ) ;
throw new NotImplementedException ( ) ;
}
}
}
}
string querySorts = "" ;
string querySorts = "" ;
if ( sorts . Length > 0 ) {
if ( sorts . Length > 0 ) {
List < string > sortParts = new List < string > ( ) ;
List < string > sortParts = new List < string > ( ) ;
foreach ( SortSpec sortSpec in sorts ) {
foreach ( SortSpec sortSpec in sorts ) {
if ( sortSpec . ascending ) {
if ( sortSpec . ascending ) {
sortParts . Add ( sortSpec . column . compile ( this . traits ) + " ASC" ) ;
sortParts . Add ( sortSpec . column . compile ( this . traits ) + " ASC" ) ;
} else {
} else {
sortParts . Add ( sortSpec . column . compile ( this . traits ) + " DESC" ) ;
sortParts . Add ( sortSpec . column . compile ( this . traits ) + " DESC" ) ;
}
}
querySorts = "ORDER BY " + string . Join ( ", " , sortParts . ToArray ( ) ) ;
}
}
}
querySorts = "ORDER BY " + string . Join ( ", " , sortParts . ToArray ( ) ) ;
}
string queryMain = "FROM " + table . compile ( this . traits ) + " " + queryJoins + " " + queryConditions ;
string queryMain = "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 ) ;
}
}
command . CommandText = "SELECT COUNT(*) " + queryMain ;
command . CommandText = "SELECT COUNT(*) " + queryMain ;
object rawCount ;
object rawCount ;
//try {
//try {
rawCount = command . ExecuteScalar ( ) ;
rawCount = command . ExecuteScalar ( ) ;
//} catch(Npgsql.NpgsqlException e) {
//} catch(Npgsql.NpgsqlException e) {
//throw new FLocalException("Error while trying to execute " + command.CommandText + ": " + e.Message);
//throw new FLocalException("Error while trying to execute " + command.CommandText + ": " + e.Message);
//}
//}
long count = ( long ) rawCount ;
long count = ( long ) rawCount ;
if ( count < 1 ) {
if ( count < 1 ) {
diapasone . total = 0 ;
diapasone . total = 0 ;
return new List < string > ( ) ;
return new List < string > ( ) ;
} else {
} else {
diapasone . total = count ;
diapasone . total = count ;
if ( diapasone . total > 1 0 0 0 & & diapasone . count < 0 & & ! allowHugeLists ) {
if ( diapasone . total > 1 0 0 0 & & diapasone . count < 0 & & ! allowHugeLists ) {
throw new CriticalException ( "huge list" ) ;
throw new CriticalException ( "huge list" ) ;
}
}
string queryLimits = "" ;
string queryLimits = "" ;
if ( diapasone . count > = 0 ) {
if ( diapasone . count > = 0 ) {
queryLimits = "LIMIT " + diapasone . count + " OFFSET " + diapasone . start ;
queryLimits = "LIMIT " + diapasone . count + " OFFSET " + diapasone . start ;
}
}
command . CommandText = "SELECT " + table . compile ( this . traits ) + ".* " + queryMain + " " + querySorts + " " + queryLimits ;
command . CommandText = "SELECT " + table . compile ( this . traits ) + ".* " + queryMain + " " + querySorts + " " + queryLimits ;
List < string > result = new List < string > ( ) ;
List < string > result = new List < string > ( ) ;
using ( DbDataReader reader = command . ExecuteReader ( ) ) {
using ( DbDataReader reader = command . ExecuteReader ( ) ) {
while ( reader . Read ( ) ) {
while ( reader . Read ( ) ) {
result . Add ( reader . GetValue ( 0 ) . ToString ( ) ) ;
result . Add ( reader . GetValue ( 0 ) . ToString ( ) ) ;
}
}
return result ;
}
}
}
}
return result ;
}
}
public List < string > LoadIdsByConditions ( ITableSpec table , FLocal . Core . DB . conditions . AbstractCondition conditions , Diapasone diapasone , JoinSpec [ ] joins , SortSpec [ ] sorts , bool allowHugeLists ) {
lock ( this ) {
using ( DbCommand command = this . connection . CreateCommand ( ) ) {
return this . _L oadIdsByConditions ( command , table , conditions , diapasone , joins , sorts , allowHugeLists ) ;
}
}
}
}
}
@ -231,6 +236,16 @@ namespace FLocal.MySQLConnector {
}
}
}
}
public List < string > LoadIdsByConditions ( FLocal . Core . DB . Transaction _ transaction , ITableSpec table , FLocal . Core . DB . conditions . AbstractCondition conditions , Diapasone diapasone , JoinSpec [ ] joins , SortSpec [ ] sorts , bool allowHugeLists ) {
Transaction transaction = ( Transaction ) _ transaction ;
lock ( this ) {
using ( DbCommand command = transaction . sqlconnection . CreateCommand ( ) ) {
command . Transaction = transaction . sqltransaction ;
return this . _L oadIdsByConditions ( command , table , conditions , diapasone , joins , sorts , allowHugeLists ) ;
}
}
}
public void update ( FLocal . Core . DB . Transaction _ transaction , ITableSpec table , string id , Dictionary < string , string > data ) {
public void update ( FLocal . Core . DB . Transaction _ transaction , ITableSpec table , string id , Dictionary < string , string > data ) {
Transaction transaction = ( Transaction ) _ transaction ;
Transaction transaction = ( Transaction ) _ transaction ;
lock ( transaction ) {
lock ( transaction ) {