| Server IP : 202.61.199.114 / Your IP : 216.73.217.139 Web Server : nginx/1.22.1 System : Linux de.arni-solutions.de 6.1.0-49-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.174-1 (2026-05-26) x86_64 User : web20 ( 1018) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/clients/client2/web18/web/vendor/psx/sql/src/ |
Upload File : |
<?php
/*
* PSX is a open source PHP framework to develop RESTful APIs.
* For the current version and informations visit <http://phpsx.org>
*
* Copyright 2010-2017 Christoph Kappestein <christoph.kappestein@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace PSX\Sql;
use InvalidArgumentException;
use PSX\Record\RecordInterface;
use RuntimeException;
/**
* TableManipulationTrait
*
* @author Christoph Kappestein <christoph.kappestein@gmail.com>
* @license http://www.apache.org/licenses/LICENSE-2.0
* @link http://phpsx.org
*/
trait TableManipulationTrait
{
protected $lastInsertId;
/**
* @param array|\stdClass|\PSX\Record\RecordInterface $record
* @return integer
*/
public function create($record)
{
$fields = $this->serializeFields($this->getArray($record));
if (!empty($fields)) {
$result = $this->connection->insert($this->getName(), $fields);
// set last insert id
$this->lastInsertId = $this->connection->lastInsertId();
return $result;
} else {
throw new RuntimeException('No valid field set');
}
}
/**
* @param array|\stdClass|\PSX\Record\RecordInterface $record
* @return integer
*/
public function update($record)
{
$fields = $this->serializeFields($this->getArray($record));
if (!empty($fields)) {
$pk = $this->getPrimaryKey();
if (isset($fields[$pk])) {
$condition = [$pk => $fields[$pk]];
} else {
throw new RuntimeException('No primary key set');
}
return $this->connection->update($this->getName(), $fields, $condition);
} else {
throw new RuntimeException('No valid field set');
}
}
/**
* @param array|\stdClass|\PSX\Record\RecordInterface $record
* @return integer
*/
public function delete($record)
{
$fields = $this->serializeFields($this->getArray($record));
if (!empty($fields)) {
$pk = $this->getPrimaryKey();
if (isset($fields[$pk])) {
$condition = [$pk => $fields[$pk]];
} else {
throw new RuntimeException('No primary key set');
}
return $this->connection->delete($this->getName(), $condition);
} else {
throw new RuntimeException('No valid field set');
}
}
/**
* @return integer
*/
public function getLastInsertId()
{
return $this->lastInsertId;
}
/**
* Returns an array which can be used by the dbal insert, update and delete
* methods
*
* @param array $row
* @return array
*/
protected function serializeFields(array $row)
{
$data = array();
$columns = $this->getColumns();
$builder = $this->newQueryBuilder($this->getName());
$parts = $builder->getQueryPart('from');
$part = reset($parts);
$alias = $part['alias'];
foreach ($columns as $name => $type) {
if (!empty($alias)) {
$pos = strpos($name, $alias . '.');
if ($pos !== false) {
$name = substr($name, strlen($alias) + 1);
}
}
if (isset($row[$name])) {
$data[$name] = $this->serializeType($row[$name], $type);
}
}
return $data;
}
protected function getArray($record)
{
if ($record instanceof RecordInterface) {
return $record->getProperties();
} elseif ($record instanceof \stdClass) {
return (array) $record;
} elseif ($record instanceof \ArrayObject) {
return $record->getArrayCopy();
} elseif (is_array($record)) {
return $record;
} else {
throw new InvalidArgumentException('Record must bei either an PSX\Record\RecordInterface, stdClass or array');
}
}
}