Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Interacting VFP Cursor with FireBird Tables using SQL Pa
Message
 
To
26/10/2007 09:01:52
Dorin Vasilescu
ALL Trans Romania
Arad, Romania
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 9 SP1
Database:
Firebird
Miscellaneous
Thread ID:
01263442
Message ID:
01264684
Views:
12
Hi Mathias and Dorin

Here is my version of such a class to create a back-end cursor (table actually), populate with the VFP cursors data and delete it. Few things however.

1) I have not found a way to create a temp table in FireBird, thus this is a global table and may clash with other user having the same (temp) table name

2) I was not able to use SYS(3) and SYS(2015) to provide a random name to the (temp) table in FireBird nor was I able to find a UDF in FireBird which could provide me with such a name

3) I have to create a Temp view in the currently open database. I don't know of a way to create a Free View.

4) In my testing the 2nd loCursor.DropCursor("curSelected") does not work as the resource is still in use. I tried to look for a USE equiv. in FireBird but was not successful so I guess I may have to put in a Sleep() cal

Hope you like it and find it useful. Please let me know your views.

Usage:
	loCursor = NEWOBJECT("cRemoteCursor")

	loCursor.CreateCursor("curSelected")
	loCursor.SendData("curSelected")

	TEXT TO lcSQL TEXTMERGE NOSHOW
		SELECT *
			FROM mAccounts
			WHERE iID IN (SELECT iID FROM curSelected)
	ENDTEXT

	SQLPT(lcSQL, "mAccountsLK")

	loCursor.DropCursor("curSelected")

	loCursor = NULL
Class (SQLPT() is a wrapper for SQLEXEC() the last .T. parameter will not raise an error if found):
*** 27/10/2007 class which duplicates the given VFP cursor onto the
***            backend
DEFINE CLASS cRemoteCursor AS LINE
	FUNCTION CreateCursor(tcCurName)
		LOCAL lnFields, lcSQL, lnI
		LOCAL ARRAY laFields[1]


		*** get the vfp cursor's field details
		lnFields = AFIELDS(laFields, tcCurName)


		*** drop the table if already existing
		THIS.DropCursor(tcCurName)


		*** create the table
		lcSQL    = ""
		lcSQL = lcSQL + [CREATE TABLE ] + tcCurName + [ (] + CHR(13) + CHR(10)

		FOR lnI = 1 TO lnFields
			lcSQL = lcSQL + [	] + ALLTRIM(laFields[lnI, 1])

			DO CASE
				CASE laFields[lnI, 2] = 'C'						&& Character
					lcSQL = lcSQL + [ CHAR(] + ALLTRIM(STR(laFields[lnI, 3])) + [)] + IIF(laFields[lnI, 5], [], [ NOT NULL])

				CASE INLIST(laFields[lnI, 2], 'Y', 'B')			&& Currency, Double
					lcSQL = lcSQL + [ DOUBLE PRECISION] + IIF(laFields[lnI, 5], [], [ NOT NULL])

				CASE laFields[lnI, 2] = 'D'						&& Date
					lcSQL = lcSQL + [ DATE] + IIF(laFields[lnI, 5], [], [ NOT NULL])

				CASE laFields[lnI, 2] = 'T'						&& DateTime
					lcSQL = lcSQL + [ TIMESTAMP] + IIF(laFields[lnI, 5], [], [ NOT NULL])

				CASE laFields[lnI, 2] = 'F'						&& Float
					lcSQL = lcSQL + [ FLOAT] + IIF(laFields[lnI, 5], [], [ NOT NULL])

				CASE INLIST(laFields[lnI, 2], 'G', 'Q', 'W')	&& General, Varbinary, Blob
					lcSQL = lcSQL + [ BLOB] + IIF(laFields[lnI, 5], [], [ NOT NULL])

				CASE laFields[lnI, 2] = 'I'						&& Integer
					lcSQL = lcSQL + [ INTEGER] + IIF(laFields[lnI, 5], [], [ NOT NULL])

				CASE laFields[lnI, 2] = 'L'						&& Logical
					lcSQL = lcSQL + [ SMALLINT] + IIF(laFields[lnI, 5], [], [ NOT NULL])

				CASE laFields[lnI, 2] = 'M'						&& Memo
					lcSQL = lcSQL + [ BLOB SUB_TYPE TEXT] + IIF(laFields[lnI, 5], [], [ NOT NULL])

				CASE laFields[lnI, 2] = 'N'						&& Numeric
					lcSQL = lcSQL + [ NUMERIC(] + ALLTRIM(STR(laFields[lnI, 3])) + [, ] + ALLTRIM(STR(laFields[lnI, 4])) + [)] + IIF(laFields[lnI, 5], [], [ NOT NULL])

				CASE laFields[lnI, 2] = 'V'						&& Varchar and Varchar (Binary)
					lcSQL = lcSQL + [ VARCHAR(] + ALLTRIM(STR(laFields[lnI, 3])) + [)] + IIF(laFields[lnI, 5], [], [ NOT NULL])
			ENDCASE

			lcSQL = lcSQL + [,] + CHR(13) + CHR(10)
		ENDFOR

		lcSQL = LEFT(lcSQL, LEN(lcSQL) - 3) + [)]

		SQLPT(lcSQL, tcCurName)
	ENDFUNC


	FUNCTION DropCursor(tcCurName)
		LOCAL lcSQL

		lcSQL    = ""
		lcSQL = lcSQL + [DROP TABLE ] + tcCurName

		SQLPT(lcSQL, tcCurName, .T.)
	ENDFUNC


	FUNCTION SendData(tcCurName)
		LOCAL loErr AS EXCEPTION
		
		*** drop the temp view if exists
		TRY
			DROP VIEW Temp
			
		CATCH TO loErr
			DO CASE
				CASE loErr.ErrorNo = 1562
					*** Ignore: Cannot find object Temp in the database.
					
				OTHERWISE
					THROW
			ENDCASE
		ENDTRY


		*** create the temp view
		CREATE SQL VIEW Temp ;
			REMOTE CONNECTION (goApp.cProjectCode + '_Data_FB') AS ;
			SELECT * ;
			FROM &tcCurName

		DBSETPROP("Temp", "VIEW", "SendUpdates", .T.)


		*** append data into the view
		USE Temp IN 0

		SELECT Temp

		APPEND FROM (DBF(tcCurName))

		TABLEUPDATE(.T., .T.)


		*** drop the view after use
		TRY
			DROP VIEW Temp
			
		CATCH TO loErr
			DO CASE
				CASE loErr.ErrorNo = 1562
					*** Ignore: Cannot find object Temp in the database.
					
				OTHERWISE
					THROW
			ENDCASE
		ENDTRY
	ENDFUNC
ENDDEFINE
Regards
Bhavbhuti
___________________________________________
Softwares for Indian Businesses at:
http://venussoftop.tripod.com
___________________________________________
venussoftop@gmail.com
___________________________________________
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform