HTSQL is a high-level navigational query language for relational databases. It is written by Kirill Simonov and Clark Evans in the Python language. HTSQL works with SQLite, PostgreSQL, MySQL, Oracle, and Microsoft SQL Server.
Presenter: | Clark C. Evans, Prometheus Research, LLC |
---|---|
Date: | March 12th, 2011 |
Location: | PyCon US 2011, Atlanta, GA |
For a university database, the following HTSQL request returns returns schools, and for each school the number of corresponding programs and departments (link):
/school{name, count(program), count(department)}
The un-optimized SQL equivalent is:
SELECT sch.name, (SELECT COUNT(1) FROM program WHERE school = sch.code), (SELECT COUNT(1) FROM department WHERE school = sch.code) FROM school AS sch ORDER BY sch.code ASC
/school{name, count(program), count(department)} optimally translates into:
SELECT school.name, coalesce(correlated_department.count,0), coalesce(correlated_program.count,0), FROM school as school LEFT OUTER JOIN ( SELECT school, COUNT(1) as count FROM department GROUP BY school ) AS correlated_department ON (correlated_department.school = school.code) LEFT OUTER JOIN ( SELECT school, COUNT(1) as count FROM program GROUP BY school ) AS correlated_program ON (correlated_program.school = school.code) ORDER BY school.code ASC
HTSQL can be used either as a WSGI component, or directly as a data query tool within Python.
>>> from htsql import HTSQL >>> from htsql.request import produce >>> htsql = HTSQL('pgsql:htsql_regress') >>> with htsql: ... for row in produce('/school'): ... print row ...
<body> <h3>Select a School</h3> <select id="school" data-htsql="/school{code, name}?exists(department)"> </select> <h3>Departments</h3> <p>Filter by name: <input id="dept_name"/></p> <table id="department" data-htsql="/department{code, name, school.name} ?school=$school&name~$dept_name"></table> <p>The number of courses in this department: <strong data-htsql="/department{count(course)} ?code=$department"></strong> </p> <h3>Courses</h3> <table id="course" data-htsql="/course?department=$department"></table> </body>
HTSQL is...
HTSQL processor takes a URL, translates it into SQL, executes the query against the relational backend, and returns the formatted result.
HTSQL wraps your database with a comprehensive, documented web service API.
HTSQL is a new kind of query language; although implemented as SQL -- it isn't relational
HTSQL is a full-blown compiler targeting SQL variants from various DBMS implementations.
Let's assume we have a data model, with schools, departments, programs and courses. Here it is:
+-------------+ +--------+ /---m| DEPARTMENT |>-----o| SCHOOL |m----\ |. +-------------+ . +--------+ .| | . . . | | department department a school | | offers may be part has one or | | courses of school programs | | | | +-------------+ +---------+ | \---<| COURSE | | PROGRAM |>---/ +-------------+ +---------+
An example (E1):
/course{department+, no, credits-, title}
An example (E2):
/department{school.name, *}
For example (E3):
/course?credits<3&department.school='ns'
An example (E4):
/school{code, avg(department.count(course))}
An example (E5):
/(program^degree){*,count(^)}
An example (E6):
/school/:txt
An example (E7):
/department.sort(school).limit(10,5)
An example (E8):
/school{code, /department, /program}
HTSQL is quite usable currently, but it may have gaps for a given application. Particular items we'll be addressing in coming months are:
Open Community
Dual-License & Support
Please visit our community site, http://htsql.org, our commercial site http://htsql.com, we are best found at #htsql on freenode. The source code is freely available at http://bitbucket.org/prometheus/htsql
Generous support for HTSQL was provided by Prometheus Research, LLC and The Simons Foundation. This material is also based upon work supported by the National Science Foundation under Grant #0944460.
Social Data Set Sharing (1/2)