first commit

This commit is contained in:
Souti
2025-03-06 11:09:58 +01:00
commit 11f7d440ff
330 changed files with 38306 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
require "path"
require "sqlite3"
local db = sqlite3.open_memory()
assert( db:exec[[
CREATE TABLE test (col1, col2);
INSERT INTO test VALUES (1, 2);
INSERT INTO test VALUES (2, 4);
INSERT INTO test VALUES (3, 6);
INSERT INTO test VALUES (4, 8);
INSERT INTO test VALUES (5, 10);
]] )
assert( db:set_aggregate("my_stats", 2, function()
local square_error_sum = 0
local function step(a, b)
local error = a - b
local square_error = error * error
square_error_sum = square_error_sum + square_error
end
local function final(num_called)
return square_error_sum / num_called
end
return step, final
end))
local my_stats = db:first_cols("SELECT my_stats(col1, col2) FROM test")
print("my_stats:", my_stats)

View File

@@ -0,0 +1,28 @@
require "path"
require "sqlite3"
local db = sqlite3.open_memory()
assert( db:exec[[
CREATE TABLE test (col1, col2);
INSERT INTO test VALUES (1, 2);
INSERT INTO test VALUES (2, 4);
INSERT INTO test VALUES (3, 6);
INSERT INTO test VALUES (4, 8);
INSERT INTO test VALUES (5, 10);
]] )
assert( db:set_function("my_sum", 2, function(a, b)
return a + b
end))
for col1, col2, sum in db:cols("SELECT *, my_sum(col1, col2) FROM test") do
print(col1, col2, sum)
end

View File

@@ -0,0 +1,118 @@
require "path"
require "sqlite3"
local db = assert( sqlite3:open_memory() )
assert( db:exec[[
CREATE TABLE customer (
id INTEGER PRIMARY KEY,
name VARCHAR(40)
);
CREATE TABLE invoice (
id INTEGER PRIMARY KEY,
customer INTEGER NOT NULL,
title VARCHAR(80) NOT NULL,
article1 VARCHAR(40) NOT NULL,
price1 REAL NOT NULL,
article2 VARCHAR(40),
price2 REAL
);
CREATE TABLE invoice_overflow (
id INTEGER PRIMARY KEY,
invoice INTEGER NOT NULL,
article VARCHAR(40) NOT NULL,
price REAL NOT NULL
);
INSERT INTO customer VALUES(
1, "Michael" );
INSERT INTO invoice VALUES(
1, 1, "Computer parts", "harddisc", 89.90, "floppy", 9.99 );
INSERT INTO customer VALUES(
2, "John" );
INSERT INTO invoice VALUES(
2, 2, "Somme food", "apples", 2.79, "pears", 5.99 );
INSERT INTO invoice_overflow VALUES(
NULL, 2, "grapes", 6.34 );
INSERT INTO invoice_overflow VALUES(
NULL, 2, "strawberries", 4.12 );
INSERT INTO invoice_overflow VALUES(
NULL, 2, "tomatoes", 6.17 );
INSERT INTO invoice VALUES(
3, 2, "A new car", "Cybercar XL-1000", 65000.00, NULL, NULL );
]] )
local function customer_name(id)
local stmt = db:prepare("SELECT name FROM customer WHERE id = ?")
return stmt:bind(id):first_cols()
end
local function all_invoices()
return db:rows("SELECT id, customer, title FROM invoice")
end
local function all_articles(invoice)
local function iterator()
local stmt, row
-- Get the articles that are contained in the invoice table itself.
stmt = db:prepare("SELECT article1, price1, article2, price2 FROM invoice WHERE id = ?")
row = stmt:bind(invoice):first_row()
-- Every Invoice has at least one article.
coroutine.yield(row.article1, row.price1)
-- Maybe the Invoice has a second article?
if row.article2 then
-- Yes, there is a second article, so return it.
coroutine.yield(row.article2, row.price2)
-- When there was an second article, maybe there are even
-- more articles in the overflow table? We will see...
stmt = db:prepare("SELECT article, price FROM invoice_overflow WHERE invoice = ? ORDER BY id")
for row in stmt:bind(invoice):rows() do
coroutine.yield(row.article, row.price)
end
end
end
return coroutine.wrap(iterator)
end
for invoice in all_invoices() do
local id = invoice.id
local name = customer_name(invoice.customer)
local title = invoice.title
print()
print("Invoice #"..id..", "..name..": '"..title.."'")
print("----------------------------------------")
for article, price in all_articles(id) do
print( string.format("%20s %8.2f", article, price) )
end
print()
end

View File

@@ -0,0 +1,8 @@
local path = "?;?.lua;../?;../?.lua"
if package == nil then
LUA_PATH = path -- Lua 5.0
else
package.path = path -- Lua 5.1
end

View File

@@ -0,0 +1,18 @@
require "path"
require "sqlite3"
local db = sqlite3.open_memory()
db:exec[[
CREATE TABLE test (id INTEGER PRIMARY KEY, content);
INSERT INTO test VALUES (NULL, 'Hello World');
INSERT INTO test VALUES (NULL, 'Hello Lua');
INSERT INTO test VALUES (NULL, 'Hello Sqlite3')
]]
for row in db:rows("SELECT * FROM test") do
print(row.id, row.content)
end

View File

@@ -0,0 +1,18 @@
require "path"
require "sqlite3"
local db = sqlite3.open_memory()
db:exec[[ CREATE TABLE test (id INTEGER PRIMARY KEY, content) ]]
local stmt = db:prepare[[ INSERT INTO test VALUES (:key, :value) ]]
stmt:bind{ key = 1, value = "Hello World" }:exec()
stmt:bind{ key = 2, value = "Hello Lua" }:exec()
stmt:bind{ key = 3, value = "Hello Sqlite3" }:exec()
for row in db:rows("SELECT * FROM test") do
print(row.id, row.content)
end

View File

@@ -0,0 +1,40 @@
require "path"
require "sqlite3"
local db = sqlite3.open_memory()
db:exec[[
CREATE TABLE test (
id INTEGER PRIMARY KEY,
content VARCHAR
);
]]
local insert_stmt = assert( db:prepare("INSERT INTO test VALUES (NULL, ?)") )
local function insert(data)
insert_stmt:bind(data)
insert_stmt:exec()
end
local select_stmt = assert( db:prepare("SELECT * FROM test") )
local function select()
for row in select_stmt:rows() do
print(row.id, row.content)
end
end
insert("Hello World")
print("First:")
select()
insert("Hello Lua")
print("Second:")
select()
insert("Hello Sqlite3")
print("Third:")
select()

View File

@@ -0,0 +1,21 @@
require "path"
require "sqlite3"
local db = sqlite3.open_memory()
db:set_trace_handler( function(sql)
print("Sqlite Trace:", sql)
end )
db:exec[[
CREATE TABLE test ( id INTEGER PRIMARY KEY, content VARCHAR );
INSERT INTO test VALUES (NULL, 'Hello World');
INSERT INTO test VALUES (NULL, 'Hello Lua');
INSERT INTO test VALUES (NULL, 'Hello Sqlite3');
]]
for row in db:rows("SELECT * FROM test") do
-- NOP
end