Ferreteria/v0.6/clade/Sys/Data/Engine/Conn/MyMar

From Woozle Writes Code
< Ferreteria‎ | v0.6‎ | clade‎ | Sys‎ | Data‎ | Engine‎ | Conn
Jump to navigation Jump to search
clade: Sys\Data\Engine\Conn\MyMar
Clade Family
Conn MyMar

Maria
MySQL

Clade Aliases
Alias Clade
ActionIFace Sys\Events\ItWent
Base* [c,i] Sys\Data\Engine\Conn
BufferIface IO\Aspect\Connx\Buffer
CommOpIface Sys\Events\ItWent\CommOp
ViewClass Sys\Data\aux\Viewer
Subpages

About

This is the parent clade for both MariaDB and MySQL, which (obviously, since the former is a fork of the latter) have a lot in common but also some minor differences (hence the two podling-classes).

Code

as of 2025-10-22

interface iMyMar extends BaseIface {
    // ACTION: information
    function CredsForCmd() : string;
}
abstract class caMyMar extends BaseClass implements iMyMar {

    // ++ CONFIG ++ //

    protected function InspectorClass() : string { return ViewClass::class; }

    // -- CONFIG -- //
    // ++ ACTION ++ //

    // TODO: handle when $saCmd is array
    public function DoCommand(string|array $saCmd, BufferIface $oBuff, ?CommOpIface $oAct=NULL) : CommOpIface {
        $sCreds = $this->CredsForCmd();
        $sCmd = "echo $saCmd| mysql $sCreds";
        #echo "SENDING SHELL COMMAND: [$sCmd]".CRLF;

        $oPlug = $this->OSock()->OPlug();
        $oAct = $oPlug->DoCommand($sCmd,$oBuff,$oAct);
        return $oAct;
    }

    // ++ ACTION: figuring ++ //

    public function CredsForCmd() : string {
        $oSock = $this->OSock();  // DB connection-from-endpoint socket
        $oHost = $oSock->OHost();  // DB host from endpoint
        $sAddr = $oHost->QSAddr()->GetIt();
        $sPort = $oHost->QIPort()->GetStrNz('',' --port='); // empty string if not set, else " --port=<#>"

        $oCred = $oSock->OCred(); // DB credentials
        $sUser = $oCred->QSUser()->GetIt(); // DB user
        $sPass = $oCred->QSPass()->GetIt(); // DB password

        #return "$sPort--host=$sHost --user=$sUser --password=$sPass";
        return "-h$sAddr$sPort -u$sUser -p$sPass";
    }

    // -- ACTION -- //
    // ++ OUTPUT ++ //

    // CEMENT
    public function DescribeInline() : string {

        $sType = static::ClassSlug();
        $ftCred = $this->OCred()->DescribeInline();
        $ftSock = $this->OSock()->DescribeInline();
        $ftConj1 = self::Screen()->GreenIt('on');
        $ftConj2 = self::Screen()->GreenIt('via');
        #$ftOID = self::Screen()->FaintIt($this->ObjectID());
        #return "($ftOID) $ftCred $ftConj1 $sType $ftConj2 $ftSock";
        return "$ftCred $ftConj1 $sType $ftConj2 $ftSock";
    }
    // KLUGE for Register (TODO: regularize this)
    public function RenderSummary() : string { return $this->DescribeInline(); }

    // -- OUTPUT -- //
    // ++ LIFECYCLE ++ //

    public function ActualOpen() : ActionIface {
        $oSock = $this->OSock();
        $oConn = $oSock->OPlug();
        $oCred = $oConn->Cred();

        $oConn->Open();
        $oAct = $this->OOper()->EngineOpen(
          $oSock->OHost->SAddr(),
          $oCred->QSUser()->GetIt(),
          $oCred->QSPass()->GetIt(),
          $oCred->QSSchema()->GetItNz(),
          $oCred->QIPort()->GetItNz(),
          );
        return $oAct;
    }
    public function ActualShut() : ActionIface {
        $oAct = $this->OOper()->EngineShut();
        $this->OSock()->OConn()->Shut();
        return $oAct;
    }

    // -- LIFECYCLE -- //
}

Removed

2025-10-22

Commented out 2025-06-10:

// 2025-06-10 old version, no longer quite working
    public function ActualOpen() : ActionIface {
        $oa = new ($this->ActionClass());  // result object

        $oSock = $this->OSock();
        $oConn = $oSock->OPlug();
        $oConn->Open();
        $oCred = $oConn->Cred();
        #echo $oCred->ReflectThis()->Report();
        // 2025-06-03 This will need updating.
        $onDB = new mysqli(
          $oSock->OHost->SAddr(),
          $oCred->QSUser()->GetIt(),
          $oCred->QSPass()->GetIt(),
          $oCred->QSSchema()->GetItNz(),
          $oCred->QIPort()->GetItNz(),
          );	 // open the connection natively
        if (is_object($onDB)) {
            $oa->SetOkay(TRUE);
            $this->OOper()->QNative()->SetIt($onDB);
        } else {
            // 2022-01-26 probably want a method to do all this
            $oa->SetOkay(FALSE);
            $oa->SetNumber($this->ErrorNumner());
            $oa->AddMsgString($this->ErrorMessage());
        }
        return $oa;
    }
    public function ActualShut() : ActionIface {
        // close endpoint-to-engine connection
        $ok = $this->QNative()->GetIt()->close();
        // close code-to-endpoint connection
        $this->OSock()->OConn()->Shut();
        $oAct = new ($this->ActionClass());
        $oAct->SetOkay($ok);
        return $oAct;
    }