-
Notifications
You must be signed in to change notification settings - Fork 7
jSQLquery interface
This interface is the heart of jSQL and handles all database operations. There are 6 classes that implement this interface, depending on the query's type
property. Query types are "CREATE", "UPDATE", "SELECT", "INSERT", "DROP", and "DELETE".
While all the methods of this interface are available in all of the classes that implement it, some of these methods only apply to certain types and if building a query, these methods should be called in logical order.
For example, in SQL, you would NOT say UPDATE USERS WHERE B = 3 SET C = 4
. Likewise, in jSQL you wouldn't say jSQL.update(
users).where('B').equals(3).set({C:4})
The correct SQL syntax is UPDATE USERS SET C = 4 WHERE B = 3
and likewise, the correct jSQL syntax is jSQL.update(
users).set({C:4}).where('B').equals(3)
.
Used for "CREATE" queries to set a flag that will prevent overwriting this table if it already exists.
- The
jSQLQuery
object.
jSQL.createTable({myTable: [
{ name: "ID", type: "INT", args: [] },
{ name: "Name", type: "VARCHAR", args: [30] }
]}).ifNotExists().execute();
Used for "INSERT" queries to set a flag that will prevent an error from being thrown on a key violation.
- The
jSQLQuery
object.
jSQL.insertInto('myTable').values({ID:0, Name:'Bob'}).ignore().execute();
Used for ALL query types. This function executes the query, setting the result set, if any, and performing the operation on the tables.
- preparedVals: Queries may substitute a question mark for a given value to create a prepared statement, when used, the question marks are replaced with the values provided in this array, in order.
- The
jSQLQuery
object.
jSQL.query('UPDATE Users SET Name = ?').execute(['Frank']);
Used for "SELECT" queries to return the first row in the result set.
- mode: This can be either "ASSOC" or "ARRAY" and defaults to "ASSOC". When "ARRAY" is provided, this function will return a flat array, else it will return the result as an object with column name keys.
- An array or object of the first result in the query's result set.
var query =
jSQL.select('*')
.from('Users')
.where('Name')
.equals('Frank')
.execute();
var Frank = query.fetch('ASSOC');
Used for select queries to return the all rows in the result set.
- mode: This can be either "ASSOC" or "ARRAY" and defaults to "ASSOC". When "ARRAY" is provided, this function will return an array of flat array, else it will return the result as an array of objects with column name keys.
- An array containing all records in the result set.
var sql = 'SELECT `Name`, `Age` FROM `Users` WHERE `Age` > 32';
var query = jSQL.query(sql);
query.execute();
var oldPeople = query.fetchAll('ASSOC');
Used for "INSERT" queries to set the values to be inserted into the newly created row.
- data: May be either a flat array of values or an object with column names as keys.
- The
jSQLQuery
object.
var q = jSQL.insertInto('Users').values({Name: "?", Age: "?"});
q.execute(['Susan', 37]);
Used for "UPDATE" queries to set the columns and values to be altered.
- data: An object with column names as keys.
- The
jSQLQuery
object.
var q = jSQL.insertInto('Users').values({Name: "?", Age: "?"});
q.execute(['Susan', 37]);
Used for "DELETE", "SELECT", and "UPDATE" queries to refine the result set, or the set of results to be altered or deleted.
- column: The first column name to add a condition to.
- The
jSQLWhereClause
object that belongs to the currentjSQLQuery
object.
jSQL.deleteFrom('Users').where('Age').greaterThan(37).execute();
Used for "DELETE", "SELECT", and "UPDATE" queries to limit the result set, or the set of results to be altered or deleted.
- limit: The maximum number of results allowed in the set.
- offset: The index at which to start the result set.
- The
jSQLWhereClause
object that belongs to the currentjSQLQuery
object.
jSQL.deleteFrom('Users').where('Age').greaterThan(37).limit(2, 7).execute();
Used for "DELETE", "SELECT", and "UPDATE" queries to sort the result set, or the set of results to be altered or deleted.
- column: The column or columns to order results by.
- The
jSQLWhereClause
object that belongs to the currentjSQLQuery
object.
jSQL.deleteFrom('Users').where('Age').greaterThan(37).orderBy('Age').asc().limit(2, 7).execute();
Used for "DELETE", "SELECT", and "UPDATE" queries to sort the result set, or the set of results to be altered or deleted, in an ASCENDING order.
- The
jSQLWhereClause
object that belongs to the currentjSQLQuery
object.
jSQL.deleteFrom('Users').where('Age').greaterThan(37).orderBy('Age').asc().limit(2, 7).execute();
Used for "DELETE", "SELECT", and "UPDATE" queries to sort the result set, or the set of results to be altered or deleted, in a DESCENDING order.
- The
jSQLWhereClause
object that belongs to the currentjSQLQuery
object.
jSQL.deleteFrom('Users').where('Age').greaterThan(37).orderBy('Age').desc().limit(2, 7).execute();
Used for "DELETE", "SELECT", and "UPDATE" queries to ensure all rows in the result set are unique.
- The
jSQLWhereClause
object that belongs to the currentjSQLQuery
object.
jSQL.select("*").distinct().from("myTable").execute();
Used for ""SELECT" queries to define the table from which to pull the results.
- table: The table from which to pull the results.
- The
jSQLQuery
object.
var allUsers = jSQL.select('*').from('Users').execute().fetchAll();
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