Ejemplo n.º 1
0
 /**
  * Delete one or more rows from the database for an individual table
  *
  *  @param string $table Database table name to use
  *  @param array $ids Array of ids to remove
  *  @param string $pkey Database column name to match the ids on for the
  *    delete condition. If not given the instance's base primary key is
  *    used.
  *  @private
  */
 function _remove_table($table, $ids, $pkey = null)
 {
     if ($pkey === null) {
         $pkey = $this->_pkey;
     }
     $stmt = $this->_db->query('delete')->table($table)->or_where($pkey, $ids)->exec();
 }
Ejemplo n.º 2
0
 /**
  * Delete one or more rows from the database for an individual table
  *
  * @param string $table Database table name to use
  * @param array $ids Array of ids to remove
  * @param string $pkey Database column name to match the ids on for the
  *   delete condition. If not given the instance's base primary key is
  *   used.
  * @private
  */
 function _remove_table($table, $ids, $pkey = null)
 {
     if ($pkey === null) {
         $pkey = $this->_pkey;
     }
     // Check there is a field which has a set option for this table
     $count = 0;
     foreach ($this->_fields as $field) {
         if (strpos($field->dbField(), '.') === false || $this->_part($field->dbField(), 'table') === $table && $field->set() !== Field::SET_NONE) {
             $count++;
         }
     }
     if ($count > 0) {
         $this->_db->query('delete')->table($table)->or_where($pkey, $ids)->exec();
     }
 }
Ejemplo n.º 3
0
 /**
  * Create a row.
  *  @param \DataTables\Database $db Database reference to use
  *  @param int $parentId Parent row's primary key value
  *  @param string[] $data Data to be set for the join
  *  @private
  */
 private function _insert($db, $parentId, $data)
 {
     if (isset($this->_join['table'])) {
         // Insert keys into the join table
         $stmt = $db->query('insert')->table($this->_join['table'])->set($this->_join['parent'][1], $parentId)->set($this->_join['child'][1], $data[$this->_join['child'][0]])->exec();
     } else {
         // Insert values into the target table
         $stmt = $db->query('insert')->table($this->_table)->set($this->_join['child'], $parentId);
         for ($i = 0; $i < count($this->_fields); $i++) {
             $field = $this->_fields[$i];
             if ($field->apply('set', $data)) {
                 $stmt->set($field->dbField(), $field->val('set', $data));
             }
         }
         // If the where condition variables should also be added to the database
         // Note that `whereSet` is now deprecated
         if ($this->_whereSet) {
             for ($i = 0, $ien = count($this->_where); $i < $ien; $i++) {
                 if (!is_callable($this->_where[$i])) {
                     $stmt->set($this->_where[$i]['key'], $this->_where[$i]['value']);
                 }
             }
         }
         $stmt->exec();
     }
 }
Ejemplo n.º 4
0
 /**
  * Add a record to the database for a newly uploaded file
  *
  * @param  \DataTables\Database $db Database instance
  * @return int Primary key value for the newly uploaded file
  */
 private function _dbExec($db)
 {
     $upload = $_FILES['upload'];
     $pathFields = array();
     // Insert the details requested, for the columns requested
     $q = $db->query('insert')->table($this->_dbTable);
     foreach ($this->_dbFields as $column => $prop) {
         switch ($prop) {
             case self::DB_CONTENT:
                 $q->set($column, file_get_contents($upload['tmp_name']));
                 break;
             case self::DB_CONTENT_TYPE:
             case self::DB_MIME_TYPE:
                 $finfo = finfo_open(FILEINFO_MIME);
                 $mime = finfo_file($finfo, $upload['tmp_name']);
                 finfo_close($finfo);
                 $q->set($column, $mime);
                 break;
             case self::DB_EXTN:
                 $extn = pathinfo($upload['name'], PATHINFO_EXTENSION);
                 $q->set($column, $extn);
                 break;
             case self::DB_FILE_NAME:
                 $q->set($column, $upload['name']);
                 break;
             case self::DB_FILE_SIZE:
                 $q->set($column, $upload['size']);
                 break;
             case self::DB_SYSTEM_PATH:
                 $pathFields[$column] = self::DB_SYSTEM_PATH;
                 $q->set($column, '-');
                 // Use a temporary value to avoid cases
                 break;
                 // where the db will reject empty values
             // where the db will reject empty values
             case self::DB_WEB_PATH:
                 $pathFields[$column] = self::DB_WEB_PATH;
                 $q->set($column, '-');
                 // Use a temporary value (as above)
                 break;
             default:
                 if (is_callable($prop) && is_object($prop)) {
                     // is a closure
                     $q->set($column, $prop($db, $upload));
                 } else {
                     $q->set($column, $prop);
                 }
                 break;
         }
     }
     $res = $q->exec();
     $id = $res->insertId();
     // Update the newly inserted row with the path information. We have to
     // use a second statement here as we don't know in advance what the
     // database schema is and don't want to prescribe that certain triggers
     // etc be created. It makes it a bit less efficient but much more
     // compatible
     if (count($pathFields)) {
         // For this to operate the action must be a string, which is
         // validated in the `exec` method
         $path = $this->_path($upload['name'], $id);
         $webPath = str_replace($_SERVER['DOCUMENT_ROOT'], '', $path);
         $q = $db->query('update')->table($this->_dbTable)->where($this->_dbPKey, $id);
         foreach ($pathFields as $column => $type) {
             $q->set($column, $type === self::DB_WEB_PATH ? $webPath : $path);
         }
         $q->exec();
     }
     return $id;
 }