sqlparser/statements.cpp
// DECLARE for variable in SQL Server, DB2, MySQL; DEFINE in Informix
DECLARE for variable in SQL Server, DB2, MySQL; DEFINE in Informix
DECLARE for variable in SQL Server DB2 MySQL ; DEFINE in Informix
bool SqlParser::ParseDeclareVariable(Token *declare, Token *name, Token *type, int cnt_list)
{
	if(name == NULL || type == NULL)
		return false;
    STATS_DECL
	PL_STMS_STATS_V("DECLARE", declare);
	if(_spl_last_declare != NULL && cnt_list == 0 && _target == SQL_POSTGRESQL)
		Token::Remove(declare);
	else
	if(_source == SQL_INFORMIX && _target != SQL_INFORMIX)
		Token::Change(declare, "DECLARE", L"DECLARE", 7);
	ConvertIdentifier(name, SQL_IDENT_VAR);
	if(Target(SQL_SQL_SERVER, SQL_SYBASE) == true)
		ConvertToTsqlVariable(name);
	_spl_variables.Add(name);
	ListW vars;
	vars.Add(name);
	int num = 1;
	while(true)
	{
		if(Token::Compare(type, ',', L',') == false)
			break;
		if(Target(SQL_ORACLE, SQL_POSTGRESQL) == true)
			Token::Change(type, ";", L";", 1);
		Token *var = GetNextToken();
		if(var == NULL)
			break;
		if(Target(SQL_SQL_SERVER, SQL_SYBASE) == true)
			ConvertToTsqlVariable(var);
		_spl_variables.Add(var);
		vars.Add(var);
		num++;
		type = GetNextToken();
	}
	bool typed = ParseTypedVariable(name, type);
	if(typed == false)
	{
		ParseDataType(type);
		ParseVarDataTypeAttribute();
	}
	name->data_type = type->data_type;
	name->data_subtype = type->data_subtype;
	Token *default_ = GetNextWordToken("DEFAULT", L"DEFAULT", 7);
	Token *default_exp = NULL;
	Token *default_equal = NULL;
	if(default_ == NULL)
		default_equal = GetNextCharToken('=', '=');
	if(default_ != NULL || default_equal != NULL)
	{
		default_exp = GetNextToken();
		ParseExpression(default_exp);
		if(default_equal != NULL)
		{
			if(_target == SQL_ORACLE)
				PrependNoFormat(default_equal, ":", L":", 1);
			else
			if(Target(SQL_MYSQL) == true)
				ChangeWithSpacesAround(default_equal, "DEFAULT", L"DEFAULT", 7, declare);
		}
		else
		if(_target == SQL_SQL_SERVER && default_ != NULL)
			Token::Change(default_, "=", L"=", 1);
	}
	Token *last = GetLastToken();
	if(Target(SQL_SQL_SERVER, SQL_ORACLE, SQL_POSTGRESQL) == true && num > 1)
	{
		ListwItem *item = vars.GetFirst();
		for(int i=0; i < num - 1; i++)
		{
			if(item == NULL)
				break;
			Token *var = (Token*)item->value;
			Append(var, " ", L" ", 1);
			AppendCopy(var, type, last, false);
			item = item->next;
		}
	}
	if(_spl_first_non_declare == NULL)
		_spl_last_declare = last;
	if(_target == SQL_ORACLE)
		Token::Remove(declare);
	SqlServerAddStmtDelimiter();
	if(Target(SQL_DB2) == false &&
		(Token::Compare(name, "SQLCODE", L"SQLCODE", 7) || Token::Compare(name, "SQLSTATE", L"SQLSTATE", 8)))
	{
		 (void) GetNextCharToken(';', L';');
		Token::Remove(declare, GetLastToken());
	}
	return true;
}
bool SqlParser :: ParseDeclareVariable ( Token * declare , Token * name , Token * type , int cnt_list ) { if ( name == NULL || type == NULL ) return false ; STATS_DECL PL_STMS_STATS_V ( " " , declare ) ; if ( _spl_last_declare != NULL && cnt_list == 0 && _target == SQL_POSTGRESQL ) Token :: Remove ( declare ) ; else if ( _source == SQL_INFORMIX && _target != SQL_INFORMIX ) Token :: Change ( declare , " " , L" " , 7 ) ; ConvertIdentifier ( name , SQL_IDENT_VAR ) ; if ( Target ( SQL_SQL_SERVER , SQL_SYBASE ) == true ) ConvertToTsqlVariable ( name ) ; _spl_variables . Add ( name ) ; ListW vars ; vars . Add ( name ) ; int num = 1 ; while ( true ) { if ( Token :: Compare ( type , ' ' , L' ' ) == false ) break ; if ( Target ( SQL_ORACLE , SQL_POSTGRESQL ) == true ) Token :: Change ( type , " " , L" " , 1 ) ; Token * var = GetNextToken ( ) ; if ( var == NULL ) break ; if ( Target ( SQL_SQL_SERVER , SQL_SYBASE ) == true ) ConvertToTsqlVariable ( var ) ; _spl_variables . Add ( var ) ; vars . Add ( var ) ; num ++ ; type = GetNextToken ( ) ; } bool typed = ParseTypedVariable ( name , type ) ; if ( typed == false ) { ParseDataType ( type ) ; ParseVarDataTypeAttribute ( ) ; } name -> data_type = type -> data_type ; name -> data_subtype = type -> data_subtype ; Token * default_ = GetNextWordToken ( " " , L" " , 7 ) ; Token * default_exp = NULL ; Token * default_equal = NULL ; if ( default_ == NULL ) default_equal = GetNextCharToken ( ' ' , ' ' ) ; if ( default_ != NULL || default_equal != NULL ) { default_exp = GetNextToken ( ) ; ParseExpression ( default_exp ) ; if ( default_equal != NULL ) { if ( _target == SQL_ORACLE ) PrependNoFormat ( default_equal , " " , L" " , 1 ) ; else if ( Target ( SQL_MYSQL ) == true ) ChangeWithSpacesAround ( default_equal , " " , L" " , 7 , declare ) ; } else if ( _target == SQL_SQL_SERVER && default_ != NULL ) Token :: Change ( default_ , " " , L" " , 1 ) ; } Token * last = GetLastToken ( ) ; if ( Target ( SQL_SQL_SERVER , SQL_ORACLE , SQL_POSTGRESQL ) == true && num > 1 ) { ListwItem * item = vars . GetFirst ( ) ; for ( int i = 0 ; i < num - 1 ; i ++ ) { if ( item == NULL ) break ; Token * var = ( Token * ) item -> value ; Append ( var , " " , L" " , 1 ) ; AppendCopy ( var , type , last , false ) ; item = item -> next ; } } if ( _spl_first_non_declare == NULL ) _spl_last_declare = last ; if ( _target == SQL_ORACLE ) Token :: Remove ( declare ) ; SqlServerAddStmtDelimiter ( ) ; if ( Target ( SQL_DB2 ) == false && ( Token :: Compare ( name , " " , L" " , 7 ) || Token :: Compare ( name , " " , L" " , 8 ) ) ) { ( void ) GetNextCharToken ( ' ' , L' ' ) ; Token :: Remove ( declare , GetLastToken ( ) ) ; } return true ; }
DECLARE for variable in SQL Server, DB2, MySQL; DEFINE in Informix
DECLARE for variable in SQL Server DB2 MySQL ; DEFINE in Informix
// In PostgreSQL DECLARE is specified once (start DECLARE section that goes until BEGIN) // Make sure it is not the same DECLARE keyword (when variables are in a list) // Convert DEFINE to DECLARE from Informix // Add @ for parameter names for SQL Server and Sybase // DECLARE v1, v2, v3 INT DEFAULT 0 in DB2, MySQL, MariaDB, Informix // Check for list of variables // Separate statements in Oracle, PostgreSQL // Next variable in list // Add @ for parameter names for SQL Server and Sybase // Check and resolve Oracle %TYPE, Informix LIKE variable // Get the variable data type // Propagate data type to variable // Optional DEFAULT exp in Teradata, DB2, MySQL // = is used in SQL Server to specify default // = default_exp specified // Oracle uses := and DEFAULT  // MySQL uses DEFAULT keyword // SQL Server uses =  // If multiple variables specified copy data type and DEFAULT for Oracle, SQL Server, PostgreSQL // If there are no non-declare statements above, set last declare // Remove DECLARE keyword for Oracle, declarations will be in DECLARE section after IS // Add statement delimiter if not set when source is SQL Server // Remove DB2 SQLCODE and SQLSTATE declarations /*Token *semi */