File: n_tr.sru
Size: 8771
Date: Mon, 07 Apr 2008 21:30:57 +0200
$PBExportHeader$n_tr.sru
$PBExportComments$Base Transaction Object
forward
global type n_tr from transaction
end type
end forward

global type n_tr from transaction
end type
global n_tr n_tr

type variables
Protected:

CONSTANT string SQL_COMMIT = "COMMIT"
CONSTANT string SQL_ROLLBACK = "ROLLBACK"
CONSTANT string SQL_BEGIN = "BEGIN TRAN"
Boolean #PromptErrorMessage
end variables

forward prototypes
public function integer of_begintran ()
public function integer of_execute (string as_sqlstring)
public function integer of_commit ()
public function integer of_rollback ()
public function integer of_connect ()
public function integer of_disconnect ()
public function boolean of_isconnected ()
public subroutine of_showerrormessage ()
public function string of_tostring ()
public function string of_geterrormessage ()
public subroutine of_promptonerror (readonly boolean ab_set)
public subroutine of_clone (ref n_tr atr)
public function boolean of_createdsfromsql (ref datastore ads_ret, readonly string as_sql, ref string as_errorstring) throws n_x
public function boolean of_createdsfromsql (ref datastore ads_ret, readonly string as_sql) throws n_x
public function integer of_readinifile (readonly string as_filename, readonly string as_section, readonly string as_cryptkey)
public function integer of_readinifile (readonly string as_filename, readonly string as_section)
public subroutine of_autopopulatedbparm ()
end prototypes

public function integer of_begintran ();/*
  <Name>        of_Begintran
  <Object>      n_tr
  <Access>      public 
  <Returns>     Integer
  <Throws>      

  <Arguments>   

  <Description> Issues a BEGIN TRAN directly to the database.

  <Author>      (Yeyi) Gabriel Abulencia
  <Company>     BAX Global

  <Updated>     Tuesday, 3. Jan. 2006
  <Version>     1.0
*/
RETURN of_Execute(SQL_BEGIN)
end function

public function integer of_execute (string as_sqlstring);as_sqlstring = TRIM(as_sqlstring)

IF as_sqlstring = "" THEN RETURN CRet.FAILURE

TRY
   EXECUTE IMMEDIATE :as_sqlstring USING this;
CATCH (Exception ex)
   RETURN CRet.FAILURE
END TRY

IF SQLCode = CRet.FAILURE THEN of_ShowErrorMessage()

RETURN SQLCode
end function

public function integer of_commit ();/*
  <Name>        of_Commit
  <Object>      n_tr
  <Access>      public 
  <Returns>     Integer
  <Throws>      

  <Arguments>   

  <Description> Issues a COMMIT directly to the database.

  <Author>      (Yeyi) Gabriel Abulencia
  <Company>     BAX Global

  <Updated>     Tuesday, 3. Jan. 2006
  <Version>     1.0
*/
RETURN of_Execute(SQL_COMMIT)
end function

public function integer of_rollback ();RETURN of_Execute(SQL_ROLLBACK)
end function

public function integer of_connect ();/*
  <Name>        of_Connect
  <Object>      n_tr
  <Access>      public 
  <Returns>     Integer
  <Throws>      

  <Arguments>   

  <Description> Connects n_tr to the database and reports errors.

  <Author>      (Yeyi) Gabriel Abulencia
  <Company>     BAX Global

  <Updated>     Tuesday, 3. Jan. 2006
  <Version>     1.0
*/
CONNECT USING this;

IF SQLCode = CRet.FAILURE THEN of_ShowErrorMessage()

RETURN SQLCode;
end function

public function integer of_disconnect ();DISCONNECT USING this;

RETURN SQLCode;
end function

public function boolean of_isconnected ();RETURN (DBHandle() > 0)
end function

public subroutine of_showerrormessage ();IF #PromptErrorMessage THEN
   MessageBox(Servername + "\" + Database, &
         of_GetErrorMessage(), &
         StopSign!)
END IF
end subroutine

public function string of_tostring ();RETURN Servername + "\" + Database
end function

public function string of_geterrormessage ();RETURN "Database Code: " + STRING(SQLDBCode) + CString.CR_NEWLINE + &
      "SQL Code: " + STRING(SQLCode) + CString.CR_NEWLINE + CString.CR_NEWLINE + &
      "Message:" + CString.CR_NEWLINE + SQLErrText
end function

public subroutine of_promptonerror (readonly boolean ab_set);#PromptErrorMessage = ab_set
end subroutine

public subroutine of_clone (ref n_tr atr);/*
  <Name>        of_Clone
  <Object>      n_tr
  <Access>      public 
  <Returns>     (none)
  <Throws>      

  <Arguments>   reference n_tr atr

  <Description> Clones the current n_tr instance.

  <Author>      (Yeyi) Gabriel Abulencia
  <Company>     BAX Global

  <Updated>     Tuesday, 3. Jan. 2006
  <Version>     1.0
*/
IF NOT ISVALID(atr) THEN atr = CREATE n_tr

atr.ServerName = this.ServerName
atr.LogID = this.LogID
atr.LogPass = this.LogPass
atr.DBMS = this.DBMS
atr.Database = this.Database
atr.UserID = this.UserID
atr.DBPass = this.DBPass
atr.Lock = this.Lock
atr.DBParm = this.DBParm
atr.AutoCommit = this.AutoCommit
end subroutine

public function boolean of_createdsfromsql (ref datastore ads_ret, readonly string as_sql, ref string as_errorstring) throws n_x;//====================================================================
// [PUBLIC] Function of_createdsfromsql in n_tr inherited from transaction
//--------------------------------------------------------------------
/* 
   KEYWORDS

   Description=Creates a datstore from a SQL statement;

*/
//--------------------------------------------------------------------
// Arguments:  
//
// [reference] datastore ads_ret
//    Description=Pointer to a datastore;
// [readonly] string as_sql
//    Description=SQL syntax;
//--------------------------------------------------------------------
// Returns: (NONE) 
//--------------------------------------------------------------------
// Author:  Yeyi     Date: 8/27/2003
//====================================================================

string ls_presentation, ls_error, ls_source
n_svc_isempty lnv_check
n_svc_mgr lnv_svcman

//load services
lnv_svcman.of_loadsvc(lnv_check, CSvc.ISEMPTY)

//check if the datastore is valid
IF NOT ISVALID(ads_ret) THEN
   as_errorstring = "Datastore variabe is null"
   RETURN FALSE
END IF

ls_source = this.SyntaxFromSQL(as_sql, ls_presentation, ls_error)

//check for errors
IF lnv_check.of_isempty(ls_source) THEN
   as_errorstring = ls_error
   RETURN FALSE
ELSE
   ls_error = ""
   IF ads_ret.Create(ls_source, ls_error) < 0 THEN
      IF lnv_check.of_isempty(ls_error) THEN 
         as_errorstring = "Unknown Error"
         RETURN FALSE
      END IF
   END IF
END IF

RETURN TRUE
end function

public function boolean of_createdsfromsql (ref datastore ads_ret, readonly string as_sql) throws n_x;string ls

RETURN of_CreateDSFromSQL(ads_ret, as_sql, ls)
end function

public function integer of_readinifile (readonly string as_filename, readonly string as_section, readonly string as_cryptkey);integer li_ret
string ls_val

//check ini file
IF NOT FileExists(as_filename) THEN RETURN CRet.FAILURE

ServerName = ProfileString(as_filename, as_section, "ServerName", CString.EMPTY)
DBMS = ProfileString(as_filename, as_section, "DBMS", CString.EMPTY)
Database = ProfileString(as_filename, as_section, "Database", CString.EMPTY)
DBParm = ProfileString(as_filename, as_section, "DBParm", CString.EMPTY)
LogID = ProfileString(as_filename, as_section, "LogID", CString.EMPTY)
LogPass = ProfileString(as_filename, as_section, "LogPassword", CString.EMPTY)
ls_val= ProfileString(as_filename, as_section, "Autocommit", CString.EMPTY)
AutoCommit = (lower(TRIM(ls_val)) = "true" ) OR TRIM(ls_val) = "1"

//decrypt?
IF as_cryptkey <> CString.EMPTY AND LogPass <> CString.EMPTY THEN
   n_svc_mgr lnv_svc
   n_svc_crypto lnv_c
   
   lnv_svc.of_LoadSvc(lnv_c, CSvc.CRYPTO)
   ls_val = LogPass
   LogPass = lnv_c.of_decryptdata(ls_val, as_cryptkey)
END IF

RETURN li_ret
end function

public function integer of_readinifile (readonly string as_filename, readonly string as_section);RETURN of_readinifile(as_filename, as_section, CString.EMPTY)
end function

public subroutine of_autopopulatedbparm ();//PROVIDER='SQLOLEDB',DATASOURCE='localhost\netsdk',PROVIDERSTRING='Database=BAXTS_NHG_HIMS'
CHOOSE CASE UPPER(DBMS)
   CASE "ADO.NET"
      DBParm = "Namespace='System.Data.SqlClient',DataSource=' " +ServerName + &
      "',TrimSpaces=1,DelimitIdentifier='Yes',Database='" + Database + "'"
   CASE "OLE DB", "OLEDB"
      DBParm = "PROVIDER='SQLOLEDB',DATASOURCE='" + ServerName + &
            "',PROVIDERSTRING='Database=" + Database + "'"
   CASE "MSS"
      //convert to OLE DB
      DBMS = "OLE DB"
      DBParm = "PROVIDER='SQLOLEDB',DATASOURCE='" + ServerName + &
            "',PROVIDERSTRING='Database=" + Database + "'"     
   CASE ELSE
      DBParm = ""
END CHOOSE
end subroutine

on n_tr.create
call super::create
TriggerEvent( this, "constructor" )
end on

on n_tr.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on