Post AXPiO7LeeLPiXel6pc by CGM@mastodon.scot
(DIR) More posts by CGM@mastodon.scot
(DIR) Post #AXPiO6jivN4Ye10oi0 by alecui@qoto.org
2023-07-06T01:24:24Z
0 likes, 1 repeats
Every day I get more and more fascinated about #tcltk and its simplicity, it was the sort of language I was looking for without knowing.While searching for libsqlite3, I noticed that #openSUSE has sqlite3-tcl, so that made me curious, but then I remembered #sqlite began as a Tcl extension (that’s the reason SQLite has a feather in its logo). There’s something about this combination that fits… so well.For example, this is how you can all table names from a SQLite database: set tableNames [db eval {SELECT tbl_name FROM sqlite_master}] (after you do sqlite3 db example.db).(yes, you can just do strings like that like it’s nobody’s business, no fancy schmancy string interpolation). I find it satisfying to straight up write SQL uninterrupted.Another example: db eval {CREATE TABLE t1(a TEXT, b INTEGER)}, then you can do db eval {SELECT a FROM t1} { puts a=$a} and it will print out the first column, exactly as you’d expect.You can also naturally extend SQLite using Tcl. Let’s take a trivial example: a square root function. It might look something like this: proc sql_sqrt {v} {return [expr {sqrt($x)}], then you register it using db function sqrt sql_sqrt. Once that’s defined, you can use it wherever any of the built-in SQL functions are allowed: db eval {CREATE TABLE t2 AS SELECT sqrt(b) FROM t1} or db eval {SELECT x FROM t3 WHERE sqrt(a*a+b*b)>10}. The beauty of this is that the procedures can be arbitrarily complex and can even take advantage of the metaprogramming Tcl offers using uplevel, upvar and eval. For example, if you need a function that executes arbitrary Tcl code supplied as its argument, you can do something like proc sql_eval {code} {uplevel #0 $code} and then db function eval sql_eval, thus giving you the ability to evaluate Tcl from within SQL (ain’t that cool?)I severely underestimated Tcl and you, the reader, probably have too, so come and give it a try, join the dark side, we have cookies, milk and a good Emacs mode (I think vim’s mode is also good fwiw)
(DIR) Post #AXPiO7LeeLPiXel6pc by CGM@mastodon.scot
2023-07-06T08:52:26Z
2 likes, 0 repeats
@alecui Don't know if you're aware, but #redis, one of the best-known no-sql database systems, started life as a Tcl prototype before being rewritten in C. The Redis command structure still looks very similar to Tcl.See https://wiki.tcl-lang.org/page/redis and https://gist.github.com/antirez/6ca04dd191bdb82aad9fb241013e88a8#tcl #tcltk
(DIR) Post #AXPiS8WaDb6hLemqw4 by icedquinn@blob.cat
2023-07-06T09:47:51.573986Z
1 likes, 1 repeats
@alecui the good doctor is still a tclhead. fossil uses a derivative of it for a scripting language.
(DIR) Post #AXQnR30zVtzQC2brBA by alecui@qoto.org
2023-07-06T13:13:51Z
0 likes, 0 repeats
@icedquinn what do you mean by “the good doctor”?
(DIR) Post #AXQnR3iEv6aIMAqOae by icedquinn@blob.cat
2023-07-06T22:18:31.042386Z
2 likes, 0 repeats
@alecui Dr Richard Hipp, author of Sqlite and Fossil. I had blanked on his name at the time