/** * Insert or update a row in a single database table, based on the data * given and the fields configured for the instance. * * The function will find the fields which are required for this specific * table, based on the names of fields and use only the appropriate data for * this table. Therefore the full submitted data set can be passed in. * * @param string $table Database table name to use (can include an alias) * @param array $where Update condition * @return Database.Result Result from the query or null if no query * performed. * @private */ private function _insert_or_update_table($table, $where = null) { $set = array(); $tableName = $this->_alias($table, 'orig'); $tableAlias = $this->_alias($table, 'alias'); for ($i = 0; $i < count($this->_fields); $i++) { $field = $this->_fields[$i]; $tablePart = $this->_part($field->dbField()); if ($this->_part($field->dbField(), 'db')) { $tablePart = $this->_part($field->dbField(), 'db') . '.' . $tablePart; } // Does this field apply to this table (only check when a join is // being used) if (count($this->_leftJoin) && $tablePart !== $tableAlias) { continue; } // Check if this field should be set, based on options and // submitted data if (!$field->apply('set', $this->_formData)) { continue; } // Some db's (specifically postgres) don't like having the table // name prefixing the column name. TODO it might be nicer to have // the db layer abstract this out? $fieldPart = $this->_part($field->dbField(), 'field'); $set[$fieldPart] = $field->val('set', $this->_formData); // Add where fields if setting where values and required for this // table if ($this->_whereSet) { for ($j = 0, $jen = count($this->_where); $j < $jen; $j++) { $cond = $this->_where[$j]; if ($tableAlias === $this->_table_part($cond['key']) && !isset($set[$cond['key']])) { $set[$cond['key']] = $cond['value']; } } } } // If nothing to do, then do nothing! if (!count($set)) { return null; } // Insert or update if ($where === null) { return $this->_db->insert($this->_table, $set); } else { return $this->_db->push($table, $set, $where); } }
/** * Insert or update a row in a single database table, based on the data * given and the fields configured for the instance. * * The function will find the fields which are required for this specific * table, based on the names of fields and use only the appropriate data for * this table. Therefore the full submitted data set can be passed in. * * @param string $table Database table name to use (can include an alias) * @param array $where Update condition * @return \DataTables\Database\Result Result from the query or null if no query * performed. * @throws \Exception Where set error * @private */ private function _insert_or_update_table($table, $values, $where = null) { $set = array(); $action = $where === null ? 'create' : 'edit'; $tableAlias = $this->_alias($table, 'alias'); for ($i = 0; $i < count($this->_fields); $i++) { $field = $this->_fields[$i]; $tablePart = $this->_part($field->dbField()); if ($this->_part($field->dbField(), 'db')) { $tablePart = $this->_part($field->dbField(), 'db') . '.' . $tablePart; } // Does this field apply to this table (only check when a join is // being used) if (count($this->_leftJoin) && $tablePart !== $tableAlias) { continue; } // Check if this field should be set, based on options and // submitted data if (!$field->apply($action, $values)) { continue; } // Some db's (specifically postgres) don't like having the table // name prefixing the column name. Todo: it might be nicer to have // the db layer abstract this out? $fieldPart = $this->_part($field->dbField(), 'field'); $set[$fieldPart] = $field->val('set', $values); } // Add where fields if setting where values and required for this // table // Note that `whereSet` is now deprecated if ($this->_whereSet) { for ($j = 0, $jen = count($this->_where); $j < $jen; $j++) { $cond = $this->_where[$j]; if (!is_callable($cond)) { // Make sure the value wasn't in the submitted data set, // otherwise we would be overwriting it if (!isset($set[$cond['key']])) { $whereTablePart = $this->_part($cond['key'], 'table'); // No table part on the where condition to match against // or table operating on matches table part from cond. if (!$whereTablePart || $tableAlias == $whereTablePart) { $set[$cond['key']] = $cond['value']; } } else { throw new \Exception('Where condition used as a setter, ' . 'but value submitted for field: ' . $cond['key']); } } } } // If nothing to do, then do nothing! if (!count($set)) { return null; } // Insert or update if ($action === 'create') { return $this->_db->insert($table, $set); } else { return $this->_db->push($table, $set, $where); } }
/** * Update 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 _update_row($db, $parentId, $data) { if (isset($this->_join['table'])) { // Got a link table, just insert the pkey references $db->push($this->_join['table'], array($this->_join['parent'][1] => $parentId, $this->_join['child'][1] => $data[$this->_join['child'][0]]), array($this->_join['parent'][1] => $parentId)); } else { // No link table, just a direct reference $set = array($this->_join['child'] => $parentId); for ($i = 0; $i < count($this->_fields); $i++) { $field = $this->_fields[$i]; if ($field->apply('set', $data)) { $set[$field->dbField()] = $field->val('set', $data); } } // Add WHERE conditions $where = array($this->_join['child'] => $parentId); for ($i = 0, $ien = count($this->_where); $i < $ien; $i++) { $where[$this->_where[$i]['key']] = $this->_where[$i]['value']; // Is there any point in this? Is there any harm? // Note that `whereSet` is now deprecated if ($this->_whereSet) { if (!is_callable($this->_where[$i])) { $set[$this->_where[$i]['key']] = $this->_where[$i]['value']; } } } $db->push($this->_table, $set, $where); } }