First I have a CString(MFC)
CString csSQL
When format a sql string, for example
csSQL.Format( szFormat, szTableName, szColumn, szValue, intValue )
I need to handle the special char in szValue, so I need write a new class MySQLString
mySQLcs.FormatSQL( szFormat, szTableName, szColumn, szValue, intValue )
which has the functionality
csSQL.Format( szFormat, szTableName, szColumn, HandleSpecialChar(szValue), intValue )
But because the parameter The format function accept is not fixed. I found it difficult. Is there any solution?
You don't add a handler to the CString class.
You write a (new) function
CString FormatSQLString(CString const& format, CString const& tableName, CString const& columnName, CString const& value) {
CString csSQL;
return csSQL.Format(format, tableName, columnName, HandleSpecialChar(value));
}
and you wrap that function further up (e.g. CString GetUpdateStmt(tableName, columnName, value)
) so that you don't have to sprinkle format strings all over your code.
When you write code where you use the input for an SQL statement in a structured way, be that as with a collection of functions such as I proposed, be that by using or creating a SQLQueryStringBuilder
(made up name) class, then you won't have the problem of having to shoehorn something into the string formatting that doesn't belong there.