public static function getPdo() { global $CMATIC; $conf = $CMATIC['conf']; $pdo = new PDO(PdoHelper::getPgsqlDsn($conf['db']['host'], $conf['db']['port'], $conf['db']['db']), $conf['db']['user'], $conf['db']['password']); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $pdo; }
$errorMessages[] = 'The table prefix must start with a letter and may only contain letters, numbers, and underscores'; } } if (count($errorMessages) > 0) { throw new CmaticInstallerException($errorMessages); } try { // TODO: Probably should refactor this logic out into a function // Create the schema in the DB // Open the file, replace the table prefix, strip comments $schemaFile = 'schema/postgres8.sql'; $sql = fread(fopen($schemaFile, 'r'), filesize($schemaFile)); $sql = preg_replace('/cmatic_/', $requestParams['tablePrefix'], $sql); // Strip away inline comments beginning with "--" or "//" $sql = PdoHelper::removeComments($sql); $db = new PDO(PdoHelper::getPgsqlDsn($requestParams['host'], $requestParams['port'], $requestParams['db']), $requestParams['user'], $requestParams['password']); // The squeaky wheel gets the oil, make it known when things went wrong. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->beginTransaction(); try { foreach ($sql as $singleSql) { $singleSql = trim($singleSql); if (!empty($singleSql)) { $db->exec($singleSql); } } $db->commit(); // This should be in a "finally" block, but PHP doesn't have such a thing?! $db = null; } catch (Exception $e) { // Roll back the transaction and then re-throw the exception