modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-06-24 20:51:32 +04:00
parent 9090543924
commit 76a5b9a850
6 changed files with 86 additions and 24 deletions

View File

@ -64,7 +64,7 @@ class Capacitor implements ITransactor {
return $this->db()->inTransaction(); return $this->db()->inTransaction();
} }
function beginTransaction(?callable $func=null): void { function beginTransaction(?callable $func=null, bool $commit=true): void {
if ($this->subManageTransactions === null && $this->subChannels !== null) { if ($this->subManageTransactions === null && $this->subChannels !== null) {
foreach ($this->subChannels as $channel) { foreach ($this->subChannels as $channel) {
$name = $channel->getName(); $name = $channel->getName();
@ -81,10 +81,12 @@ class Capacitor implements ITransactor {
$commited = false; $commited = false;
try { try {
func::call($func, $this); func::call($func, $this);
$this->commit(); if ($commit) {
$commited = true; $this->commit();
$commited = true;
}
} finally { } finally {
if (!$commited) $this->rollback(); if ($commit && !$commited) $this->rollback();
} }
} }
} }

View File

@ -13,8 +13,16 @@ interface ITransactor {
function inTransaction(): bool; function inTransaction(): bool;
/** si $func!==null, la lancer puis commiter la transaction */ /**
function beginTransaction(?callable $func=null): void; * démarrer une transaction
*
* si $func!==null, l'apppeler. ensuite, si $commit===true, commiter la
* transaction. si une erreur se produit lors de l'appel de la fonction,
* annuler la transaction
*
* $func est appelée avec la signature ($this)
*/
function beginTransaction(?callable $func=null, bool $commit=true): void;
function commit(): void; function commit(): void;

View File

@ -165,7 +165,7 @@ class Pdo implements IDatabase {
return $this->db()->inTransaction(); return $this->db()->inTransaction();
} }
function beginTransaction(?callable $func=null): void { function beginTransaction(?callable $func=null, bool $commit=true): void {
$this->db()->beginTransaction(); $this->db()->beginTransaction();
if ($this->transactors !== null) { if ($this->transactors !== null) {
foreach ($this->transactors as $transactor) { foreach ($this->transactors as $transactor) {
@ -176,10 +176,12 @@ class Pdo implements IDatabase {
$commited = false; $commited = false;
try { try {
func::call($func, $this); func::call($func, $this);
$this->commit(); if ($commit) {
$commited = true; $this->commit();
$commited = true;
}
} finally { } finally {
if (!$commited) $this->rollback(); if ($commit && !$commited) $this->rollback();
} }
} }
} }

View File

@ -200,7 +200,7 @@ class Sqlite implements IDatabase {
return $this->inTransaction; return $this->inTransaction;
} }
function beginTransaction(?callable $func=null): void { function beginTransaction(?callable $func=null, bool $commit=true): void {
$this->db()->exec("begin"); $this->db()->exec("begin");
$this->inTransaction = true; $this->inTransaction = true;
if ($this->transactors !== null) { if ($this->transactors !== null) {
@ -212,10 +212,12 @@ class Sqlite implements IDatabase {
$commited = false; $commited = false;
try { try {
func::call($func, $this); func::call($func, $this);
$this->commit(); if ($commit) {
$commited = true; $this->commit();
$commited = true;
}
} finally { } finally {
if (!$commited) $this->rollback(); if ($commit && !$commited) $this->rollback();
} }
} }
} }

View File

@ -4,6 +4,7 @@ namespace nur\sery\ext\spreadsheet;
use nur\sery\file\csv\AbstractBuilder; use nur\sery\file\csv\AbstractBuilder;
use nur\sery\file\csv\TAbstractBuilder; use nur\sery\file\csv\TAbstractBuilder;
use nur\sery\os\path; use nur\sery\os\path;
use nur\sery\php\func;
use nur\sery\php\time\Date; use nur\sery\php\time\Date;
use nur\sery\php\time\DateTime; use nur\sery\php\time\DateTime;
use nur\sery\web\http; use nur\sery\web\http;
@ -77,7 +78,7 @@ class SpoutBuilder extends AbstractBuilder {
/** /**
* @param string|int|null $wsname * @param string|int|null $wsname
*/ */
function setWsname($wsname): self { function setWsname($wsname, ?array $params=null): self {
$ss = $this->ss; $ss = $this->ss;
$this->rowStyle = self::STYLE_ROW; $this->rowStyle = self::STYLE_ROW;
if ($this->firstSheet) { if ($this->firstSheet) {
@ -85,11 +86,42 @@ class SpoutBuilder extends AbstractBuilder {
$ws = $ss->getCurrentSheet(); $ws = $ss->getCurrentSheet();
} else { } else {
$ws = $ss->addNewSheetAndMakeItCurrent(); $ws = $ss->addNewSheetAndMakeItCurrent();
$this->wroteHeaders = false;
$this->built = false;
} }
if ($wsname !== null) $ws->setName($wsname); if ($wsname !== null) $ws->setName($wsname);
$ws->setSheetView((new SheetView()) $sheetView = (new SheetView())
->setFreezeRow(2) ->setFreezeRow(2);
); $ws->setSheetView($sheetView);
if ($params !== null) {
if (array_key_exists("schema", $params)) {
$this->schema = $params["schema"] ?? null;
}
if (array_key_exists("headers", $params)) {
$this->headers = $params["headers"] ?? null;
}
if (array_key_exists("rows", $params)) {
$rows = $params["rows"] ?? null;
if (is_callable($rows)) $rows = $rows();
$this->rows = $rows;
}
if (array_key_exists("cook_func", $params)) {
$cookFunc = $params["cook_func"] ?? null;
$cookCtx = $cookArgs = null;
if ($cookFunc !== null) {
func::ensure_func($cookFunc, $this, $cookArgs);
$cookCtx = func::_prepare($cookFunc);
}
$this->cookCtx = $cookCtx;
$this->cookArgs = $cookArgs;
}
if (array_key_exists("type_numeric", $params)) {
$this->typeNumeric = boolval($params["type_numeric"] ?? static::TYPE_NUMERIC);
}
if (array_key_exists("type_date", $params)) {
$this->typeDate = boolval($params["type_date"] ?? static::TYPE_DATE);
}
}
return $this; return $this;
} }

View File

@ -105,7 +105,7 @@ abstract class AbstractBuilder extends TempStream implements IBuilder {
} }
if ($rows !== null) { if ($rows !== null) {
foreach ($rows as $row) { foreach ($rows as $row) {
$this->write($row); $this->write(cl::with($row));
} }
} }
if ($unsetRows) $this->rows = null; if ($unsetRows) $this->rows = null;
@ -132,14 +132,30 @@ abstract class AbstractBuilder extends TempStream implements IBuilder {
abstract protected function _checkOk(): bool; abstract protected function _checkOk(): bool;
function build(?iterable $rows=null): bool { protected bool $built = false, $closed = false;
$this->_build($rows);
return $this->_checkOk(); function build(?iterable $rows=null, bool $close=true): bool {
$ok = true;
if (!$this->built) {
$this->_build($rows);
$this->built = true;
}
if ($close && !$this->closed) {
$ok = $this->_checkOk();
$this->closed = true;
}
return $ok;
} }
function sendFile(?iterable $rows=null): int { function sendFile(?iterable $rows=null): int {
$this->_build($rows); if (!$this->built) {
if (!$this->_checkOk()) return 0; $this->_build($rows);
$this->built = true;
}
if (!$this->closed) {
if (!$this->_checkOk()) return 0;
$this->closed = true;
}
$this->sendHeaders(); $this->sendHeaders();
return $this->fpassthru(); return $this->fpassthru();
} }