[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/joomla/database/src/Pgsql/ -> PgsqlExporter.php (source)

   1  <?php
   2  /**
   3   * Part of the Joomla Framework Database Package
   4   *
   5   * @copyright  Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
   6   * @license    GNU General Public License version 2 or later; see LICENSE
   7   */
   8  
   9  namespace Joomla\Database\Pgsql;
  10  
  11  use Joomla\Database\DatabaseExporter;
  12  
  13  /**
  14   * PDO PostgreSQL Database Exporter.
  15   *
  16   * @since  1.5.0
  17   */
  18  class PgsqlExporter extends DatabaseExporter
  19  {
  20      /**
  21       * Builds the XML data for the tables to export.
  22       *
  23       * @return  string  An XML string
  24       *
  25       * @since   1.0
  26       * @throws  \Exception if an error occurs.
  27       */
  28  	protected function buildXml()
  29      {
  30          $buffer = [];
  31  
  32          $buffer[] = '<?xml version="1.0"?>';
  33          $buffer[] = '<postgresqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">';
  34          $buffer[] = ' <database name="">';
  35  
  36          if ($this->options->withStructure)
  37          {
  38              $buffer = array_merge($buffer, $this->buildXmlStructure());
  39          }
  40  
  41          if ($this->options->withData)
  42          {
  43              $buffer = array_merge($buffer, $this->buildXmlData());
  44          }
  45  
  46          $buffer[] = ' </database>';
  47          $buffer[] = '</postgresqldump>';
  48  
  49          return implode("\n", $buffer);
  50      }
  51  
  52      /**
  53       * Builds the XML structure to export.
  54       *
  55       * @return  array  An array of XML lines (strings).
  56       *
  57       * @since   1.0
  58       * @throws  \Exception if an error occurs.
  59       */
  60  	protected function buildXmlStructure()
  61      {
  62          $buffer = [];
  63  
  64          foreach ($this->from as $table)
  65          {
  66              // Replace the magic prefix if found.
  67              $table = $this->getGenericTableName($table);
  68  
  69              // Get the details columns information.
  70              $fields    = $this->db->getTableColumns($table, false);
  71              $keys      = $this->db->getTableKeys($table);
  72              $sequences = $this->db->getTableSequences($table);
  73  
  74              $buffer[] = '  <table_structure name="' . $table . '">';
  75  
  76              foreach ($sequences as $sequence)
  77              {
  78                  $buffer[] = '   <sequence Name="' . $this->getGenericTableName($sequence->sequence) . '" Schema="' . $sequence->schema . '"' .
  79                      ' Table="' . $table . '" Column="' . $sequence->column . '" Type="' . $sequence->data_type . '"' .
  80                      ' Start_Value="' . $sequence->start_value . '" Min_Value="' . $sequence->minimum_value . '"' .
  81                      ' Max_Value="' . $sequence->maximum_value . '" Last_Value="' . $this->db->getSequenceLastValue($sequence->sequence) . '"' .
  82                      ' Increment="' . $sequence->increment . '" Cycle_option="' . $sequence->cycle_option . '"' .
  83                      ' Is_called="' . $this->db->getSequenceIsCalled($sequence->sequence) . '"' .
  84                      ' />';
  85              }
  86  
  87              foreach ($fields as $field)
  88              {
  89                  $buffer[] = '   <field Field="' . $field->column_name . '" Type="' . $field->type . '" Null="' . $field->null . '"' .
  90                      ' Default="' . $field->Default . '" Comments="' . $field->comments . '" />';
  91              }
  92  
  93              foreach ($keys as $key)
  94              {
  95                  $buffer[] = '   <key Index="' . $this->getGenericTableName($key->idxName) . '" is_primary="' . $key->isPrimary . '"' .
  96                      ' is_unique="' . $key->isUnique . '" Key_name="' . $this->db->getNamesKey($table, $key->indKey) . '"' .
  97                      ' Query=\'' . $key->Query . '\' />';
  98              }
  99  
 100              $buffer[] = '  </table_structure>';
 101          }
 102  
 103          return $buffer;
 104      }
 105  
 106      /**
 107       * Builds the XML data to export.
 108       *
 109       * @return  array  An array of XML lines (strings).
 110       *
 111       * @since   2.0.0
 112       * @throws  \Exception if an error occurs.
 113       */
 114  	protected function buildXmlData()
 115      {
 116          $buffer = [];
 117  
 118          foreach ($this->from as $table)
 119          {
 120              // Replace the magic prefix if found.
 121              $table = $this->getGenericTableName($table);
 122  
 123              // Get the details columns information.
 124              $fields  = $this->db->getTableColumns($table, false);
 125              $colblob = [];
 126  
 127              foreach ($fields as $field)
 128              {
 129                  // Catch blob for xml conversion
 130                  // PostgreSQL binary large object type
 131                  if ($field->Type == 'bytea')
 132                  {
 133                      $colblob[] = $field->Field;
 134                  }
 135              }
 136  
 137              $query = $this->db->getQuery(true);
 138              $query->select($query->quoteName(array_keys($fields)))
 139                  ->from($query->quoteName($table));
 140              $this->db->setQuery($query);
 141  
 142              $rows = $this->db->loadObjectList();
 143  
 144              if (!count($rows))
 145              {
 146                  continue;
 147              }
 148  
 149              $buffer[] = '  <table_data name="' . $table . '">';
 150  
 151              foreach ($rows as $row)
 152              {
 153                  $buffer[] = '   <row>';
 154  
 155                  foreach ($row as $key => $value)
 156                  {
 157                      if (!in_array($key, $colblob))
 158                      {
 159                          $buffer[] = '    <field name="' . $key . '">' . htmlspecialchars($value, ENT_COMPAT, 'UTF-8') . '</field>';
 160                      }
 161                      else
 162                      {
 163                          $buffer[] = '    <field name="' . $key . '">' . stream_get_contents($value) . '</field>';
 164                      }
 165                  }
 166  
 167                  $buffer[] = '   </row>';
 168              }
 169  
 170              $buffer[] = '  </table_data>';
 171          }
 172  
 173          return $buffer;
 174      }
 175  
 176      /**
 177       * Checks if all data and options are in order prior to exporting.
 178       *
 179       * @return  $this
 180       *
 181       * @since   1.5.0
 182       * @throws  \RuntimeException
 183       */
 184  	public function check()
 185      {
 186          // Check if the db connector has been set.
 187          if (!($this->db instanceof PgsqlDriver))
 188          {
 189              throw new \RuntimeException('Database connection wrong type.');
 190          }
 191  
 192          // Check if the tables have been specified.
 193          if (empty($this->from))
 194          {
 195              throw new \RuntimeException('ERROR: No Tables Specified');
 196          }
 197  
 198          return $this;
 199      }
 200  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer