2 de diciembre de 2005

Determinar si un campo es AutoIncremental

Determinar si es un campo específico es un campo AutoIncremental, es un poco molesto. Esa información está solamente disponible comprobando la 17° o 18° columna de la matriz creada por AFIELDS() en la fila del campo.

La rutina X8IsAutoInc.PRG de este artículo hace que esa información sea fácil de consultar.

* 
*  X8IsAutoInc.PRG
*
*  RETURNs a logical value indicating whether the
*  passed field is an AutoInc field.
*
*  Author:  Drew Speedie  
*
*  lParameters
*  tcFieldName (R)  FieldName or Alias.FieldName
*                     to be checked to see if it
*                     is an AutoInc field.
*      tcAlias (O)  If tcFieldName is passed as
*                     Alias.FieldName, this parameter
*                     is ignored.
*                   If tcFieldName is passed as 
*                     just a FieldName, this parameter
*                     is REQUIRED, specifying the ALIAS()
*                     whose tcFieldName is to be
*                     checked to see if it is an AutoInc
*                     field.
*
*  If tcFieldName is in a REMOTE VIEW, this routine
*    RETURNS .F.
*  If tcFieldName is in a LOCAL VIEW, this routine 
*    RETURNS a logical value indicating whether tcFieldName
*    in its base table is an AutoInc field.
*
*  The Alias specified in tcFieldName/tcAlias must be USED(),
*  if the Alias is a local view, its base table must
*  also be USED().
*
LPARAMETERS tcFieldName, tcAlias

IF VERSION(5) < 800
  RETURN .f.
ENDIF

IF NOT VARTYPE(tcFieldName) = "C" ;
     OR EMPTY(tcFieldName) 
  ASSERT .f. MESSAGE "tcFieldName is required"
  RETURN .NULL.
ENDIF

LOCAL lcAlias, lcFieldName
lcFieldName = UPPER(ALLTRIM(tcFieldName))
IF OCCURS(".",lcFieldName) = 1
  lcAlias = JUSTSTEM(lcFieldName)
  lcFieldName = JUSTEXT(lcFieldName)
 ELSE
  IF VARTYPE(tcAlias) = "C" AND NOT EMPTY(tcAlias) 
    lcAlias = UPPER(ALLTRIM(tcAlias))
   ELSE
    ASSERT .f. MESSAGE "tcAlias is required when po does not include the Alias"
    RETURN .NULL.
  ENDIF
ENDIF       

IF CURSORGETPROP("SourceType",lcAlias) = 2
  *
  *  remote view
  *
  RETURN .f.
ENDIF

LOCAL laFields[1], xx, llAutoInc, llView, lnSelect
llAutoInc = .f.

lnSelect = SELECT(0)
SELECT (lcAlias)

llView = CURSORGETPROP("SourceType",lcAlias) = 1
IF llView
  lcAlias = X8BTABLE(lcAlias+"."+lcFieldName,CURSORGETPROP("Database",lcAlias))
  SELECT (lcAlias)
ENDIF

AFIELDS(laFields)
xx = ASCAN(laFields,lcFieldName,1,-1,1,15)
IF xx > 0
  llAutoInc = laFields[xx,17] > 0
ENDIF

SELECT (lnSelect)

RETURN llAutoInc

VFP Tips & Tricks - Drew Speedie

1 comentario :