EASY.SQL.2025.08.26

HMG Samples and Enhancements

Moderator: Rathinagiri

Post Reply
User avatar
Roberto Lopez
HMG Founder
Posts: 4022
Joined: Wed Jul 30, 2008 6:43 pm

EASY.SQL.2025.08.26

Post by Roberto Lopez »

Hi All,

Changes on this Version (2025.08.26):

- New: SetLang(cLangId) method. Set library information messages language.
Useful when you do not want to use default language for messages
(current hb_langselect() setting).

Accepted values for <cLangId> are the following:

'EN' -> English
'ES' -> Spanish
'PT' -> Portuguese
'FR' -> French
'DE' -> German
'IT' -> Italian
'NL' -> Dutch
'SV' -> Swedish
'DA' -> Danish
'FI' -> Finnish
'NO' -> Norwegian
'HU' -> Hungarian

- New: 'lAutoReconnect' variable. When set to .T. (the default) operations that
requires connection to the server, on error, will check if the connection is still alive,
if not, reconnection will be attempted.

One BIG problem when working with some shared AMP hostings, are unexpected
disconnections. So, you could get frequent (and very frustrating)
'MySQL server has gone away' error messages.

To address this, I've added support for automatic handling of reconnections
to the library, so (hopefully) you should not care about this annoyance :D

- New: IsConnected() method. Returns .T. if the current connection still alive,
.F. if not.

- New: Reconnect() method. Attempts to re-connect using the current parameters.


This class must be considered EXPERIMENTAL.

My idea to start this library, was to have a wrapper for 'Simple SQL Interface' contribution by Mindaugas Kavaliauskas that I've used for many years.

This wrapper aims to have the following features:

- Handle all possible runtime error situations (bad parameters, connection, SQL syntax, etc.).

- Show all required progress information and error messages to the user.

- Handle simultaneous connections in an easy/intuitive way.

This is a very very basic documentation:

METHODS:

AffectedRows(): Returns the number of rows modified by the last executed
INSERT, UPDATE, or DELETE command.

CloseAreas(): Close all workareas open with 'Select' method (if any).
Returns: Number of workareas closed.

Commit(): Executes 'COMMIT' command.
Returns: .T. if command was successfully executed (.F. otherwise).

Connect(cServer, cUser, cPassword [, cDatabase]): Connect to a MySql server.
Returns: .T. if connection was successful (.F. otherwise).

Delete( cTable, cWhere ): Delete rows based on 'cWhere' condition.
Returns: .T. if command was successfully executed (.F. otherwise).

Destroy(): Set all object variables to NIL, closes all workareas used by
'Select' method (if any) and disconnects.
Returns: NIL

Disconnect(): Disconnect from server.
Returns: NIL

Exec(cCommand): Executes a MySql command.
Returns: .T. if command was successfully executed (.F. otherwise).

Insert(cTable, aCols): Insert row. aCols, specifies cColumnname => xData pairs (hash array).
Returns: .T. if command was successfully executed (.F. otherwise).

IsConnected(): Returns .T. if the current connection still alive, .F. if not.

New(): Object Initialization.
Returns: SELF (refers to the instance of the class that is being constructed).

Reconnect(). Attempts to re-connect using the current parameters.

RollBack(): Executes 'ROLLBACK' command.
Returns: .T. if command was successfully executed (.F. otherwise).

Select(cSelect,cWorkArea): cSelect must be a MySql SELECT statement.
A recordset is created with the name specified as cWorkArea.
Returns: .T. if query was successful (.F. otherwise).

StartTransaction(): Executes 'START TRANSACTION' command.
Returns: .T. if command was successfully executed (.F. otherwise).

Update(cTable,cWhere,aCols): Modify rows based on 'cWhere' condition. aCols, specifies
cColumnname => xData pairs (hash array).
Returns: .T. if command was successfully executed (.F. otherwise).

Use(cDatabase): Selects the default database. Useful when connecting without
specifying a database or when working with multiple databases.
Returns: .T. if command was successfully executed (.F. otherwise).

VARIABLES:

cErrorDesc (Read Only): Error description (Empty if no error).

cMsgLang (Read Only): Current language for error/progress messages.

cNoQuoteChar: Set leftmost character indicator to instruct Update and Insert
methods to not quote a character value on aColumns
(cColumnName,xColumnValue) array (default value is '@').

lAutoReconnect: When set to .T. (the default value) operations that requires
connection to the server, will check if the connection still alive, if not,
reconnection will be attempted.

lError (Read Only): .T. if an error occured on last operation (.F. otherwise).

lShowMsgs: Determines if error/progress messages will be shown.

lTrace: When set to .T., a file called 'trace.log' containing last command
executed.

MariaDB:

Last years, for new projects (even local-data ones), I've used portable MariaDB.

It is compact, fast, reliable, secure and requires no installation.

You can download it at https://mariadb.org/download/ and select 'ZIP file' on
'Package Type'. You can unpack it at any place (<base_folder>).

For newer versions, prior to start MariaDB, you must initialize data directory:

<base_folder>\bin\mysql_install_db.exe --datadir=<base_folder>\data

Then you can run MariaDB:

<base_folder>\bin\mysqld.exe.

The default user is root with no password.

Best database utility: HeidiSQL (https://www.heidisql.com/)


I hope this be useful for someone.


Roberto.
Attachments
hmg.easy.sql.2025.08.26.zip
(1.71 MiB) Downloaded 34 times
Regards/Saludos,

Roberto


(Veritas Filia Temporis)
User avatar
serge_girard
Posts: 3333
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: EASY.SQL.2025.08.26

Post by serge_girard »

Thanks Roberto !

Do you think it possible (for me) to use your project? I use MySQL for many years and all my programs have this construction:

Code: Select all

IF SQL_Connect()  == Nil	 
   MSGINFO('No database connection','NOK')
   RETURN
ENDIF

 
cQuery2 := 'SELECT CODE_DESC, CODE_VALUE  '  
cQuery2 += 'FROM CODE_TABLE ' 
cQuery2 += 'WHERE CODE = "KBO_LVC" '
cSQL2   := cQuery2
cQuery2	:= dbo:Query( cQuery2 )
IF cQuery2:NetErr()
   SQL_ERRORS(PROCNAME(), '4', cQuery2:Error(), cSQL2  )
   ? 'SQL ERROR  ' , cQuery2:Error(), cSQL2
   RETURN(FALSE)
ENDIF 
 
FOR nZZ := 1 To cQuery2:LastRec()
   aCurRow2      := cQuery2:GetRow(nZZ)
   cCODE_DESC    := aCurRow2:fieldGet(1)             
   cCODE_VALUE   := aCurRow2:fieldGet(2)   
   // action
   cQuery3 := 'INSERTO INTO ........... '  
   cQuery3	:= dbo:Query( cQuery3 )
   /*
   ETC..
   */
NEXT nZZ 

SQL_DisConnect()
RETURN







FUNCTION SQL_Connect()
/**********************************************/
LOCAL cQuery2, cSQL2  

IF xINP_PARAM == 'T'  // TEST HOST
	xDB_NAME		:= 'SG_TEST'    
	xDB_HOST		:= 'axc.nl'     
	xDB_USER		:= 'TSG_TEST'    
	xDB_PW		:= 'tabracadabra'     
ELSE
	xDB_NAME		:= 'SG_PROD'   
	xDB_HOST		:= 'axc.nl'     
	xDB_USER		:= 'PSG_TEST'    
	xDB_PW		:= 'pabracadabra'     
ENDIF
 
dbo := tmysqlserver():new(STRVALUE(xDB_HOST),STRVALUE(xDB_USER),STRVALUE(xDB_PW))
IF dbo:NetErr()
   MSGINFO('Call me')
   RETURN nil
ENDIF

IF!EMPTY(xDB_NAME) 
   dbo:selectdb(xDB_NAME)
   IF dbo:NetErr()
      MSGINFO('Call me')
      RETURN nil
   ENDIF
ENDIF

cQuery2 := "SET NAMES 'utf8mb4' "
cSQL2   := cQuery2
cQuery2	:= dbo:Query( cQuery2 )
IF cQuery2:NetErr()
   SQL_ERRORS(PROCNAME(), '1', cQuery2:Error(), cSQL2  )   
   RETURN(FALSE)
ENDIF 

RETURN dbo



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

Of course I don't want to rewrite tons of lines.... Is it possible?

Serge
There's nothing you can do that can't be done...
User avatar
Roberto Lopez
HMG Founder
Posts: 4022
Joined: Wed Jul 30, 2008 6:43 pm

Re: EASY.SQL.2025.08.26

Post by Roberto Lopez »

serge_girard wrote: Tue Aug 26, 2025 6:07 am Thanks Roberto !

Do you think it possible (for me) to use your project? I use MySQL for many years and all my programs have this construction:
Hi Serge!

Easy.Sql has a very different approach, focused on minimize code writing and automation of many task, so, I'm afraid yes, your code will required a lot of changes to make it work with Easy.Sql.

If you want to minimize the required changes, consider using Exec(cCommand) method, so porting your code could be easier, but loosing many of Easy.Sql advantages.
Regards/Saludos,

Roberto


(Veritas Filia Temporis)
User avatar
serge_girard
Posts: 3333
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: EASY.SQL.2025.08.26

Post by serge_girard »

Hi Roberto,

Thanks for replying.
Maybe then in some other life...!

Serge
There's nothing you can do that can't be done...
Post Reply