====== std.db.mysql ======
Vollständige MySQL-Clientimplementierung auf Basis des MySQL-Binärprotokolls (Protocol 41) direkt über TCP – ohne externe Clientbibliothek. Unterstützt Verbindungsaufbau mit Authentifizierung, SQL-Abfragen, strukturierte Ergebnisverarbeitung, Transaktionen (BEGIN/COMMIT/ROLLBACK) und typsichere Prepared Statements mit Parameterbindung.
Einsatzbereiche sind alle Anwendungen, die Daten persistent in MySQL- oder MariaDB-Datenbanken speichern – von einfachen CRUD-Operationen bis hin zu komplexen transaktionalen Workflows.
import std.db.mysql;
var conn: MySQLConn := MySQLConnect("127.0.0.1", 3306, "user", "pass", "mydb");
var res: MySQLResult := MySQLQuery(conn, "SELECT id, name FROM users WHERE active = 1");
while (MySQLFetchRow(res) != 0) {
Print(MySQLGetRowStr(res, 0, 1)); // Spalte "name"
}
MySQLFreeResult(res);
MySQLClose(conn);
----
===== Imports =====
* ''std.net.types''
* ''std.net.syscalls''
* ''std.string''
* ''std.crypto.sha1''
----
===== Konstanten =====
^ Name ^ Typ ^ Wert ^ Sichtbarkeit ^
| ''CLIENT_LONG_PASSWORD'' | ''int64'' | ''1'' | pub |
| ''CLIENT_FOUND_ROWS'' | ''int64'' | ''2'' | pub |
| ''CLIENT_LONG_FLAG'' | ''int64'' | ''4'' | pub |
| ''CLIENT_CONNECT_WITH_DB'' | ''int64'' | ''8'' | pub |
| ''CLIENT_PROTOCOL_41'' | ''int64'' | ''512'' | pub |
| ''CLIENT_TRANSACTIONS'' | ''int64'' | ''8192'' | pub |
| ''CLIENT_SECURE_CONNECTION'' | ''int64'' | ''32768'' | pub |
| ''CLIENT_MULTI_STATEMENTS'' | ''int64'' | ''65536'' | pub |
| ''CLIENT_MULTI_RESULTS'' | ''int64'' | ''131072'' | pub |
| ''CLIENT_PLUGIN_AUTH'' | ''int64'' | ''524288'' | pub |
| ''DEFAULT_CAP_FLAGS'' | ''int64'' | ''762383'' | pub |
| ''SERVER_STATUS_IN_TRANSACTION'' | ''int64'' | ''1'' | pub |
| ''SERVER_STATUS_AUTOCOMMIT'' | ''int64'' | ''2'' | pub |
| ''MYSQL_TYPE_DECIMAL'' | ''int64'' | ''0'' | pub |
| ''MYSQL_TYPE_TINY'' | ''int64'' | ''1'' | pub |
| ''MYSQL_TYPE_SHORT'' | ''int64'' | ''2'' | pub |
| ''MYSQL_TYPE_LONG'' | ''int64'' | ''3'' | pub |
| ''MYSQL_TYPE_FLOAT'' | ''int64'' | ''4'' | pub |
| ''MYSQL_TYPE_DOUBLE'' | ''int64'' | ''5'' | pub |
| ''MYSQL_TYPE_NULL'' | ''int64'' | ''6'' | pub |
| ''MYSQL_TYPE_TIMESTAMP'' | ''int64'' | ''7'' | pub |
| ''MYSQL_TYPE_LONGLONG'' | ''int64'' | ''8'' | pub |
| ''MYSQL_TYPE_INT24'' | ''int64'' | ''9'' | pub |
| ''MYSQL_TYPE_DATE'' | ''int64'' | ''10'' | pub |
| ''MYSQL_TYPE_TIME'' | ''int64'' | ''11'' | pub |
| ''MYSQL_TYPE_DATETIME'' | ''int64'' | ''12'' | pub |
| ''MYSQL_TYPE_YEAR'' | ''int64'' | ''13'' | pub |
| ''MYSQL_TYPE_VARCHAR'' | ''int64'' | ''15'' | pub |
| ''MYSQL_TYPE_BIT'' | ''int64'' | ''16'' | pub |
| ''MYSQL_TYPE_NEWDECIMAL'' | ''int64'' | ''246'' | pub |
| ''MYSQL_TYPE_BLOB'' | ''int64'' | ''252'' | pub |
| ''MYSQL_TYPE_VARSTRING'' | ''int64'' | ''253'' | pub |
| ''MYSQL_TYPE_STRING'' | ''int64'' | ''254'' | pub |
| ''MYSQL_TYPE_GEOMETRY'' | ''int64'' | ''255'' | pub |
| ''COM_QUIT'' | ''int64'' | ''1'' | pub |
| ''COM_INIT_DB'' | ''int64'' | ''2'' | pub |
| ''COM_QUERY'' | ''int64'' | ''3'' | pub |
| ''COM_PING'' | ''int64'' | ''14'' | pub |
| ''COM_STMT_PREPARE'' | ''int64'' | ''22'' | pub |
| ''COM_STMT_EXECUTE'' | ''int64'' | ''23'' | pub |
| ''COM_STMT_CLOSE'' | ''int64'' | ''25'' | pub |
| ''MAX_PACKET_SIZE'' | ''int64'' | ''16777215'' | pub |
| ''MYSQL_ERRMSG_SIZE'' | ''int64'' | ''256'' | pub |
| ''MYSQL_NAME_SIZE'' | ''int64'' | ''64'' | pub |
| ''SCRAMBLE_LENGTH'' | ''int64'' | ''20'' | pub |
| ''MYSQL_STATUS_NOT_CONNECTED'' | ''int64'' | ''0'' | pub |
| ''MYSQL_STATUS_CONNECTED'' | ''int64'' | ''1'' | pub |
----
===== Typen =====
==== MySQLConn (struct) ====
^ Feld ^ Typ ^
| ''fd'' | ''int64'' |
| ''host'' | ''pchar'' |
| ''port'' | ''int64'' |
| ''user'' | ''pchar'' |
| ''password'' | ''pchar'' |
| ''database'' | ''pchar'' |
| ''packet_num'' | ''int64'' |
| ''last_payload_len'' | ''int64'' |
| ''server_ver'' | ''int64'' |
| ''cap_flags'' | ''int64'' |
| ''charset'' | ''int64'' |
| ''status'' | ''int64'' |
| ''server_scramble'' | ''int64'' |
| ''last_error'' | ''int64'' |
| ''errmsg'' | ''pchar'' |
| ''error_num'' | ''int64'' |
| ''affected_rows'' | ''int64'' |
| ''insert_id'' | ''int64'' |
| ''field_count'' | ''int64'' |
==== MySQLResult (struct) ====
^ Feld ^ Typ ^
| ''field_count'' | ''int64'' |
| ''affected_rows'' | ''int64'' |
| ''insert_id'' | ''int64'' |
| ''server_status'' | ''int64'' |
| ''warning_count'' | ''int64'' |
| ''message'' | ''pchar'' |
| ''fields'' | ''int64'' |
| ''field_alloc'' | ''int64'' |
| ''current_field'' | ''int64'' |
| ''row_count'' | ''int64'' |
| ''rows'' | ''int64'' |
| ''row_alloc'' | ''int64'' |
| ''current_row'' | ''int64'' |
| ''row_lengths'' | ''int64'' |
| ''field_buf'' | ''int64'' |
==== MySQLField (struct) ====
^ Feld ^ Typ ^
| ''catalog'' | ''pchar'' |
| ''db'' | ''pchar'' |
| ''table'' | ''pchar'' |
| ''org_table'' | ''pchar'' |
| ''name'' | ''pchar'' |
| ''org_name'' | ''pchar'' |
| ''charset'' | ''int64'' |
| ''length'' | ''int64'' |
| ''ft'' | ''int64'' |
| ''flags'' | ''int64'' |
| ''decimals'' | ''int64'' |
| ''def'' | ''pchar'' |
==== MySQLRow (struct) ====
^ Feld ^ Typ ^
| ''values'' | ''int64'' |
| ''lengths'' | ''int64'' |
| ''count'' | ''int64'' |
==== MySQLStmt (struct) ====
^ Feld ^ Typ ^
| ''param_count'' | ''int64'' |
| ''column_count'' | ''int64'' |
| ''params_alloc'' | ''int64'' |
| ''result'' | ''MySQLResult'' |
==== StmtParam (struct) ====
^ Feld ^ Typ ^
| ''is_null'' | ''bool'' |
| ''error'' | ''int64'' |
----
===== Funktionen =====
^ Signatur ^ Sichtbarkeit ^ Beschreibung ^
| ''MySQLSendPacket(conn: MySQLConn, data: int64, len: int64): bool'' | pub | Sendet Paket über MySQL-Verbindung |
| ''MySQLReadPacket(conn: MySQLConn): int64'' | pub | Empfängt Paket von MySQL-Server |
| ''MySQLConnect(host: pchar, port: int64, user: pchar, pass: pchar, db: pchar): MySQLConn'' | pub | Stellt MySQL-Verbindung her |
| ''MySQLClose(conn: MySQLConn): void'' | pub | Schließt MySQL-Verbindung |
| ''MySQLQuery(conn: MySQLConn, sql: pchar): MySQLResult'' | pub | Führt SQL-Abfrage aus |
| ''MySQLError(conn: MySQLConn): pchar'' | pub | Gibt letzte Fehlermeldung zurück |
| ''MySQLErrno(conn: MySQLConn): int64'' | pub | Gibt letzten MySQL-Fehlercode zurück |
| ''MySQLAffectedRows(conn: MySQLConn): int64'' | pub | Gibt Anzahl betroffener Zeilen zurück |
| ''MySQLInsertId(conn: MySQLConn): int64'' | pub | Gibt Auto-Increment-ID des letzten Inserts zurück |
| ''MySQLNumFields(res: MySQLResult): int64'' | pub | Gibt Spaltenanzahl des Ergebnisses zurück |
| ''MySQLNumRows(res: MySQLResult): int64'' | pub | Gibt Zeilenanzahl des Ergebnisses zurück |
| ''MySQLFreeResult(res: MySQLResult): void'' | pub | Gibt Ergebnis-Speicher frei |
| ''MySQLFetchRow(res: MySQLResult): int64'' | pub | Liest nächste Ergebniszeile |
| ''MySQLDataSeek(res: MySQLResult, rowNum: int64): void'' | pub | Setzt Cursor auf bestimmte Zeile |
| ''MySQLGetFieldName(res: MySQLResult, idx: int64): pchar'' | pub | Gibt Spaltenname nach Index zurück |
| ''MySQLGetFieldType(res: MySQLResult, idx: int64): int64'' | pub | Gibt Datentyp einer Spalte zurück |
| ''MySQLGetFieldLength(res: MySQLResult, idx: int64): int64'' | pub | Gibt maximale Feldlänge zurück |
| ''MySQLGetRowStr(res: MySQLResult, row: int64, idx: int64): pchar'' | pub | Liest Zellwert als String |
| ''MySQLGetRowInt(res: MySQLResult, row: int64, idx: int64): int64'' | pub | Liest Zellwert als Integer |
| ''MySQLIsNull(res: MySQLResult, row: int64, idx: int64): bool'' | pub | Prüft ob Zellwert NULL ist |
| ''MySQLBegin(conn: MySQLConn): bool'' | pub | Startet Datenbanktransaktion |
| ''MySQLCommit(conn: MySQLConn): bool'' | pub | Bestätigt offene Transaktion |
| ''MySQLRollback(conn: MySQLConn): bool'' | pub | Bricht offene Transaktion zurück |
| ''MySQLSetAutoCommit(conn: MySQLConn, enable: bool): bool'' | pub | Setzt Auto-Commit-Modus |
| ''MySQLInTransaction(conn: MySQLConn): bool'' | pub | Prüft ob Transaktion aktiv ist |
**Die Bindeindizes sind 0-basiert.** Der erste Platzhalter ''?'' ist Index **0**, nicht 1 — wie in [[lyx_-_programmiersprache:units:db:postgres|std.db.postgres]], aber **anders als** in [[lyx_-_programmiersprache:units:db:sqlite|std.db.sqlite]], wo 1-basiert gebunden wird.
^ Unit ^ Bindeindex ^
| ''std.db.mysql'' | 0-basiert |
| ''std.db.postgres'' | 0-basiert |
| ''std.db.sqlite'' | **1-basiert** |
Wer zwischen den Units wechselt, verschiebt sonst alle Parameter um eins — mit der unangenehmen Eigenschaft, dass die Abfrage oft trotzdem läuft und nur falsche Werte einsetzt.
| ''MySQLStmtPrepare(conn: MySQLConn, sql: pchar): MySQLStmt'' | pub | Bereitet Prepared Statement vor |
| ''MySQLStmtExecute(conn: MySQLConn, stmt: MySQLStmt): MySQLResult'' | pub | Führt Prepared Statement aus |
| ''MySQLStmtBindParam(stmt: MySQLStmt, paramIdx: int64, typeCode: int64, buffer: int64, bufferLen: int64): bool'' | pub | Bindet Puffer an Statement-Parameter |
| ''MySQLStmtBindNull(stmt: MySQLStmt, paramIdx: int64, isNull: bool): bool'' | pub | Setzt Parameter auf NULL |
| ''MySQLStmtBindInt(stmt: MySQLStmt, paramIdx: int64, value: int64): bool'' | pub | Bindet Integer an Statement-Parameter |
| ''MySQLStmtBindStr(stmt: MySQLStmt, paramIdx: int64, str: pchar): bool'' | pub | Bindet String an Statement-Parameter |
| ''MySQLStmtBindResult(stmt: MySQLStmt, idx: int64, typeCode: int64, buffer: int64, bufferLen: int64): bool'' | pub | Bindet Ergebnispuffer an Statement |
| ''MySQLStmtFetch(stmt: MySQLStmt): int64'' | pub | Liest nächste Zeile des Statement-Ergebnisses |
| ''MySQLStmtClose(stmt: MySQLStmt): void'' | pub | Schließt Prepared Statement |
| ''MySQLStmtReset(stmt: MySQLStmt): void'' | pub | Setzt Prepared Statement zurück |
| ''MySQLPoolCreate(maxConnections: int64): int64'' | pub | Erstellt Connection-Pool |
| ''MySQLPoolDestroy(poolHandle: int64): void'' | pub | Zerstört Connection-Pool |