Files
harbour-core/contrib/rddsql
Aleksander Czajczynski 362b7a32de 2019-02-11 13:09 UTC+0100 Aleksander Czajczynski (hb fki.pl)
* contrib/sddsqlt3/core.c
    ! fix DBUSEAREA() operation with SQLITE3 SDD to return empty result
      when query conditions are false or the source table has no rows.

      Previously an logically correct example caused RTE:
      DBUSEAREA(,, "SELECT * FROM existing_table WHERE FALSE")

    ! fix double-free error in sqLite3Disconnect(), looks like the
      sqlite3_close() return value checking was reverted, SQLITE_OK is 0

    * use CDP API to get UTF8 string length

    * use new sqlite3_prepare_v3() when built against
      sqlite 3.20.0 or upper (change borrowed from Viktor's 3.4 fork)

    + add HB_SQLT3_MAP_DECLARED_EMULATED define (not yet enabled by default)
      which make this SDD additionally parse SQLite column declarations.
      Right now it can make HB_FT_DATE fields working using standard
      ISO 8601 "yyyy-mm-dd" syntax. Also declarations not significant for
      SQLite, but useful in xBase-style programming - SQL numeric(len,dec)
      columns are detected in this mode and will be reflected in dbStruct().

    + add support for alternative StoD() like syntax for HB_FT_DATE columns

    + add support for ISO 8601 "YYYY-MM-DD HH:MM:SS.FFF" timestamp declared
      columns, SQLite stored strings are converted to proper HB_FT_TIMESTAMP
      fields

    + added HB_SQLT3_FIELDNAME_STRICT define, which enables shortening
      of field to "name" if SQLite returns "table.name". Such fields are
      not completly usable in xBase code - WA->T.FIELD syntax is not
      valid, but FieldPos("t.field") is OK. I think it should be default
      behaviour or some runtime setting should be introduced for convenience
      when working with specific SQL queries.

  * contrib/rddsql/sqlbase.c
  * contrib/rddsql/sqlmix.c
    + added ZAP functionality to SQLBASE and SQLMIX RDDs,
      index tags are preserved while ZAP-ing SQLMIX area.
      They are cleaned, no REINDEX is needed

    * changed to allow values of any type in "V" SIX3 / HB_FT_ANY fields
      in SQLBASE/SQLMIX RDD workareas

  * contrib/hbfoxpro/relfunc.c
    ! fix InList() FoxPro compatible function not looking at the last
      parameter passed. Thanks to Attila Szabo for the information
      posted on the developers list.
2019-02-11 13:10:05 +01:00
..

                    Simple SQL Interface for Harbour



1. Introduction

   Simple SQL interface implements accessing SQL query result via RDD
interface. It is not intended to be replacement for "transparent" move of
DBFCDX application to SQL world.

   I want to discuss this in more detail. Many current RDDs for SQL servers
(ex. SQLRDD from xHarbour.com) tries to make a feeling you are working with
DBF file, but not with SQL database. SQL server does not support many
features, ex. RecNo(), deleted flag, file locks, record locks. These RDDs
are emulating these features to make feeling of DBF. Deleted() function is
emulated by creating additional table columns to store delete flag. Some
"hidden system" tables are used to register locking operations and emulate
record and file locks in DBF style. The idea of SQL query is also lost. If
you do a simple loop

 dbUseArea( , "select * from my_table" )
 DO WHILE ! Eof()
    somefunc( FIELD->some_sql_field )
    dbSkip()
 ENDDO

RDD usualy will read SQL rows in portions, let's say 100 records per query.
So, hidden queries are generated. If you are using indexes these queries
are really complicated. Let's have index on FIELD1 + Str( FIELD2 ). A seek
to value cValue1 + Str( nValue2 ) will generate a query like:

 SELECT * FROM my_table
     WHERE (FIELD1 == cValue1 and FIELD2 >= nValue2) or FIELD1 > cValue1
     ORDER BY FIELD1, FIELD2, _RECNO
     LIMIT 100

After evaluation of first 100 cached records, next query will be generated:

 SELECT * FROM my_table
     WHERE (FIELD1 == cLastField1 and FIELD2 == nLastValue2 and _RECNO > nLastRecno) or
           (FIELD1 == cLastField1 and FIELD2 > nLastValue2) or
           FIELD1 > cLastValue1
     ORDER BY FIELD1, FIELD2, _RECNO
     LIMIT 100

To optimize these queries the SQL index expresion should be
"FIELD1,FIELD2,_RECNO", but not "FIELD1,FIELD2" as written in INDEX ON
command.

   "Simple SQL interface" is too long to repeat every time I want to
address this library. I'll also use acronym "SSI" to address it.

   The idea of SSI is different. It does not make hidden queries. All
queries should be made explicitly by programmer. SSI gives access to query
result via RDD interface, it does not tries to emulate DBF and be
"plug-and-play" solution for DBF to SQL migration. If you do

 dbUseArea( , "select * from my_table")

all query (it could contain millions of records!) will be cached.

   The features of SSI approach are:

- It's possible to access SQL database of other applications. Other
 applications usualy does not follow agreement of "plug-and-play" SQL drivers
 about additional DELETED column, _RECNO in the end of index expression, etc.
 Access of SQL database of other applications is sometimes not possible.

- It's query oriented. That means a simple DO WHILE ! Eof() loop will iterate
 each records once and only once. This is not true for "plug-and-play" SQL
 drivers, if indexing is used. Just like in the case of loop over DBF file.
 It is not guaranteed that all records are included! Yes! If key value of the
 first record in index is changed to be the last record in index during the
 phase of record processing, DO WHILE ! Eof() loop will iterate only this
 single records even if the database contains millions of records. Your sould
 do FLock() on DBF to guarantee the records are not changed. Do you use FLock()
 before readonly DO WHILE ! Eof() loops? :)



2. Architecture


             +-------------+
             |             |
             | SQLMIX RDD  |
             |             |
             +-------------+
                  |  ^
                  V  |
             +-------------+    +---------+
             |             |--->|         |
             | SQLBASE RDD |    |   SDD   |
             |             |<---|         |
             +-------------+    +---------+


   SQLBASE RDD implements basic functionality for accessing SQL query result
via RDD interface. This RDD could be used, if indexing of query result is not
necessary or all indexing is done by SQL server (by using ORDER BY clause).

   SQLMIX RDD implements indexing of query result. This indexing is not
related to SQL server ORDER BY clause. SQLMIX do indexing of the query on the
client side.

   SDD is acronym for Sql Database Driver. RDD is used to implement access
of different database formats like DBF, SDF, etc. SDD is used to implement
access of different SQL databases. Every SQL server (MySQL, PostgreSQL, etc.)
has a corresponding SDD. SDD driver implements a specific part of data
exchange interface between SQLBASE and SQL server.



3. Modifying database

   SSI presents a query result via RDD interface and generates no hidden
SQL queries. So, how database can be changed? Does dbAppend() and FieldPut()
works, or is it readonly SQL interface?
   dbAppend(), FieldPut() and other similiar functions work on cached query
result, i.e. query can be appended by new rows and field values can be
changed, but SQL database is not changed. dbCreate() function can also be
used to create an "empty query result" but no table is created on SQL server.
So, SSI can also be used as implementation of "array RDD".
   The programmer must call SQL command explicitly to modify SQL tables.
SSI provides a method to detect which cached rows was changed or appended.