"Fossies" - the Fresh Open Source Software Archive

Member "papayacms-core-6.9.4/src/system/Papaya/Database/Connection/AbstractConnection.php" (13 Dec 2019, 4000 Bytes) of package /linux/www/papayacms-core-6.9.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AbstractConnection.php" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 6.9.0_vs_6.9.1.

    1 <?php
    2 
    3 namespace Papaya\Database\Connection {
    4 
    5   use Papaya\Database\Connection as DatabaseConnection;
    6   use Papaya\Database\Result as DatabaseResult;
    7   use Papaya\Database\Schema as DatabaseSchema;
    8   use Papaya\Database\Source\Name as DataSourceName;
    9   use Papaya\Database\Statement\Prepared as PreparedStatement;
   10   use Papaya\Database\Syntax as DatabaseSyntax;
   11   use Papaya\Database\Syntax\MassInsert;
   12 
   13   abstract class AbstractConnection implements DatabaseConnection {
   14 
   15     /**
   16      * @var DataSourceName
   17      */
   18     private $_dsn;
   19     /**
   20      * @var DatabaseSyntax
   21      */
   22     private $_syntax;
   23     /**
   24      * @var DatabaseSchema
   25      */
   26     private $_schema;
   27 
   28     /**
   29      * @var NULL|DatabaseResult Buffer for the current database result
   30      */
   31     private $_buffer;
   32 
   33     public function __construct(
   34       DataSourceName $dsn, DatabaseSyntax $syntax = NULL, DatabaseSchema $schema = NULL
   35     ) {
   36       $this->_dsn = $dsn;
   37       $this->_syntax = $syntax;
   38       $this->_schema = $schema;
   39     }
   40 
   41     /**
   42      * @return DataSourceName
   43      */
   44     public function getDSN() {
   45       return $this->_dsn;
   46     }
   47 
   48     /**
   49      * @return DatabaseSchema
   50      */
   51     public function schema() {
   52       return $this->_schema;
   53     }
   54 
   55     /**
   56      * @return DatabaseSyntax
   57      */
   58     public function syntax() {
   59       return $this->_syntax;
   60     }
   61 
   62     /**
   63      * Escape a string for database sql
   64      *
   65      * @param mixed $literal Value to escape
   66      * @return string escaped value.
   67      */
   68     public function escapeString($literal) {
   69       return str_replace("'", "''", $this->ensureString($literal));
   70     }
   71 
   72     /**
   73      * @param mixed $literal
   74      * @return string
   75      */
   76     protected function ensureString($literal) {
   77       if (is_bool($literal)) {
   78         return $literal ? '1' : '0';
   79       }
   80       if (isset($literal)) {
   81         return (string)$literal;
   82       }
   83       return '';
   84     }
   85 
   86     /**
   87      * Escape and quote a string for the database sql
   88      *
   89      * @param mixed $literal Value to escape
   90      * @return string escaped value.
   91      */
   92     public function quoteString($literal) {
   93       return "'".$this->escapeString($literal)."'";
   94     }
   95 
   96     /**
   97      * @param string $name
   98      * @param string $quoteChar
   99      * @return string
  100      */
  101     public function quoteIdentifier($name, $quoteChar = '"') {
  102       $quotedParts = array_map(
  103         static function ($part) use ($name, $quoteChar) {
  104           if (!preg_match('(^[a-z\\d_ ]+$)Di', $part)) {
  105             throw new \InvalidArgumentException(
  106               "Invalid identifier name: $name"
  107             );
  108           }
  109           return $quoteChar.$part.$quoteChar;
  110         },
  111         explode('.', trim($name))
  112       );
  113       return implode('.', $quotedParts);
  114     }
  115 
  116     /**
  117      * Insert records into table
  118      *
  119      * @param string $table
  120      * @param array $values
  121      * @access public
  122      * @return boolean
  123      */
  124     public function insert($table, array $values) {
  125       $insert = new MassInsert($this, $table, $values);
  126       return $insert();
  127     }
  128 
  129     /**
  130      * @param string $name
  131      * @param bool $usePrefix
  132      * @return string
  133      */
  134     public function getTableName($name, $usePrefix = FALSE) {
  135       return $name;
  136     }
  137 
  138     /**
  139      * @param string $sql
  140      * @return PreparedStatement
  141      */
  142     public function prepare($sql) {
  143       return new PreparedStatement($this, $sql);
  144     }
  145 
  146     /**
  147      * @param string $name
  148      * @param callable $function
  149      */
  150     public function registerFunction($name, callable $function) {
  151       throw new \LogicException(
  152         sprintf('Not implemented: %s::registerFunction()', static::class)
  153       );
  154     }
  155 
  156     protected function cleanup() {
  157       if (
  158         $this->_buffer instanceof DatabaseResult
  159       ) {
  160         $this->_buffer->free();
  161         $this->_buffer = NULL;
  162       }
  163     }
  164 
  165     protected function buffer(DatabaseResult $buffer = NULL) {
  166       if (NULL !== $buffer) {
  167         $this->_buffer = $buffer;
  168       }
  169       return $this->_buffer;
  170     }
  171   }
  172 }