Not strictly Delphi, but useful for those that have to do diagnostics on SQL tables.

Not strictly Delphi, but useful for those that have to do diagnostics on SQL tables.

Originally shared by Lars Fosdal

Finding fields with a specific value in any table

(SQL Server)

Trying to find some mixed up data references, I wrote this little snippet to check for any field named “something” in any table, where the value =”value”.

Example 1

Find rows with fields ending in ‘id’ and value equals 6653

exec [p_UTIL_FindFields]@Name= N’%id’,@Value= N’6653′ 

gives an output like

[dbo].[p_UTIL_FindFields]@Name= N’%id’,@Value= N’6653′

Finding Fields with names like %id where value = 6653

select @countOut= count() from t_purchase_order_line_mvx_data where Id =  6653 returns 1

select @countOut= count() from t_purchase_order_tpack_reportings where Id =  6653 returns 1

select @countOut= count() from t_purchase_order_tpack_reportings where PurchaseOrderTPackLnkId =  6653 returns 2

select @countOut= count() from t_tpack_logs where Id =  6653 returns 1

select @countOut= count() from t_tpack_logs where TPackId =  6653 returns 4

select @countOut= count() from t_tpack_logs where DeliveryId =  6653 returns 14

select @countOut= count() from t_customer_order_delivery_shipping_packages where Id =  6653 returns 1

select @countOut= count() from t_customer_order_delivery_shipping_packages where CustomerOrderDeliveryId =  6653 returns 12

select @countOut= count() from t_tpack_move_orders where ToStoragePositionId =  6653 returns 1

select @countOut= count() from t_tpack_move_orders where OriginalToStoragePositionId =  6653 returns 1

select @countOut= count() from t_lot_tpack_logs where Id =  6653 returns 1

select @countOut= count() from t_lot_tpack_logs where LotTPackId =  6653 returns 1

select @countOut= count() from t_lot_tpack_logs where LotId =  6653 returns 8

select @countOut= count() from t_lot_tpack_logs where TPackId =  6653 returns 2

select @countOut= count() from t_lot_logs where LotId =  6653 returns 7

select @countOut= count() from t_tpack_purchaseorder_imports where Id =  6653 returns 1

select @countOut= count() from t_tpacks_to_production where Id =  6653 returns 1

Checked 435 table and column combos with 7 errors

Example 2

Find rows with fields containing ‘storage’ in their name, and value equals 6553

exec [p_UTIL_FindFields]@Name= N’%storage%’,@Value= N’6653′

gives an output like

[p_UTIL_FindFields]@Name= N’%storage%’,@Value= N’6653′

Finding Fields with names like %storage% where value = 6653

select @countOut= count() from t_tpack_move_orders where ToStoragePositionId =  6653 returns 1

select @countOut= count(*) from t_tpack_move_orders where OriginalToStoragePositionId =  6653 returns 1

Checked 64 table and column combos with 3 errors

Janez Atmapuri Makovsek found some #XE6  array optimizations

Janez Atmapuri Makovsek found some  array optimizations

Inline declared setter and getter functions can yield up to six times the performance on one-dimensional arrays and up to four times the performance on 2D arrays, compared to XE5.

Spotted at the EMBT forums.

Learning more from your debug output

Learning more from your debug output

In the context Nick Hodges’s post on memory usage, here are some tweaks for OutputDebugString which I’ve been using for a few years to help debug trace live services running in production environment.  The memory consumption code looks like the spitting image of the stackoverflow post, so I probably gleaned it from there, or possibly from 

http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/monitormemorymanager_xml.html.

or the MemoryManager usage tracker sample:

http://sourceforge.net/p/radstudiodemos/code/HEAD/tree/branches/RadStudio_XE4/Delphi/RTL/Usage%20Tracker/MemoryManagerUsageTracker.pas

If you peruse the documentation –

http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/monitormemorymanager_xml.html

and

http://docwiki.embarcadero.com/RADStudio/XE4/en/Monitoring_Memory_Usage

also give hints about GetMemoryMap which supposedly helps you track memory for the entire process, in case you have sub-processes or COM stuff to deal with.  

I haven’t dabbled there, though.

unit CustomDebugOut;

// Lars Fosdal

interface

uses

  Windows, SysUtils, DateUtils;

  procedure OutputDebugString(aStr:PChar); Inline; overload;

  procedure OutputDebugString(const aStr:String); Inline; overload;

  function CurrentThreadId:String; Inline;

  function CurrentTime:String; Inline;

  function MemoryUsed: cardinal; Inline;

implementation

function CurrentThreadId:String; Inline;

begin

  Result := IntToStr(GetCurrentThreadId);

end;

function CurrentTime:String; Inline;

begin

  Result := FormatDateTime(‘hh:nn:ss,zzz ‘, Now);

end;

function MemoryUsed: cardinal; Inline;

var

  MMS: TMemoryManagerState;

  Block: TSmallBlockTypeState;

begin

  GetMemoryManagerState(MMS);

  Result := MMS.TotalAllocatedMediumBlockSize + MMS.TotalAllocatedLargeBlockSize;

  for Block in MMS.SmallBlockTypeStates

   do Result := Result + (Block.UseableBlockSize * Block.AllocatedBlockCount);

  Result := Result DIV 1024;

end;

procedure OutputDebugString(aStr:PChar); Inline;

begin

  Windows.OutputDebugString(pChar(‘{‘

   + CurrentTime                       // time

   + ‘(‘+CurrentThreadId+’)’           // thread

   + ‘ ‘+ IntToStr(MemoryUsed) +’k’   // memory in k

   + ‘} ‘ + aStr));

end;

procedure OutputDebugString(const aStr:String); inline;

begin

  OutputDebugString(pChar(aStr));

end;

end.

—-

Simple Generics: A way to reduce the number of explicit declarations for action, setter and getter types.

Simple Generics: A way to reduce the number of explicit declarations for action, setter and getter types.

 

type

  // procedure/function pointers

  TPtrProc = procedure;

  TPtrParamProc = procedure (const v: TA);

 

  TPtrFunc= function:TR;

  TPtrParamFunc = function(const v:TA):TR;

 

  // Anonymous procedure type templates

  TAnonProc = reference to procedure;

  TAnonParamProc = reference to procedure (const v:TA);

 

  TAnonFunc= reference to function:TR;

  TAnonParamFunc = reference to function (const v:TA):TR;

 

  // Object method variable type templates

  TObjectProc = procedure of object;

  TObjectParamProc = procedure (const v:TA) of object;

 

  TObjectFunc= function:TR of object;

  TObjectParamFunc = function (const v:TA):TR of object;

 

Allows you to declare like this

 

.. property OnChangeTime: TObjectParamProc;

.. property OnValidate: TAnonParamFunc;

 

You can of course be explicit with these as well…

type

  TOnTimeChange = TObjectParamProc;

  TOnValidate = TAnonParamFunc;

Testing sharing of source straight off Google Drive

Testing sharing of source straight off Google Drive

 

My good, old text file device drivers that speeds up reading and writing text files.

 

For some reason, I can’t share directly to the Community, but have to share it as a link from the +Share button.