Friday, June 16, 2017

Lambda, lambda, lambda...


Lambda expression, it's an anonymous function it can use to create delegates or expression tree, by using lambda conditions, we can write local functions that can be passed as parameters, let's see it in action for deleting a records with condition in Managed Dbf and it's equivalent Xbase/VFP/Clipper code.

// TASK : Delete all customers from USA country and state of New York or California.


|------------------------------------------------------------------------------------------------------------------------------------------------------


//--> SOLUTION 1:  (SLOW) : Single line of code
using  Where/For condition
//

var
CUSTOMER = new STDB.Table();
CUSTOMER.File.
Use( @"\\MyServer\Fileman\Customer.Dbf", TableShare.Shared, TableAccess.ReadWrite, TableMaxSize.MaxOf16G );
//

CUSTOMER.Remover.
DeleteAll (  true,   rec => cust.C("COUNTRY") == "USA"  &&  (  rec.C("STATE") == "NY" || rec.C("STATE") == "CA");


*--> X
Base / VFP / Clipper  equivalent code
*
USE \\MyServer\Fileman\CUSTOMER.Dbf  SHARED ALIAS cust
GO TOP
DELETE ALL FOR cust.COUNTRY="USA" AND (  cust.STATE="NY" OR cust.STATE="CA" )


|------------------------------------------------------------------------------------------------------------------------------------------------------



//--> SOLUTION 2:  (  FAST ) Using index order by Country. and using For and  While condition overload.
//

var CUSTOMER = new STDB.Table();
CUSTOMER.File.
Use( @"\\MyServer\Fileman\Customer.Dbf", TableShare.Shared, TableAccess.ReadWrite, TableMaxSize.MaxOf16G );//
CUSTOMER.Indexer.SetIndexOrder("COUNTRY");
CUSTOMER.Searcher.
Find("USA");
//


if
(  CUSTOMER.Searcher.Found()  )
{   
      CUSTOMER.Remover.
DeleteAll (  false,
                                        
//For
                                             rec => ( rec.C("STATE") == "NY" || rec.C("STATE")=="CA") ,                                                              
                                                           
//While
                                                                       rec => ( rec.C("COUNTRY") == "USA" )   );
}



*-->  XBase / VFP / Clipper equivalent code
*
USE \\MyServer\Fileman\CUSTOMER.Dbf  SHARED ALIAS cust
SET ORDER TO COUNTRY
SEEK "USA"
IF FOUND()
     DELETE ALL FOR  ( cust.STATE="NY" OR cust.STATE="CA" ) WHILE cust.COUNTRY="USA"
ENDIF


|------------------------------------------------------------------------------------------------------------------------------------------------------

 
//--> SOLUTION 3: ( FASTEST )  Using index order by Country. and State. and While condition only
//

var CUSTOMER = new STDB.Table();
CUSTOMER.File.Use( @"\\MyServer\Fileman\Customer.Dbf", TableShare.Shared, TableAccess.ReadWrite, TableMaxSize.MaxOf16G );
CUSTOMER.Indexer.
SetIndexOrder("COUNTRYSTATE");
//

CUSTOMER.Searcher.Find("USANY");
if
( CUSTOMER.Searcher.Found() )
{                                                                           
//For    //While
     CUSTOMER.Remover.DeleteAll ( falsenull ,   rec => ( rec.C("COUNTRY") == "USA"  &&  rec.C("STATE") == "NY" )   );
}

//


USTOMER.Searcher.
Find("USACA");if (  CUSTOMER.Searcher.Found()  )
{                                                                            
//For    //While
      CUSTOMER.Remover.DeleteAll ( falsenull ,   rec => ( rec.C("COUNTRY") == "USA" &&  rec.C("STATE") == "CA");
}

 
*--> xBase / VFP / Clipper code*
*
USE \\MyServer\Fileman\CUSTOMER.Dbf  SHARED ALIAS cust
SET ORDER TO CNTRYSTAT
*
SEEK "USANY"
IF FOUND()
    DELETE ALL WHILE cust.COUNTRY="USA" AND cust.STATE="NY"
ENDIF
*
SEEK "USACA"
IF FOUND()
    DELETE ALL WHILE cust.COUNTRY="USA" AND cust.STATE="CA"
ENDIF


Saturday, April 29, 2017

What's wrong extending the life of Dbf file format : - D


I already know what's your thinking... You may say maybe I'm just reinventing the wheel. Why not design my own programming languages and write my own compilers for them? Or even better create my own operating system from scratch? 😄 because there are fricking hundreds if not thousands of available database projects out there in the wild which are ready to use and battle tested.

Whenever you can’t find some information you are looking for a certain applications, or a good software library but lacking some option and features you need, why not create your own? If you have the luxury of time and you believe you can do it, why bother using a third-party software or the work of others.

Alright, aside from the pleasure I get writing codes, fulfilling my enthusiasm for extending the life of DBF file format and staying close to the xBase commands in terms of data manipulation; has given me a great deal of enjoyment particularly in the learning process, these already are greenlight indications that I should keep continuing what I’m doing.


Imagine without any heavy weight database engine or anything to be installed on ones computer but yet you can create a fast, multiuser and easy record manipulation even in billions of records.


C# Using Managed Dbf :


// Create table free instance
var _CUSTOMER = new MDb.Table();

// Open Dbf file in shared mode and read-only
_CUSTOMER.File.Use( @"\\MyServer\Fileman\Customer.Dbf",
                                       TableShare.Shared, TableAccess.ReadOnly, TableMaxSize.MaxOf16G );


// Set sort order by country + state

_CUSTOMER.Indexer.SetIndexOrderTo("COUNTRYSTATE");




Visual FoxPro/xBase Equivalent commands :

SELECT 0
USE \\MyServer\Fileman\CUSTOMER.Dbf  SHARED NOUPDATE
SET ORDER TO COUNRYSTATE




Cheers ^_^y