Getting started
To add it into your webpage you can use:
Javascript Docs
IndexSQL
Class that represents a single IndexSQL database
Parameters
dbName
String The name of the database you want to open.
execute
It executes several IndexSQL queries.
Parameters
query
String A string containing the queries you want to execute, every query has to finish with a semicolon ";". For additional information about the use of queries visit https://dandimrod.github.io/IndexSQL/docs/#SQL-syntaxcallback
dbQueryCallback Callback with the response after the queries are executed.
drop
It drops the IndexSQL database. Afterwards the object is unusable and a new IndexSQL object has to be created.
backup
It backs up the database by returning the whole Javascript object that contanins the database.
Parameters
callback
dbBackupCallback Callback with the backup.
restore
It restores an old backup of the database. Warning! The system wont perform any checks on this backup. An incorrect restore will make the library fail.
Parameters
backup
Object The backup of the whole database.
&& a.datatype === "NUMBER"
&& a.datatype === "NUMBER"
queryResult
The result of a IndexSQL query. It will contain at least one of these properties.
Type: Object
Properties
message
String? A message, it usually means that the query has been executed sucessfully.warn
String? A warn, it means an error was detected, but it won't stop a transaction. Example: Dropping a table that does not exist.error
String? An error, it means that the query was erroneus, it will stop a transaction.result
tableResult? It represents a table generated as a result of a query.
dbQueryCallback
Callback for queries execution.
Type: Function
Parameters
results
Array<queryResult> An array with the results of the queries.
tableResultHeader
The headers of a result table
Type: Object
Properties
name
String Name of the header.datatype
String The datatype of the header.constraints
string The constraints of the header, separed by commas ",".
tableResult
The result table of a query.
Type: Object
Properties
header
Array<tableResultHeader> It is a list of the headers of this table and it's properties.values
Array<Array> It is a list of the rows returned by the query, it consists of a list of the values with the same order as the header.
dbBackupCallback
Callback for backup execution.
Type: Function
Parameters
backup
Object The backup of the whole database.
IndexSQL language
(The symbols < and > means the parameter is optional)
CREATE TABLE
Syntax: “CREATE TABLE table_name (
column1 datatype <constraints>,
column2 datatype <constraints>,
....
PRIMARY KEY (columnName),
< FOREIGN KEY (columnName) REFERENCES
foreignTableName(foreignTableColumn)>
);”
It creates the table table_name . It will contain the columns, with the datatypes and constraints specified, the constraints are optional, and they are:
- NOT_NULL The column value won’t be null
- UNIQUE The column value won’t be repeated
- DEFAULT If the column value is not specified, it will be the default
- AUTO_INCREMENT If the column value is not specified, it will be the the next one, only available with datatype number.
The possible datatypes are:
- NUMBER
- STRING
- BOOLEAN
IndexSQL will accept other SQL datatypes like VARCHAR but it will assign them one of the primitive datatypes and it won’t check the length of the columns data.
The primary key is a column that will act as the key to the whole column. It will be NOT_NULL and UNIQUE by default.
The foreign keys will be a series of keys that are related to another column in another table. It means that during the insertion, it will check if the value of the column exists on the foreign table.
DROP TABLE
Syntax: “DROP TABLE table_name;”
It will delete the table_name table and all their values. This action cannot be reverted.
TRUNCATE TABLE
Syntax: “DROP TRUNCATE table_name;”
It will delete all the values of the table_name table and leave it clean. This action cannot be reverted.
TABLES
Syntax: “TABLES;”
It will return a list with all the tables of the system.
INSERT INTO
Syntax:” INSERT INTO table_name <(column1, column2, column3, ...)>
VALUES (value1, value2, value3, ...);”
It will insert the values into the table_name. The columns are optional, if they are not specified, it will take the values in the order the table was originally created.
If the columns contain the primary key, and the primary key matches any other primary key of the table. It will rewrite the contents.
SELECT
Syntax:”SELECT <DISTINCT> column1, column2, ... FROM table_name <WHERE condition> <ORDER BY column1 ASC|DESC, column2 ASC|DESC, ... ;>”
It will return the values selected. To select all columns, you can write an asterisk (*) instead.
The DISTINCT parameter will make any rows that are the same to collapse into one.
The WHERE and ORDER BY statements are optional.
To see more about the WHERE statement, check this part of the documentation .
The ORDER BY will order the columns by the list provided, ASC means ascendant and DESC is descendant. If it’s not specified it will be ASC. If the columns are the same, it will check the next column in the list.
UPDATE
Syntax:” UPDATE table_name SET column1 = value1, column2 = value2, ... <WHERE condition;>”
It will update the columns with the new values provided.
The WHERE statement is optional, if it is not specified, it will update all the rows of the table.
To see more about the WHERE statement, check this part of the documentation .
DELETE
Syntax:” DELETE FROM table_name <WHERE condition>;
It will delete the columns.
The WHERE statement is optional, if it is not specified, it will delete all the rows of the table.
To see more about the WHERE statement, check this part of the documentation .
START TRANSACTION
Syntax: “START TRANSACTION;”
It starts a transaction, any operation performed during the transaction won’t be recorded into the database. Any errors encountered during a transaction will halt the execution of all the queries, including the ones outside the transaction.
END TRANSACTION
Syntax: ”END TRANSACTION:”
It ends a transaction, committing all the changes made during the transaction to the database.
The WHERE statement
Syntax: ”WHERE condition”
It will match the columns that check the conditions. It supports a series of operations:
Operator |
Description |
= |
Equal |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal |
<= |
Less than or equal |
<> |
Not equal. |
AND |
Logic and |
OR |
Logic or |
NOT |
Logic not |
TRUE |
Logic true |
FALSE |
Logic false |
IS |
Similar to = |
NULL |
Matches empty values |
() |
Used to separate between conditions |
The following are some examples of WHERE statements:
- WHERE Age = 1
- WHERE Name = “Pipo”
- WHERE IsMale = TRUE
- WHERE Age > 5
- WHERE NOT (Age > 5 AND IsMale = TRUE)
SQL Coverage
From the standard SQL syntaxis, IndexSQL covers:
- [x] SQL Transactions
- [x] DB operations
- [x] CREATE TABLE Statement
- [x] DROP TABLE Statement
- [ ] ALTER TABLE Statement
- [x] SQL Constraints
- [x] SELECT
- [x] SELECT DISTINCT
- [x] WHERE
- [x] AND, OR and NOT Operators
- [x] ORDER BY Keyword
- [x] INSERT INTO
- [x] NULL Values
- [x] UPDATE Statement
- [x] DELETE Statement
- [ ] TOP, LIMIT or ROWNUM Clause
- [ ] MIN() and MAX() Functions
- [ ] COUNT(), AVG() and SUM() Functions
- [ ] LIKE Operator
- [ ] SQL Wildcards
- [ ] IN Operator
- [ ] BETWEEN Operator
- [ ] SQL Aliases
- [ ] SQL Joins
- [ ] INNER JOIN Keyword
- [ ] LEFT JOIN Keyword
- [ ] RIGHT JOIN Keyword
- [ ] FULL OUTER JOIN Keyword
- [ ] Self JOIN
- [ ] UNION Operator
- [ ] GROUP BY Statement
- [ ] HAVING Clause
- [ ] EXISTS Operator
- [ ] ANY and ALL Operators
- [ ] SELECT INTO Statement
- [ ] INSERT INTO SELECT Statement
- [ ] INSERT INTO SELECT Statement
- [ ] CASE Statement
- [ ] NULL Functions