How to use the CREATE ALIAS command to access system tables

Posted by Rom Elwell on 27-Feb-2014 10:08

SETUP:

I am having difficulty using the CREATE ALIAS command to create an alias in one class that can subsequently be utilized in a second class.

I want to query the system table "_Field" for the connected DBs to find the value of the "_Format" field.  I have two classes, 'AClass' and 'BClass'.  AClass creates the DB Alias and BClass attempts to use the alias to query the system table.

In Class 'AClass', I have defined a method that creates an alias for a defined DB connection.  

METHOD PROTECTED CHAR GetFieldFormat (INPUT fieldName AS CHAR, INPUT tableName AS CHAR): 
  DEF VAR oBClass AS CLASS BClass.
DEF VAR tmpFormat AS CHAR INIT "". DEF VAR iCount AS INT INIT 1.

oBClass = NEW BClass (). DO iCount = 1 TO NUM-DBS: CREATE ALIAS VSTAlias FOR DATABASE VALUE(LDBNAME(iCount)) NO-ERROR. tmpFormat = oBClass:GetFieldFormat(fieldName, tableName). /* this method is defined in class BClass, seen below */ IF tmpFormat <> "" THEN LEAVE. END.
DELETE OBJECT oBClass. RETURN tmpFormat. END METHOD.


In the class 'BClass', I have created the method called from 'AClass', GetFieldFormat.

METHOD PUBLIC CHAR GetFieldFormat (INPUT fieldName AS CHAR, INPUT tableName AS CHAR):
  FIND FIRST _File NO-LOCK WHERE _file-name = tableName NO-ERROR.
  FIND FIRST _Field OF _File NO-LOCK WHERE _Field-Name = fieldName NO-ERROR.
  IF AVAIL _Field THEN RETURN Field._Format.
  ELSE RETURN ?.
END METHOD.

QUESTION:

The issue is that I need to use the alias VSTAlias in BClass to ensure I am querying the desired DB. I tried the syntax seen below however the compiler threw an error because it did not recognize the variable 'VSTAlias'.
Thus, how can I use the CREATE ALIAS command to query multiple DB instances in an OE Class? Does the CREATE ALIAS command need to be in a separate .R file than the .R file that attempts to use it?

FIND FIRST VSTAlias._File NO-LOCK WHERE _file-name = tableName NO-ERROR. FIND FIRST VSTAlias._Field OF VSTAlias._File NO-LOCK WHERE _Field-Name = fieldName NO-ERROR.


All Replies

Posted by James Palmer on 27-Feb-2014 10:12

You've basically answered your own question. Yes the Alias is only available if you use it in a separate piece of code to the one that creates it.

Posted by Rom Elwell on 27-Feb-2014 10:14

James,

when I attempt to reference the Alias as seen in the Question paragraph of my initial post, the compiler throws an error that the variable 'VSTAlias' is unknown.  How can I get the method in ClassB to recognize the Alias created in ClassA?

Posted by James Palmer on 27-Feb-2014 10:48

I've got some code that does the following...

a.p:

     CREATE ALIAS DBVIEW FOR DATABASE VALUE(T_DB.DB_NAME).

     RUN b.P (T_FILE.DESCRIPTION).

     DELETE ALIAS DBVIEW.

b.p:

DEFINE INPUT PARAMETER iFileName AS CHAR NO-UNDO.

FOR FIRST DBVIEW._File FIELDS(_File-Number _Prime-Index) WHERE DBVIEW._File._File-Name = iFileName NO-LOCK:

END.

Posted by Rom Elwell on 27-Feb-2014 10:55

Thank you again James.  Your code matches very closely what is in the documentation for this command. I'm curious if the same logic can be accomplished using OE Classes instead of Procedures.

Posted by Håvard Danielsen on 28-Feb-2014 11:46

You can use ALIAS with classes the same way you do with procedures. The ALIAS must be defined before you NEW the class. There was a bug that was fixed in 11.3 that could cause the class to not pick up the next ALIAS. I do not remember the exact details, but I think it happened in cases where the r-code was still in memory when it was NEWed again.  

This thread is closed