/* 
   MySQLSQLExpression.m

   Copyright (C) 2000 Free Software Foundation, Inc.

   Author: Stephen Peters <portnoy@portnoy.org>
	      based on the Postgres95 Adaptor written by 
	   Mircea Oancea <mircea@jupiter.elcom.pub.ro>
   Date: August 2000

   This file is part of the GNUstep Database Library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; see the file COPYING.LIB.
   If not, write to the Free Software Foundation,
   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include <Foundation/NSUtilities.h>

#include <extensions/NSException.h>

#include <eoaccess/EOEntity.h>
#include <eoaccess/EORelationship.h>
#include <eoaccess/EOAttribute.h>
#include <eoaccess/EOAttributeOrdering.h>

#include <eoadaptors/MySQL/MySQLExceptions.h>
#include <eoadaptors/MySQL/MySQLSQLExpression.h>
#include <eoaccess/EOAdaptor.h>

@implementation MySQLSQLExpression

//Serveur does not accept where clause like field = null
//we replace this by field is null
+(NSString*)convertWhereClause:(NSString*)whereClause
{
    if (whereClause) {
	NSString* _replace=@"= NULL";
	NSRange range=[whereClause rangeOfString:_replace];
	if (range.length>0) {
	    NSString* _by=@"is null";
	    int byLen=[_by length];
	    whereClause=[whereClause mutableCopy];
	    do {
		[(NSMutableString*)whereClause replaceCharactersInRange:range
				   withString: _by];
		range.location+=byLen;
		range.length=[whereClause length]-range.location;
		range=[whereClause rangeOfString: _replace
				   options: 0
				   range: range];
	    }
	    while (range.length>0);
	};
    };
    return whereClause;
}


+ (Class)selectExpressionClass
{
    return [MySQLSelectSQLExpression class];
}

+ (Class)deleteExpressionClass 
{
    return [MySQLDeleteSQLExpression class];
}

+ (Class)updateExpressionClass
{
    return [MySQLUpdateSQLExpression class];
}

@end /* MySQLSQLExpression */


@implementation MySQLSelectSQLExpression

- selectExpressionForAttributes:(NSArray*)attributes
  lock:(BOOL)flag
  qualifier:(EOQualifier*)qualifier
  fetchOrder:(NSArray*)fetchOrder
  channel:(EOAdaptorChannel*)channel
{
    lock = flag;
    [super selectExpressionForAttributes:attributes
	   lock:flag
	   qualifier:qualifier
	   fetchOrder:fetchOrder
	   channel:channel];
    return self;
}

//Serveur does not accept where clause like field = null
//we replace this by field is null
- (NSString*)whereClauseForQualifier:(EOQualifier*)qualifier
  joinExpression:(NSString*)joinExpression
{
  NSString* whereClause=[super whereClauseForQualifier:qualifier
							   joinExpression:joinExpression];
  whereClause=[MySQLSQLExpression convertWhereClause:whereClause];
  return whereClause;
}


@end /* MySQLSelectSQLExpression */

@implementation MySQLDeleteSQLExpression : EODeleteSQLExpression
//Serveur does not accept where clause like field = null
//we replace this by field is null
- (NSString*)whereClauseForQualifier:(EOQualifier*)qualifier
  joinExpression:(NSString*)joinExpression
{
  NSString* whereClause=[super whereClauseForQualifier:qualifier
							   joinExpression:joinExpression];
  whereClause=[MySQLSQLExpression convertWhereClause:whereClause];
  return whereClause;
}
@end

@implementation MySQLUpdateSQLExpression : EOUpdateSQLExpression
//Serveur does not accept where clause like field = null
//we replace this by field is null
- (NSString*)whereClauseForQualifier:(EOQualifier*)qualifier
  joinExpression:(NSString*)joinExpression
{
    NSString* whereClause = [super whereClauseForQualifier:qualifier
				   joinExpression:joinExpression];
    whereClause=[MySQLSQLExpression convertWhereClause:whereClause];
    return whereClause;
}
@end

