sqlparser/statements.cpp
// SQL Server, DB2, MySQL DECLARE variable statement; Oracle DECLARE block (inner or anonymous)
SQL Server, DB2, MySQL DECLARE variable statement; Oracle DECLARE block (inner or anonymous)
SQL Server DB2 MySQL DECLARE variable statement ; Oracle DECLARE block ( inner or anonymous )
bool SqlParser::ParseDeclareStatement(Token *declare)
{
	if(declare == NULL)
		return false;
	_declare_format = declare;
	if(_source == SQL_ORACLE)
	{
		if(_spl_scope == 0)
			_spl_scope = SQL_SCOPE_PROC;
		bool exists = ParseOracleVariableDeclarationBlock(declare);
		if(exists == true)
			return true;
	}
	int cnt_list = 0;
	while(true)
	{
		Token *name = GetNextToken();
		if(name == NULL)
			break;
		if(ParseDeclareHandler(declare, name) == true)
			break;
		if(ParseDeclareGlobalTemporaryTable(declare, name) == true)
			break;
		if(ParseDeclareLocalTemporaryTable(declare, name) == true)
			break;
		Token *as = GetNext("AS", L"AS", 2);
		if(as != NULL && _target != SQL_SQL_SERVER)
			Token::Remove(as);
		Token *type = GetNextToken();
		if(type == NULL)
			break;
		if(type->Compare("CURSOR", L"CURSOR", 6) == true)
			ParseDeclareCursor(declare, name, type);
		else
		if(type->Compare("CONDITION", L"CONDITION", 9) == true || type->Compare("EXCEPTION", L"EXCEPTION", 9) == true)
			ParseDeclareCondition(declare, name, type);
		else
		if(type->Compare("TABLE", L"TABLE", 5) == true)
			ParseDeclareTable(declare, name, type);
		else
		if(type->Compare("RESULT_SET_LOCATOR", L"RESULT_SET_LOCATOR", 18) == true)
			ParseDb2ResultSetLocatorDeclaration(declare, name, type);
		else
			ParseDeclareVariable(declare, name, type, cnt_list);
		Token *comma = GetNextCharToken(',', L',');
		if(comma == NULL)
			break;
		cnt_list++;
		if(Target(SQL_ORACLE, SQL_POSTGRESQL))
			TOKEN_CHANGE(comma, ";");
        else
        if(Target(SQL_MYSQL, SQL_MARIADB))
        {
			Token *next_name = GetNextToken();
			if(next_name != NULL && next_name->line == comma->line)
				AppendWithSpaceAfter(comma, " DECLARE", L" DECLARE", 8, declare); 
			else
				PREPEND_FMT(next_name, "DECLARE ", declare); 
			TOKEN_CHANGE(comma, ";");
			PushBack(next_name);
        }
	}
	return true;
}
bool SqlParser :: ParseDeclareStatement ( Token * declare ) { if ( declare == NULL ) return false ; _declare_format = declare ; if ( _source == SQL_ORACLE ) { if ( _spl_scope == 0 ) _spl_scope = SQL_SCOPE_PROC ; bool exists = ParseOracleVariableDeclarationBlock ( declare ) ; if ( exists == true ) return true ; } int cnt_list = 0 ; while ( true ) { Token * name = GetNextToken ( ) ; if ( name == NULL ) break ; if ( ParseDeclareHandler ( declare , name ) == true ) break ; if ( ParseDeclareGlobalTemporaryTable ( declare , name ) == true ) break ; if ( ParseDeclareLocalTemporaryTable ( declare , name ) == true ) break ; Token * as = GetNext ( " " , L" " , 2 ) ; if ( as != NULL && _target != SQL_SQL_SERVER ) Token :: Remove ( as ) ; Token * type = GetNextToken ( ) ; if ( type == NULL ) break ; if ( type -> Compare ( " " , L" " , 6 ) == true ) ParseDeclareCursor ( declare , name , type ) ; else if ( type -> Compare ( " " , L" " , 9 ) == true || type -> Compare ( " " , L" " , 9 ) == true ) ParseDeclareCondition ( declare , name , type ) ; else if ( type -> Compare ( " " , L" " , 5 ) == true ) ParseDeclareTable ( declare , name , type ) ; else if ( type -> Compare ( " " , L" " , 18 ) == true ) ParseDb2ResultSetLocatorDeclaration ( declare , name , type ) ; else ParseDeclareVariable ( declare , name , type , cnt_list ) ; Token * comma = GetNextCharToken ( ' ' , L' ' ) ; if ( comma == NULL ) break ; cnt_list ++ ; if ( Target ( SQL_ORACLE , SQL_POSTGRESQL ) ) TOKEN_CHANGE ( comma , " " ) ; else if ( Target ( SQL_MYSQL , SQL_MARIADB ) ) { Token * next_name = GetNextToken ( ) ; if ( next_name != NULL && next_name -> line == comma -> line ) AppendWithSpaceAfter ( comma , " " , L" " , 8 , declare ) ; else PREPEND_FMT ( next_name , " " , declare ) ; TOKEN_CHANGE ( comma , " " ) ; PushBack ( next_name ) ; } } return true ; }
SQL Server, DB2, MySQL DECLARE variable statement; Oracle DECLARE block (inner or anonymous)
SQL Server DB2 MySQL DECLARE variable statement ; Oracle DECLARE block ( inner or anonymous )
// Check for Oracle inner or anonymous DECLARE block // If the scope is not set yet, it means anonymous block for Oracle // Variable name of unknown type yet (local variable, cursor, table, condition etc.) // Check for HANDLER // Check for GLOBAL TEMPORARY TABLE in DB2 // Check for LOCAL TEMPORARY TABLE in Sybase ASA // Optional AS keyword before the data type in SQL Server // AS not allowed in other databases // DB2, MySQL DECLARE CURSOR // DB2 DECLARE name CONDITION; Sybase ASA DECLARE name EXCEPTION // SQL Server TABLE // DB2 RESULT_SET_LOCATOR // Variable declaration // Oracle and PostgreSQL use a statement for each declaration // MySQL and MariaDB require DECLARE for each variable // If the next variable are on the same line put DECLARE on this line as well // otherwise move DECLARE to the variable name line