Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SQL Select expression for both VFP and SQL DB
Message
From
29/03/2019 09:15:13
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01667660
Message ID:
01667686
Views:
56
Likes (2)
>Hi,
>
>I have an expression that works SQL Server DB:
>
>select  CAST(0 AS BIT) AS MYFIELD
>
>
>Of course the above will not work for VFP. Is there a way to change the expression above that it would create a calculated logical field in VFP as well as SQL DB?
>
>TIA

For using MS SQL Server and VFP simultaneously for an application I have created a series of wrappers. One of them is GetSQLText, which is called before any command is sent to processing. Its purpose is to preprocess the SQL passed and return an SQL command that is compatible with the backend. In code I separate VFP and SQL specific portions with < VFP > and < SQL > tags:
* GetSQLText.prg
Lparameters tcSQL,tcMode,tlDebug

Local lcLeft,lcRight,lnOccurence
lcLeft  = Iif(Upper(m.tcMode) = 'SQL', '<VFP>','<SQL>')
lcRight = Iif(Upper(m.tcMode) = 'SQL', '</VFP>','</SQL>')

For lnOccurence = 1 To Occurs(m.lcLeft,Upper(m.tcSQL))
	If Atc(m.lcLeft,m.tcSQL) > 0
		tcSQL = Strtran(m.tcSQL,Strextract(m.tcSQL,m.lcLeft,m.lcRight,1,1+4))
	Endif
Endfor
For lnOccurence = 1 To Occurs(m.lcLeft,Upper(m.tcSQL)) && check against erronous closing tags
	If Atc(m.lcLeft,m.tcSQL) > 0
		tcSQL = Strtran(m.tcSQL,Strextract(m.tcSQL,m.lcLeft,m.lcLeft,1,1+4))
	Endif
Endfor


** Remove tcMode tags
tcSQL = Strtran(Strtran(m.tcSQL,'<'+m.tcMode+'>','',1,-1,1),'</'+m.tcMode+'>','',1,-1,1)
** All CRLF to CR
tcSQL = Chrtran(Strtran(m.tcSQL,Chr(13)+Chr(10),Chr(13)),Chr(10),Chr(13))
** Eliminate blanklines
Do While Occurs(Chr(13)+Chr(13),m.tcSQL) > 0
	tcSQL = Strtran(m.tcSQL,Chr(13)+Chr(13),Chr(13))
Enddo
** All CR to CRLF + if mode is VFP: if tlDebug to ' ;'+CRLF else ' '
tcSQL = Strtran(m.tcSQL,Chr(13),Chr(13)+Chr(10))
If m.tlDebug
	_Cliptext = Iif(Upper(m.tcMode) = 'VFP', ;
		Strtran(m.tcSQL, Chr(13)+Chr(10),' ;'+Chr(13)+Chr(10)), ;
		m.tcSQL)
Endif
If Upper(m.tcMode) = 'VFP'
	tcSQL = Strtran(m.tcSQL, Chr(13)+Chr(10), ' ')
Endif
Return tcSQL
Sample call with your problem:
Local lcSQL
Text to m.lcSQL textmerge noshow
Select 
  <VFP>Cast(0 as Logical)</VFP>
  <SQL>Cast(0 as bit)</SQL> 
     as myField
from myTable
EndText

MessageBox( GetSQLText(m.lcSQL,'VFP'), 0, 'VFP', 10000)
GetSQLText(m.lcSQL,'VFP', .T.) && copied to clipboard
MessageBox( _cliptext, 0, 'VFP-Debug mode', 10000)
MessageBox( GetSQLText(m.lcSQL,'SQL'), 0, 'SQL', 10000)
PS: I send all commands to VFP using VFPOLEDB.
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform