44
Hyper-Text SQL Harun Ćerim Charles University, Prague 2018

Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

Hyper-Text SQLHarun Ćerim

Charles University, Prague 2018

Page 2: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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

Page 3: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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.

Page 4: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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.

Page 5: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

HTSQL Overview

Page 6: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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.

Page 7: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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

Page 8: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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‘.

Page 9: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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 (~).

Page 10: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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.

Page 11: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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.

Page 12: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

HTSQL Design

3. Join Specifiers

Example 2

Filtering by a Specifier:

Example 3

Projects Filtered by Aggregate:

Page 13: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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.

Page 14: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

HTSQL Design

4. Column Selectors and Projections

Example 4

Selecting Aggregate Expressions:

Page 15: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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).

Page 16: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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.

Page 17: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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.

Page 18: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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.

Page 19: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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.

Page 20: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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 (@).

Page 21: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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.

Page 22: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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.

Page 23: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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().

Page 24: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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.

Page 25: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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:

Page 26: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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:

Page 27: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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:

Page 28: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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:

Page 29: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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:

Page 30: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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:

Page 31: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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:

Page 32: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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.

Page 33: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

Technical Analysis

Basic Linking

One-to-many relationships are the primary

building block of relational structures.

Write:

/course{department.name, title}

Result:

Page 34: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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:

Page 35: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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:

Page 36: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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.

Page 37: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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:

Page 38: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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))}

Page 39: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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.

Page 40: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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:

Page 41: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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:

Page 42: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

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.

Page 43: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

References

● HTSQL Documentation:

http://htsql.org/doc/

● Clark C. Evans

HTSQL -- a "Native" Web Query Language

Page 44: Hyper-Text SQLpokorny/dj/prezentace/3_142.pdf · In HTSQL, result-set column selection is accomplished using curly braces, for example, project{name,title}. If an expression has a

Thank You