docs.roxen.comView this page in a printer friendly mode
DocsRoxen2.2TutorialsDatabase Tutorial
Copyright  2004, Roxen Internet Software
Suggestions, comments & compliments

Building a Sample Database
The query() function
The big_query() function
SQL Syntax
Features Missing from MySQL
Insertion Syntax
The tablify Container
The Business Graphics Module
The emit and sqlquery Tags
Database Creation
Creating Tables


The condition part of the query, as shown in the "syntax" paragraph is a boolean expression, usually arbitrarily complex (old versions of MiniSQL have heavy limitations the syntax of this portion). Only rows that satisfy it will appear in the results set. If none does, the results set will be empty.

When evaluating the condition, column names are substituted with the data they contain, and operators are evaluated according to a well-specified grammar. Constants must be quoted according to their type.

Numeric Constants

Integer and floating-point numbers are not quoted. They can be told apart because floating-point numbers have the decimal separator (.). Usually the server's parser is quite lenient though, fixing types when possible according to the context.

Column Names

These are not quoted. Since they mustn't be ambiguous this poses a bit of limitations on column names. As a general rule, legal C variable names are legal column names (unless they are reserved words of course). SQL is a bit more lenient than C, so you should get a little more leeway.

String Constants and Quoting

Strings are quoted using the apostrophe symbol ('). If a string contains the literal apostrophe character, it must be escaped. Different escaping schemes are specified, the most usual ones being doubling it (i.e. 'It''s a shame') or prepending it with a backslash (i.e. 'It\'s a shame').

Let's obtain from our sample database the total area of Italy.

With RXML:

<emit source="sql" host="mysql://user:password@localhost/sample"
  query="select name, area_tot from ids, countries 
    where ids.code = and'Italy'>
&;'s total surface is &_.area_tot; sq. km.

Other Data Types

Other data types are usually represented as formatted string, which get interpreted by the server according to the context.

The LIKE Operator

This operator is used to do glob-like matching. It has the syntax value LIKE PATTERN where the value is usually a column, and the pattern a string literal, possibly containing two magic characters: '_' and '%', which act like glob characters '?' and '*', that is they match any (single) character, and any arbitrarily long sequence of any character. If what you're matching against contains the literal '_' or '%' characters, you can escape them prepending the backslash character '\'.

Let's try to find out the countries neighbouring Italy. The right way to do so would be looking in the 'boundaries' table. But a summary can be found in the countries.location text, and we'll use that.

With RXML:

<sqltable border=1 host="mysql://user:password@localhost/sample"
  query="select name from ids, countries 
         where and location like '%italy%'">

Notice that the "column like '%something%'" syntax (with leading and tailing globs) is very inefficient, and should be avoided whenever possible.

MySQL offers the more powerful REGEXP operator, with the syntax value REGEXP expression where the value is usually a column name or a function result, and expression is a string-quoted regular expression.

NULL Column Values

Some columns can be empty, or (in SQL terms) be NULL. To deal with them when selecting data, you use the 'IS' syntax, which takes the form value IS [NOT] NULL where value can be obtained from a a column (thus be a column name) or can be a constant value (of course it would be rather dumb to evaluate a constant expression, but you can of course do that if you wish).