<%

<!--#INCLUDE FILE="functions.nxscript"-->

var
  aSession: TnxSession;
  adb: TnxDatabase;
  aQuery: TnxQuery;
  aTable: TnxTable;
  IndexFieldName: string;
  orderfieldname: string;
  TableName: string;
  DatabaseName: string;
  i: integer;
  astart: integer;
  all: string;
begin
%>

<!--#INCLUDE FILE="header.html"-->

<h3>NexusDB Simple WebScript Example</h3>


<%
  Output.WriteLn(NXServer.GetUsers);
  DatabaseName:=Request('DatabaseName');
  // if there's no database name set default
  if DatabaseName='' then
    DatabaseName:='Test';

  TableName:=Request('TableName');
  // if there's no table name set default
  if TableName='' then
    TableName:='CD';

  IndexFieldName:=Request('IndexFieldName');
  // if there's no index field name set default
  if IndexFieldName='' then
    IndexFieldName:='DiskSerialNumber';

  OrderFieldName:=Request('OrderFieldName');
  // if there's no index field name set default
  if OrderFieldName='' then
    OrderFieldName:=IndexFieldName;

  // defined in functions.nxscript
  CreateNxAccessComponents(aSession, aDB, aQuery, aTable);
  // it's important to wrap that into a try/finally to make sure the 
  // created instances are freed again!
  try
    // set the db name
    adb.AliasName:=DatabaseName;

    // check where to start or if we should show all
    // and construct the query accordingly
    astart:=strtointdef(Request('Start'),0);
    all:=Request('All');
    if all='' then
    begin
      if astart>0 then
        aQuery.SQL.Text:='select top 10,'+inttostr(astart)+' * from '+TableName+' order by '+orderfieldname
      else
        aQuery.SQL.Text:='select top 10 * from '+TableName+' order by '+OrderFieldName;
    end
    else
      aQuery.SQL.Text:='select * from '+TableName+' order by '+OrderFieldName;
    // open the record set
    aQuery.Open; 
%>


<table width="80%">
<tr class=header>


<%      
    // write the table headers
    for i:=0 to aQuery.FieldCount-1 do
      Output.WriteLn('<td>'+aQuery.Fields.Fields[i].FieldName+'</td>');
    Output.WriteLn('<td>Edit</td>');
    Output.WriteLn('<td>Delete</td>');
%>


</tr>
<%
    // iterate the records
    while not aQuery.eof do
    begin
      // and write one row wiht the values
      Output.WriteLn('  <tr>');
      for i:=0 to aQuery.FieldCount-1 do
        Output.WriteLn('    <td>'+aQuery.Fields.Fields[i].AsString+'</td>');

      // add the links for editing and deleting the records
      Output.WriteLn('    <td><a href="editrecord.nxscript?databasename='+adb.AliasName+'&tablename='+TableName+'&IndexFieldName='+IndexFieldName+'&id='+aQuery.FindField(IndexFieldName).AsString+'">Edit</a></td>');
      Output.WriteLn('    <td><a href="deleterecord.nxscript?databasename='+adb.AliasName+'&tablename='+TableName+'&IndexFieldName='+IndexFieldName+'&id='+aQuery.FindField(IndexFieldName).AsString+'">Delete</a></td>');
      Output.WriteLn('  </tr>');
      aQuery.Next;
    end;
    Output.WriteLn('</table>');

    // write the page navigation
    if (aStart>0) or (all<>'') then
      Output.WriteLn('<br><a href="index.nxscript?start=0">First 10</a>');
    if all='' then
    begin
      if aStart>0 then
        Output.WriteLn('<br><a href="index.nxscript?start='+inttostr(max(0, aStart-10))+'">Previous 10</a>');
      if aQuery.RecordCount=10 then
        Output.WriteLn('<br><a href="index.nxscript?start='+inttostr(aStart+10)+'">Next 10</a>');
      Output.WriteLn('<br><a href="index.nxscript?all=1">All</a>');
    end;
    Output.Writeln('<br><a href="addrecord.nxscript?databasename='+adb.AliasName+'&tablename='+TableName+'&IndexFieldName='+IndexFieldName+'">Add Record</a><br>');
  finally
    aQuery.Free;
    aTable.Free;
    adb.free;
    aSession.Free;
  end;
%>

<!--#INCLUDE FILE="footer.html"-->

<%
end.
%>