Switch to: V9V8V7V6V5

Lesson 2 - "Employees" Database on Server

In the previous lesson we have created an application for the work with local database. In the given lesson we will improve the application in such a way that it will be able to work both with local and remote database located under Valentina Server. We will use the project got in the step 5 of the lesson 1 as the basis.

Step 1: Project Preparation for the Work with Server

Initialization Modification

Copy file Lesson1_step5 to the convenient place for you. Open it.

To solve the task of initialization Modification you should create a new global variable gClient and use the function CreateOrOpenDatabase().

To do this you should do the next:

  • Open Cast Window, if it is not opened.
  • Choose the first cast member and open Script Window for it.
  • In the edit window write the next text after the string global Valentina:
global gClient

global gConn

Job the value of the new variable at the beginning of startmovie function:

gClient = true
  • Change the text in order it has such type:
global Valentina
global gClient

on startmovie
    gClient = true
    Valentina = new ( Xtra "Valentina" )

    if( gClient ) then
      Valentina.InitClient()
    else
      Valentina.Init( 10 * 1024 * 1024 )
    end if
  
    -- Now we create or open database: 
    CreateOrOpenDatabase()
end

Creation Xtra VDatabase Modification

For the work with Server we have to use another Xtra VDatabase creation form, that is why we have to modify the place of its creation.

To do it you should make the next:

  • Choose the second cast member and open Script Window for it.
  • In the code editor window at the beginning of CreateOrOpenDatabase() function write the following text:
on CreateOrOpenDatabase()
  if( gClient ) then
    gConn = new (Xtra "VConnection", "localhost", "sa", "sa")
    gConn.Open()

    gDatabase = new (Xtra "VDatabase", #kClient, gConn)
    dbLocation = "database1.vdb"
  else
    gDatabase = new (Xtra "VDatabase", #kLocal )
    dbLocation = the moviePath & "database1.vdb" 
  end if
  
  -- We try to open the database, if we fail to open then we create it
  gDatabase.Open( dbLocation )
  
  if Valentina.lastError then
    if Valentina.lastError <> 532480 then // not a ConnectionError
      gDatabase.Create( dbLocation, #kDscDatBlbInd )
      gDatabase.dateTimeFormat = [ #kYMD, "-", ":", 10 ]
       
      CreateStructure()  
    else
      put Valentina.lastErrorString
      return 1
    end if
  else
    gDatabase.dateTimeFormat = [ #kYMD, "-", ":", 10 ]
    
    gDatabase.Table("Person").FirstRecord()
    PopulatePanes()
  end  if
end 

Step 2: Adding error checking

Adding new function CheckValError()

To do it you should do the next:

  • Choose the second cast member and open Script Window for it.
  • In the code editor window after the CloseDatabase() function write the following text:
on CheckValError ()
  
  if objectP(Valentina) then
    if ( Valentina.lastError <> 0 ) then
      alert "ERROR: " & Valentina.lastErrorSymbol & "(" & Valentina.lastErrorString & ")"
      return 1
    else
      return 0
    end if
  end if
  
end CheckValError

Improving CreateOrOpenDatabase() function

To do it you should do the next:

  • In the code editor window changing function CreateOrOpenDatabase():
  if( gClient ) then
    gConn = new (Xtra "VConnection", "localhost", "sa", "sa")
    gConn.Open()
    if CheckValError() then exit

    gDatabase = new (Xtra "VDatabase", #kClient, gConn)
    dbLocation = "database1.vdb"
  else
    gDatabase = new (Xtra "VDatabase", #kLocal )
    dbLocation = the moviePath & "database1.vdb" 
  end if