/**
  * Imports a SQL dump into the database.
  * This function can not be called statically.
  * @returns True if successful, false if not successful and report_errors = false or
  *               Error object if report_errors = true
  */
 public static function import($text, $report_errors = true)
 {
     PHPWS_DB::touchDB();
     // first_import makes sure at least one query was completed
     // successfully
     $first_import = false;
     $sqlArray = PHPWS_Text::sentence($text);
     $error = false;
     foreach ($sqlArray as $sqlRow) {
         if (empty($sqlRow) || preg_match("/^[^\\w\\d\\s\\(\\)]/i", $sqlRow)) {
             continue;
         }
         $sqlCommand[] = $sqlRow;
         if (preg_match("/;\$/", $sqlRow)) {
             $query = implode(' ', $sqlCommand);
             $sqlCommand = array();
             if (!DB_ALLOW_TABLE_INDEX && preg_match('/^create index/i', $query)) {
                 continue;
             }
             PHPWS_DB::homogenize($query);
             $result = PHPWS_DB::query($query);
             if (DB::isError($result)) {
                 if ($report_errors) {
                     return $result;
                 } else {
                     PHPWS_Error::log($result);
                     $error = true;
                 }
             }
             $first_import = true;
         }
     }
     if (!$first_import) {
         if ($report_errors) {
             return PHPWS_Error::get(PHPWS_DB_IMPORT_FAILED, 'core', 'PHPWS_DB::import');
         } else {
             PHPWS_Error::log(PHPWS_DB_IMPORT_FAILED, 'core', 'PHPWS_DB::import');
             $error = true;
         }
     }
     if ($error) {
         return false;
     } else {
         return true;
     }
 }
Esempio n. 2
0
 public function export($structure = true, $contents = true)
 {
     PHPWS_DB::touchDB();
     $table = $this->addPrefix($this->tables[0]);
     if ($structure == true) {
         $GLOBALS['PHPWS_DB']['connection']->loadModule('Reverse', null, true);
         $columns = $GLOBALS['PHPWS_DB']['connection']->tableInfo($table);
         $column_info = $this->parseColumns($columns);
         $index = $this->getIndex();
         $sql[] = "CREATE TABLE {$table} ( " . implode(', ', $column_info['parameters']) . ' );';
         if (isset($column_info['index'])) {
             $sql = array_merge($sql, $column_info['index']);
         }
     }
     if ($contents == true) {
         if ($rows = $this->select()) {
             if (PHPWS_Error::isError($rows)) {
                 return $rows;
             }
             foreach ($rows as $dataRow) {
                 foreach ($dataRow as $key => $value) {
                     $allKeys[] = $key;
                     $allValues[] = PHPWS_DB::escape($value);
                 }
                 $sql[] = "INSERT INTO {$table} (" . implode(', ', $allKeys) . ') VALUES (' . implode(', ', $allValues) . ');';
                 $allKeys = $allValues = array();
             }
         }
     }
     if (!empty($sql)) {
         return implode("\n", $sql);
     } else {
         return null;
     }
 }