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

From Woozle Writes Code
Jump to navigation Jump to search
The item documented here has been removed and is no longer in use.
It was retired on 2025-10-26. Its niche is replaced(ish) by ToDbase.
clade: Sys\Data\Engine\aux\ActionRq\Admin\ToDbOper
Clade Family
Admin ToDbOper

EngDbExport
EngDbScList
EngDbSetup
EngDbTest
EngDbUConf

Clade Aliases
Alias Clade
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.

History

  • 2025-03-25 created
  • 2025-05-31 renamed ToDbConn -> ToDbOper
  • 2025-09-24 Apparently this is now completely redundant?
  • 2025-09-25 Or not. (Did I delete something by mistake?) Rescued mostly from .md, but also reconstructing ODbOper().
  • 2025-10-11 moved ListHosts() to Admin (parent) from here so it's also available to ToSchema classes
  • 2025-10-26 functionality consolidated with ToDbConn; clade/file is now removed (and archived in ToDbOper.md)
  • 2026-01-30 code in ToDbOper.md now copied to here, and ToDbOper.md removed

Code

2025-10-26: final version

Retrieved/archived from ToDbOper.md on 2026-01-30:

<?php namespace Woozalia\Ferret\Sys\Data\Engine\aux\ActionRq\Admin;
// DOCS: https://wooz.dev/Ferreteria/v0.6/clade/Sys/Data/Engine/aux/ActionRq/Admin/ToDbOper

/* BaseClass */     use Woozalia\Ferret\Sys\Data\Engine\aux\ActionRq\caAdmin as BaseClass;
/* BaseIface */     use Woozalia\Ferret\Sys\Data\Engine\aux\ActionRq\iAdmin as BaseIface;
/* ConnAdmin */     use Woozalia\Ferret\Sys\Data\Engine\caConn as ConnAdmin;
/* DbOperAdmin */   #use Woozalia\Ferret\Sys\Data\Engine\caOps as DbOperAdmin;
/* DbOperIface */   use Woozalia\Ferret\Sys\Data\Engine\iOps as DbOperIface;
/* InRqAdmin */     use Woozalia\Ferret\Sys\Events\caInputRq as InRqAdmin;
/* ObjListClass */  #use Woozalia\Ferret\Sys\Data\Engine\aux\cDbList as ObjListClass;
/* ObjListIface */  #use Woozalia\Ferret\Config\Roster\iObjects as ObjListIface;
/* ObjListIface */  use Woozalia\Ferret\Sys\Data\Engine\aux\iDbList as ObjListIface;
/* QDbConnClass */  use Woozalia\Ferret\Sys\Data\Engine\aux\cQDbConn as QDbConnClass;
/* QDbConnIface */  use Woozalia\Ferret\Sys\Data\Engine\aux\iQDbConn as QDbConnIface;
/* QDbOperClass */  use Woozalia\Ferret\Sys\Data\Engine\aux\ActionRq\aux\cQDbOper as QDbOperClass;
/* QDbOperIface */  use Woozalia\Ferret\Sys\Data\Engine\aux\ActionRq\aux\iQDbOper as QDbOperIface;
/* QStrIface */     use Woozalia\Ferret\Data\Mem\QVar\iStr as QStrIface;
/* RegClass */      use Woozalia\Ferret\Sys\Data\Engine\aux\cDbRegister as RegClass;
/* RegClass */      #use Woozalia\Ferret\Sys\Events\InputRq\aux\cInRqDatReg as RegClass;
/* RegIface */      #use Woozalia\Ferret\Aux\iRegister as RegIface;
/* RegIface */      #use Woozalia\Ferret\Sys\Data\Engine\aux\iDbRegister as RegIface;

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

    function QDbOper() : QDbOperIface;
}
trait tToDbOper /* implements iToDbOper */ {

    // ++ REGISTRY ++ //

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

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

    private $oDbOp = NULL;
    pubic function QDbOper() : QDbOperIface {
        #$this->AmHere('FINDING DbOp object');
        $qoDbOp = $this->FetchDBOper();
        $oItWent = $this->HowItWent();
        if ($qoDbOp->HasIt()) {
            #$this->AmHere('Got the DbOp object');
            #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);
            #$this->AmHere('Could NOT get the DbOp object');

            // 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 Ops object from the db registry
    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 -- //
}
abstract class caToDbOper extends BaseClass implements iToDbOper { use tToDbOper; }

2025-10-05: complete main code

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 -- //
}

2025-09-26: removed

Commented out yesterday:

// ++ SETUP ++ //

    // 2025-09-25 I *think* none of this is necessary anymore. (Update: ODbOper() is needed, but it needs to fetch instead of creating.)
    public static function FromDbOper(DbOperIface $o) : iToDbOper {
        $oThis = new static;
        $oThis->WithDbOper($o);
        return $oThis;
    }

    // ++ SETUP: dynamic ++ //

    protected function WithDbOper(DbOperIface $o) { $this->ODbOper($o); }

    // -- SETUP -- //

2025-09-24: removed

Commented out yesterday (but then reinstated 9/25 because the place I thought I moved them does not exist):

// ++++ from iface
    
    static function FromDbOper(DbOperIface $o) : self;

    // ++++ from class
    
    // 2025-09-23 moved to [WFe]Sys\Data\aux\uses\tDbOper

    // ++ SETUP ++ //

    public static function FromDbOper(DbOperIface $o) : iToDbOper {
        $oThis = new static;
        $oThis->WithDbOper($o);
        return $oThis;
    }

    // ++ SETUP: dynamic ++ //

    protected function WithDbOper(DbOperIface $o) { $this->ODbOper($o); }

    // -- SETUP -- //