FWMariaRecord
Source: source/classes/mariarec.prg
Inherits from: TDataRow
FWMariaRecord provides single-record ORM-style access to MariaDB/MySQL tables. It extends TDataRow to automatically detect table structure, primary keys, and auto-increment columns. The class handles the complete CRUD lifecycle: loading a record from the database, editing field values via TDataRow's property access, and saving changes with automatic upsert (INSERT or UPDATE as appropriate).
Key DATA Members
| DATA | Type | Description |
|---|---|---|
oCn | Object | FWMariaConnection object |
cTable | Character | Table name |
cWhere | Character | WHERE clause identifying the current record |
aStruct | Array | Table column structure (name, type, length, decimals, flags) |
aBlank | Array | Blank values for new record creation |
aPrimary | Array | Indices of primary key columns in aStruct |
nAutoIncFld | Numeric | Position of auto-increment field (0 if none) |
lValidData | Logical | ACCESS: .T. when record data is loaded |
Methods
| Method | Description |
|---|---|
New( oCn, cTable, cWhere ) | Create and load a record. If cWhere is nil or empty, creates a blank record for new entry. |
Read( cWhere ) | Load a specific record identified by the WHERE clause |
Blank() | Reset the record to a blank template for inserting a new row |
Load( lBlank ) | Load record from server (or blank template if lBlank is .T.) |
Save() | Save changes using automatic upsert: INSERT if new, UPDATE if existing |
MakePrimaryWhere() | Build a WHERE clause from current primary key field values |
Field Access
Since FWMariaRecord inherits from TDataRow, field values are accessible via the
FieldGet() and FieldPut() methods using either field names
or numeric positions:
oRec:FieldGet( "name" ) // by field name
oRec:FieldGet( 2 ) // by numeric position
oRec:FieldPut( "phone", "555-1234" ) // set by field name
Example: Load and Update Record
#include "FiveWin.ch"
function Main()
local oCn, oRec
// Connect to database
oCn := FWMariaConnection():New( "localhost", "mydb", "root", "password" )
if oCn:nError != 0
MsgStop( "Connection failed" )
return nil
endif
// --- READ existing record ---
oRec := FWMariaRecord():New( oCn, "customers", "id = 1" )
if oRec:lValidData
? "Customer:", oRec:FieldGet( "name" )
? "Email:", oRec:FieldGet( "email" )
? "Phone:", oRec:FieldGet( "phone" )
endif
// --- UPDATE record ---
oRec:FieldPut( "phone", "555-9999" )
oRec:FieldPut( "city", "Madrid" )
if oRec:Save()
MsgInfo( "Record updated successfully" )
endif
// --- CREATE new record ---
oRec := FWMariaRecord():New( oCn, "customers" ) // blank record
oRec:FieldPut( "name", "Jane Smith" )
oRec:FieldPut( "email", "jane@example.com" )
oRec:FieldPut( "phone", "555-4321" )
if oRec:Save()
MsgInfo( "New customer ID: " + Str( oRec:FieldGet( "id" ) ) )
endif
// --- Load a different record ---
oRec:Read( "email = 'jane@example.com'" )
if oRec:lValidData
MsgInfo( "Found: " + oRec:FieldGet( "name" ) )
endif
oCn:Close()
return nil
Notes
- FWMariaRecord automatically detects the table structure, primary keys, and auto-increment columns on first load. This enables intelligent upsert behavior without manual configuration.
- The
Save()method usesoCn:Upsert()internally, which generates anINSERT ... ON DUPLICATE KEY UPDATEstatement. The primary key detection ensures correct behavior. - After inserting a new record with an auto-increment primary key, the new ID is automatically retrieved and stored in the record's data.
- Character fields are padded to their defined length when Unicode mode is active (controlled by
oCn:lUnicode).