Ferreteria/v0.6/clade/Sys/Data/Engine/aux/ActionRq/Admin/ToDbOper

From Woozle Writes Code
< Ferreteria‎ | v0.6‎ | clade‎ | Sys‎ | Data‎ | Engine‎ | aux‎ | ActionRq‎ | Admin
Revision as of 14:21, 25 October 2025 by Woozle (talk | contribs)
Jump to navigation Jump to search
clade: Sys\Data\Engine\aux\ActionRq\Admin\ToDbOper
Clade Family
Admin ToDbOper

EngDbExport
EngDbScList
EngDbSetup
EngDbTest
EngDbUConf

Clade Aliases
Alias Clade
Base* [c,i] Sys\Data\Engine\aux\ActionRq\Admin
DbOper* [i] Sys\Data\Engine\Ops
QStr* [i] Data\Mem\QVar\Str
Subpages

About

The caToDbOper class needs to know how to find a Database Operations object from a db name-slug, but can't know where that slug is specified. The QDbConnSlug() function, which returns the value/status of that parameter, is therefore abstract. The same is true of the Schema name-slug. It is up to descendant classes (generally found within application code) to define where those slugs can be found, and return a value/status string object for each.

Code

as of 2025-10-05

interface iToDbOper extends BaseIface {
    #static function FromDbOper(DbOperIface $o) : self;
    // ERROR CODES
    const N_ERR_DB_DEFN = 1;
    const N_ERR_DB_SLUG = 2;
}
abstract class caToDbOper extends BaseClass implements iToDbOper {

    // ++ REGISTRY ++ //

    protected function DbasesOArray() : ObjListIface { return ConnAdmin::ObjectRoster(); }

    // -- REGISTRY -- //
    // ++ OBJECTS ++ //

    private $oDbOp = NULL;
    protected function QDbOper() : QDbOperIface {
        $qoDbOp = $this->FetchDBOper();
        $oItWent = $this->HowItWent();
        if ($qoDbOp->HasIt()) {
            #echo $qoDbOp->ReflectThis()->Report();
            $oDbOp = $qoDbOp->GetObj();
            $this->oDbOp = $oDbOp;
            $qoDbOp->SetIt($oDbOp);
            $oItWent->SetOkay(TRUE);
        } else {
            // the ops object was not retrieved
            $oItWent->SetOkay(FALSE);

            // get more details
            $qsDbSlug = $this->QDbConnSlug();
            if ($qsDbSlug->HasIt()) {
                #echo $qoDbOp->ReflectThis()->Report();
                $sSlug = $qsDbSlug->GetIt();
                $oItWent->AddMsgString(self::Screen()->ErrorIt("Internal issue while loading the '$sSlug' DB definition."));
                $oItWent->SetError(self::N_ERR_DB_DEFN);
            } else {
                $oItWent->AddMsgString(self::Screen()->ErrorIt('Internal issue accessing the DB slug.'));
                $oItWent->SetError(self::N_ERR_DB_SLUG);
            }
        }
        return $qoDbOp;
    }
    protected function DbOper() : QDbOperIface {
        $oDbOp = $this->oDbOp;
        if (is_null($oDbOp)) {
            $qoDbOp = $this->FetchDBOper();
        }
        return $oDbOp;
    }
    // ACTION: tries to retrieve the slug-requested DB from the db classifest
    protected function FetchDbOper() : QDbOperIface {
        $osDbSlug = $this->QDbConnSlug();
        #$osScSlug = $this->QSchemaSlug();
        #echo $oaData->ReflectThis()->Report();
        $doListFiles = FALSE;   // flip this on if the user needs it
        $qoDB = QDbOperClass::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();
                #echo $oDbConn->ReflectThis()->Report();
                $oDbOper = $oDbConn->OOper();
                $qoDB->SetIt($oDbOper);
                #$this->SetOkay(TRUE);
            }
        } else {
            #$this->SetOkay(FALSE);
        }
        return $qoDB;
    }
    abstract protected function QDbConnSlug() : QStrIface;
    abstract protected function QSchemaSlug() : QStrIface;

    // -- OBJECTS -- //
    // ++ OUTPUT ++ //

    public function ListHosts() : string {

        $oScrn = self::Screen();

        #$oaDBs = DbConnAdmin::Register();
        $oaDBs = $this->DbasesOArray();

        $nDBs = $oaDBs->Count();
        if ($nDBs > 0) {
            $oTable = $oScrn->NewTable();
            $sS = ($nDBs == 1) ? '' : 's';
            $sOut = $oScrn->InfoIt($nDBs)." available DB connection$sS:".CRLF;
            $oHdr = $oTable->RowFromData(['name','specs']);
            $oHdr->IsHeader(TRUE);
            $oaDBs->RewindMe();
            foreach ($oaDBs as $sKey => $oConn) {
                #echo "[$sKey] DB CLASS: ".get_class($oConn).CRLF;
                $arRow = [$sKey,$oConn->DescribeInline()];
                $oTable->RowFromData($arRow);
            }
            $sOut .= $oTable->Render();
        } else {
            $sOut = 'No database connections are defined yet.'.CRLF;
        }
        return $sOut;
    }

    // -- OUTPUT -- //
}