entities using an ORM library for your application ◦ SQLAlchemy (Python) ◦ ActiveRecord (Rails) • Takes care of data retrieval and persistence • Great, all done...nothing more to do here? 7 Application runtime Models ORM Backend DB Driver
done...nothing more to do here? ◦ No! • There will be a point in your life, where you need to break out and write som raw SQL strings ◦ Not long passes before we have a growing number of string literals in our code 8 Application runtime Models ORM Backend DB Driver
Prepares a ready to execute functools.partial object for you • No query strings dotted all over source files • A light wrapper around your DB Driver • Genesis from YeSQL 9 In source control! queries.sql Application runtime Whatever you want DB Driver Source Control
messages WHERE 1=1 AND id = %(id_low)s OR id = %(id_high)s AND message = %(msg)s; sql = sqlpy.Queries('queries.sql') .... args = {'id_low': 1} results = sql.BUILT_SQL_QUERY(cur, args) SELECT * FROM messages WHERE 1=1 AND id = 1; id message next_message 1 hello there 2 SQLpy NULL 3 databases rule! 4 hello friend Query still passes through the DB2 .0 API driver sanitiser for safe execution
writing a large web application ◦ Naive enough to (basically) ignore convention of an ORM ◦ Just starting to get enough Python experience to be a danger to myself and others ◦ I knew SQL, I liked SQL….I wanted SQL ◦ ...so I went looking and found, YeSQL….and then started Pythoning • I just made something which helped me at the start ◦ As I found limitations and wanted it to do more, it developed further • Using SQL directly is good in many ways ◦ It is a high performance machine...why not use it? ◦ Make use of advanced data structures ◦ Do your computation where the data is ◦ Enforce business rules 11 Thi t ra f le s il r
made Python the interface language of choice • But the glue has been clunky bash scripts, python scripts, CSV dumps and loads • Why not use use Python all the way through and really integrate your systems and pipelines with one universal language? 12 Database Object Store --> Log Streaming --> Distributed Computation
datastores are cloud native, distributed and big in scale • Are converging on SQL as their data modelling language, BUT No traditional relational Database ORM support • All provide Python libraries that expose their SQL language interfaces ◦ kSQL (kafka), sparkSQL (Apache Spark), BigQuery (Google Cloud), Athena (AWS S3) 13 SQL SQL SQL SQL SQL
no opinion on the backend datastore interface mechanism, it just expects a DB 2.0 API compliant cursor object ◦ Make an object defining execute, fetchone, fetchall etc… methods 14 https://github.com/HUSSTECH/talks-code/tree/master/party-like-its-ansi-1999