Defines the cw.database library, the server's query builder and connection layer for SQLite and MySQL.

Query objects made with cw.database:Select, Insert, Update, Delete, Create, Drop and Truncate are filled in with Where, Limit, Callback and similar methods and sent with Execute; EasyRead and EasyWrite cover the common select and update-or-insert cases. cw.database:Connect uses the module named in cw.database.Module (sqlite by default, mysqloo for MySQLOO 9; the tmysql4 path is kept but untested), queues queries until the connection is ready and retries a failed MySQLOO connection after 30 seconds. Once connected, OnConnected creates the bans, players and characters tables and runs the DatabaseConnected hook.

Summary

Methods

Sets the function that receives the result once the query has run.
Adds a column definition to a CREATE query.
Escapes a value for use inside a quoted SQL string with the current database module.
Builds the SQL for the query and sends it to the database.
Changes the table the query acts on.
Sets a column value for an INSERT query.
Limits how many rows a SELECT or DELETE query affects.
Creates a query object for a table.
Sets how many matching rows a SELECT query skips.
Sorts the results of a SELECT query by a column, lowest first.
Sorts the results of a SELECT query by a column, highest first.
Sets the primary key column of a CREATE query.
Adds a column to the list a SELECT query returns.
Sets a column value for an UPDATE query.
Adds a key = value condition to the query's WHERE clause.
Adds a key = value condition to the query's WHERE clause.
Adds a key > value condition to the query's WHERE clause.
Adds a key >= value condition to the query's WHERE clause.
Adds a key LIKE value condition to the query's WHERE clause.
Adds a key < value condition to the query's WHERE clause.
Adds a key <= value condition to the query's WHERE clause.
Adds a key != value condition to the query's WHERE clause.
Adds a key NOT LIKE value condition to the query's WHERE clause.

Methods

QUERY_CLASS:Callback(queryCallback)

server gamemodes/catwork/gamemode/core/libraries/sv_database.lua:234

Sets the function that receives the result once the query has run.

Errors in the callback are caught and printed. The callback is not called when the query fails.

Parameters

  • queryCallback Function Called as queryCallback(result, status, lastID): the list of row maps (or nil for queries without rows), true, and the last inserted ID for INSERT queries

Adds a column definition to a CREATE query.

With the SQLite module, AUTO_INCREMENT/AUTOINCREMENT are removed from the definition and INT becomes INTEGER .

Parameters

  • key String Column name
  • value String Column type and constraints, such as 'int(11) NOT NULL AUTO_INCREMENT'; inserted as is

See also

Escapes a value for use inside a quoted SQL string with the current database module.

Parameters

  • text Any The value; it is converted with tostring first

Returns

  • String The escaped text, without surrounding quotes

See also

QUERY_CLASS:Execute(bQueueQuery)

server gamemodes/catwork/gamemode/core/libraries/sv_database.lua:486

Builds the SQL for the query and sends it to the database.

Does nothing if the query could not be built (no table name, or an INSERT or UPDATE without values). The query runs immediately when the connection is ready and is queued otherwise. With MySQLOO the callback runs asynchronously; with SQLite it runs before this returns.

Parameters

  • bQueueQuery Boolean optional, defaults to false Add the query to the queue instead of running it now

See also

QUERY_CLASS:ForTable(tableName)

server gamemodes/catwork/gamemode/core/libraries/sv_database.lua:140

Changes the table the query acts on.

Parameters

  • tableName String Name of the table

Sets a column value for an INSERT query.

Parameters

  • key String Column name
  • value Any Value to insert; it is escaped and quoted

Limits how many rows a SELECT or DELETE query affects.

Parameters

  • value Number Maximum number of rows

QUERY_CLASS:New(tableName, queryType)

server gamemodes/catwork/gamemode/core/libraries/sv_database.lua:117

Creates a query object for a table.

Query objects are normally made through the cw.database constructors (cw.database:Select, cw.database:Insert, cw.database:Update, cw.database:Delete, cw.database:Create, cw.database:Drop and cw.database:Truncate). The builder methods add clauses, then QUERY_CLASS:Execute builds the SQL and sends it. Values are escaped and wrapped in single quotes; column and table names are only wrapped in backticks, so never build them from player input.

local queryObj = cw.database:Select(config.Get('mysql_characters_table'):Get())
  queryObj:Where('_SteamID', player:SteamID())
  queryObj:Where('_Schema', cw.core:GetSchemaFolder())
  queryObj:OrderByDesc('_LastPlayed')
  queryObj:Limit(5)
  queryObj:Callback(function(result, status, lastID)
    if cw.database:IsResult(result) then
      print(result[1]._Name)
    end
  end)
queryObj:Execute()

Parameters

  • tableName String Name of the table the query acts on
  • queryType String One of 'SELECT', 'INSERT', 'UPDATE', 'DELETE', 'DROP', 'TRUNCATE' or 'CREATE'

Returns

  • QUERY_CLASS The new query object

Sets how many matching rows a SELECT query skips.

Only used together with QUERY_CLASS:Limit.

Parameters

  • value Number Number of rows to skip

Sorts the results of a SELECT query by a column, lowest first.

Several calls sort by each column in the order they were added.

Parameters

  • key String Column name

Sorts the results of a SELECT query by a column, highest first.

Several calls sort by each column in the order they were added.

Parameters

  • key String Column name

Sets the primary key column of a CREATE query.

Parameters

  • key String Column name

Adds a column to the list a SELECT query returns.

Without any calls the query selects every column.

Parameters

  • fieldName String Column name

Sets a column value for an UPDATE query.

Parameters

  • key String Column name
  • value Any New value; it is escaped and quoted

Adds a key = value condition to the query's WHERE clause.

Conditions are joined with AND. Used by SELECT, UPDATE and DELETE queries.

Parameters

  • key String Column name
  • value Any Value the column must equal; it is escaped and quoted

See also

QUERY_CLASS:WhereEqual(key, value)

server gamemodes/catwork/gamemode/core/libraries/sv_database.lua:157

Adds a key = value condition to the query's WHERE clause.

Parameters

  • key String Column name
  • value Any Value the column must equal; it is escaped and quoted

QUERY_CLASS:WhereGT(key, value)

server gamemodes/catwork/gamemode/core/libraries/sv_database.lua:187

Adds a key > value condition to the query's WHERE clause.

Parameters

  • key String Column name
  • value Any Value the column must be greater than; it is escaped and quoted

QUERY_CLASS:WhereGTE(key, value)

server gamemodes/catwork/gamemode/core/libraries/sv_database.lua:201

Adds a key >= value condition to the query's WHERE clause.

Parameters

  • key String Column name
  • value Any Value the column must be greater than or equal to; it is escaped and quoted

QUERY_CLASS:WhereLike(key, value)

server gamemodes/catwork/gamemode/core/libraries/sv_database.lua:173

Adds a key LIKE value condition to the query's WHERE clause.

% and _ in the value keep their LIKE meaning.

Parameters

  • key String Column name
  • value String Pattern the column must match; it is escaped and quoted

QUERY_CLASS:WhereLT(key, value)

server gamemodes/catwork/gamemode/core/libraries/sv_database.lua:194

Adds a key < value condition to the query's WHERE clause.

Parameters

  • key String Column name
  • value Any Value the column must be less than; it is escaped and quoted

QUERY_CLASS:WhereLTE(key, value)

server gamemodes/catwork/gamemode/core/libraries/sv_database.lua:208

Adds a key <= value condition to the query's WHERE clause.

Parameters

  • key String Column name
  • value Any Value the column must be less than or equal to; it is escaped and quoted

QUERY_CLASS:WhereNotEqual(key, value)

server gamemodes/catwork/gamemode/core/libraries/sv_database.lua:164

Adds a key != value condition to the query's WHERE clause.

Parameters

  • key String Column name
  • value Any Value the column must not equal; it is escaped and quoted

QUERY_CLASS:WhereNotLike(key, value)

server gamemodes/catwork/gamemode/core/libraries/sv_database.lua:180

Adds a key NOT LIKE value condition to the query's WHERE clause.

Parameters

  • key String Column name
  • value String Pattern the column must not match; it is escaped and quoted

Defined in