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

DATATypeDescription
oCnObjectFWMariaConnection object
cTableCharacterTable name
cWhereCharacterWHERE clause identifying the current record
aStructArrayTable column structure (name, type, length, decimals, flags)
aBlankArrayBlank values for new record creation
aPrimaryArrayIndices of primary key columns in aStruct
nAutoIncFldNumericPosition of auto-increment field (0 if none)
lValidDataLogicalACCESS: .T. when record data is loaded

Methods

MethodDescription
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

See Also