This is a continuation from the previous post. I integrated the postgresql into Rye, and made it work with the same SQL dialect that the SQLite integration uses.
The "web framework" code (5 functions) is the same as in the previous post. The web-application and server code is below.
A couple of notes ...
The SQL dialect is in short a
dialect of Rye values (not a string) that produces prepared SQL
statements avoiding the usual bug-prone positional arguments.
Open, query and exec are generic methods
and dispatch on the kind of the first argument (in this case a postgresql
database). SQLite also uses these generic words, so do other
also non-database kinds.
Query returns value of type Spreadsheet.
It's similar to your Excel spreadsheet or in more technical terms like
R-s frame. Some spreadsheet specific functions are sum, avg, max, min,
and also two to quickly retrieve a value of the first and second cell like
A1 and B1 ... that correlates to columns A and B in row 1.
Rye has some specific ideas about failure handling.
Failure means that the function failed to produce an intended result.
Failure is first information and only unhandled failure becomes an
Error. We have specific patterns around handling the failures like check, fix, tidy. I should write a blog post about this soon.
Words that start with a ^ are returning words . They conditionally return to the caller. This line with ^check:
read %profile.txt |^check "couldn't read profile" |print
would in a more classical language look something like:
data = read("profile.txt")
if (data is Error) {
return new Error("couldn't read profile", data)
}
print data
The current idea I am trying out is
that we use failure very strictly when the function couldn't produce a desired result. Various languages use nulls, empty strings, empty blocks,
zeroes, etc. to convey additional information. This is exactly what I am trying to avoid. We will see if it works out well.
For example if a dictionary
lookup returns no result it doesn't return null but failure, also if a SQL query returns no result. In the code above "try-login" returns the ID of
the user on successful login. You could make up a silent "convention"
that it should return 0 if the user login failed. But such unwritten conventions are very frail and can pile up into a mess. So our function fails if there
is no user with that email and it fails via assert if the passwords
don't match. Only if it validates and can return the user's ID, it returns the
integer.
The get-page functions like this:
And the get-token like this ...
Next I should add session related functions and work on better error reporting from the handler functions.
Links:
Rye website (wip)
Github
Komentarji
Objavite komentar