Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
Hyper-Text SQLHarun Ćerim
Charles University, Prague 2018
Content
Introduction HTSQL
OverviewHTSQL Design
ECONOMIC
ANALYSIS
Conclusion and
ReferencesLorem ipsum dolor sit
amet, consectetur
adipiscing elit, sed do
eiusmod tempor
incididunt ut labore et
dolore magna aliqua.
1 432 5
Technical
Analysis
Introduction
Usual database aspect from the application:
- HTTP request is sent from a web browser to an application server.
- The server then processes the request and sends SQL command to the database.
- After execution, it processes the response and responds with the result set formatted as CSV, HTML, XML, JSON
or YAML.
Hyper-Text Structured Query Language is both a specification and an implementation of a "native" web query
language.
HTSQL works with modern web browsers and open source relational databases, such as PostgreSQL, SQLite and
MySQL.
HTSQL is open source software.
HTSQL Overview
A query language for the accidental programmer.
(HTSQL Creators)
HTSQL makes accessing data as easy as browsing the web.
An HTSQL processor translates web requests into relational database queries and returns the results in a form
ready for display or processing.
HTSQL Overview
HTSQL Overview
The HTSQL query processor does heavy lifting for you.
By doing graph to relational mapping on your behalf, HTSQL permits your time to be spent exploring
information instead of debugging.
HTSQL is designed to be broadly usable by semi-technical domain experts, or what creators of the language
call accidental programmers.
HTSQL Design
This section covers the design of the HTSQL language, explaining how various features of SQL are covered, why
particular decisions were made and what is their impact upon usability.
Quick demo before start:
We’ll use a fictional university that maintains a database for its
student enrollment system. There are four tables that describe the
business units of the university and their relationship to the courses
offered.
Demo url: demo.htsql.org.
HTSQL requests start with a forward-slash /. To return all rows and
columns from the school table, sorted by primary key, write:
/school.
SQL query:
SELECT p.*
FROM project p
ORDER BY p.proj_id
HTSQL Design
1. Character Set, Escaping & Strings
Following SQL's lead, HTSQL has two kinds of strings: literal values and catalog identifiers.
Literal strings in HTSQL are single-quoted, doubling the single quote when one occurs in a value, 'O''Reilly‘.
HTSQL Design
2. Predicate Expressions
Query parameters in HTSQL emulate the HTML FORM tradition in that the ampersand (&) signifies
conjunction and the equal sign (=) signifies equality.
Following the "C" language tradition, HTSQL adds the vertical bar (|) for alternation, the exclamation mark
(!) for negation, and parentheses to indicate scoping.
POSIX regular expressions are specified via tilde (~).
HTSQL Design
2. Predicate Expressions
Example 1
Filtered List of Tasks:
HTSQL URIs do not follow HTML FORM semantics.
However, they can be constructed with JavaScript and submitted via XMLHTTPRequest.
HTSQL Design
3. Join Specifiers
In HTSQL, a specifier is a reference to a column of the current table, or it is a series of links to a related table
or to a column in a related table.
These links are delimited by the period (.), where each period represents an SQL join to be constructed.
HTSQL Design
3. Join Specifiers
Example 2
Filtering by a Specifier:
Example 3
Projects Filtered by Aggregate:
HTSQL Design
4. Column Selectors and Projections
In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}.
If an expression has a trailing plus (+) or minus (-), then the sort is ascending or descending, respectively,
on that expression.
Aggregate expressions are indicated in a column selector with a vertical bar (|).
Expressions before the vertical bar are selected and used in the corresponding GROUP BY clause.
We call the vertical bar projection.
HTSQL Design
4. Column Selectors and Projections
Example 4
Selecting Aggregate Expressions:
HTSQL Design
5. Row Locators
Since an HTSQL processor is configured to use primary key constraints, naming the columns is redundant.
Therefore, a request such as task?proj_id='MEYERS'&task_no=1 can be shortened significantly to
task[MEYERS.1].
This locator syntax is inspired by the domain name system (DNS).
HTSQL Design
5. Row Locators
Example 5
Locating rows via Primary Key:
When a table's primary key contains a foreign-key
reference to another table, then those columns are not
included in the locator.
The referenced table‘s locator is used.
HTSQL Design
6. Path Segments
Relative URIs, indicated with the forward-slash (/).
Filter expressions in a path segment context are indicated by the semi-colon(;) instead of the question
mark.
When two or more path segments are used, they must be connectable via a primary link, where the
descendant's primary key contains a reference to a candidate key of the parent table.
HTSQL Design
6. Path Segments
Example 6
Joining via Path Segments: This syntax has optional join modifiers.
If a segment's table is prefixed by "(+)" then an
OUTERJOIN is used.
If "(*)" is indicated, the tables are not joined and a cross
product is performed instead.
HTSQL Design
7. Commands, Assignment, and Lookups
The default operation for an HTSQL request is the select() command without arguments, corresponding to
an SQL SELECT statement.
There are also insert(),update(), and delete(), among others.
Example 7
Insert Statement: To support data modification, the assignment
operator (:=) is used.
In an insert() command, locators imply assignment
rather than a test for equality.
HTSQL Design
7. Commands, Assignment, and Lookups
Example 8
Update Statement:
Example 9
Assignment via Lookup:
HTSQL provides a mechanism to make assignments by specifier, which then affects the foreign key columns
of the corresponding link by using lookup operator (@).
HTSQL Design
8. Users, Roles, and Conflicts
The user ‘s role can be set via HTSQL URI using a tilde in the first path segment, such as /~clerk/project.
This causes SET ROLE 'clerk‘ be performed before any query is executed.
HTSQL Design
9. Types and NULLs
HTSQL has a powerful type system, designed to be intuitive for casual users.
Automatic type conversion only occurs in the presence of boolean operators (including the implicit top
level conjunction).
To facilitate NULL comparison, HTSQL employs a doubled equal sign (==) to mean SQL99 IS NOT
DISTINCT FROM operator.
HTSQL Design
10. Namespaces and Objects
Namespaces in HTSQL follow XML's lead, using the colon (:) as a delimiter.
HTSQL support methods and attributes on objects.
Calling SQL‘s CHARACTER_LENGTH function is accomplished through a length() method on a text column,
e.g., project{proj_id,name.length()}.
In HTSQL, ARRAYs are constructed using array(), and ROWs through row().
HTSQL Design
11. Output Formats
HTSQL result sets are returned in industry-standard formats, including TXT, XML, CSV, JSON, HTML, and
YAML.
Calling /task.xml will return the result in xml format.
Technical Analysis
Basic Expressions
Demo: http://demo.htsql.org
To return all rows and columns from the
school table, sorted by primary key we
append this to the demo url: /school
Result:
To cound the number of rows inside the
school table we use cound function and
write this: /count(school)
Result:
Technical Analysis
Choosing Columns
Demo: http://demo.htsql.org
To output more than one column:
/{count(school), count(program), count(department)}
Result:
To choose columns to display:
/program{school_code, code, title}
Result:
Technical Analysis
Linking Data
Demo: http://demo.htsql.org
It is possible to link data through several relationships.
Write:
/course{department.school.name, department.name, title}
Result:
For cases where you don’t wish to specify each
column explicitly, use the wildcard * selector.
Write:
/department{*, school.*}
Result:
Technical Analysis
Filtering Data
Demo: http://demo.htsql.org
Use the filter operator ? to show only data that satisfies
some criteria.
Write:
/department?school_code='eng’
Result:
Filters can be combined with selectors and links.
Write:
/course{department_code, no, title}
?credits<3&department.school.code='ns’
Result:
Technical Analysis
Sorting & Truncating
Demo: http://demo.htsql.org
We can sort by multiple columns.
Write:
/course.sort(department_code+, credits-)
Result:
To list a range of rows, the limit() function takes one
or two arguments. The first argument is the number
of rows to return, the optional second argument is
the starting offset.
Write:
/program.limit(5,10)
Result:
Technical Analysis
Formatting Output
Demo: http://demo.htsql.org
By default, HTSQL tries to guess the desired output
format depending upon the browser or the tool used to
make the request which can be overriden.
Write:
/school/:json
Result:
Technical Analysis
Combination of previously mentioned expressions
Demo: http://demo.htsql.org
Write:
/department{school.name, name, count(course)-}.filter(school.name~‘art‘).limit(5)/:csv
Result:
Technical Analysis
Relating and Aggregating Data
HTSQL distinguishes between singular and plural relationships to simplify query construction.
By requiring aggregates for plural expressions, HTSQL reduces query construction time and reduces errors.
Technical Analysis
Basic Linking
One-to-many relationships are the primary
building block of relational structures.
Write:
/course{department.name, title}
Result:
Technical Analysis
Aggregate Expressions
Demo: http://demo.htsql.org
Filters may be used within an aggregate expression.
Write:
/department{name, count(course?no>=400)}
Result:
Numerical aggregates are supported.
Write:
/department{code, min(course.credits),
max(course.credits)}
Result:
Technical Analysis
Aggregate Expressions
Demo: http://demo.htsql.org
Filtering can be done on one column, with
aggregation on another.
Write:
/department{name, avg((course?no>400).credits)}
Result:
The every aggregate tests that a predicate is true for
every row in the correlated set.
Write:
/department{name, avg(course.credits)}
?every(course.credits=3)
Result:
Technical Analysis
Calculations
Demo: http://demo.htsql.org
It is possible to avoid the duplication of count method by defining a calculated
attribute num_dept.
Write:
/school.define(num_dept:=count(department))
{name, num_dept}? num_dept>3
Result:
As syntax sugar, you could combine definition and selection and
write:
/school{name, num_dept:=count(department)}? num_dept>3
The output would be the same.
Technical Analysis
Parameterized Calculations
Demo: http://demo.htsql.org
Write:
/department.define(freshman :=
course?no>=100&no<200,
sophomore :=
course?no>=200&no<300,
junior := course?no>=300&no<400,
senior := course?no>=400&no<500,
stats(set) := {count(set),
max(set.credits),
min(set.credits),
avg(set.credits)})
{name, stats(freshman),
stats(sophomore),
stats(junior),
stats(senior)}
Result:
Technical Analysis
Argument References
To overcome the limitation of scopes, we mark
level with a dollar sign to indicate that searched
object can be referenced from nested scopes.
Write:
/department.define(course($level) :=
course?no>=$level*100&no<($level+1)*100)
{name, count(course(1)),
count(course(2)),
count(course(3)),
count(course(4))}
Technical Analysis
Logical Expressions
A filter refines results by including or excluding data by specific criteria.
Logical expressions include reviews comparison operators, boolean expressions, and NULL handling.
If you’re not sure of the exact department name, use the case-insensitive contains operator (~).
Write:
/department?name~'engineering‘
Result:
When you know the exact name (=) is of course the
option.
Technical Analysis
Boolean Expressions
HTSQL uses function notation for constants such as true(), false() and null().
The is_null() function returns true() if it’s operand is null().
Write:
/department{code, name}?is_null(school_code)
Result:
Technical Analysis
Defined References
Demo: http://demo.htsql.org
References are not limited to parameters of
calculations, they could be defined separately.
Write:
define($avg_credits := avg(course.credits))
.course?credits>$avg_credits
The same result using where:
/course?credits>$avg_credits
:where $avg_credits := avg(course.credits)
Result:
Conclusion
Starting with an initial vision of a coherent URI-to-SQL translator over a decade
ago, HTSQL has matured in numerous ways and has proven itself to be a
valuable component in a rapid web application development architecture.
Beyond what was discussed above, HTSQL has facilities for details like
transactions, as well as pluggable commands for things like mail merging and
label generation. HTSQL has made development faster and more reliable than
previous methods and facilitates reusable modules and database schemas.
References
● HTSQL Documentation:
http://htsql.org/doc/
● Clark C. Evans
HTSQL -- a "Native" Web Query Language
Thank You