patterncppMinor
Construct SQL query with a lot of parameters from configuration
Viewed 0 times
sqlwithqueryconfigurationlotconstructfromparameters
Problem
I have implemented an interface through which one can select, update and remove items of the
The database, column and table names are all configured in a settings file.
Because of this I have to call the interface which gets the configuration from the settings file while preparing my query. This gives me a huge code block of code with little string concatenations in between:
```
updateStmt(connection->prepareStatement(
"INSERT INTO " + GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_TABLE_NAME)
+ "(" + GETSET(PTH_STORAGE_SYSTEM_IDCOL) + ","
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_OWNERCOL) + ","
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_NAMECOL) + ","
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_DESCRIPTIONCOL) + ","
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_PASSWORDCOL) + ","
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_BALANCECOL) + ","
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_LOCKEDCOL) + ","
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_PASSWORDEDTRANSACTIONSCOL)
+ "," + GETSET(PTH_STORAGE_SYSTEM_CHANGEDCOL) + ","
+ GETSET(PTH_STORAGE_SYSTEM_CREATEDCOL) + ","
+ GETSET(PTH_STORAGE_SYSTEM_DELETEDCOL)
+ ") VALUES (?,?,?,?,?,?,?,?,NOW(),NOW(),NULL) ON DUPLICATE KEY UPDATE"
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_OWNERCOL) + "=VALUES("
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_OWNERCOL) + "), "
Account class in a MySQL database. For the three functions I'm preparing statements.The database, column and table names are all configured in a settings file.
Because of this I have to call the interface which gets the configuration from the settings file while preparing my query. This gives me a huge code block of code with little string concatenations in between:
```
updateStmt(connection->prepareStatement(
"INSERT INTO " + GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_TABLE_NAME)
+ "(" + GETSET(PTH_STORAGE_SYSTEM_IDCOL) + ","
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_OWNERCOL) + ","
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_NAMECOL) + ","
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_DESCRIPTIONCOL) + ","
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_PASSWORDCOL) + ","
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_BALANCECOL) + ","
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_LOCKEDCOL) + ","
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_PASSWORDEDTRANSACTIONSCOL)
+ "," + GETSET(PTH_STORAGE_SYSTEM_CHANGEDCOL) + ","
+ GETSET(PTH_STORAGE_SYSTEM_CREATEDCOL) + ","
+ GETSET(PTH_STORAGE_SYSTEM_DELETEDCOL)
+ ") VALUES (?,?,?,?,?,?,?,?,NOW(),NOW(),NULL) ON DUPLICATE KEY UPDATE"
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_OWNERCOL) + "=VALUES("
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_OWNERCOL) + "), "
Solution
Well to be blunt as it stands it is totally unreadable and thus unmaintainable.
You should break that up into multiple variables that are combined in a way that is readable.
You should break that up into multiple variables that are combined in a way that is readable.
std::stringstream columnName;
std::stringstream statement;
columnNames << storageAccount << ", "
<< storageName << ", "
<< storageDesc << ", "
<< storagePass << ", "
<< storageBalance << ", "
<< storageLoc << ", "
<< storageAccount << ", "
<< storageChanged << ","
<< storageDelCol;
statement << "INSERT INTO " << table
<< " (" << columnNames.str() << ") "
<< " VALUES(?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW(), NULL) "
<< "ON DUPLICATE KEY UPDATE "
<< storageAccount << " = VALUES(" << storageAccount << "), "
<< storageName << " = VALUES(" << storageName << "), "
<< storageDesc << " = VALUES(" << storageDesc << "), "
<< storagePass << " = VALUES(" << storagePass << "), "
<< storageBalance << " = VALUES(" << storageBalance << "), "
<< storageLoc << " = VALUES(" << storageLoc << "), "
<< storageAccount << " = VALUES(" << storageAccount << "), "
<< storageChanged << " = NOW(),"
<< storageDelCol << " = NULL";Code Snippets
std::stringstream columnName;
std::stringstream statement;
columnNames << storageAccount << ", "
<< storageName << ", "
<< storageDesc << ", "
<< storagePass << ", "
<< storageBalance << ", "
<< storageLoc << ", "
<< storageAccount << ", "
<< storageChanged << ","
<< storageDelCol;
statement << "INSERT INTO " << table
<< " (" << columnNames.str() << ") "
<< " VALUES(?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW(), NULL) "
<< "ON DUPLICATE KEY UPDATE "
<< storageAccount << " = VALUES(" << storageAccount << "), "
<< storageName << " = VALUES(" << storageName << "), "
<< storageDesc << " = VALUES(" << storageDesc << "), "
<< storagePass << " = VALUES(" << storagePass << "), "
<< storageBalance << " = VALUES(" << storageBalance << "), "
<< storageLoc << " = VALUES(" << storageLoc << "), "
<< storageAccount << " = VALUES(" << storageAccount << "), "
<< storageChanged << " = NOW(),"
<< storageDelCol << " = NULL";Context
StackExchange Code Review Q#157652, answer score: 2
Revisions (0)
No revisions yet.