[Release] Database (MySQL) Plugin for ETMuS

05/08/2012 11:46 M2Commander#1
+----------+
| English |
+----------+
Hello everybody,

I will now publish the first plugin for ETMuS. It also can be used without ETMuS so you don't have to use the hole script environment if you don't like it.

Requirements:
  • bcompiler
  • PHP 5.3 or higher

This plugin helps creating database connections with a hugh abstraction layer. If you have 2 database connections (for example 2 servers) you will probably have the following problems:
  • maybe 2 different databases (IP, login credentails)
  • if on one server: different post or prefixes either on your databasename or tablename
In such case you have to write complex code for abstracting your SQL statements. By using this plugin you can easily create new connections by using
PHP Code:
$newinstance[] = +OutstandDesign+ETMuS+Modules+Database+MySQL(); 
Note: Because \ isn't working inside the [PHP] -Tag I used +. Keep in mind to use a backslash (\)

Now you can set the settings for your new instance:
PHP Code:
$newinstance[0]->IP "localhost";
$newinstance[0]->User "Test";
$newinstance[0]->Password "Test";
$newinstance[0]->Prefix "foo_";
$newinstance[0]->DB "bar";
$newinstance[0]->Postfix "_woot";
$newinstance[0]->TablePrefix "bar0_";
$newinstance[0]->TablePostfix "_0foo"
If you want to reuse some informations (for example an identical database) clone the created object:
PHP Code:
$newinstance[] = clone $newinstance[0]; 
The cloned object holds now the same informations as the first instance. We now manipulate some variables:
PHP Code:
$newinstance[1]->IP "127.0.0.1";
$newinstance[1]->Prefix "woot_";
$newinstance[1]->Postfix "_foo"
Lets say $newinstance[0] is server 1 and $newinstance[1] is our server 2. If you want to have a command executed on both servers you can use this code:
PHP Code:
foreach($newinstance as $server)
{
      
$sql "SELECT * FROM ".$server->Prefix.$server->DB.$server->Postfix.".".$server->TablePrefix."hello".$server->TablePostfix." WHERE id=23";
      
var_dump($server->Select($sql));

$sql would have this value:
1. iteration: SELECT * FROM foo_bar_woot.bar0_hello_0foo WHERE id=23
2. iteration: SELECT * FROM woot_bar_foo.bar0_hello_0foo WHERE id=23

You can also create a list with each serverinformations and pick out a specific server and assign the informations to a global var which is used in your scripts.
PHP Code:
$playerdb $newinstance[1]; #now server 2 is selected
$playerdb $newinstance[0]; #now server 1 is selected
...
$playerdb->Exec("UPDATE ....."); 
A full documented list of methods and properties can be found here:
[Only registered and activated users can see links. Click Here To Register...]
You can download it here:
[Only registered and activated users can see links. Click Here To Register...]

You can ask me here or in skype if you have any questions. I can help you also in building up a new webpage if you have any problems.

+-----------+
| Deutsch |
+-----------+
Hallo zusammen,

ich möchte euch hiermit mein 1. Plugin für ETMuS vorstellen. Ihr könnt es auch seperat nutzen, falls das ETMuS script euch nicht zusagt oder aus anderen Gründen.

Vorraussetzungen:
  • bcompiler
  • PHP 5.3 oder höher

Dieses Plugin soll euch dabei unterstützen, eine Datenbankverbindung herzustellen, und gleichzeitig eine möglichst große Verallgemeinerung bieten. Dies soll dazu dienen, dass ihr folgende Probleme in größeren Skripts lösen könnt:
  • bei 2 unterschiedlichen Servern: verschiedene IPs/Logindaten
  • falls ein Server: Unterschiedliche Datenbanknamen/ Präfixe/Suffixe von Datenbank oder Tabelle
In diesen Fällen müsste man eine recht complexen code schreiben, um diese SQL statements zu vereinfachen.
Durch dieses plugin lassen sich neue Verbindung schnell realisieren. Durch diesen Befehl wird eine "leere" Instance erstellt
PHP Code:
$newinstance[] = +OutstandDesign+ETMuS+Modules+Database+MySQL(); 
Note: Bitte beachtet, dass ich statt dem \ ein + nehmen musste, da das [PHP]-Tag keine \ aktzeptiert/sie entfernt.

Nun können die Eigenschaften der Instanz gesetzt werden:
PHP Code:
$newinstance[0]->IP "localhost";
$newinstance[0]->User "Test";
$newinstance[0]->Password "Test";
$newinstance[0]->Prefix "foo_";
$newinstance[0]->DB "bar";
$newinstance[0]->Postfix "_woot";
$newinstance[0]->TablePrefix "bar0_";
$newinstance[0]->TablePostfix "_0foo"
Falls ihr bestehende Informationen wiederverwenden wollt (z.B. bei einer 2. Datenbank), kopiert am besten das bestehende Objekt:
PHP Code:
$newinstance[] = clone $newinstance[0]; 
Das kopierte Objekt enthält alle Eigenschaften des Ursprungsobjekts. Ändern wir einige der Daten:
PHP Code:
$newinstance[1]->IP "127.0.0.1";
$newinstance[1]->Prefix "woot_";
$newinstance[1]->Postfix "_foo"
Nehmen wir an, $newinstance[0] ist Server 1 und $newinstance[1] ist unser Server 2. Falls nun ein SQL-Befehl auf beiden Servern ausgeführt werden soll, kann man folgendes Snippet nutzen:
PHP Code:
foreach($newinstance as $server)
{
      
$sql "SELECT * FROM ".$server->Prefix.$server->DB.$server->Postfix.".".$server->TablePrefix."hello".$server->TablePostfix." WHERE id=23";
      
var_dump($server->Select($sql));

$sql hat dabei folgende Werte:
1. Durchlauf: SELECT * FROM foo_bar_woot.bar0_hello_0foo WHERE id=23
2. Durchlauf: SELECT * FROM woot_bar_foo.bar0_hello_0foo WHERE id=23

Falls man nur eine Verbindungsinformation für die Skriptausführung braucht, kann man aus einer Liste von Verbindungsinformationen eine heraussuchen und diese z.B. einer globalen Variable zuweisen, welche im weiteren Verlauf des Skripts genutzt wird:
PHP Code:
$playerdb $newinstance[1]; #jetzt ist Server 2 gewählt
$playerdb $newinstance[0]; #jetzt ist Server 1 gewählt
...
$playerdb->Exec("UPDATE ....."); 
Eine ausführliche Dokumentation aller Methoden und Eigenschaften kann hier gefunden werden:
[Only registered and activated users can see links. Click Here To Register...]
Die Daten können hier gefunden werden:
[Only registered and activated users can see links. Click Here To Register...]

Falls ihr Fragen habt, bin ich natürlich per Skype oder hier direkt verfügbar. Ich kann euch natürlich bei Problemen mit der Erstellung eurer eigenen Webseite helfen.

M2Commander