sqlparser/language.cpp
// DEFAULT or DB2 WITH DEFAULT clause
DEFAULT or DB2 WITH DEFAULT clause
DEFAULT or DB2 WITH DEFAULT clause
bool SqlParser::ParseDefaultExpression(Token *type, Token *type_end, Token *token, int pos)
{
	if(token == NULL)
		return false;
	if(token->Compare("WITH", L"WITH", 4) == true)
	{
		if(_target != SQL_DB2)
			Token::Remove(token);
		token = GetNextToken();
	}
	Token *first = GetNextToken();
	if(first == NULL)
		return false;
	Token *append_default_value = token;
	Token *default_end = NULL;
	if(first->Compare(',', L',') == true || first->Compare(')', L')') == true || 
		first->Compare("CHECK", L"CHECK", 5) == true || first->Compare("FOR", L"FOR", 3) == true)
	{
		PushBack(first);
		first = NULL;
	}
	else
	if(first->Compare("GLOBAL", L"GLOBAL", 6) == true)
	{
		Token *autoincrement = GetNext("AUTOINCREMENT", L"AUTOINCREMENT", 13);
		if(autoincrement != NULL)
		{
			 (void) GetNext('(', L'(');
			 (void) GetNextToken();
			Token *close = GetNext(')', L')');
			if(_target == SQL_SQL_SERVER)
			{
				Token::Change(token, "IDENTITY", L"IDENTITY", 8);
				Token::Remove(first, close);
			}
		}
	}
	else
	{
		ParseExpression(first);
		default_end = GetLastToken();
	}
    bool commented = false;
    if(first != NULL && first->type == TOKEN_FUNCTION)
    {
        if(_target == SQL_ESGYNDB && TOKEN_CMP(first, "CURRENT_DATE") == false && 
            TOKEN_CMP(first, "CURRENT_TIMESTAMP") == false && TOKEN_CMP(first, "CURRENT_TIME") == false)
        {
            Comment(token, default_end);
            commented = true;
        }
    }
	Token *new_default = token;
	Token *new_default_first = first;
	if(pos > 0 && Target(SQL_ORACLE, SQL_ESGYNDB) && commented == false)
	{
		new_default = Append(type_end, " DEFAULT ", L" DEFAULT ", 9, token);
		new_default_first = AppendCopy(type_end, first, default_end, false);
		append_default_value = type_end;
		Token::Remove(token);
		Token::Remove(first, default_end);
	}
	FormatDefaultExpression(type, new_default_first, new_default);
	if(first == NULL && _target != SQL_DB2)
	{
		if(type->Compare("CHAR", L"CHAR", 4) == true || type->Compare("CHAR", L"CHAR", 4))
			Append(append_default_value, " ''", L" ''", 3);
		else
		if(type->Compare("INTEGER", L"INTEGER", 7) == true || type->Compare("DECIMAL", L"DECIMAL", 7) == true || 
			type->Compare("NUMERIC", L"NUMERIC", 7) == true)
			Append(append_default_value, " 0", L" 0", 2);
		else
		if(type->Compare("TIMESTAMP", L"TIMESTAMP", 9) == true)
		{
			if(_target == SQL_SQL_SERVER)
				Append(append_default_value, " GETDATE()", L" GETDATE()", 10, token);
			else
			if(_target == SQL_ORACLE)
				Append(append_default_value, " SYSTIMESTAMP", L" SYSTIMESTAMP", 13, token);
			else
				Append(append_default_value, " CURRENT_TIMESTAMP", L" CURRENT_TIMESTAMP", 18, token);
		}
		else
		if(type->Compare("DATE", L"DATE", 4) == true)
		{
			if(_target == SQL_SQL_SERVER)
				Append(append_default_value, " GETDATE()", L" GETDATE()", 10, token);
			else
			if(_target == SQL_ORACLE)
				Append(append_default_value, " SYSDATE", L" SYSDATE", 8, token);
			else
				Append(append_default_value, " CURRENT_DATE", L" CURRENT_DATE", 13, token);
		}
		else
		if(type->Compare("CLOB", L"CLOB", 4) == true)
		{
			if(_target == SQL_ORACLE)
				Append(append_default_value, " EMPTY_CLOB()", L" EMPTY_CLOB()", 13, token);
			else
				Append(append_default_value, " ''", L" ''", 3);
		}
	}
    if(_target == SQL_HIVE)
        Token::Remove(token, default_end);
	return true;
}
bool SqlParser :: ParseDefaultExpression ( Token * type , Token * type_end , Token * token , int pos ) { if ( token == NULL ) return false ; if ( token -> Compare ( " " , L" " , 4 ) == true ) { if ( _target != SQL_DB2 ) Token :: Remove ( token ) ; token = GetNextToken ( ) ; } Token * first = GetNextToken ( ) ; if ( first == NULL ) return false ; Token * append_default_value = token ; Token * default_end = NULL ; if ( first -> Compare ( ' ' , L' ' ) == true || first -> Compare ( ' ' , L' ' ) == true || first -> Compare ( " " , L" " , 5 ) == true || first -> Compare ( " " , L" " , 3 ) == true ) { PushBack ( first ) ; first = NULL ; } else if ( first -> Compare ( " " , L" " , 6 ) == true ) { Token * autoincrement = GetNext ( " " , L" " , 13 ) ; if ( autoincrement != NULL ) { ( void ) GetNext ( ' ' , L' ' ) ; ( void ) GetNextToken ( ) ; Token * close = GetNext ( ' ' , L' ' ) ; if ( _target == SQL_SQL_SERVER ) { Token :: Change ( token , " " , L" " , 8 ) ; Token :: Remove ( first , close ) ; } } } else { ParseExpression ( first ) ; default_end = GetLastToken ( ) ; } bool commented = false ; if ( first != NULL && first -> type == TOKEN_FUNCTION ) { if ( _target == SQL_ESGYNDB && TOKEN_CMP ( first , " " ) == false && TOKEN_CMP ( first , " " ) == false && TOKEN_CMP ( first , " " ) == false ) { Comment ( token , default_end ) ; commented = true ; } } Token * new_default = token ; Token * new_default_first = first ; if ( pos > 0 && Target ( SQL_ORACLE , SQL_ESGYNDB ) && commented == false ) { new_default = Append ( type_end , " " , L" " , 9 , token ) ; new_default_first = AppendCopy ( type_end , first , default_end , false ) ; append_default_value = type_end ; Token :: Remove ( token ) ; Token :: Remove ( first , default_end ) ; } FormatDefaultExpression ( type , new_default_first , new_default ) ; if ( first == NULL && _target != SQL_DB2 ) { if ( type -> Compare ( " " , L" " , 4 ) == true || type -> Compare ( " " , L" " , 4 ) ) Append ( append_default_value , " " , L" " , 3 ) ; else if ( type -> Compare ( " " , L" " , 7 ) == true || type -> Compare ( " " , L" " , 7 ) == true || type -> Compare ( " " , L" " , 7 ) == true ) Append ( append_default_value , " " , L" " , 2 ) ; else if ( type -> Compare ( " " , L" " , 9 ) == true ) { if ( _target == SQL_SQL_SERVER ) Append ( append_default_value , " " , L" " , 10 , token ) ; else if ( _target == SQL_ORACLE ) Append ( append_default_value , " " , L" " , 13 , token ) ; else Append ( append_default_value , " " , L" " , 18 , token ) ; } else if ( type -> Compare ( " " , L" " , 4 ) == true ) { if ( _target == SQL_SQL_SERVER ) Append ( append_default_value , " " , L" " , 10 , token ) ; else if ( _target == SQL_ORACLE ) Append ( append_default_value , " " , L" " , 8 , token ) ; else Append ( append_default_value , " " , L" " , 13 , token ) ; } else if ( type -> Compare ( " " , L" " , 4 ) == true ) { if ( _target == SQL_ORACLE ) Append ( append_default_value , " " , L" " , 13 , token ) ; else Append ( append_default_value , " " , L" " , 3 ) ; } } if ( _target == SQL_HIVE ) Token :: Remove ( token , default_end ) ; return true ; }
DEFAULT or DB2 WITH DEFAULT clause
DEFAULT or DB2 WITH DEFAULT clause
// Optional WITH in DB2 // Remove for other databases // DEFAULT keyword // DB2 DEFAULT maybe without the expression and terminated by , ) or CHECK () or FOR BIT | SBCS | MIXED DATA // Sybase ASA DEFAULT GLOBAL AUTOINCREMENT(partition_size) /*Token *open */ /*Token *partition_size */ // Not all databases supports functions in DEFAULT // Only CURRENT_DATE/TIME/TIMESTAMP allowed in EsgynDB // In Oracle and EsgynDB unlike DB2, MySQL DEFAULT must go right after data type // Oracle requires TO_TIMESTAMP around datetime literals i.e. // In DB2 default value can be omitted, add the value explicitly depending on the data type // Specify DEFAULT '' for CHAR, VARCHAR and CLOB // Specify DEFAULT 0 for INTEGER, DECIMAL and NUMERIC // Specify current timestamp for TIMESTAMP // Specify current date for DATE // Default for CLOB // Hive does not support DEFAULT