SQL
|
|||||||||||||||||||||||||||||||||||||||||||||||
open database southwind
exec sql
SELECT * from orders
SAVE AS XML orders.xml;
|
or a more complex multi-table query:
open database southwind
exec sql
SELECT orders.orderid, orders.customerid,
employees.employeeid, employees.lastname, employees.firstname,
orders.orderdate, orders.freight, orders.requireddate,
orders.shippeddate, orders.shipvia, orders.shipname,
orders.shipaddress, orders.shipcity,
orders.shipregion, orders.shippostalcode, orders.shipcountry,
customers.companyname, customers.address, customers.city,
customers.region, customers.postalcode, customers.country
FROM orders INNER JOIN customers
ON customers.customerid = orders.customerid,
orders INNER JOIN employees
ON orders.employeeid = employees.employeeid
SAVE AS XML orderinfo;
|
The resulting XML file can then be further processed within the same or a different Recital environment or transferred to a third party product.
<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
xmlns:rs='urn:schemas-microsoft-com:rowset'
xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'>
<s:ElementType name='row' content='eltOnly' rs:updatable='true'>
<s:AttributeType name='orderid' rs:number='1' rs:nullable='true'
rs:write='true' rs:basetable='orders.dbf' rs:basecolumn='orderid'>
<s:datatype dt:type='number' rs:dbtype='numeric' dt:maxLength='10'
rs:scale='0' rs:precision='14' rs:fixedlength='true' />
</s:AttributeType>
<s:AttributeType name='customerid' rs:number='2' rs:nullable='true'
rs:write='true' rs:basetable='orders.dbf' rs:basecolumn='customerid'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='5'
rs:fixedlength='true' />
</s:AttributeType>
<s:AttributeType name='employeeid' rs:number='3' rs:nullable='false'
rs:write='true' rs:basetable='orders.dbf' rs:basecolumn='employeeid'>
<s:datatype dt:type='number' rs:dbtype='numeric' dt:maxLength='10'
rs:scale='0' rs:precision='20' rs:fixedlength='true' />
</s:AttributeType>
<s:AttributeType name='lastname' rs:number='4' rs:nullable='false'
rs:write='true' rs:basetable='orders.dbf' rs:basecolumn='lastname'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='20'
rs:fixedlength='true' />
</s:AttributeType>
<s:AttributeType name='firstname' rs:number='5' rs:nullable='false'
rs:write='true' rs:basetable='orders.dbf' rs:basecolumn='firstname'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='10'
rs:fixedlength='true' />
</s:AttributeType>
<s:AttributeType name='orderdate' rs:number='6' rs:nullable='true'
rs:write='true' rs:basetable='orders.dbf'rs:basecolumn='orderdate'>
<s:datatype dt:type='Date' rs:dbtype='Date' dt:maxLength='10'
rs:fixedlength='true' />
</s:AttributeType>
<s:AttributeType name='freight' ...
|
Click image to display full size
Fig 1: Microsoft® Office Excel 2003: orderinfo.xml.
For data accessed through a Recital Database Gateway, such as Oracle, MySQL or PostgreSQL, the FETCH command can be used to save a cursor results set into an XML file:
// Connect to MySQL Database 'mydata' via Recital Database Gateway
nStatHand=SQLSTRINGCONNECT("mys@mysql1:user1/pass1-mydata",.T.)
if nStatHand < 1
dialog box [Could not connect]
else
exec sql
DECLARE cursor1 CURSOR FOR
SELECT account_no, last_name, first_name FROM example;
exec sql
OPEN cursor1;
exec sql
FETCH cursor1 INTO XML exa1.xml;
SQLDISCONNECT(nStatHand)
endif
|
The CREATE TABLE statement allows a new table to be created based on the structure defined in an XML file. The data from the XML file can optionally be loaded into this new table if the LOAD keyword is included. For example, a new 'orderinfo' table can be created and populated with data from the orderinfo.xml file created by the SELECT...SAVE AS XML statement shown earlier:
exec sql
open database southwind;
exec sql
SELECT orders.orderid, orders.customerid,
employees.employeeid, employees.lastname, employees.firstname,
orders.orderdate, orders.freight, orders.requireddate,
orders.shippeddate, orders.shipvia, orders.shipname,
orders.shipaddress, orders.shipcity,
orders.shipregion, orders.shippostalcode, orders.shipcountry,
customers.companyname, customers.address, customers.city,
customers.region, customers.postalcode, customers.country
FROM orders INNER JOIN customers
ON customers.customerid = orders.customerid,
orders INNER JOIN employees
ON orders.employeeid = employees.employeeid
SAVE AS XML orderinfo;
exec sql
CREATE TABLE orderinfo
FROM XML orderinfo LOAD;
|
The INSERT statement can be used to load data when the table structure already exists. Taking our earlier orderinfo.xml file again, the data can be loaded using INSERT:
exec sql
open database southwind;
exec sql
SELECT orders.orderid, orders.customerid,
employees.employeeid, employees.lastname, employees.firstname,
orders.orderdate, orders.freight, orders.requireddate,
orders.shippeddate, orders.shipvia, orders.shipname,
orders.shipaddress, orders.shipcity,
orders.shipregion, orders.shippostalcode, orders.shipcountry,
customers.companyname, customers.address, customers.city,
customers.region, customers.postalcode, customers.country
FROM orders INNER JOIN customers
ON customers.customerid = orders.customerid,
orders INNER JOIN employees
ON orders.employeeid = employees.employeeid
SAVE AS XML orderinfo;
exec sql
CREATE TABLE orderinfo
FROM XML orderinfo;
exec sql
INSERT INTO orderinfo
FROM XML orderinfo;
|
The examples above show the export and import in a single piece of code. To transfer data between binary-incompatible platforms, the export phase using SELECT...SAVE AS XML would be carried out on the source platform, the resulting XML file would be transferred to the target platform, then the import phase using CREATE TABLE...LOAD or CREATE TABLE + INSERT would be run on the target platform.
The Recital/4GL offers the ability to export data into XML files using the COPY TO ... TYPE XML command and import from XML using the XMLFIRST() and XMLNEXT() functions.
The COPY TO command can be used to export data from Recital and other natively supported tables out to a wide range of formats. This includes exporting to an XML file. The '.xml' file extension is added automatically. The COPY TO command can be used to export an entire table:
open database southwind use orders copy to orders type xml |
or, using the FIELDS clause and the FOR or WHILE clauses, restrict the field list and export only those records which match a particular condition:
open database southwind use orders copy to orders type xml fields orderid for year(orderdate) = 1996 |
Only the orderid field from those records which match the condition is exported:
<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
xmlns:rs='urn:schemas-microsoft-com:rowset'
xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'>
<s:ElementType name='row' content='eltOnly' rs:updatable='true'>
<s:AttributeType name='ORDERID' rs:number='1' rs:nullable='true'
rs:write='true' rs:basetable='ORDERS' rs:basecolumn='ORDERID'>
<s:datatype dt:type='number' rs:dbtype='numeric' dt:maxLength='10'
rs:scale='0' rs:precision='10' rs:fixedlength='true' />
</s:AttributeType>
</s:ElementType>
</s:Schema>
<rs:data>
<z:row ORDERID = '10248' />
<z:row ORDERID = '10249' />
<z:row ORDERID = '10250' />
<z:row ...
|
Data from an XML file can be extracted one record at a time using the XMLFIRST() and XMLNEXT() functions. XMLFIRST() reads the first record from an XML file and loads information from the file into a series of memory variables and arrays. The record data is loaded into a one-dimensional array which is created automatically. Each element in the array contains the data for its corresponding field in string format. The field names are loaded into another automatically-created array. The XMLNEXT() function works in a similar way to deal with all the subsequent records in the XML file. The XMLCOUNT() function can be used, as in the example below, to determine how many data records the XML file has.
The Recital/4GL includes a vast range of functions for manipulation and conversion of arrays and their individual elements. In the example program below, the XMLFIRST() and XMLNEXT() functions are used to sequentially extract each record from an XML file, whose name is passed to the program as a parameter. Once loaded into an array, the data is converted to the correct Recital data type then appended into a table. The table name is also passed as a parameter.
procedure replaceit
append blank
for i = 1 to numfields
if type(field(i)) = "N"
replace &(field(i)) with val(data[&i])
elseif type(field(i)) = "D"
replace &(field(i)) with stod(data[&i])
elseif type(field(i)) = "T"
replace &(field(i)) with ctot(data[&i])
elseif type(field(i)) = "L"
replace &(field(i)) with iif(data[&i]="T",.T.,.F.)
elseif type(field(i)) = "Y"
replace &(field(i)) with val(data[&i])
else
replace &(field(i)) with data[&i]
endif
next
return
parameters cTable, cFile
numfields=xmlfirst(cFile,targ,trans,where,fldnames,data)
if numfields < 1
dialog box [No records in XML file]
else
use &cTable
replaceit()
endif
numrecs = xmlcount(cFile)
if numrecs > 1
numleft = numrecs -1
for i = 1 to numleft
xmlnext(trans,where,fldnames,data)
replaceit()
next
endif
|
Other features exist in Recital to facilitate the import and export of data:
RDDs
The RDDs, Replaceable Database Drivers, are available on
Windows, Linux and all supported 32-bit UNIX platforms. They allow
for the use and creation of database tables and indexes in FoxPro,
dBase and Clipper formats. The file format is the same across all
the platforms that support the RDDs, allowing the tables and indexes
to be transferred as required. The formats are also supported by
a wide range of third-party products as well as their originating
database systems. For more information on the RDDs, please see the
online documentation on Xbase migration and the SET FILETYPE command.
BUILD/INSTALL
These are Recital/4GL commands for the export (BUILD) and
import (INSTALL) of Recital tables and their associated memo,
dictionary and multiple index files in ASCII format to allow them
to be transferred across binary incompatible platforms. For more
information, please see the online documentation on Recital/4GL
commands.
COPY Commands
The COPY TO, COPY STRUCTURE, COPY STRUCTURE EXTENDED and CREATE FROM commands can all be used to enable data to be transferred between different formats and different platforms. For more information, please see the online documentation on Recital/4GL commands.
For detailed information on the Microsoft® ActiveX® Data Objects XML Format, please consult the following Microsoft documentation:
|
Link |