Ferreteria/v0.6/clade/IO/Aspect/Connx/run/Session/@code

From WoozleCodes
Jump to navigation Jump to search
Code Snapshots & Removals
interface iMyMar extends BaseIface {
    // LIFECYCLE
    #static function FromSpecs(string $sAddr, string $sUser, string $sPass, ?string $sSchema, ?int $nzPort) : self;
    // INFO
    function SchemaExists(string $sName) : bool;    // 2025-05-30 Eventually, will want a base-class for all multi-schema engines.
}
abstract class caMyMar extends BaseClass implements iMyMar {

    // ++ CONFIG ++ //

    protected function SyntaxClass() : string { return SyntaxClass::class; }
    protected function OpCommClass() : string { return OpCommClass::class; } // specialize later
    protected function SchemaClass() : string { return SchemaClass::class; }
    protected function ScribeClass() : string { return ScribeClass::class; }

    // -- CONFIG -- //
    // ++ SETTINGS ++ //

    private $eFmt = BkupModeEnum::USE_SQL;  // DEFAULT
    public function DataFormat(?BkupModeEnum $eMode=NULL) : BkupModeEnum { return is_object($eMode) ? ($this->eFmt = $eMode) : $this->eFmt; }

    // -- SETTINGS -- //
    // ++ OBJECTS ++ //

    protected function QONative() : QObjIface { return $this->OEngine()->QONative(); }  // SHORTCUT

    private $oStatus = NULL;
    protected function OpCommStatus() : OpCommIface { return $this->oStatus ?? ($this->oStatus = new ($this->OpCommClass())); }

    // -- OBJECTS -- //
    // ++ ACTION ++ //
    // ++ ACTION: UI requests ++ //

    public function DoDataRequest(DataReqIface $oReq) : PlayerIface {
        $this->Open();
        $sql = $oReq->SQL();

        $onRes = $this->QONative()->GetIt()->query($sql);
        $oReq->Action()->IsOkay = is_object($onRes);

        $scScribe = $this->ScribeClass();
#        echo $oReq->ReflectThis()->Report();
        $oScribe = $scScribe::FromRequestAndNative($oReq,$onRes);
        $oPlay = $oReq->MakePlayer($oScribe);
        $this->Shut();
        return $oPlay;
    }

    // ++ ACTION: Schema: ops: listing ++ //

    private $oaSchemas = NULL;
    public function SchemaList()
    : SchemasIface
    { return $this->oaSchemas ?? ($this->oaSchemas = $this->NewSchemaList()); }
    public function SchemaExists(string $sName) : bool { return $this->SchemaList()->HasIt($sName); }
    // DOCS: https://wooz.dev/Ferreteria/v0.6/clade/Sys/Data/Engine/endpt/Client/MyMar/@fx/NewSchemaList
    protected function NewSchemaList() : SchemasIface {
        $oEng = $this->OEngine();
        $oEng->OServer()->MainCommand();  // activate the main command

        $oSess = $oEng->OSession();

        $oSyntax = $this->ODialect();

        $oCmdThis = $oSyntax->ListSchemas();

        // stream for receiving standard output (data):
        $oSListen = new BufferClass;
        $oSListen->Open();
        $oSess->QOAppListener->SetIt($oSListen);

        // stream for receiving error messages:
        $oSErrors = new BufferClass;
        $oSErrors->Open();
        $oSess->QOAppErrorBin->SetIt($oSErrors);

        $oOpOpen = $oSess->Open();
        $oActCmd = $oSess->DoCommand($oCmdThis);

        $oActApp = $this->OpCommStatus();
        $oScrn = self::Screen();

        $oaSchemas = SchemasClass::AsNew();

        #$this->AmHere('Checking for errors...');
        $oEng->CheckForMsgs($oSErrors);

        if ($oActCmd->IsOkay) {
            $oOpData = $oSListen->ReadAvail();
            if ($oOpData->IsOkay) {
                $sNames = trim($oOpData->QData()->GetIt());
                if (strlen($sNames) > 0) {
                    $arNames = explode("\n",trim($sNames)); // we might want to ensure that CRLF, CR, and LF all work as EOL.

                    // This was only necessary when MyMar is returning the data in a "friendly" format where the first line is "Database".
                    //   One of the command-options turns that off.
                    #$sFirst = array_shift($arNames);
                    $oActApp->IsOkay = TRUE;
                    foreach ($arNames as $sName) {
                        $oSchema = $this->GetSchema($sName);
                        $oaSchemas->AddSchema($oSchema);
                    }
                }
            } else {
                $this->AmHere('ReadAvail() result:'.CRLF.$oOpData->VIEW_AsBlock());
            }
        } else {
            $this->AmHere('Command did not succeed');
            $sMsg = self::Screen()->ErrorIt('Problem').': the connection does not seem to be working.';
            $oActApp->IsOkay = FALSE;
            $oActApp->AddMsgString($sMsg);
        }
        $oSListen->Shut();
        $oSErrors->Shut();
        return $oaSchemas;
    }

    // ++ ACTION: UI request callbacks ++ //

    // 2026-02-08 TODO This is going to need updating.
    public function DoUserSetup() {
        $oScrn = self::Screen();

        // 2025-03-30 has worked at least once now
        #echo $oScrn->YellowIt('Warning').': This functionality has not been tested to the point of success yet. It may not work.'.CRLF;

        $ok = $this->TestRootAccess();
        if ($ok) {
            echo $oScrn->GreenIt('Ok').': we seem to have unconfigured root access. Proceeding...'.CRLF;
        } else {
            echo $oScrn->ErrorIt('Error').': This DB engine is configured, and can only be accessed with credentials.'.CRLF;
            exit;
        }

        $oCreds = $this->Creds();
        $sDUser = $oCreds->QSUser->GetIt();
        $sDPass = $oCreds->QSPass->GetIt();
        $sDHost = $oCreds->QSHost->GetIt();

        // IN-SHELL COMMAND: sudo mysql "CREATE USER '<USERNAME>'@<HOST> IDENTIFIED BY '<PASSWORD>'"
        #$q = '"';
        #$sql = $q."ALTER USER '$sDUser'@$sDHost IDENTIFIED BY '$sDPass';$q";
        $sql = "ALTER USER '$sDUser'@$sDHost IDENTIFIED BY '$sDPass';";

        #$sCmd = "echo $sql