Andrew Cooke | Contents | Latest | RSS | Previous | Next

C[omp]ute

Welcome to my blog, which was once a mailing list of the same name and is still generated by mail. Please reply via the "comment" links.

Always interested in offers/projects/new ideas. Eclectic experience in fields like: numerical computing; Python web; Java enterprise; functional languages; GPGPU; SQL databases; etc. Based in Santiago, Chile; telecommute worldwide. CV; email.

Personal Projects

Choochoo Training Diary

Last 100 entries

Surprise Paradox; [Books] Good Author List; [Computing] Efficient queries with grouping in Postgres; [Computing] Automatic Wake (Linux); [Computing] AWS CDK Aspects in Go; [Bike] Adidas Gravel Shoes; [Computing, Horror] Biological Chips; [Books] Weird Lit Recs; [Covid] Extended SIR Models; [Art] York-based Printmaker; [Physics] Quantum Transitions are not Instantaneous; [Computing] AI and Drum Machines; [Computing] Probabilities, Stopping Times, Martingales; bpftrace Intro Article; [Computing] Starlab Systems - Linux Laptops; [Computing] Extended Berkeley Packet Filter; [Green] Mainspring Linear Generator; Better Approach; Rummikub Solver; Chilean Poetry; Felicitations - Empowerment Grant; [Bike] Fixing Spyre Brakes (That Need Constant Adjustment); [Computing, Music] Raspberry Pi Media (Audio) Streamer; [Computing] Amazing Hack To Embed DSL In Python; [Bike] Ruta Del Condor (El Alfalfal); [Bike] Estimating Power On Climbs; [Computing] Applying Azure B2C Authentication To Function Apps; [Bike] Gearing On The Back Of An Envelope; [Computing] Okular and Postscript in OpenSuse; There's a fix!; [Computing] Fail2Ban on OpenSuse Leap 15.3 (NFTables); [Cycling, Computing] Power Calculation and Brakes; [Hardware, Computing] Amazing Pockit Computer; Bullying; How I Am - 3 Years Post Accident, 8+ Years With MS; [USA Politics] In America's Uncivil War Republicans Are The Aggressors; [Programming] Selenium and Python; Better Walking Data; [Bike] How Fast Before Walking More Efficient Than Cycling?; [COVID] Coronavirus And Cycling; [Programming] Docker on OpenSuse; Cadence v Speed; [Bike] Gearing For Real Cyclists; [Programming] React plotting - visx; [Programming] React Leaflet; AliExpress Independent Sellers; Applebaum - Twilight of Democracy; [Politics] Back + US Elections; [Programming,Exercise] Simple Timer Script; [News] 2019: The year revolt went global; [Politics] The world's most-surveilled cities; [Bike] Hope Freehub; [Restaurant] Mama Chau's (Chinese, Providencia); [Politics] Brexit Podcast; [Diary] Pneumonia; [Politics] Britain's Reichstag Fire moment; install cairo; [Programming] GCC Sanitizer Flags; [GPU, Programming] Per-Thread Program Counters; My Bike Accident - Looking Back One Year; [Python] Geographic heights are incredibly easy!; [Cooking] Cookie Recipe; Efficient, Simple, Directed Maximisation of Noisy Function; And for argparse; Bash Completion in Python; [Computing] Configuring Github Jekyll Locally; [Maths, Link] The Napkin Project; You can Masquerade in Firewalld; [Bike] Servicing Budget (Spring) Forks; [Crypto] CIA Internet Comms Failure; [Python] Cute Rate Limiting API; [Causality] Judea Pearl Lecture; [Security, Computing] Chinese Hardware Hack Of Supermicro Boards; SQLAlchemy Joined Table Inheritance and Delete Cascade; [Translation] The Club; [Computing] Super Potato Bruh; [Computing] Extending Jupyter; Further HRM Details; [Computing, Bike] Activities in ch2; [Books, Link] Modern Japanese Lit; What ended up there; [Link, Book] Logic Book; Update - Garmin Express / Connect; Garmin Forerunner 35 v 230; [Link, Politics, Internet] Government Trolls; [Link, Politics] Why identity politics benefits the right more than the left; SSH Forwarding; A Specification For Repeating Events; A Fight for the Soul of Science; [Science, Book, Link] Lost In Math; OpenSuse Leap 15 Network Fixes; Update; [Book] Galileo's Middle Finger; [Bike] Chinese Carbon Rims; [Bike] Servicing Shimano XT Front Hub HB-M8010; [Bike] Aliexpress Cycling Tops; [Computing] Change to ssh handling of multiple identities?; [Bike] Endura Hummvee Lite II; [Computing] Marble Based Logic; [Link, Politics] Sanity Check For Nuclear Launch; [Link, Science] Entropy and Life

© 2006-2017 Andrew Cooke (site) / post authors (content).

Very First Steps to C-ORM

From: andrew cooke <andrew@...>

Date: Sat, 23 Feb 2013 17:24:45 -0300

I will post something mor epolished to the work blog eventually, but here 
is a quick example that I finally have working.

This example directory is included in the C-ORM sources.  This is the
contents after running configure for the package:

  > ls
  example_foo.c  example_foo.sh  foo.h  Makefile  Makefile.am  Makefile.in

And this is the struct that we want "ORM" for (in C):

  > cat foo.h
  #ifndef FOO_H
  #define FOO_H

  typedef struct foo {
    int id;
    int bar;
    char *baz;
  } foo;

  #endif

And this is the file used to populate the database:

  > cat example_foo.sh
  #!/bin/bash
  rm -f example_foo.db
  sqlite3 example_foo.db < foo.corm.sql
  sqlite3 example_foo.db <<EOF
  insert into foo (id,bar,baz) values (1,42,'towel');

(note that uses a file that doesn't exist yet - foo.corm.sql).

And this is the example program itself (again, including a file that does not
exist yet).

  > cat example_foo.c

  #include <stdio.h>

  #include "isti.h"
  #include "isti_flow.h"
  #include "isti_sql.h"
  #include "isti_sqlite.h"

  #include "foo.corm.h"

  ISTI_SQL_AS(sql)
  ISTI_SQLITE_AS(sqlite)
  CORM_FOO_AS(corm_foo)

  int main()
  {
    STATUS;

    // Obtain a connection to the database.  This uses an SQLite database, but
    // the handle returned is a generic API.

    isti_db *db = NULL;
    CHECK(sqlite.connect(&db, "example_foo.db", 60));

    // Construct a selector.  This does the work of extracting a value from the
    // database.

    corm_foo_select *s = NULL;
    CHECK(corm_foo.select(db, &s));

    // Call the selector, filtering the database entries, and extracting a
    // single instance of the `foo` struct.

    foo *f = NULL;
    CHECK(s->where(s)->bar(s, 42)->and(s)->\
	_bra(s)->baz(s, "towel")->or(s)->baz(s, "fish")->_ket(s)->\
	one(s, &f));
    printf("found foo instance with bar=%d and baz=%s\n", f->bar, f->baz);

    EXIT;

    if (f) free(f);
    if (s) status = corm_foo.free(s, status);
    if (db) status = db->close(db, status);

    if (status) fprintf(stderr, "error: %d\n", status);

    RETURN;
  }

Now, when I run make:

  > make
  PYTHONPATH=../../cgen/src python ../../cgen/src/isti/cgen/run.py foo.h
  gcc -std=gnu99 -DHAVE_CONFIG_H -I. -I../..   -I../../clib/src  -g -O0 -MT
  foo.corm.o -MD -MP -MF .deps/foo.corm.Tpo -c -o foo.corm.o foo.corm.c
  mv -f .deps/foo.corm.Tpo .deps/foo.corm.Po
  gcc -std=gnu99 -DHAVE_CONFIG_H -I. -I../..   -I../../clib/src  -g -O0 -MT
  example_foo.o -MD -MP -MF .deps/example_foo.Tpo -c -o example_foo.o
  example_foo.c
  mv -f .deps/example_foo.Tpo .deps/example_foo.Po
  /bin/bash ../../libtool --tag=CC   --mode=link gcc -std=gnu99  -g -O0
  -L../../clib/src/.libs -o example_foo foo.corm.o example_foo.o -lcorm
  -lsqlite3 
  libtool: link: gcc -std=gnu99 -g -O0 -o .libs/example_foo foo.corm.o
  example_foo.o  -L../../clib/src/.libs
  /home/andrew/project/c-orm/hg/clib/src/.libs/libcorm.so
  /usr/lib/x86_64-linux-gnu/libsqlite3.so
  ./example_foo.sh

    to run the example from the example/src directory:
      LD_LIBRARY_PATH=../../clib/src/.libs ./example_foo

And let's follow the instructions:

  > LD_LIBRARY_PATH=../../clib/src/.libs ./example_foo
  select id,bar,baz from foo where bar=? and ( baz=? or baz=? )
  found foo instance with bar=42 and baz=towel

Yay!

Andrew

Updated Syntax

From: andrew cooke <andrew@...>

Date: Sun, 24 Feb 2013 02:01:53 -0300

I've changed the syntax for restricting queries.  As a consequence you have
several options:

This is verbose, but parameters are strongly typed and the compiler can detect
spelling mistakes.  "bra" and "ket" are particularly kludgy.

  s->_where(s)->bar(s, "=", 42)->_and(s)->\
  _bra(s)->baz(s, "like", "towel")->_or(s)->baz(s, "like", "fish")->_ket(s)->\
  _one(s, &f)

This is equivalentm, but much more compact and with much less checking:

  s->_(s ,"where bar=%d and (baz like %s or baz like %s)", 42, "towel", "fish")->\
  _one(s, &f)

For simple queries, the verbose approach has implicit "and":

  s->bar(s, "=", 42)->baz(s, "like", "towel")->_one(s, &f)

Literals are allowed anywhere:

  s->_(s, "where")->bar(s, "=", 42)->baz(s, "like", "towel")->\
  _(s, "limit 1")->_one(s, &f)

Andrew

Comment on this post