SQLite User Forum

”sqlite3 oriented” programming
Login

"sqlite3 oriented" programming

(1) By anonymous on 2024-12-31 02:29:48 [link] [source]

Are there any good books on the following ?

  1. For webapps, it is common for the app to just be a simple CRUD wrapper around a Postgres / MySQL / Sqlite3 database.

  2. For desktop/mobile apps, it is common to use Sqlite3 as the file storage format.

  3. My question is: is there a style of programming (and books on this) that uses Sqlite3 as the "source of truth" for a desktop app ? I.e. instead of using Vecs, HashMaps, ... for data structures, we just:

3a. try to make the desktop app a simple MVC 3b. the View is "sql queries -> render them" 3c. the Control is "mouse keyboard events -> SQL queries" 3d. the Model is just a sqlite3 in process

The observation being: given the sqlite3 is in process, for non-dumb queries, we can run them on modern hardware fast enough for the app to be real time-ish feel

If so, is there a good book on these types of techniques? (Most books involving SQL seems to go the CRUD webapp route).

Thanks!

(2) By Richard Hipp (drh) on 2024-12-31 09:42:33 in reply to 1 [link] [source]

I know of no books are articles on "SQLite-oriented programming". However, I observe that the Forum software that you used to post your question is an example of SQLite-oriented programming. The program is Fossil. It uses an SQLite database as its source of truth. It runs on both desktop and as a web app.

(3) By anonymous on 2024-12-31 17:12:30 in reply to 2 [link] [source]

Do you have any advice / recommended reading on this general direction ?

I really like this Fred Brooks quote:

"Show me your flowcharts and conceal your tables, and I shall continue to be mystified. Show me your tables, and I won’t usually need your flowcharts; they’ll be obvious."

Typically, we tend to think of program state in terms of Lists, Vectors, Hashmaps, ... other data structures + algorithms to manipulate them. This tends to result in a situation where:

  1. state / "truth" is spread all over the place
  2. state / "truth" is often not easy to access (encapsulation), behind nested data structures
  3. debugging is messy; even figuring out how to print the data is non trivial

Now, traditionally we may have had to do this due to "efficiency" and ensuring good big-Oh running time. However, with modern CPUs, it seems we can just stuff everything w/ tables ... and with proper indexes, it should be fast enough.

This type of thinking seems to require quite a bit of "unlearning"; where we:

  1. view tables as the core data strcuture
  2. have state/truth be a collection of tables (instead of multi level nested data structures)
  3. read/write via SQL (via pointers / references)

Fossil technically satisfies my definition above, but was not quite what I had in mind for the following reason. Fossil sorta == forum software. Forum software sorta == CRUD apps, where it's tolerable to have latency on actions measured in 100ms - single digit seconds.

I was thinking of even lower latency requirements -- say something like a notepad editor, a vim/emacs, a window manager, ...

Anyway, any advice in this generation direction is welcome. I'm very interested in how far "table/sql is all you need" can take one in modern software.

(4) By Spindrift (spindrift) on 2024-12-31 17:29:57 in reply to 3 [link] [source]

Forum software sorta == CRUD apps, where it's tolerable to have latency on actions measured in 100ms - single digit seconds.

Interestingly this very page tells me that:

This page was generated in about 0.004s by Fossil 2.26

And I've never noticed any particular latency.

Perhaps your requirements are even more specific?

(5) By anonymous on 2024-12-31 17:33:18 in reply to 4 [link] [source]

  1. I did not realize Fossil was that fast. Thank you for pointing it out.

  2. I tend to view "Forum Software" as "we can tolerate slow" -- because we do lots of writing, then we hit "Post" once in a while.

  3. I'm especially interested in "interactive software" things where we want to be able to hit 30fps (30 ms / render -- which Fossil is beating at 4ms / render) -- things like a text editor, an IDE, a window manager, etc ... -- things that "refresh" on every key stroke.

(10) By anonymous on 2025-01-03 07:21:03 in reply to 5 [link] [source]

realize Fossil was that fast.

Fossil is a single-file executable that essentially wraps domain-specific knowledge around a heavily-optimized, local database. Even running as a CGI, it'll be quick for the previous two reasons and, also, Unix systems can fork and exec processes like mad.

(6) By Richard Hipp (drh) on 2024-12-31 18:45:30 in reply to 4 [link] [source]

In fairness, that timer starts when the HTTP request has been fully parsed and passed down to the Fossil CGI, and ends as last bytes of the page content are being sent back to the web server from the CGI. It does not include network latency. (How could it?). The time shown is just the time needed by the Fossil CGI to generate the page, not the total round-trip time. The time shown does include total round-trip time for all SQL queries needed to generate the page, though, and perhaps that is what is relevant to this conversation.

(7) By anonymous on 2024-12-31 18:56:48 in reply to 6 [link] [source]

I think the core issue of this discussion so far is:

I have done a terrible job of defining "what type of software" is the target here ?

To address this issue, let us define it as:

  1. IntelliJ /IDEA style IDE
  2. Window Manager

the second is drastically simpler than the first; but I think both are situations where:

  1. we want to display lots of data
  2. we want near instant (< 30ms) response on every key stoke / mouse movement
  3. these are situations where scripting / extending would be much much much easier if all we had to do was write an SQL query

I am wondering if anyone has readings / advice towards an IDE / window manager style software where sqlite3 serves as the "source of truth"

(8.1) By punkish on 2024-12-31 22:38:26 edited from 8.0 in reply to 7 [link] [source]

Would any of the numerous SQLite guis fit your bill of requirements? For example, I use a program called TablePlus, on occasion, to view the data or to test convoluted queries, primarily for the ease it’s built in editor offers. Data/result update is immediate.

That said, I can’t imagine any program even slightly more involved than a db gui that could be driveable solely with SQL.

(9) By anonymous on 2024-12-31 23:19:56 in reply to 8.1 [link] [source]

That said, I can’t imagine any program even slightly more involved than a db gui that could be driveable solely with SQL.

With all due respect, but without much evidence, I disagree with this line (and am looking for resources that shows precisely how to break this barrier.)

I am often reminded of these two quotes:

Any sufficiently complicated C or Fortran program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp.

Any sufficiently complicated concurrent program in another language contains an ad hoc informally-specified bug-ridden slow implementation of half of Erlang.

and, as I stare down nested levels of List / Vec / HashMap / adhoc indexing, I wonder if there is something similar dealing with "state / source of truth" and "containing a half-impl of SQL / Relational DB" -- and sqlite3 being special here in that sqlite3 can go inprocess, unlike mysql/postgres.

(14) By ralf (ralfbertling) on 2025-01-07 10:57:15 in reply to 7 [link] [source]

Hi,

the question is how much data loss are you ready to accept to persist truth/state.

This question exists independently of the data structures in use.

If your IDE or Window Manager groups input to save it less frequently (e.g. every second if changes occured) most users would be satisfied. If reading from “disk“ is too slow/ressource intensice you can always attach in memory databases to cache/buffer data, so you can use the same abstraction. There are many frameworks that allow for simple object/relational mapping.

cheers, ralf

(11) By ingo on 2025-01-03 13:55:02 in reply to 3 [link] [source]

state / "truth"

You can build state machines using SQL.

Tables are just data structures and queries et al. are just functions, procedures tied to that table (+ connection).

It seems SQL + CTE + Windowing is Turing complete.

I'm very interested in how far "table/sql is all you need" can take one in modern software.

It may look like a hammer, but not everything is a nail

(12) By J-L Hainaut (JLHainaut) on 2025-01-03 18:25:48 in reply to 11 [link] [source]

(13) By Impossibly Stupid (droleary) on 2025-01-06 22:16:47 in reply to 3 [source]

I posted on the Ars Technica forums not too long ago looking to do something along these lines myself.

Typically, we tend to think of program state in terms of Lists, Vectors, Hashmaps, ... other data structures + algorithms to manipulate them

To be realistic, you probably can't escape the basic data structures of whatever underlying language manipulates them. I think the best you can hope for is a database persistence layer for native types/objects that is as transparent as possible. This is probably most easy to do with scripting languages, and I'm currently favoring Lua for my project because its most advanced data type out of the box is already a table.

have state/truth be a collection of tables (instead of multi level nested data structures)

Given the relational nature of a database, they are functionally equivalent. I'd say the biggest benefit to a database as a "source of truth" is that it is (or at least can be) independent of the algorithms implemented to manipulate it.

I was thinking of even lower latency requirements -- say something like a notepad editor, a vim/emacs, a window manager, ...

Well, you still have to architect how that all works from a database perspective. If a single keystroke results in a query that selects a 5MB (or whatever) document, alters the data appropriately, and then updates the table with the new value, I think you're going to find a lot more latency than you want for modifying those 8 bits. You'd probably do better with a message queue as middleware to write data in sufficiently large/meaningful chunks.

Anyway, any advice in this generation direction is welcome. I'm very interested in how far "table/sql is all you need" can take one in modern software.

Break it down and start eliminating all the non-DB features of your programs. For example, I started by getting rid of the filesystem read/write calls in favor of putting that data in either structured tables (configuration, logging, etc.) or stored in sqlar (making it easy to "externalize"). Where does the real friction come from for the software you're writing?

(16) By anonymous on 2025-01-08 21:20:03 in reply to 13 [link] [source]

Great insights! Replying to you in general, not any point in particular:

Alan Perlis has this great quote: 9. It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. Two languages that take this to the extreme are: LISP: LIST processing language; APL: ARRAY processing language.

  1. I think what I really want is more "TABLE processing language" than "sqlite3 oriented" ; i.e. treating Table s the fundamental data structure. I.e. in Rust, I am now playing with code of the form:
pub struct MyTable {
  data: Vec<T>,
  idx_1: HashMap<.., usize>,
  idx_2: HashMap<..., usize>,
}
  1. Never though about Lua as "Table oriented" -- great insight -- thanks! (Always been bothered by the index-by-1).

  2. There is this interesting entry "Table Oriented Programming" over at https://wiki.c2.com/?TableOrientedProgramming -- but I'm not sure about the content. I.e. I agree with the title, but can't figure out if the content is actual helpful or flamewars.

(15) By Max (Maxulite) on 2025-01-07 12:29:37 in reply to 3 [link] [source]

I think that "table/sql is all you need" still needs some "convention layer" on top of SQLite. So in order to make your life easier someone created some imperative/function software that exposes some rules for you to follow. One of examples not exactly fitting your description but allowing to create sites only with SQL statements is https://sql-page.com (also the github page) (SQLite is one of the supported engines, so at least in this respect it's SQLite oriented). I didn't actually try it, but the description is quite straightforward. Still, it's a convention to place particularly named files with SQL statements with columns having particular names.

Also from your comments I read that you think that this approach is/was underrated because of the performance penalties. I'm not sure this is the only reason. One example is my experiments with virtual tables and partly with what you suggested. Once I made a virtual table in SQLite for drawing different primitives. So, in a sense, it was a "convention" table in that it accepted rows, saved them temporarily in the memory and interpreted every row as a command for a drawing primitive (like rectangle, circle, text etc). The table reacts to the change in its data by ensuring that the window for drawing is popped up and invalidated. So, theoretically it can be used to draw a GUI interface. But it lacks interactivity. Thinking about it today, I would probably introduce one or several event-oriented fields (like OnClick, OnHover, etc) and assume that there are semicolon-delimited SQL statements (Insert/Update/Delete) inside. They can both manipulate some global state and this drawing table so the GUI will become interactive and dynamic. What I see here has at least two problems:

  • The drawing primitives usually have different properties. So, the Circle may have a Radius, while for Rectangle the Radius is nothing. In my implementation I actually ended up introducing more fields, but the fact that the collection of different graphic objects has a single schema is already breaking the "single truth" principle. And if you suggest using a single field with a customizable JSON data, you will break your own "don't use complex structures" rule.
  • Another problem is bigger. When programming imperatively I'm used to having reusable entities and they should be properly named just for the sake of clarity and sustainability. If you look at my suggestion of making event-oriented fields for this table, it's easy to see that starting with some size, this solution would become an unsustainable spaghetti mess. So in order to make it manageable I have to invent some new conventions/syntax/rules. I suspect that sql-page.com may also suffer this for comparatively large sites

Also an honorable mention comes to the Eve project (no longer active, not related to SQLite in any way). Some interesting ideas, but the idea related to this discussion was to treat everything as database records. The demo projects were very impressive, but as I recall my thoughts, to manage many declarative pieces interconnected implicitly is something the human brain is not good at.

(17) By anonymous on 2025-01-08 21:22:59 in reply to 15 [link] [source]

Since you mention reactivity and event handling; I am curious if you have tried https://curtclifton.net/papers/MoseleyMarks06a.pdf ("Functional Relational Programming")

On multiple occasions, I have run into this paper, found it to describe the problem I'm facing well; but I have never managed to use this in practice.