sqldata/sqlapibase.cpp
// Build a condition to select objects from the catalog
Build a condition to select objects from the catalog
Build a condition to select objects from the catalog
void SqlApiBase::GetSelectionCriteria(const char *selection_template, const char *in_schema, const char *in_object, 
	std::string &output, const char *default_schema, bool upper_case)
{
	if(selection_template == NULL)
		return;
	const char *cur = selection_template;
	std::string conditions;
	int count = 0;
	bool more = true;
	while(more)
	{
		std::string item;
		cur = Str::GetNextInList(cur, item);
		if(item.empty() == true)
		{
			more = false;
			break;
		}
		const char *table_template = item.c_str();
		const char *dot = strchr(table_template, '.');
		std::string schema;
		std::string table;
		if(dot != NULL)
		{
			schema.assign(table_template, (size_t)(dot - table_template)); 
			table = dot + 1;
		}
		else
		{
			if(default_schema != NULL)
				schema = default_schema;
			table = table_template;
		}
		char *s_ast = strchr((char*)schema.c_str(), '*');
		char *t_ast = strchr((char*)table.c_str(), '*');
		std::replace(schema.begin(), schema.end(), '*', '%');
		std::replace(table.begin(), table.end(), '*', '%');
		if(upper_case == true)
		{
			std::transform(schema.begin(), schema.end(), schema.begin(), ::toupper);
			std::transform(table.begin(), table.end(), table.begin(), ::toupper);
		}
		std::string condition;
		if(schema.empty() == false)
		{
			if(s_ast != NULL && strcmp(schema.c_str(), "%") != 0)
			{
				condition += in_schema;
				condition += " LIKE '";
				condition += schema;
				condition += "'";
			}
			else
			if(s_ast == NULL)
			{
				condition += in_schema;
				condition += " = '";
				condition += schema;
				condition += "'";
			}
		}
		bool and_exists = false;
		if(t_ast && strcmp(table.c_str(), "%") != 0)
		{
			if(condition.empty() == false)
			{
				condition += " AND ";
				and_exists = true;
			}
			condition += in_object;
			condition += " LIKE '";
			condition += table;
			condition += "'";
		}
		else
		if(t_ast == NULL)
		{
			if(condition.empty() == false)
			{
				condition += " AND ";
				and_exists = true;
			}
			condition += in_object;
			condition += " = '";
			condition += table;
			condition += "'";
		}
		if(condition.empty() == false)
		{
			if(conditions.empty() == false)
				conditions += " OR ";
			if(and_exists == true)
			{
				conditions += '(';
				conditions += condition;
				conditions += ')';
			}
			else
				conditions += condition;
			count++;
		}
	}
	if(count > 1)
	{
		output = '(';
		output += conditions;
		output += ')';
	}
	else
	if(count == 1)
		output = conditions;
}
void SqlApiBase :: GetSelectionCriteria ( const char * selection_template , const char * in_schema , const char * in_object , std :: string & output , const char * default_schema , bool upper_case ) { if ( selection_template == NULL ) return ; const char * cur = selection_template ; std :: string conditions ; int count = 0 ; bool more = true ; while ( more ) { std :: string item ; cur = Str :: GetNextInList ( cur , item ) ; if ( item . empty ( ) == true ) { more = false ; break ; } const char * table_template = item . c_str ( ) ; const char * dot = strchr ( table_template , ' ' ) ; std :: string schema ; std :: string table ; if ( dot != NULL ) { schema . assign ( table_template , ( size_t ) ( dot - table_template ) ) ; table = dot + 1 ; } else { if ( default_schema != NULL ) schema = default_schema ; table = table_template ; } char * s_ast = strchr ( ( char * ) schema . c_str ( ) , ' ' ) ; char * t_ast = strchr ( ( char * ) table . c_str ( ) , ' ' ) ; std :: replace ( schema . begin ( ) , schema . end ( ) , ' ' , ' ' ) ; std :: replace ( table . begin ( ) , table . end ( ) , ' ' , ' ' ) ; if ( upper_case == true ) { std :: transform ( schema . begin ( ) , schema . end ( ) , schema . begin ( ) , :: toupper ) ; std :: transform ( table . begin ( ) , table . end ( ) , table . begin ( ) , :: toupper ) ; } std :: string condition ; if ( schema . empty ( ) == false ) { if ( s_ast != NULL && strcmp ( schema . c_str ( ) , " " ) != 0 ) { condition += in_schema ; condition += " " ; condition += schema ; condition += " " ; } else if ( s_ast == NULL ) { condition += in_schema ; condition += " " ; condition += schema ; condition += " " ; } } bool and_exists = false ; if ( t_ast && strcmp ( table . c_str ( ) , " " ) != 0 ) { if ( condition . empty ( ) == false ) { condition += " " ; and_exists = true ; } condition += in_object ; condition += " " ; condition += table ; condition += " " ; } else if ( t_ast == NULL ) { if ( condition . empty ( ) == false ) { condition += " " ; and_exists = true ; } condition += in_object ; condition += " " ; condition += table ; condition += " " ; } if ( condition . empty ( ) == false ) { if ( conditions . empty ( ) == false ) conditions += " " ; if ( and_exists == true ) { conditions += ' ' ; conditions += condition ; conditions += ' ' ; } else conditions += condition ; count ++ ; } } if ( count > 1 ) { output = ' ' ; output += conditions ; output += ' ' ; } else if ( count == 1 ) output = conditions ; }
Build a condition to select objects from the catalog
Build a condition to select objects from the catalog
// Handle each item in the list // Get next table template // Find schema and table delimiter // Define schema and table // if schema name is not specified use the default schema // Convert schema and table to upper case // Add schema filter, do not add LIKE if all schema are searched (*.t1) // No template in the schema name // Add table filter, do not add LIKE if all tables are searched (s1.*) // No template in object name // Condition will be empty in case of *.* // Enclose multiple OR condition with () 