static HRESULT ConfigureSqlData( __in SCA_ACTION saAction ) { //AssertSz(FALSE, "debug ConfigureSqlData()"); HRESULT hr = S_OK; SCA_DB* psdList = NULL; SCA_SQLSTR* psssList = NULL; // check for the prerequsite tables if (S_OK != WcaTableExists(L"SqlDatabase")) { WcaLog(LOGMSG_VERBOSE, "skipping SQL CustomAction, no SqlDatabase table"); ExitFunction1(hr = S_FALSE); } // read tables hr = ScaDbsRead(&psdList, saAction); ExitOnFailure(hr, "failed to read SqlDatabase table"); hr = ScaSqlStrsRead(&psssList, saAction); ExitOnFailure(hr, "failed to read SqlStrings table"); hr = ScaSqlStrsReadScripts(&psssList, saAction); ExitOnFailure(hr, "failed to read SqlScripts table"); if (SCA_ACTION_UNINSTALL == saAction) { // do uninstall actions (order is important!) hr = ScaSqlStrsUninstall(psdList, psssList); ExitOnFailure(hr, "failed to execute uninstall SQL strings"); hr = ScaDbsUninstall(psdList); ExitOnFailure(hr, "failed to uninstall databases"); } else { // do install actions (order is important!) hr = ScaDbsInstall(psdList); ExitOnFailure(hr, "failed to install databases"); hr = ScaSqlStrsInstall(psdList, psssList); ExitOnFailure(hr, "failed to execute install SQL strings, length may be too long, try add GO to break up"); } LExit: if (psssList) ScaSqlStrsFreeList(psssList); if (psdList) ScaDbsFreeList(psdList); return hr; }
HRESULT ScaDbsRead( __inout SCA_DB** ppsdList, __in SCA_ACTION saAction ) { HRESULT hr = S_OK; UINT er = ERROR_SUCCESS; PMSIHANDLE hView; PMSIHANDLE hRec; PMSIHANDLE hViewFileSpec = NULL; LPWSTR pwzData = NULL; LPWSTR pwzId = NULL; LPWSTR pwzComponent = NULL; SCA_DB* psd = NULL; if (S_OK != WcaTableExists(L"SqlDatabase")) { WcaLog(LOGMSG_VERBOSE, "Skipping ScaCreateDatabase() - SqlDatabase table not present"); ExitFunction1(hr = S_FALSE); } if (S_OK == WcaTableExists(L"SqlFileSpec")) { hr = WcaOpenView(vcsSqlFileSpecQuery, &hViewFileSpec); ExitOnFailure(hr, "failed to open view on SqlFileSpec table"); } // loop through all the sql databases hr = WcaOpenExecuteView(vcsSqlDatabaseQuery, &hView); ExitOnFailure(hr, "Failed to open view on SqlDatabase table"); while (S_OK == (hr = WcaFetchRecord(hView, &hRec))) { BOOL fHasComponent = FALSE; INSTALLSTATE isInstalled = INSTALLSTATE_UNKNOWN; INSTALLSTATE isAction = INSTALLSTATE_UNKNOWN; hr = WcaGetRecordString(hRec, sdqSqlDb, &pwzId); ExitOnFailure(hr, "Failed to get SqlDatabase.SqlDb"); hr = WcaGetRecordString(hRec, sdqComponent, &pwzComponent); ExitOnFailure1(hr, "Failed to get Component for database: '%ls'", psd->wzKey); if (pwzComponent && *pwzComponent) { fHasComponent = TRUE; er = ::MsiGetComponentStateW(WcaGetInstallHandle(), pwzComponent, &isInstalled, &isAction); hr = HRESULT_FROM_WIN32(er); ExitOnFailure1(hr, "Failed to get state for component: %ls", pwzComponent); // If we're doing install but the Component is not being installed or we're doing // uninstall but the Component is not being uninstalled, skip it. if ((WcaIsInstalling(isInstalled, isAction) && SCA_ACTION_INSTALL != saAction) || (WcaIsUninstalling(isInstalled, isAction) && SCA_ACTION_UNINSTALL != saAction)) { continue; } } hr = NewDb(&psd); ExitOnFailure1(hr, "Failed to allocate memory for new database: %D", pwzId); hr = ::StringCchCopyW(psd->wzKey, countof(psd->wzKey), pwzId); ExitOnFailure1(hr, "Failed to copy SqlDatabase.SqlDbL: %ls", pwzId); hr = ::StringCchCopyW(psd->wzComponent, countof(psd->wzComponent), pwzComponent); ExitOnFailure1(hr, "Failed to copy SqlDatabase.Component_: %ls", pwzComponent); psd->fHasComponent = fHasComponent; psd->isInstalled = isInstalled; psd->isAction = isAction; hr = WcaGetRecordFormattedString(hRec, sdqServer, &pwzData); ExitOnFailure1(hr, "Failed to get Server for database: '%ls'", psd->wzKey); hr = ::StringCchCopyW(psd->wzServer, countof(psd->wzServer), pwzData); ExitOnFailure1(hr, "Failed to copy server string to database object:%ls", pwzData); hr = WcaGetRecordFormattedString(hRec, sdqInstance, &pwzData); ExitOnFailure1(hr, "Failed to get Instance for database: '%ls'", psd->wzKey); hr = ::StringCchCopyW(psd->wzInstance, countof(psd->wzInstance), pwzData); ExitOnFailure1(hr, "Failed to copy instance string to database object:%ls", pwzData); hr = WcaGetRecordFormattedString(hRec, sdqDatabase, &pwzData); ExitOnFailure1(hr, "Failed to get Database for database: '%ls'", psd->wzKey); hr = ::StringCchCopyW(psd->wzDatabase, countof(psd->wzDatabase), pwzData); ExitOnFailure1(hr, "Failed to copy database string to database object:%ls", pwzData); hr = WcaGetRecordInteger(hRec, sdqAttributes, &psd->iAttributes); ExitOnFailure(hr, "Failed to get SqlDatabase.Attributes"); hr = WcaGetRecordFormattedString(hRec, sdqUser, &pwzData); ExitOnFailure1(hr, "Failed to get User record for database: '%ls'", psd->wzKey); // if a user was specified if (*pwzData) { psd->fUseIntegratedAuth = FALSE; hr = ScaGetUser(pwzData, &psd->scau); ExitOnFailure1(hr, "Failed to get user information for database: '%ls'", psd->wzKey); } else { psd->fUseIntegratedAuth = TRUE; // integrated authorization doesn't have a User record } hr = WcaGetRecordString(hRec, sdqDbFileSpec, &pwzData); ExitOnFailure1(hr, "Failed to get Database FileSpec for database: '%ls'", psd->wzKey); // if a database filespec was specified if (*pwzData) { hr = GetFileSpec(hViewFileSpec, pwzData, &psd->sfDb); ExitOnFailure1(hr, "failed to get FileSpec for: %ls", pwzData); if (S_OK == hr) { psd->fHasDbSpec = TRUE; } } hr = WcaGetRecordString(hRec, sdqLogFileSpec, &pwzData); ExitOnFailure1(hr, "Failed to get Log FileSpec for database: '%ls'", psd->wzKey); // if a log filespec was specified if (*pwzData) { hr = GetFileSpec(hViewFileSpec, pwzData, &psd->sfLog); ExitOnFailure1(hr, "failed to get FileSpec for: %ls", pwzData); if (S_OK == hr) { psd->fHasLogSpec = TRUE; } } *ppsdList = AddDbToList(*ppsdList, psd); psd = NULL; // set the db NULL so it doesn't accidentally get freed below } if (E_NOMOREITEMS == hr) { hr = S_OK; } ExitOnFailure(hr, "Failure occured while processing SqlDatabase table"); LExit: if (psd) { ScaDbsFreeList(psd); } ReleaseStr(pwzComponent); ReleaseStr(pwzId); ReleaseStr(pwzData); return hr; }