sqlparser/statements.cpp
// EXIT statement in Oracle, Informix, Netezza
EXIT statement in Oracle, Informix, Netezza
EXIT statement in Oracle Informix Netezza
bool SqlParser::ParseExitStatement(Token *exit)
{
	if(exit == NULL)
		return false;
    STATS_DECL
	STATS_SET_DESC(PLSQL_STMT_EXIT_DESC)
	PL_STMS_STATS(exit);
	Token *loop_type = GetNextWordToken("FOR", L"FOR", 3);
	if(loop_type == NULL)
		loop_type = GetNextWordToken("FOREACH", L"FOREACH", 7);
	if(loop_type == NULL)
		loop_type = GetNextWordToken("LOOP", L"LOOP", 4);
	if(loop_type == NULL)
		loop_type = GetNextWordToken("WHILE", L"WHILE", 5);
	if(_target == SQL_ORACLE)
		Token::Remove(loop_type);
	Token *when = GetNextWordToken("WHEN", L"WHEN", 4);
	if(_target_app == APP_JAVA)
		TOKEN_CHANGE_NOFMT(exit, "break");
	if(when == NULL)
		return true;
	Token *next = GetNextToken();
	if(next == NULL)
		return false;
	Token *cent = GetNextCharToken('%', L'%');
    Token *notfound = NULL;
    if(cent != NULL)
	    notfound = GetNextWordToken("NOTFOUND", L"NOTFOUND", 8);
	if(notfound != NULL)
	{
		if(_target == SQL_SQL_SERVER)
		{
			Token::Change(exit, "IF", L"IF", 2);
			Token::Change(when, "@@FETCH_STATUS <> 0 BREAK", L"@@FETCH_STATUS <> 0 BREAK", 25);
			Token::Remove(next, notfound); 
		}
        else
        if(Target(SQL_MARIADB, SQL_MYSQL))
        {
            Token::Change(exit, "IF NOT_FOUND = 1", L"IF NOT_FOUND = 1", 16);
            Token::Change(when, "THEN", L"THEN", 4);
            TokenStr label("loop_label", L"loop_label", 10);
            if(_spl_loop_labels > 0)
                label.Append(_spl_loop_labels + 1);            
            Token::Change(notfound, "LEAVE", L"LEAVE", 5);
            AppendNoFormat(notfound, " ", L" ", 1);
            AppendNoFormat(notfound, &label);
            AppendNoFormat(notfound, ";", L";", 1);
            ListwItem *i = _spl_loops.GetLast();
            if(i != NULL)
            {
                Token *loop = (Token*)i->value;
                PrependNoFormat(loop, &label);
                Prepend(loop, ":\n", L":\n", 2);
            }
            _spl_loop_labels++;
             (void) GetNext(';', L';');
            Append(notfound, " END IF;", L" END IF;", 7);
            Token::Remove(next, cent); 
			_spl_need_not_found_handler = true;
        }
		else
		if(Target(SQL_POSTGRESQL, SQL_NETEZZA))
		{
			Token::Change(notfound, "NOT FOUND", L"NOT FOUND", 9);
			Token::Remove(next, cent); 
		}
	}
    else
    {
        ParseBooleanExpression(SQL_BOOL_IF, exit);
        Token *last = GetLastToken();
        if(Target(SQL_MARIADB, SQL_MYSQL))
        {
            Token::Change(exit, "IF", L"IF", 2);
            Token::Remove(when);
            Append(last, " THEN LEAVE", L" THEN LEAVE", 11, exit);
            AppendNoFormat(last, " label;", L" label;", 7);
            Append(last, " END IF;", L" END IF;", 7);
        }
    }
	return true;
}
bool SqlParser :: ParseExitStatement ( Token * exit ) { if ( exit == NULL ) return false ; STATS_DECL STATS_SET_DESC ( PLSQL_STMT_EXIT_DESC ) PL_STMS_STATS ( exit ) ; Token * loop_type = GetNextWordToken ( " " , L" " , 3 ) ; if ( loop_type == NULL ) loop_type = GetNextWordToken ( " " , L" " , 7 ) ; if ( loop_type == NULL ) loop_type = GetNextWordToken ( " " , L" " , 4 ) ; if ( loop_type == NULL ) loop_type = GetNextWordToken ( " " , L" " , 5 ) ; if ( _target == SQL_ORACLE ) Token :: Remove ( loop_type ) ; Token * when = GetNextWordToken ( " " , L" " , 4 ) ; if ( _target_app == APP_JAVA ) TOKEN_CHANGE_NOFMT ( exit , " " ) ; if ( when == NULL ) return true ; Token * next = GetNextToken ( ) ; if ( next == NULL ) return false ; Token * cent = GetNextCharToken ( ' ' , L' ' ) ; Token * notfound = NULL ; if ( cent != NULL ) notfound = GetNextWordToken ( " " , L" " , 8 ) ; if ( notfound != NULL ) { if ( _target == SQL_SQL_SERVER ) { Token :: Change ( exit , " " , L" " , 2 ) ; Token :: Change ( when , " " , L" " , 25 ) ; Token :: Remove ( next , notfound ) ; } else if ( Target ( SQL_MARIADB , SQL_MYSQL ) ) { Token :: Change ( exit , " " , L" " , 16 ) ; Token :: Change ( when , " " , L" " , 4 ) ; TokenStr label ( " " , L" " , 10 ) ; if ( _spl_loop_labels > 0 ) label . Append ( _spl_loop_labels + 1 ) ; Token :: Change ( notfound , " " , L" " , 5 ) ; AppendNoFormat ( notfound , " " , L" " , 1 ) ; AppendNoFormat ( notfound , & label ) ; AppendNoFormat ( notfound , " " , L" " , 1 ) ; ListwItem * i = _spl_loops . GetLast ( ) ; if ( i != NULL ) { Token * loop = ( Token * ) i -> value ; PrependNoFormat ( loop , & label ) ; Prepend ( loop , " \n " , L" \n " , 2 ) ; } _spl_loop_labels ++ ; ( void ) GetNext ( ' ' , L' ' ) ; Append ( notfound , " " , L" " , 7 ) ; Token :: Remove ( next , cent ) ; _spl_need_not_found_handler = true ; } else if ( Target ( SQL_POSTGRESQL , SQL_NETEZZA ) ) { Token :: Change ( notfound , " " , L" " , 9 ) ; Token :: Remove ( next , cent ) ; } } else { ParseBooleanExpression ( SQL_BOOL_IF , exit ) ; Token * last = GetLastToken ( ) ; if ( Target ( SQL_MARIADB , SQL_MYSQL ) ) { Token :: Change ( exit , " " , L" " , 2 ) ; Token :: Remove ( when ) ; Append ( last , " " , L" " , 11 , exit ) ; AppendNoFormat ( last , " " , L" " , 7 ) ; Append ( last , " " , L" " , 7 ) ; } } return true ; }
EXIT statement in Oracle, Informix, Netezza
EXIT statement in Oracle Informix Netezza
// FOR, FOREACH, LOOP or WHILE keyword in Informix // Loop type not required for Oracle // Optional WHEN condition in Oracle, Informix // cursor%NOTFOUND // IF @@FETCH_STATUS <> 0 BREAK in SQL Server // Check generated NOT_FOUND variable  // Generate the label name /*Token *semi */ // Add NOT FOUND handler and variable // WHEN NOT FOUND in PostgreSQL, Netezza // Boolean condition // IF-LEAVE in MySQL