Page 3 of 5

Re: HB_WebView (a cross-platform Webview library)

Posted: Sat Mar 22, 2025 5:40 pm
by srvet_claudio
serge_girard wrote: Sat Mar 22, 2025 10:41 am Claudio,

Here is a small example (not tested or compiled !) but it gives an idea!

Many thanks, Serge


Code: Select all

#include "hmg.ch"
 


FUNCTION MAIN()
/***************/
PUBLIC dbo

PUBLIC dbname          
PUBLIC host            
PUBLIC user            
PUBLIC password  

PUBLIC cVersion
PUBLIC aRESULT := {} 
 
IF SQL_Connect()
   cQuery1a  := " SELECT SOME_FIELD "
   cQuery1a  += " FROM   SOME_TABLE "
   cQuery1a  += " WHERE  SOME_REC_NO  = 1  "
   cSQL1    := cQuery1a
   ? cSQL1
   cQuery1a	:= dbo:Query( cQuery1a )
   IF cQuery1a:NetErr()												
      MSGINFO( 'SQL DEMO ' + UPPER(cQuery1a:Error()))
   ENDIF 


   FOR i := 1 to cQuery1a:Lastrec()
      aCurRowa			   := cQuery1a:GetRow(i)
      cNAAM             := Strvalue(aCurRow:fieldGet(1))  
      AADD(aRESULT, {cNAAM } )
   NEXT
   
   SQL_Disconnect()
   ?
   ?
ELSE
   ? 'NO CONNECTION'
ENDIF

 

DEFINE WINDOW Form_1 ;
	AT 0,0 ;
	WIDTH  1200  ;
	HEIGHT 500 ;
	TITLE '';
	MAIN  ;
   FONT 'Arial' SIZE 15   ;

   @ 10,10 GRID Grid_ZF		;  
      WIDTH  1150			;
      HEIGHT 450		;
      HEADERS {'SOME_FIELD'};  
      WIDTHS  {250    };
      JUSTIFY { GRID_JTFY_LEFT  } ;
      FONT "Arial" SIZE 09		;
      iTEMS aRESULT
 
END WINDOW
  
CENTER WINDOW Form_1

ACTIVATE WINDOW Form_1


FUNCTION SQL_Connect()
/***********************/
dbname      := 'dbname'  
host			:= 'host.db.host.xx'    
user			:= 'dbname_user'   
password		:= 'secret_password'    

dbo := tmysqlserver():new(AllTrim(host), AllTrim(user), AllTrim(password))

IF dbo:NetErr()
   ?'R1',  dbo:ERROR() 
   RETURN nil
ENDIF

IF!EMPTY(dbname) 
   dbo:selectdb(dbname)
   IF dbo:NetErr()
     ?'R2',  dbo:ERROR() 
      RETURN nil
   ENDIF
ENDIF

cVersion := dbo:sql_Version()
? 'cVersion', cVersion
RETURN dbo


FUNCTION SQL_Disconnect()
/************************/
dbo:Destroy()
RETURN
 
Hi Serge,
I have slightly modified your code, adding the SQL_GetData() function, with two versions, one for a real SQL connection and another for a simulated connection for testing (see comments in the code).

Code: Select all

/*
 * HB_WebView Demo
 *
 * Copyright 2025 by Dr. Claudio Soto (from Uruguay). 
 * mail: <srvet@adinet.com.uy>
 * blog: http://srvet.blogspot.com
 */

#include "../hb_webview-lib/hb_webview.ch"
// #include "hb_webview-lib/hb_webview.prg"


REQUEST hb_alert

MEMVAR dbo, dbname, host, user, password, cVersion


FUNCTION Main
LOCAL w, html, allowDevTools
LOCAL context

PUBLIC dbo, dbname, host, user, password, cVersion

// aRESULT = SQL_GetData()   // returns an array with subarray like: { { data1 }, { data2 }, ... , { dataN } }


html = ''
html += '<div>'
html += '  <p>Press CTRL+SHIFT+I to open the DevTools window</p>'
html += '  <button onclick="disp()"> SQL_GetData() </button>'
html += '  <pre id="idResult"></pre>'
html += '</div>'
html += '<script>'
html += '   async function disp(){'
html += '      let arrayData = await SQL_GetData();'
html += '      console.log( arrayData );'
html += '      let txt = "";'
html += '      for(let i=0; i < arrayData.length; i++)'
html += '         txt += "Row "+ i + ": " + arrayData[ i ].toString() + "\n";'
html += '      document.getElementById("idResult").textContent = "SQL Response: \n" + txt;'
html += '   }'
html += '</script>'

allowDevTools = .T.

   w = hb_webview_create( allowDevTools, NIL )

      hb_webview_set_size(w, 480, 320, WEBVIEW_HINT_NONE)

      hb_webview_set_title(w, "Harbour WebView - SQL Demo")

/*
   Remember that JavaScript is case sensitive: 
   If you bind the HB_ALERT() function as "hb_Alert" in the Harbour environment, 
   you must call it as hb_Alert() in JavaScript environment.

   On the other hand, Harbour binding functions are passed as promises to JavaScript. 
   Therefore, they are executed in the JavaScript environment as asynchronous functions.

   For more details on promises in JavaScript, see:
   https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

   For promises to execute synchronously, you must chain the calls in chained .then() statements 
   or call them with the await statement inside an asynchronous function.
   For more details see:
   https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
   https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

*/
      context = hb_webview_bind2(w, "SQL_GetData", HB_CALL_FUNC_DIRECT)

      hb_webview_set_html(w, html)

   hb_webview_run(w)

   hb_webview_destroy_context( context )

   hb_webview_terminate( w )

RETURN NIL


//============================================================================================
// Warning, this is a simulate response, please comment out this line in a real SQL connection.
#define SIMULATE_SQL
//============================================================================================


#ifdef SIMULATE_SQL

FUNCTION SQL_GetData()
LOCAL aRESULT := {}
   aRESULT := Simulate_SQL_GetData_Response()
RETURN aRESULT   // returns an array with subarray like: { { data1 }, { data2 }, ... , { dataN } }


FUNCTION Simulate_SQL_GetData_Response()
LOCAL aRESULT := {}
   AADD( aRESULT, { "data1 A", "data1 B" } )
   AADD( aRESULT, { "data2" } )
   AADD( aRESULT, { "data3" } )
RETURN aRESULT


#else

FUNCTION SQL_Connect()
/***********************/
   dbname      := 'dbname'
   host        := 'host.db.host.xx'
   user        := 'dbname_user'
   password    := 'secret_password'

   dbo := tmysqlserver():new(AllTrim(host), AllTrim(user), AllTrim(password))

   IF dbo:NetErr()
      ?'R1',  dbo:ERROR() 
      RETURN nil
   ENDIF

   IF!EMPTY(dbname) 
      dbo:selectdb(dbname)
      IF dbo:NetErr()
         ?'R2',  dbo:ERROR() 
         RETURN nil
      ENDIF
   ENDIF

   cVersion := dbo:sql_Version()
   ? 'cVersion', cVersion
RETURN dbo


FUNCTION SQL_Disconnect()
/************************/
   dbo:Destroy()
RETURN NIL


FUNCTION SQL_GetData()
LOCAL cQuery1a, i, aCurRow, cNAAM, cSQL1
LOCAL aRESULT := {}

   IF SQL_Connect()
      cQuery1a  := " SELECT SOME_FIELD "
      cQuery1a  += " FROM   SOME_TABLE "
      cQuery1a  += " WHERE  SOME_REC_NO  = 1  "
      cSQL1    := cQuery1a
      ? cSQL1
      cQuery1a := dbo:Query( cQuery1a )
      IF cQuery1a:NetErr()
         MSGINFO( 'SQL DEMO ' + UPPER(cQuery1a:Error()))
      ENDIF 
      FOR i := 1 to cQuery1a:Lastrec()
         aCurRow  := cQuery1a:GetRow(i)
         cNAAM    := Strvalue(aCurRow:fieldGet(1))
         AADD( aRESULT, { cNAAM } )
      NEXT
      SQL_Disconnect()
      ?
      ?
   ELSE
      ? 'NO CONNECTION'
   ENDIF

RETURN aRESULT   // returns an array with subarray like: { { data1 }, { data2 }, ... , { dataN } }

#endif

Compile with:

Code: Select all

call ..\build_hb_cpp.bat   demo_SQL.prg   -w2 -es2 -lshlwapi demo.hbc

Re: HB_WebView (a cross-platform Webview library)

Posted: Sun Mar 23, 2025 7:27 am
by serge_girard
Great ! Many thanks, Serge

Re: HB_WebView (a cross-platform Webview library)

Posted: Sun Mar 23, 2025 8:32 am
by serge_girard
Claudio,

How to capture runtime errors? I get "Error BASE/1066 Argument error: conditional". But no line-number. I try DEFERROR function...

Serge

Re: HB_WebView (a cross-platform Webview library)

Posted: Sun Mar 23, 2025 11:41 am
by srvet_claudio
Hi Serge,
To test your code, place the following line at the beginning of the Main() function:

msgdebug( SQL_GetData() )

This causes first all MySQL functions to run on Harbour's "side", before creating the Webview. This may make it easier to capture the error.

Re: HB_WebView (a cross-platform Webview library)

Posted: Sun Mar 23, 2025 12:06 pm
by serge_girard
I get: hbmk2: Error: Referenced, missing, but unknown function(s): MSGDEBUG()

Re: HB_WebView (a cross-platform Webview library)

Posted: Sun Mar 23, 2025 12:16 pm
by srvet_claudio
serge_girard wrote: Sun Mar 23, 2025 12:06 pm I get: hbmk2: Error: Referenced, missing, but unknown function(s): MSGDEBUG()
I'm sorry msgdebug() is a HMG function, this version is in pure Harbour.

Code: Select all

*-----------------------------------------------------------------------------*
FUNCTION MsgDebug
*-----------------------------------------------------------------------------*
LOCAL i, cMsg, cTitle
   #define CRLF CHR(13)+CHR(10)
   cMsg := "Called from: " + PROCNAME(1) + "(" + LTRIM(STR(PROCLINE(1))) + ") --> " + PROCFILE (1) + CRLF + CRLF
   FOR i = 1 TO PCOUNT()
       cMsg := cMsg + HB_VALTOEXP (PVALUE (i)) + IIF (i < PCOUNT(), ", ", "")
   NEXT
   hb_alert (cMsg)
 RETURN cMsg

Re: HB_WebView (a cross-platform Webview library)

Posted: Sun Mar 23, 2025 11:32 pm
by srvet_claudio
Hi all,
I have added a new demo (demo5.prg), a SQLite query where the results are displayed in an HTML table.

Download code from: https://www.hmgforum.com/viewtopic.php?p=71617#p71617

Re: HB_WebView (a cross-platform Webview library)

Posted: Mon Mar 24, 2025 3:10 pm
by serge_girard
Claudio,

The MySQL demo is having a problem in the Javascript...

Code: Select all

<script>   
async function disp(){      
alert("xx1");      
let arrayData = await SQL_GetData();      
alert("xx2");      
console.log( arrayData );      
alert("xx3");      
let txt = "";      
alert("xx4");      
alert(arrayData.length);      
alert("xx5");      
for(let i=0; i < arrayData.length; i++)      
txt += "Row "+ i + ": " + arrayData[ i ].toString() + "\n";   
document.getElementById("idResult").textContent = "SQL Response: \n" + txt;   }
</script>
- alert xx1 is reached
- data is retrieved well from database
- alert xx2 is never reached and it is stopped without a message.
Something went wrong somewhere....

Re: HB_WebView (a cross-platform Webview library)

Posted: Mon Mar 24, 2025 8:32 pm
by srvet_claudio
Hi Serge,
msgdebug(SQL GetDate()) when run at the start of the Main() function displays the correct result? Does the harbor application crash?

Re: HB_WebView (a cross-platform Webview library)

Posted: Tue Mar 25, 2025 8:05 am
by serge_girard
Claudio,

1 - msgdebug(SQL GetDate()) -> works perfect with all MySQL results
2 - app does not crash.

Data is not displayed and

alert(arrayData.length) gives 'undefined'.
I tried : let arrayData = SQL_GetData() without success neither.

My Javascript knowledge is very poor...
I will prepare a kind of demo with real database and send it you at your emailaddress srvet@adinet.com.uy ?

Serge