From d5bc58c38d040b2aba0bcbbe397ecd6a265d9ccb Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Thu, 31 Jan 2013 15:17:40 +0000 Subject: [PATCH] Using sqlite as an example --- test/page.lp | 78 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/test/page.lp b/test/page.lp index 0f7901e1..5024acb7 100644 --- a/test/page.lp +++ b/test/page.lp @@ -1,40 +1,52 @@ - -

Prime numbers from 0 to 100, calculated by Lua:

- - function is_prime(n) - if n <= 0 then return false end - if n <= 2 then return true end - if (n % 2 == 0) then return false end - for i = 3, n / 2, 2 do - if (n % i == 0) then return false end - end - return true - end +

This is an example Lua server page served by +Mongoose web server. +Mongoose has Lua, Sqlite, and other functionality built in the binary. +This example page stores the request in the Sqlite database, and shows +all requests done previously.

- for i = 1, 100 do - if is_prime(i) then print('' .. i .. ' ') end - end +
+
+  -- Setup a trace callback, to show SQL statements we'll be executing.
+  -- db:trace(function(data, sql) print('Executing: ' .. sql .. '\n') end, nil)
 
- 

Reading POST data from Lua (click submit):

-
+ -- Create a table if it is not created already + db:exec([[ + CREATE TABLE IF NOT EXISTS requests ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + timestamp NOT NULL, + method NOT NULL, + uri NOT NULL, + user_agent + ); + ]]) -
-   POST data: []
-   request method: []
-   IP/port: []
-   URI: []
-   HTTP version []
-   HEADERS:
-   
-     
- 
+ -- Add entry about this request + local stmt = db:prepare( + 'INSERT INTO requests VALUES(NULL, datetime("now"), ?, ?, ?);'); + stmt:bind_values(request_info.request_method, request_info.uri, + request_info.http_headers['User-Agent']) + stmt:step() + stmt:finalize() + -- Show all previous records + print('Previous requests:\n') + stmt = db:prepare('SELECT * FROM requests ORDER BY id DESC;') + while stmt:step() == sqlite3.ROW do + local v = stmt:get_values() + print(v[1] .. ' ' .. v[2] .. ' ' .. v[3] .. ' ' + .. v[4] .. ' ' .. v[5] .. '\n') + end - + -- Close database + db:close() +?> +