Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I have something similar, that I think "goes further" than this, which I use as part of my Python Cydia backends. I got extremely addicted to using the moral equivalent of TCL's uplevel, inspect.currentframe(-...), to build interfaces in Python where I don't have to pull out parameters. My "cyql" interface (which is barely anything, really: just a thin wrapper for psycopg2, which is itself excellent; in Clojure I wrap a slightly modified copy of the PostgreSQL JDBC driver to provide an even better implementation that does "compile"-time type SQL statement verification), uses this to allow me to remove what feels like all of the boilerplate.

http://gitweb.saurik.com/cyql.git

In addition to .run (which returns the number of affected rows) and .all (which returns an array of ordered dicts?), I have .has (which returns a bool and wraps an exists query), .gen (which returns a generator and iterates a cursor), and .one (which verifies you only got back a single row and returns just that row). I also have easy support for transactions (either on an existing connection or in one line as part of making a connection), easily turning off synchronous commit (for log tables), and I carefully manage autocommit to make certain that I am using the minimum number of possible SQL statements to the server (which I care about a lot).

http://gitweb.saurik.com/cyql.git/blob/HEAD:/__init__.py

That said, I agree with the post by masklinn below: neither the linked library nor my library are solving problems that I believe are worth inheriting code from someone else over. There is some basic configuration of psycopg2 which is necessary, but the underlying library itself is what works here. I have spent over a decade thinking about how I like to build SQL interfaces, and have now implemented a similar interface for myself in numerous languages, each time evolving the design slightly (and sometimes having enough of an epiphany that I go back and retrofit some of the older ones), but it ends up being built around the way I think about stuff.

https://news.ycombinator.com/item?id=11053877

And that also means that as I learn more and "level up", I start making different decisions. My implementation in Clojure stresses stored procedures a lot more, as while it took me a long time to really figure out how to use them in my workflow, I now see them as exceedingly correct and feel a lot of the code I've written in the past where I had tons of free statements is essentially "what I wrote from back when I didn't know how to use the database to organize my API layer" (though I still haven't worked out some of the tooling around shifting to stored procedures, and have been distracted with other higher-level problems the last couple years).

Essentially, I'm arguing that the same will happen to you. Put differently: some problems are hard, and some problems are easy; I find a lot of libraries that seem to be solving easy problems that new developers think are hard, and a lot of libraries that pretend to solve a hard problem, but only because the problem looked easy and the result doesn't actually work (such as the PostgreSQL drivers that were available in Ruby for a long time, which were all unusably bad). Wrapping something that works well so it is slightly easier for you to use can be valuable if it is upstreamed into the original project, but even then is likely to be something you will paper over yourself in time as you will think about the problem differently than they did.

    # at the top of the code somewhere
    dsn = {'port': ..., 'user': '...', 'password': '...', 'database': '...'}
    
    with cyql.connect(dsn) as sql:
        provider, account, key = sql.one('''
            select
                "payment"."provider",
                "payment"."account",
                "payment"."transaction"
            from "cydia"."payment"
            where
                "payment"."id" = %(payment_id)s
        ''')
    
    with cyql.connect(dsn) as sql:
        sql.run('''
            update "cydia"."token" set
                "token" = %(token)s,
                "email" = %(email)s,
                "country" = %(country)s,
                "shipping" = %(shipping)s,
                "billing" = %(billing)s,
                "data" = %(data)s
            where
                "id" = %(token_id)s and
                "token" is null
        ''')


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: