' The MIT License (MIT)
'
' Copyright (C) 2026 Scott Duensing
'
' Permission is hereby granted, free of charge, to any person obtaining a copy
' of this software and associated documentation files (the "Software"), to
' deal in the Software without restriction, including without limitation the
' rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
' sell copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
' FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
' IN THE SOFTWARE.

' sqltest.bas -- SQL Database Demo
'
' Demonstrates the SQL include library:
'   SQLOpen / SQLClose    - open and close a database
'   SQLExec               - execute non-query statements
'   SQLQuery / SQLNext    - run a query and iterate rows
'   SQLField$ / SQLFieldInt / SQLFieldDbl - read column values
'   SQLFieldCount / SQLFieldName$         - inspect result schema
'   SQLEof / SQLFreeResult / SQLAffected  - cursor and status
'   SQLError$             - error reporting
'
' Add sql.bas to your project, then click Run.

DIM db AS LONG
DIM rs AS LONG

PRINT "Opening database..."
db = SQLOpen("sqltest.db")

IF db = 0 THEN
    PRINT "Failed to open database!"
    END
END IF

PRINT "Database opened (handle"; db; ")"
PRINT

' Create table and insert sample data
SQLExec db, "CREATE TABLE IF NOT EXISTS parts (id INTEGER PRIMARY KEY, name TEXT, price REAL)"
SQLExec db, "DELETE FROM parts"
SQLExec db, "INSERT INTO parts VALUES (1, 'Resistor 10K', 0.05)"
SQLExec db, "INSERT INTO parts VALUES (2, 'Capacitor 100uF', 0.25)"
SQLExec db, "INSERT INTO parts VALUES (3, 'LED Red', 0.10)"
SQLExec db, "INSERT INTO parts VALUES (4, '555 Timer', 0.75)"
SQLExec db, "INSERT INTO parts VALUES (5, 'Arduino Nano', 12.50)"

PRINT "Inserted 5 rows. Affected:"; SQLAffected(db)
PRINT

' Query and display results
rs = SQLQuery(db, "SELECT * FROM parts ORDER BY price")

IF rs = 0 THEN
    PRINT "Query failed: "; SQLError$(db)
    SQLClose db
    END
END IF

PRINT "Columns:"; SQLFieldCount(rs)

DIM i AS INTEGER
FOR i = 0 TO SQLFieldCount(rs) - 1
    PRINT "  ["; i; "] "; SQLFieldName$(rs, i)
NEXT i

PRINT
PRINT "Parts (sorted by price):"
PRINT "-----------------------------------"

DO WHILE SQLNext(rs)
    PRINT "#"; SQLFieldInt(rs, 0);
    PRINT " "; SQLField$(rs, "name");
    PRINT " $"; SQLFieldDbl(rs, 2)
LOOP

PRINT "-----------------------------------"

SQLFreeResult rs
SQLClose db

PRINT
PRINT "Done!"
