sqlparser/select.cpp
// Parse predicate DISTINCT, TOP, FIRST etc.
Parse predicate DISTINCT, TOP, FIRST etc.
Parse predicate DISTINCT TOP FIRST etc .
bool SqlParser::ParseSelectListPredicate(Token **rowlimit_slist, bool *rowlimit_percent)
{
	bool exists = false;
	while(true)
	{
		Token *next = GetNextToken();
		if(next == NULL)
			break;
		if(next->Compare("DISTINCT", L"DISTINCT", 8) == true)
		{
			exists = true;
			continue;
		}
		else
		if(next->Compare("UNIQUE", L"UNIQUE", 6) == true)
		{
			if(_target != SQL_INFORMIX)
				Token::Change(next, "DISTINCT", L"DISTINCT", 8);
			exists = true;
			continue;
		}
		else
		if(next->Compare("TOP", L"TOP", 3) == true)
		{
			Token *open = GetNextCharToken('(', L'(');
			Token *num = GetNextToken();
			Token *close = GetNextCharToken(')', L')');
			Token *percent = GetNextWordToken("PERCENT", L"PERCENT", 7);
			if(percent != NULL && rowlimit_percent != NULL)
				*rowlimit_percent = true;
			if(rowlimit_slist != NULL)
				*rowlimit_slist = num;
			if(_target == SQL_ORACLE)
			{
				Token::Remove(next, num);
				Token::Remove(open);
				Token::Remove(close);
				Token::Remove(percent);
			}
			exists = true;
			continue;
		}
		else
		if(next->Compare("FIRST", L"FIRST", 5) == true)
		{
			Token *open = GetNextCharToken('(', L'(');
			if(open != NULL)
			{
				PushBack(next);
				break;
			}
			 or variable allowed in Informix
			Token *num = GetNextToken();
			if(rowlimit_slist != NULL)
				*rowlimit_slist = num;
			if(_target == SQL_ORACLE)
				Token::Remove(next, num);
			else
			if(_target == SQL_SQL_SERVER)
			{
				Token::Change(next, "TOP", L"TOP", 3);
				if(num != NULL && num->IsNumeric() == false)
				{
					if(GetVariable(num) == NULL && GetParameter(num) == NULL)
					{
						AppendNoFormat(next, " 1", L" 1", 2);
						PushBack(num);
					}
				}
			}
			exists = true;
			continue;
		}
		PushBack(next);
		break;
	}
	return exists;
}
bool SqlParser :: ParseSelectListPredicate ( Token * * rowlimit_slist , bool * rowlimit_percent ) { bool exists = false ; while ( true ) { Token * next = GetNextToken ( ) ; if ( next == NULL ) break ; if ( next -> Compare ( " " , L" " , 8 ) == true ) { exists = true ; continue ; } else if ( next -> Compare ( " " , L" " , 6 ) == true ) { if ( _target != SQL_INFORMIX ) Token :: Change ( next , " " , L" " , 8 ) ; exists = true ; continue ; } else if ( next -> Compare ( " " , L" " , 3 ) == true ) { Token * open = GetNextCharToken ( ' ' , L' ' ) ; Token * num = GetNextToken ( ) ; Token * close = GetNextCharToken ( ' ' , L' ' ) ; Token * percent = GetNextWordToken ( " " , L" " , 7 ) ; if ( percent != NULL && rowlimit_percent != NULL ) * rowlimit_percent = true ; if ( rowlimit_slist != NULL ) * rowlimit_slist = num ; if ( _target == SQL_ORACLE ) { Token :: Remove ( next , num ) ; Token :: Remove ( open ) ; Token :: Remove ( close ) ; Token :: Remove ( percent ) ; } exists = true ; continue ; } else if ( next -> Compare ( " " , L" " , 5 ) == true ) { Token * open = GetNextCharToken ( ' ' , L' ' ) ; if ( open != NULL ) { PushBack ( next ) ; break ; } Token * num = GetNextToken ( ) ; if ( rowlimit_slist != NULL ) * rowlimit_slist = num ; if ( _target == SQL_ORACLE ) Token :: Remove ( next , num ) ; else if ( _target == SQL_SQL_SERVER ) { Token :: Change ( next , " " , L" " , 3 ) ; if ( num != NULL && num -> IsNumeric ( ) == false ) { if ( GetVariable ( num ) == NULL && GetParameter ( num ) == NULL ) { AppendNoFormat ( next , " " , L" " , 2 ) ; PushBack ( num ) ; } } } exists = true ; continue ; } PushBack ( next ) ; break ; } return exists ; }
Parse predicate DISTINCT, TOP, FIRST etc.
Parse predicate DISTINCT TOP FIRST etc .
// DISTINCT in all databases // UNIQUE in Informix // Use DISTINCT in other databases // TOP num [PERCENT] in SQL Server and Sybase (limit after sorting) // Limit can be optionally in () // Number // Limit in percents can be specified // ROWNUM <= num is used in Oracle // FIRST num in Informix and Sybase ASA (return first num rows after sorting) // Make sure it is not FIRST(column) function in ASA that means 1 row and handled separately // Number or variable allowed in Informix // ROWNUM <= num is used in Oracle // TOP n in SQL Server // In Sybase ASA number can be omitted, but Informix also allows variable name // If variable or parameter do not exists add TOP 1 // Not a predicate