-
Notifications
You must be signed in to change notification settings - Fork 7
Querying the Database
All queries are executed by a jSQLQuery
object. This object allows you to refine a query, provide values to a prepared statement, get the results of a query, etc.
There are two ways to create any given query:
- Using the query constructor for the required query type (
jSQL.createTable
,jSQL.dropTable
,jSQL.select
,jSQL.update
,jSQL.deleteFrom
, orjSQL.insertInto
) - Using the SQL Parser (
jSQL.query
) Which will parse an SQL statement and return thejSQLQuery
object of the correct type.
Parse a raw query or prepared statement from an SQL string. This method understands a subset of standard SQL and returns a jSQLQuery
object.
- sqlQuery: A string. An SQL query to be parsed and executed on the database. jSQL understands a subset of standard SQL which is covered under the jSQL Syntax header.
- A
jSQLQuery
object of the appropriate type: "CREATE", "UPDATE", "SELECT", "INSERT", "DROP", or "DELETE".
jSQL.query('UPDATE `Users` SET Age = 0 WHERE Age <= 18').execute();
Create a query that can create and populate a new table in the database.
- params: An object containing one property, it's name is the name of the table being created, it's value is an array of column definition objects. Each definition object contains a property for the name of the column, the column type and the arguments for the column type. See the Fun with DataTypes section for more info about types.
-
keys: (optional) An array of objects representing primary keys to be applied to columns. Each object in the array must have a
column
and atype
property. Thecolumn
property is the column name (a string) [or column names (an array of strings) in the event of a compound key]. The type property is either "primary" for a primary key or "unique" for a unique key. A table may have only one primary key (compound or not), but many unique keys.
- A
jSQLQuery
object of type "CREATE".
jSQL.createTable({Users:[
{name: "id", type:"INT", args:[] },
{name: "Full_Name", type: "VARCHAR", args: [30]},
{name: "Age", type: "INT", args: []},
]}, [
{column: ["id", "Full_Name"], type: "primary"}
]).ifNotExists().execute();
jSQL.createTable({myTable: [
{ name: "ID", type: "INT", args: [], key: "primary", auto_increment: true },
{ name: "Name", type: "VARCHAR", args: [30] }
]})
jSQL.createTable({myTable: [
{ name: "ID", type: "INT", args: [] },
{ name: "Name", type: "VARCHAR", args: [30] }
]})
Create a query that can create and populate a table new table in the database.
Due to a bug noted in Issue #2 this signature has been removed as of version 1.4. Use jSQL.createTable(params)
instead.
- params: An object containing one property, it's name is the name of the table being created, it's value is an object that represent the column definitions. The column definition object contains a property for each column, it's name is the column type and it's value is the definition for that column, including a "type" parameter and an "args" parameter. See the Fun with DataTypes section for more info about types.
- A
jSQLQuery
object of type "CREATE".
jSQL.createTable({Users:{
Name: {type: "VARCHAR", args: [30]},
Age: {type: "INT", args: []},
}}).ifNotExists().execute();
Create a query that can create and populate a table new table in the database.
- name: The name of the new table (string).
- columnsOrData: Can be either 1) An array of column names for the new table. or 2) An array of objects whos properties represent table column names and their values are values to be inserted.
- columnTypes: (optional) An array of objects, one for each column, each containing a "type" property, for the column type, and an "args" property as an array of column type arguments.
- A
jSQLQuery
object of type "CREATE".
var users = [];
users.push({Name: 'Bob', Age: 34, PhoneNumber: "888-999-0000"});
users.push({Name: 'Susan', Age: 37, PhoneNumber: "888-111-0000"});
jSQL.createTable('Users', ['Name', 'Age', 'PhoneNumber']).execute(users);
Create a query that will gather a result set from a table to be fetched.
-
columns: A column name (string) or an array of column names, or the special string
*
which will return all columns in the table.
- A
jSQLQuery
object of type "SELECT".
var query = jSQL.select("*").from('Users').execute();
var users = query.fetchAll();
Create a query that will insert rows into a table.
- tableName: The name of the table to insert rows to.
- A
jSQLQuery
object of type "INSERT".
var query = jSQL.insertInto('Users').values({Name: 'Jill', Age: 25}).execute();
Create a query that will delete a table.
- tableName: The name of the table to delete.
- A
jSQLQuery
object of type "DROP".
var query = jSQL.dropTable('Users').execute();
Create a query that will alter records in a table.
- tableName: The name of the table to update.
- A
jSQLQuery
object of type "UPDATE".
jSQL.update('Users')
.set({Name:'Old Person'})
.where('Age')
.greaterThan(37)
.execute();
Create a query that will delete records in a table.
- tableName: The name of the table to delete records from.
- A
jSQLQuery
object of type "DELETE".
// Without a WHERE clause, this will truncate the table.
jSQL.deleteFrom(`Users`).execute();
jSQLTable.name
jSQLTable.columns
jSQLTable.data
jSQLTable.colmap
jSQLTable.renameColumn
jSQLTable.addColumn
jSQLTable.loadData
jSQLTable.insertRow
jSQLQuery.ifNotExists
jSQLQuery.ignore
jSQLQuery.execute
jSQLQuery.fetch
jSQLQuery.fetchAll
jSQLQuery.values
jSQLQuery.set
jSQLQuery.where
jSQLQuery.from
jSQLQuery.limit
jSQLQuery.orderBy
jSQLQuery.asc
jSQLQuery.desc
jSQLQuery.distinct
jSQLWhereClause.where
jSQLWhereClause.equals
jSQLWhereClause.preparedLike
jSQLWhereClause.doesNotEqual
jSQLWhereClause.lessThan
jSQLWhereClause.contains
jSQLWhereClause.endsWith
jSQLWhereClause.beginsWith
jSQLWhereClause.and
jSQLWhereClause.or
jSQLWhereClause.limit
jSQLWhereClause.orderBy
jSQLWhereClause.asc
jSQLWhereClause.desc
jSQLWhereClause.execute