/**
  * loadScripts
  *
  * @param mixed $connection        	
  * @static
  *
  * @access public
  * @return void
  */
 public static function loadScripts($connection)
 {
     $sDirectoryScripts = DBDataBaseMigration::$sPathScrips;
     if (!is_dir($sDirectoryScripts)) {
         throw new Exception("ERRO: Diretório {$sDirectoryScripts} não existe!\n");
     }
     DBDataBaseMigration::checkTablesVersioning($connection);
     $sSqlLastVersion = "SELECT db142_versao FROM configuracao.database_version ORDER BY db142_versao DESC LIMIT 1";
     $rLastVersion = @pg_query($connection, $sSqlLastVersion);
     if (!$rLastVersion) {
         throw new Exception("ERRO: Ao executar SQL {$sSqlLastVersion} ");
     }
     $sLastVersion = null;
     if (pg_num_rows($rLastVersion) > 0) {
         $sLastVersion = pg_result($rLastVersion, 0, 0);
     }
     $sDirectoryCheck = $sDirectoryScripts;
     $aDiretorios = DBFileExplorer::listarDiretorio($sDirectoryCheck, true, false);
     sort($aDiretorios);
     foreach ($aDiretorios as $sDiretorio) {
         $sVersao = str_replace($sDirectoryScripts, '', $sDiretorio);
         $sVersao = str_replace("/", '', $sVersao);
         $lExisteDDL = is_file("{$sDiretorio}/ddl_{$sVersao}.sql");
         $aArquivosExecucao = array();
         if ($lExisteDDL && filesize("{$sDiretorio}/ddl_{$sVersao}.sql") > 0) {
             $aArquivosExecucao["ddl"] = file_get_contents("{$sDiretorio}/ddl_{$sVersao}.sql");
         }
         $sVersaoEscapada = pg_escape_string($sVersao);
         $sSqlExists = "SELECT * FROM configuracao.database_version WHERE db142_versao = '{$sVersaoEscapada}'";
         $rsExists = @pg_query($connection, $sSqlExists);
         if (!$rsExists) {
             throw new Exception("ERRO: Ao executar SQL {$sSqlExists} . " . pg_last_error());
         }
         if (pg_num_rows($rsExists) == 0) {
             $sInsert = "INSERT INTO configuracao.database_version VALUES ('{$sVersaoEscapada}')";
             $rInsert = @pg_query($connection, $sInsert);
             if (!$rInsert) {
                 throw new Exception("Erro ao Incluir Versão {$sVersao}" . pg_last_error($connection));
             }
         }
         foreach ($aArquivosExecucao as $sTipo => $sConteudo) {
             $sSqlExists = "SELECT * FROM configuracao.database_version_sql WHERE db143_versao = '{$sVersaoEscapada}' and db143_tipo = '{$sTipo}' and db143_executado is true;";
             $rsExists = @pg_query($connection, $sSqlExists);
             if (!$rsExists) {
                 throw new Exception("ERRO: Ao executar SQL {$sSqlExists} " . pg_last_error());
             }
             if (pg_num_rows($rsExists) > 0) {
                 continue;
             }
             $sArquivoEscapado = pg_escape_string("{$sDiretorio}/{$sTipo}_{$sVersao}.sql");
             $sConteudoEscapado = pg_escape_string($sConteudo);
             $rDelete = @pg_query($connection, "delete from configuracao.database_version_sql WHERE db143_versao = '{$sVersaoEscapada}' and db143_tipo = '{$sTipo}'");
             if (!$rDelete) {
                 throw new Exception("Erro ao Remover script {$sVersao}" . pg_last_error($connection));
             }
             $sInsertSql = "INSERT INTO configuracao.database_version_sql (db143_arquivo, db143_versao, db143_script, db143_tipo) ";
             $sInsertSql .= "     VALUES ('{$sArquivoEscapado}', '{$sVersaoEscapada}', '{$sConteudoEscapado}','{$sTipo}')";
             $rInsertSql = @pg_query($connection, $sInsertSql);
             if (!$rInsertSql) {
                 throw new Exception("Erro ao Incluir script {$sVersao}" . pg_last_error($connection));
             }
         }
     }
 }