Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

EDT:Tutorial: RUI With DataBase Lesson 6 Code

Access a database with EGL Rich UI > Resources

Finished code for SQLService.egl after lesson 6

The following code is the text of the SQLService.egl file after Lesson 6.

  package services;
  
  import records.paymentRec;
  
  service SQLService
  
  ds SQLDataSource?{@Resource{bindingKey = "Derby"}};
  
  function addPayment(newPayment paymentRec in)
       logEntry ( "addPayment" ) ;
       try
           add newPayment to ds
               with #sql{
                       insert into PAYMENT
                             (CATEGORY, DESCRIPTION, AMOUNT, FIXED_PAYMENT, DUE_DATE, 
                             PAYEE_NAME, PAYEE_ADDRESS1, PAYEE_ADDRESS2)
                       values
                             (?, ?, ?, ?, ?, ?, ?, ?)
                 };
       onException(ex sqlException)
           logException(ex);
       end
  end
  
  function getAllPayments() returns(paymentRec[])
      payments paymentRec[];
      logEntry("getAllPayments");
      try
          get payments from ds;
      onException(ex sqlException)
          logException(ex);
      end
      return(payments);
  end
  
  function editPayment(chgPayment paymentRec in)
      logEntry("editPayment");
      try
          replace chgPayment to ds;
      onException(ex SQLException)
          logException(ex);
      end
  end
  
  function deletePayment(delPayment paymentRec in)
      logEntry("deletePayment");
      try
          delete delPayment from ds;
      onException(ex SQLException)
          if(ex.SQLState != "02000") // sql state is five digits
              logException sqlException;
          end
      end
  end
  
  function createDefaultTable() returns(paymentRec[])
      payments paymentRec[];
      logEntry("createDefaultTable");
      try
          try
              execute from ds
                 with #sql{
                    delete from PAYMENT
             };
        onException(ex SQLException)
            if(ex.SQLState != "02000")  // sqlState is five digits
                throw ex;
            end
        end 
        ispDate date = dateTimeLib.dateFromGregorian(20140405);
        addPayment(new paymentRec{category = 1, description = "Apartment"
              , amount = 880, fixedPayment = yes, dueDate = new date
              , payeeName = "A Jones", payeeAddress1 = "100 Jones Dr"
              , payeeAddress2 = "Jonesboro, NC"
              });
        addPayment(new paymentRec{category = 2, description = "Groceries"
              , amount = 450, fixedPayment = no, dueDate = new date
              , payeeName = "B Jones", payeeAddress1 = "200 Jones Dr"
              , payeeAddress2 = "Jonesboro, NC"
          });
        addPayment(new paymentRec{category = 5, description = "ISP"
              , amount = 19.99, fixedPayment = no, dueDate = ispDate
              , payeeName = "C Jones", payeeAddress1 = "300 Jones Dr"
              , payeeAddress2 = "Jonesboro, NC"
        });
        payments = getAllPayments();
          onException(ex anyException)
              logException(ex);
          end
          return(payments);
    end
    
    logActive boolean = true;
    activeService string;
    
    private function logEntry(serviceFunction string in)
        activeService = serviceFunction;
        log("Entry:  SQLService, " + serviceFunction);
    end
    
    private function logException(ex sqlException?)
        accumulatedMessage string = "Exception:  SQLService, " + activeService;
        while(ex != null)
            accumulatedMessage = accumulatedMessage + ", SQLSTATE = " +
                    ex.SQLState + ", message = " + ex.message;
            ex = ex.nextException;
        end
        log(accumulatedMessage);
        throw new anyException{message = accumulatedMessage};
    end
    
    private function log(text string in)
        if(logActive)
            sysLib.writeStdOut(text);
        end
    end
 
 end


Related tasks

Lesson 6: Add code for the service functions

Back to the top