DBF How to obtain an array containing record number

Re: DBF How to obtain an array containing record number

Postby Otto » Thu Oct 10, 2024 12:42 pm

Hello friends,
In my php4dbf library, I do it exactly the same way.

But I see, despite my efforts to integrate as many functions as possible into the php4dbf library,
I am still far from the source code elegance of Harbour.


Best regards,
Otto

Code: Select all  Expand view


<?php

function parseRecord($record, $fields, $filterFields = []) {
    $result = [];
    $offset = 1;

    foreach ($fields as $field) {
       
        $value = rtrim(substr($record, $offset, $field['length']));
       
        if (empty($filterFields) || in_array($field['name'], $filterFields)) {
            if ($field['type'] === 'N') {
                $value = is_numeric($value) ? floatval($value) : 0.0;
            } elseif ($field['type'] === 'C') {
                $value = my_utf8_encode($value);
            }
            $result[$field['name']] = $value;
        }
        $offset += $field['length'];
    }
    return $result;
}

function read_dbf( $file_path) {
   
    $dbData = php4dbf_openFile( $file_path, true, [ 'LAST' ] );
   
    $records = [];
    $parsedRecords = [];  

    for ($i = 1; $i <= php4dbf_eof( $dbData ); $i++) {    
        $record = php4dbf_readRecordByIndex( $dbData, $i - 1 );
        if ($record !== null) {
            $parsedRecord = parseRecord( $record, $dbData['fields'], $dbData['filterFields']);
            $parsedRecords[] = $parsedRecord;
        }
    }
    php4dbf_closeDbfFile($dbData);
    return $parsedRecords;  
}

$file_path = "........";
$records = read_dbf($file_path);

?>


 
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6332
Joined: Fri Oct 07, 2005 7:07 pm

Re: DBF How to obtain an array containing record number

Postby Otto » Thu Oct 10, 2024 12:54 pm

Marco,
Some benchmarks using a table opened by other user containing 6000 records indexed in a lan
0.06 seconds using Enrico's technique and 11.57 seconds using a normal DO WHILE !EOF() ; SKIP ; ENDDO cicle

If table is opened only by this test program the speed is the same 0.06 seconds


USE reads the DBF file, similar to memoread().

The record pointer (GOTO) is moved by an offset, which is determined by the definition of the header and fields to set the starting point. The pointer is then shifted by the length resulting from the sum of the field definitions.

The result is a string corresponding to the record line. This string is then split into individual fields. For this reason, no delays occur, even if multiple users open the file. In this context, opening the file simply means that a file handle is present or the entire database has already been read into memory as a string.

Best regards,
Otto


Code: Select all  Expand view
A DBF file consists of two main components:

Header: Contains metadata about the file, such as the number of records, the number and type of fields (columns), and other administrative information.
Data Area: Contains the actual records stored in the file. Each record consists of a series of fields corresponding to the table's columns.
Header
The header of the DBF file is divided into several sections:

File Type: A byte indicating the type of file and the dBASE version. Date of Last Update: The last three bytes of the header indicate the date of the last modification. Number of Records: A 4-byte value indicating the total number of records in the file. Header Length: A 2-byte value indicating the length of the header in bytes. Record Length: A 2-byte value indicating the length of a single record in bytes. Reserved Areas: Several reserved areas for future use or specific software applications. Field Descriptions: A variable number of 32-byte blocks containing the description of each field in the file. This description includes the field name, data type, field length, decimal places (for numeric fields), and other attributes.
Data Area
The data area stores the actual table data. Each record consists of a fixed number of bytes, as specified in the header. The data is stored in a row-organized structure, with each row representing a record and each column representing a field.

Fields: Fields can have different data types, such as strings (Char), numeric values (Num), logical values (Bool), date (Date), and other specific types depending on the dBASE version. Delete Mark: Each record begins with a byte indicating whether the record is marked as deleted. A value of 0x20 (space) means the record is active, while a value of 0x2A (asterisk) means the record is deleted.
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6332
Joined: Fri Oct 07, 2005 7:07 pm

Previous

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 51 guests