sqldata/sqldata.cpp
void SqlData::GetIdentityMetaTask(SqlColMeta &col)
{
	if(col.identity == false)
		return;

	SqlMetaTask task;	

	std::string schema = col.schema;
	std::string table = col.table;

	task.s_name = schema;
	task.s_name += '.';
	task.s_name += table;

	MapObjectName(schema, table, task.t_name);
		
	// Sequence name
	task.t_o_name = task.t_name + "_seq";

	// Drop the sequence first
	/*task.type = SQLDATA_CMD_DROP_SEQUENCE;	
	task.statement = "DROP SEQUENCE ";
	task.statement += seq;

	_meta_tasks.push_back(task); */

	char start_str[11]; 
	char inc_str[11]; 

	int start = (col.id_next > 0) ? col.id_next : col.id_start;

	sprintf(start_str, "%d", start);
	sprintf(inc_str, "%d", col.id_inc);

	// Create sequence
	task.type = SQLDATA_CMD_CREATE_SEQUENCE;	
	task.statement = "CREATE SEQUENCE ";
	task.statement += task.t_o_name;
	task.statement += " START WITH ";
	task.statement += start_str;
	task.statement += " INCREMENT BY ";
	task.statement += inc_str;

	_meta_tasks.push_back(task);

	std::string column;
	std::string type;

	_db.MapColumn(task.s_name.c_str(), col.column, column, type);
	
	// Create trigger for Oracle
	if(_target_type == SQLDATA_ORACLE)
	{
		task.type = SQLDATA_CMD_CREATE_TRIGGER;
		task.statement = "CREATE OR REPLACE TRIGGER ";
		task.statement += task.t_o_name;
		task.statement += "_tr\n BEFORE INSERT ON ";
		task.statement += task.t_name;
		task.statement += " FOR EACH ROW\n";
		task.statement += " WHEN (NEW.";
		task.statement += column;
		task.statement += " IS NULL OR NEW.";
		task.statement += column;
		task.statement += " = 0)\n";
		task.statement += "BEGIN\n";
		task.statement += " SELECT ";
		task.statement += task.t_o_name;
		task.statement += ".NEXTVAL INTO :NEW.";
		task.statement += column;
		task.statement += " FROM dual;\n";
		task.statement += "END;\n";
	}
	else
	// Add DEFAULT for PostgreSQL
	if(_target_type == SQLDATA_POSTGRESQL)
	{
		task.type = SQLDATA_CMD_ADD_DEFAULT;
		task.statement = "ALTER TABLE ";
		task.statement += task.t_name;
		task.statement += " ALTER COLUMN ";
		task.statement += column;
		task.statement += " SET DEFAULT nextval ('";
		task.statement += task.t_o_name;
		task.statement += "')";
	}
   
	_meta_tasks.push_back(task);
}
// Generate statement to emulate identity in Oracle (sequence and trigger) and PostgreSQL (sequence and default)
Generate statement to emulate identity in Oracle (sequence and trigger) and PostgreSQL (sequence and default)
Generate statement to emulate identity in Oracle ( sequence and trigger ) and PostgreSQL ( sequence and default )
void SqlData::GetIdentityMetaTask(SqlColMeta &col)
{
	if(col.identity == false)
		return;
	SqlMetaTask task;	
	std::string schema = col.schema;
	std::string table = col.table;
	task.s_name = schema;
	task.s_name += '.';
	task.s_name += table;
	MapObjectName(schema, table, task.t_name);
	task.t_o_name = task.t_name + "_seq";
	char start_str[11]; 
	char inc_str[11]; 
	int start = (col.id_next > 0) ? col.id_next : col.id_start;
	sprintf(start_str, "%d", start);
	sprintf(inc_str, "%d", col.id_inc);
	task.type = SQLDATA_CMD_CREATE_SEQUENCE;	
	task.statement = "CREATE SEQUENCE ";
	task.statement += task.t_o_name;
	task.statement += " START WITH ";
	task.statement += start_str;
	task.statement += " INCREMENT BY ";
	task.statement += inc_str;
	_meta_tasks.push_back(task);
	std::string column;
	std::string type;
	_db.MapColumn(task.s_name.c_str(), col.column, column, type);
	if(_target_type == SQLDATA_ORACLE)
	{
		task.type = SQLDATA_CMD_CREATE_TRIGGER;
		task.statement = "CREATE OR REPLACE TRIGGER ";
		task.statement += task.t_o_name;
		task.statement += "_tr\n BEFORE INSERT ON ";
		task.statement += task.t_name;
		task.statement += " FOR EACH ROW\n";
		task.statement += " WHEN (NEW.";
		task.statement += column;
		task.statement += " IS NULL OR NEW.";
		task.statement += column;
		task.statement += " = 0)\n";
		task.statement += "BEGIN\n";
		task.statement += " SELECT ";
		task.statement += task.t_o_name;
		task.statement += ".NEXTVAL INTO :NEW.";
		task.statement += column;
		task.statement += " FROM dual;\n";
		task.statement += "END;\n";
	}
	else
	if(_target_type == SQLDATA_POSTGRESQL)
	{
		task.type = SQLDATA_CMD_ADD_DEFAULT;
		task.statement = "ALTER TABLE ";
		task.statement += task.t_name;
		task.statement += " ALTER COLUMN ";
		task.statement += column;
		task.statement += " SET DEFAULT nextval ('";
		task.statement += task.t_o_name;
		task.statement += "')";
	}
	_meta_tasks.push_back(task);
}
void SqlData :: GetIdentityMetaTask ( SqlColMeta & col ) { if ( col . identity == false ) return ; SqlMetaTask task ; std :: string schema = col . schema ; std :: string table = col . table ; task . s_name = schema ; task . s_name += ' ' ; task . s_name += table ; MapObjectName ( schema , table , task . t_name ) ; task . t_o_name = task . t_name + " " ; char start_str [ 11 ] ; char inc_str [ 11 ] ; int start = ( col . id_next > 0 ) ? col . id_next : col . id_start ; sprintf ( start_str , " " , start ) ; sprintf ( inc_str , " " , col . id_inc ) ; task . type = SQLDATA_CMD_CREATE_SEQUENCE ; task . statement = " " ; task . statement += task . t_o_name ; task . statement += " " ; task . statement += start_str ; task . statement += " " ; task . statement += inc_str ; _meta_tasks . push_back ( task ) ; std :: string column ; std :: string type ; _db . MapColumn ( task . s_name . c_str ( ) , col . column , column , type ) ; if ( _target_type == SQLDATA_ORACLE ) { task . type = SQLDATA_CMD_CREATE_TRIGGER ; task . statement = " " ; task . statement += task . t_o_name ; task . statement += " \n " ; task . statement += task . t_name ; task . statement += " \n " ; task . statement += " " ; task . statement += column ; task . statement += " " ; task . statement += column ; task . statement += " \n " ; task . statement += " \n " ; task . statement += " " ; task . statement += task . t_o_name ; task . statement += " " ; task . statement += column ; task . statement += " \n " ; task . statement += " \n " ; } else if ( _target_type == SQLDATA_POSTGRESQL ) { task . type = SQLDATA_CMD_ADD_DEFAULT ; task . statement = " " ; task . statement += task . t_name ; task . statement += " " ; task . statement += column ; task . statement += " " ; task . statement += task . t_o_name ; task . statement += " " ; } _meta_tasks . push_back ( task ) ; }
Generate statement to emulate identity in Oracle (sequence and trigger) and PostgreSQL (sequence and default)
Generate statement to emulate identity in Oracle ( sequence and trigger ) and PostgreSQL ( sequence and default )
// Sequence name // Drop the sequence first /*task.type = SQLDATA_CMD_DROP_SEQUENCE;	
	task.statement = "DROP SEQUENCE ";
	task.statement += seq;

	_meta_tasks.push_back(task); */ // Create sequence // Create trigger for Oracle // Add DEFAULT for PostgreSQL