diff --git a/.idea/php.xml b/.idea/php.xml index 4114efb..40a93eb 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -22,56 +22,56 @@ - - - - - - - + + + + + + + + + + + + + - - - - + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + - + + diff --git a/php/src/db/Capacitor.php b/php/src/db/Capacitor.php index 32a278b..f136fae 100644 --- a/php/src/db/Capacitor.php +++ b/php/src/db/Capacitor.php @@ -259,15 +259,20 @@ abstract class Capacitor { return $raw !== null; } - function create(CapacitorChannel $channel): void { - if ($channel->isCreated()) return; - $channel->ensureSetup(); + function _create(CapacitorChannel $channel): void { if (!$this->tableExists($channel->getTableName())) { $this->prepareMetadata(); $this->getMigration($channel)->migrate($this->db()); $this->afterCreate($channel); } - $channel->setCreated(); + } + + function create(CapacitorChannel $channel, bool $force=false): void { + if ($force || !$channel->isCreated()) { + $channel->ensureSetup(); + $this->_create($channel); + $channel->setCreated(); + } } protected function afterCreate(CapacitorChannel $channel): void { diff --git a/php/src/db/CapacitorChannel.php b/php/src/db/CapacitorChannel.php index 765bc94..f3aa911 100644 --- a/php/src/db/CapacitorChannel.php +++ b/php/src/db/CapacitorChannel.php @@ -1,6 +1,7 @@ name = $name; $this->tableName = $tableName; - $manageTransactions = $params["manageTransactions"] ?? static::MANAGE_TRANSACTIONS; - $this->manageTransactions = $manageTransactions; - - $eachCommitThreshold = $params["eachCommitThreshold"] ?? null; - $eachCommitThreshold = self::verifix_eachCommitThreshold($eachCommitThreshold); - $this->eachCommitThreshold = $eachCommitThreshold; - + $autocreate = $params["autocreate"] ?? null; + $autocreate ??= !app::get()->isProductionMode(); + $this->created = !$autocreate; $this->setup = false; - $this->created = false; + $columnDefinitions = $this->COLUMN_DEFINITIONS(); $primaryKeys = cl::withn(static::PRIMARY_KEYS); $migration = cl::withn(static::MIGRATION); @@ -128,6 +127,13 @@ class CapacitorChannel implements ITransactor { $this->columnDefinitions = $columnDefinitions; $this->primaryKeys = $primaryKeys; $this->migration = $migration; + + $manageTransactions = $params["manageTransactions"] ?? static::MANAGE_TRANSACTIONS; + $this->manageTransactions = $manageTransactions; + + $eachCommitThreshold = $params["eachCommitThreshold"] ?? null; + $eachCommitThreshold = self::verifix_eachCommitThreshold($eachCommitThreshold); + $this->eachCommitThreshold = $eachCommitThreshold; } protected string $name; @@ -142,40 +148,6 @@ class CapacitorChannel implements ITransactor { return $this->tableName; } - /** - * @var bool indiquer si les modifications de each doivent être gérées dans - * une transaction. si false, l'utilisateur doit lui même gérer la - * transaction. - */ - protected bool $manageTransactions; - - function isManageTransactions(): bool { - return $this->manageTransactions; - } - - function setManageTransactions(bool $manageTransactions=true): self { - $this->manageTransactions = $manageTransactions; - return $this; - } - - /** - * @var ?int nombre maximum de modifications dans une transaction avant un - * commit automatique dans {@link Capacitor::each()}. Utiliser null pour - * désactiver la fonctionnalité. - * - * ce paramètre n'a d'effet que si $manageTransactions==true - */ - protected ?int $eachCommitThreshold; - - function getEachCommitThreshold(): ?int { - return $this->eachCommitThreshold; - } - - function setEachCommitThreshold(?int $eachCommitThreshold=null): self { - $this->eachCommitThreshold = self::verifix_eachCommitThreshold($eachCommitThreshold); - return $this; - } - /** * initialiser ce channel avant sa première utilisation. */ @@ -330,6 +302,40 @@ class CapacitorChannel implements ITransactor { return $sum !== $psum; } + /** + * @var bool indiquer si les modifications de each doivent être gérées dans + * une transaction. si false, l'utilisateur doit lui même gérer la + * transaction. + */ + protected bool $manageTransactions; + + function isManageTransactions(): bool { + return $this->manageTransactions; + } + + function setManageTransactions(bool $manageTransactions=true): self { + $this->manageTransactions = $manageTransactions; + return $this; + } + + /** + * @var ?int nombre maximum de modifications dans une transaction avant un + * commit automatique dans {@link Capacitor::each()}. Utiliser null pour + * désactiver la fonctionnalité. + * + * ce paramètre n'a d'effet que si $manageTransactions==true + */ + protected ?int $eachCommitThreshold; + + function getEachCommitThreshold(): ?int { + return $this->eachCommitThreshold; + } + + function setEachCommitThreshold(?int $eachCommitThreshold=null): self { + $this->eachCommitThreshold = self::verifix_eachCommitThreshold($eachCommitThreshold); + return $this; + } + /** * méthode appelée lors du chargement avec {@link Capacitor::charge()} pour * créer un nouvel élément @@ -417,9 +423,8 @@ class CapacitorChannel implements ITransactor { return $this->capacitor; } - function setCapacitor(Capacitor $capacitor, bool $ensureExists=true): self { + function initCapacitor(Capacitor $capacitor): self { $this->capacitor = $capacitor; - if ($ensureExists) $this->ensureExists(); return $this; } @@ -517,15 +522,11 @@ class CapacitorChannel implements ITransactor { } function exists(): bool { - return $this->capacitor->_exists($this); - } - - function ensureExists(): void { - $this->capacitor->_ensureExists($this); + return $this->capacitor->exists($this); } function reset(bool $recreate=false): void { - $this->capacitor->_reset($this, $recreate); + $this->capacitor->reset($this, $recreate); } function charge($item, $func=null, ?array $args=null, ?array &$row=null): int { @@ -546,7 +547,7 @@ class CapacitorChannel implements ITransactor { } function discharge(bool $reset=true): Traversable { - return $this->capacitor->discharge($reset); + return $this->capacitor->discharge($this, $reset); } /** @@ -572,27 +573,27 @@ class CapacitorChannel implements ITransactor { function count($filter=null): int { $this->verifixFilter($filter); - return $this->capacitor->count($filter); + return $this->capacitor->count($this, $filter); } function one($filter, ?array $mergeQuery=null): ?array { $this->verifixFilter($filter); - return $this->capacitor->one($filter, $mergeQuery); + return $this->capacitor->one($this, $filter, $mergeQuery); } function all($filter, ?array $mergeQuery=null): Traversable { $this->verifixFilter($filter); - return $this->capacitor->all($filter, $mergeQuery); + return $this->capacitor->all($this, $filter, $mergeQuery); } function each($filter, $func=null, ?array $args=null, ?array $mergeQuery=null, ?int &$nbUpdated=null): int { $this->verifixFilter($filter); - return $this->capacitor->each($filter, $func, $args, $mergeQuery, $nbUpdated); + return $this->capacitor->each($this, $filter, $func, $args, $mergeQuery, $nbUpdated); } function delete($filter, $func=null, ?array $args=null): int { $this->verifixFilter($filter); - return $this->capacitor->delete($filter, $func, $args); + return $this->capacitor->delete($this, $filter, $func, $args); } function dbAll(array $query, ?array $params=null): iterable {