Skip to content

Commit

Permalink
new method insertFrom
Browse files Browse the repository at this point in the history
  • Loading branch information
AgelxNash committed Jun 17, 2018
1 parent 8b31124 commit 1638bc6
Showing 1 changed file with 52 additions and 28 deletions.
80 changes: 52 additions & 28 deletions src/AbstractDatabase.php
Expand Up @@ -372,40 +372,20 @@ public function insert(
$where = '',
$limit = ''
) {
$table = $this->prepareFrom($table);

$useFields = null;
$lid = null;

if (\is_array($fields)) {
$useFields = empty($fromTable) ?
$this->prepareValues($fields) :
$this->prepareFields($fields, true);
} else {
$useFields = $fields;
}
if (empty($fromTable)) {
$table = $this->prepareFrom($table);
$useFields = \is_array($fields) ? $this->prepareValues($fields) : $fields;

if (empty($useFields) || ! \is_scalar($useFields) || ($useFields === '*' && ! empty($fromTable))) {
throw (new Exceptions\InvalidFieldException('Invalid insert fields'))
->setData($fields);
}
if (empty($useFields) || ! \is_scalar($useFields) || $useFields === '*') {
throw (new Exceptions\InvalidFieldException('Invalid insert fields'))
->setData($fields);
}

if (empty($fromTable)) {
$this->query("INSERT INTO {$table} {$useFields}");
} else {
if (empty($fromFields) || $fromFields === '*') {
$fromFields = $this->prepareFields($fields, true);
} else {
$fromFields = $this->prepareFields($fromFields, true);
}

$where = $this->prepareWhere($where);
$limit = $this->prepareLimit($limit);

$lid = $this->query(
"INSERT INTO {$table} ({$useFields}) SELECT {$fromFields} FROM {$fromTable} {$where} {$limit}"
);
$lid = $this->isResult($lid) ? true : $lid;
$lid = $this->insertFrom($fields, $table, $fromFields, $fromTable, $where, $limit);
}

if ($lid === null && ($lid = $this->getInsertId()) === false) {
Expand All @@ -415,6 +395,50 @@ public function insert(
return $this->convertValue($lid);
}

/**
* @param string|array $fields
* @param string $table
* @param string|array $fromFields
* @param string $fromTable
* @param string|array $where
* @param string $limit
* @return mixed
* @throws Exceptions\InvalidFieldException
* @throws Exceptions\TableNotDefinedException
*/
public function insertFrom(
$fields,
$table,
$fromFields = '*',
$fromTable = '',
$where = '',
$limit = ''
) {
$table = $this->prepareFrom($table);
$lid = null;
$useFields = \is_array($fields) ? $this->prepareFields($fields, true) : $fields;

if (empty($useFields) || ! \is_scalar($useFields) || $useFields === '*') {
throw (new Exceptions\InvalidFieldException('Invalid insert fields'))
->setData($fields);
}

if (empty($fromFields) || $fromFields === '*') {
$fromFields = $this->prepareFields($fields, true);
} else {
$fromFields = $this->prepareFields($fromFields, true);
}

$where = $this->prepareWhere($where);
$limit = $this->prepareLimit($limit);

$lid = $this->query(
"INSERT INTO {$table} ({$useFields}) SELECT {$fromFields} FROM {$fromTable} {$where} {$limit}"
);

return $this->isResult($lid) ? true : $lid;
}

/**
* {@inheritDoc}
*/
Expand Down

0 comments on commit 1638bc6

Please sign in to comment.