Ferreteria/v0.6/clade/Sys/Data/Engine/aux/ActionRq/Admin/ToDbConn
Jump to navigation
Jump to search
| ||||||||||||||
About
Status
- 2025-10-25 under construction
- 2025-10-26 complete-ish, but still refactoring because stuff doesn't work yet
Code
interface iToDbConn {
// objects
function QODbOper() : QDbOperIface;
}
trait tToDbConn /* implements iToDbConn */ {
// ++ REGISTRY ++ //
// SHORTCUT
protected function DbasesOArray() : ObjListIface { return ConnAdmin::ObjectRoster(); }
// -- REGISTRY -- //
// ++ OBJECTS ++ /
private $qoOper = NULL;
public function QODbOper() : QDbOperIface {
$qoOper = $this->qoOper;
if (is_null($qoOper)) {
$qoOper = QDbOperClass::AsNew();
$this->qoOper = $qoOper;
}
$qoConn = $this->QODbConn();
$ok = $qoConn->HasIt();
$oItWent = $qoConn->HowItWent();
$oItWent->SetOkay($ok);
if ($ok) {
$oConn = $qoConn->GetIt();
$oOper = $oConn->OOper();
$qoOper->SetIt($oOper);
}
$qoOper->HowItWent()->CopyFrom($oItWent);
return $qoOper;
}
private $qoConn = NULL;
public function QODbConn() : QDbConnIface {
$qoConn = $this->qoConn;
$doFetch = FALSE;
if (is_null($qoConn)) {
#$qoConn = QDbConnClass::AsNew();
#$this->qoConn = $qoConn;
$doFetch = TRUE;
} elseif (!$qoConn->HasIt()) {
$this->AmHere();
$doFetch = TRUE;
}
if ($doFetch) {
$qoConn = $this->FetchDBConn();
$this->qoConn = $qoConn;
}
return $qoConn;
}
private $oDBC=NULL;
// ASSUMES: QODbConn() is successful
protected function ODbConn(?ConnIface $o=NULL) : ConnIface {
return is_object($o) ? ($this->oDBC = $o) : ($this->oDBC ?? ($this->oDBC = $this->QODbConn()->GetIt()));
}
protected function FetchDbConn() : QDbConnIface {
$oScrn = self::Screen();
$qoConn = QDbConnClass::AsNew();
$oItWent = $qoConn->HowItWent();
// STEP 1: retrieve the name for the requested DB (if any)
$qsName = $this->QDbConnSlug();
$didFind = FALSE;
if ($qsName->HasIt()) {
$sName = $qsName->GetIt();
if ($sName !== '') {
$didFind = TRUE;
}
} else {
$oItWent->SetError($oItWent::N_ERR_DB_SLUG);
$oItWent->AddMsgString(self::Screen()->ErrorIt('No DB connection-name specified.'));
}
if ($didFind) {
// STEP 2: try to get the DB connection spec object for that name:
$ftName = $oScrn->BoldIt($sName);
$oItWent->AddMsgString($oScrn->GreenIt("Using")." DB connection [$ftName].");
# echo ConnAdmin::ReflectSelf()->Report();
$oaDBs = ConnAdmin::ObjectRoster();
$osDB = $oaDBs->QryIt($sName);
if ($osDB->HasIt()) {
$oDB = $osDB->GetIt();
$qoConn->SetIt($oDB);
$oItWent->SetOkay(TRUE);
} else {
$noFind = TRUE;
$oItWent->SetError($oItWent::N_ERR_DB_DEFN);
$oItWent->AddMsgString($oScrn->ErrorIt('Input Error').": Could not find DB access specs for [$ftName].");
}
}
/*
if ($noFind) {
echo $oaDBs->ReflectThis()->Report();
echo 'Available:'.CRLF;
echo $oaDBs->Debug()->Render();
die();
// TODO: better error-handling
}
*/
return $qoConn;
}
// -- OBJECTS -- /
}
Removed
2025-10-26
// ACTION: tries to retrieve the slug-requested DB Connection object from the db registry
// 2025-10-26 old version
protected function FetchDbConn() : QDbConnIface {
$osDbSlug = $this->QDbConnSlug();
$doListFiles = FALSE; // flip this on if the user needs it
$qoDB = QDbConnClass::AsNew();
if ($osDbSlug->HasIt()) {
// Get DB Connection slug for UI display:
$sDbSlug = $osDbSlug->GetIt();
// TODO: Look up the DB object (there IS code for this, somewhere...)
$oaDBs = $this->DbasesOArray();
#echo 'oaDBs CLASS: '.get_class($oaDBs).CRLF;
$qoDbConn = $oaDBs->QryIt($sDbSlug);
if ($qoDbConn->HasIt()) {
$oDbConn = $qoDbConn->GetIt();
$qoDB->SetIt($oDbOper);
}
}
return $qoDB;
}