Navigation:  The Server in depth > Server Extended Settings > Remote Command Handler >

Server Side Pascal Scripts and NxCommand

Previous pageReturn to chapter overviewNext page

 

One major feature of NexusDB engine is the ability to run server side pascal scripts. These scripts have full access to the server infrastructure (like server engine, monitors, settings) as well as to all databases.

Please note that technical support for writing scripts is not provided with the standard support packages, but is available on a consulting basis.

As a tutorial here is a small script that executes a query and dumps the data to the output.

 

Example

 

var

 aSession: TnxSession;

 adb: TnxDatabase;

 aQuery: TnxQuery;

 i: integer;

begin

 aSession:=TnxSession.Create(nil);

 adb:=TnxDatabase.Create(nil);

 aQuery:=TnxQuery.Create(nil);

 try

   aSession.ServerEngine:=ServerEngine;

   aSession.Active:=true;

   adb.AliasName:='Test';

   adb.Session:=aSession;

   adb.Open;

   aQuery.Database:=adb;

   aQuery.SQL.Text:='select * from test';

   aQuery.Open;

   while not aQuery.eof do

   begin

     for i:=0 to aQuery.FieldCount-1 do

       Output.Write(aQuery.Fields.Fields[i].AsString+',');

     Output.WriteLn('');

     aQuery.Next;

   end;

   Output.SaveToFile('c:\out.txt');

 finally

   aQuery.Free;

   adb.free;

   aSession.Free;

 end;

end.

 

As you can see, very straight forward, the global variable ServerEngine  is a direct reference to the main server engine of the server where the script is executed. The global Output class instance (preliminary class members) is used to dump the data.

The script processing on the server side is implemented as a special handler of the also new nxRemoteRemoteCommands plugin. This plugin has an extensible Type parameter and one such type is cmdExecScript.

 

How to run a script?

To run the script simply call the plugins ExecCommand method. The server side implementation of the plugin creates an Alias 'ServerRemoteCommand' which will have the the output of the script buffered in a (per command unique) table.

 

One of the simplest ways start a script and get return the output is the following:

 

begin

 Transport:=TnxWinsockTransport.Create(nil);

 ServerEngine:=TnxRemoteServerEngine.Create(nil);

 Session:=TnxSession.Create(nil);

 rc:=TnxRemoteRemoteCommandsPlugin.Create(nil);

 try

   try

     Transport.ServerNameRuntime:='nexusdb@127.0.0.1';

     ServerEngine.Transport:=Transport;

     Session.ServerEngine:=ServerEngine;

     rc.Session:=Session;

     rc.Active:=true;

     rc.ExecCommand('cmdExecScript', 'myscript.nxscript', '', error, errormessage, taskid);

 

     if (error<>0) or (taskid=0) then

       writeln(error, ' - ', errormessage)

    else

    begin

       Taskinfo:=TnxRemoteTaskInfo.Create(TnxRemoteSession(Session.AbstractSession), TaskID);

       Database:=TnxDatabase.Create(nil);

       ProgressTable:=TnxTable.Create(nil);

       if Assigned(TaskInfo) then

       try

         Database.Session:=Session;

         Database.AliasName:='ServerRemoteCommand';

         Database.Open;

         ProgressTable.Database:=Database;

         ProgressTable.TableName:='ServerRemoteCommand_'+IntToStr(taskid);

         repeat

           ProgressTable.Open;

           while not ProgressTable.Eof do

           begin

             Write(ProgressTable.FindField('Lines').AsString);

             ProgressTable.Delete;

           end;

           ProgressTable.Close;

           TaskInfo.GetStatus(Completed, Status);

           if not Completed then begin

             Sleep(500);

           end;

         until Completed;

 

         ProgressTable.Open;

         while not ProgressTable.Eof do

         begin

           Write(ProgressTable.FindField('Lines').AsString);

           ProgressTable.Delete;

         end;

         ProgressTable.Close;

       finally

         ProgressTable.Free;

         Database.Free;

         TaskInfo.Free;

       end;

     end;

   except

     on E:Exception do

     begin

       Writeln(E.Classname, ': ', E.Message);

     end;

   end;

 finally

   rc.Free;

   Session.Free;

   ServerEngine.Free;

   Transport.Free;

 end;

end.

Even thought that's already pretty simple, there are some wrapper functions for calling scripts like:

 

function ExecuteServerScript(servername, username, password, scriptname: string; result: TStringlist; var Error: String): boolean;

 

Now say hello to a very small but in combination with server scripting very powerful tool:

 

 

Script © Nexus Database Systems Pty Ltd.