cw.database classserver
Summary
Methods
cw.database.Module.DatabaseConnectionFailed.cw.database:Connect.Methods
cw.database:Connect(host, username, password, database, port, socket, flags, id)
server gamemodes/catwork/gamemode/core/libraries/sv_database.lua:668Connects to the database with the module set in cw.database.Module.
With SQLite this only calls cw.database:OnConnected. Otherwise the binary
module is required first; if it is missing, an error is printed and nothing
else happens. Queries sent before the connection is ready are queued.
Parameters
- host String Database host
- username String Database user
- password String Database password
- database String Database name
- port Number optional, defaults to
3306Database port - socket String optional, defaults to
nilUnix socket path - flags Number optional, defaults to
nilClient flags, only passed to tmysql4 - id String optional, defaults to
nilConnection ID for an additional MySQLOO connection; defaults to the current one or'main'
See also
cw.database:ConnectMySQLOO(id, host, username, password, database, port, socket)
server internal gamemodes/catwork/gamemode/core/libraries/sv_database.lua:614Internal
This function is internal. It is not part of the public API and may change without notice.
Called by cw.database:Connect.
Creates a MySQLOO 9 database object, stores it under an ID and starts connecting.
A MySQLOO database object can only connect once, so every attempt creates a
new one. Auto-reconnect is enabled for lost connections, and a failed attempt
is retried after 30 seconds. Success calls cw.database:OnConnected,
failure cw.database:OnConnectionFailed.
Parameters
- id String Connection ID
- host String Database host
- username String Database user
- password String Database password
- database String Database name
- port Number Database port
- socket String optional, defaults to
nilUnix socket path; ignored unless it is a string
Creates a CREATE TABLE IF NOT EXISTS query object for a table.
Parameters
- tableName String Name of the table
Returns
- QUERY_CLASS The query object
See also
Creates a DELETE query object for a table.
Without any Where conditions the query deletes every row.
Parameters
- tableName String Name of the table
Returns
- QUERY_CLASS The query object
Closes the current connection and cancels any pending MySQLOO reconnect.
MySQLOO waits for queries that have already started.
Parameters
- id Any Unused; the current connection is always closed
Returns
- Any The result of tmysql4's
Disconnect; nothing for other modules
Creates a DROP TABLE query object for a table.
Parameters
- tableName String Name of the table
Returns
- QUERY_CLASS The query object
cw.database:EasyRead(tableName, where, callback)
server gamemodes/catwork/gamemode/core/libraries/sv_database.lua:1154Selects the rows matching a condition and passes them to a callback.
Parameters
- tableName String Name of the table
- where List A
{ column, value }pair, or a list of such pairs that must all match - callback Function Called as
callback(result, bHasRows); errors in it are caught and printed
Returns
- Boolean
falseifwhereis missing; nothing otherwise
See also
cw.database:EasyWrite(tableName, where, data)
server gamemodes/catwork/gamemode/core/libraries/sv_database.lua:1098Updates the rows matching a condition, or inserts a new row if none match.
cw.database:EasyWrite('cw_notes', { '_SteamID', player:SteamID() }, {
_SteamID = player:SteamID(),
_Text = text
})
Parameters
- tableName String Name of the table
- where List A
{ column, value }pair, or a list of such pairs that must all match - data Map Column names mapped to the values to write
See also
Prints a database error to the console.
Called by GM:DatabaseConnectionFailed. Does nothing if errorText is nil.
Parameters
- errorText String The error message
Escapes a value for use inside a quoted SQL string.
MySQLOO uses the connection's escape function when connected and a Lua
equivalent of mysql_real_escape_string otherwise. SQLite escapes with
sql.SQLStr, which is only safe inside single quotes.
Parameters
- text Any The value; it is converted with
tostringfirst
Returns
- String The escaped text, without surrounding quotes
cw.database:FlushQueue()
server internal gamemodes/catwork/gamemode/core/libraries/sv_database.lua:832Internal
This function is internal. It is not part of the public API and may change without notice.
Called by cw.database:OnConnected.
Runs every queued query in order.
Queries that still cannot run are queued again.
Creates an INSERT query object for a table.
local queryObj = cw.database:Insert(config.Get('mysql_bans_table'):Get())
queryObj:Insert('_Identifier', player:SteamID())
queryObj:Insert('_Schema', cw.core:GetSchemaFolder())
queryObj:Execute()
Parameters
- tableName String Name of the table
Returns
- QUERY_CLASS The query object
Returns whether the database has connected.
Returns
- Boolean Whether
cw.database:OnConnectedhas run since the last disconnect
Returns whether a query result contains at least one row.
Parameters
- result Any The
resultpassed to a query callback
Returns
- Boolean Whether it is a non-empty table
cw.database:OnConnected()
server internal gamemodes/catwork/gamemode/core/libraries/sv_database.lua:922Internal
This function is internal. It is not part of the public API and may change without notice.
Called by cw.database:Connect and the MySQLOO connection callback.
Called when the database connects; creates the default tables and runs queued queries.
Creates the bans, players and characters tables if they do not exist,
flushes the query queue, marks the database connected and fires the
DatabaseConnected hook.
cw.database:OnConnectionFailed(errorText)
server internal gamemodes/catwork/gamemode/core/libraries/sv_database.lua:1037Internal
This function is internal. It is not part of the public API and may change without notice.
Called by the database module callbacks.
Called when a connection attempt fails; prints the error and fires DatabaseConnectionFailed.
Parameters
- errorText String The error reported by the database module
cw.database:Queue(queryString, callback)
server gamemodes/catwork/gamemode/core/libraries/sv_database.lua:822Adds an SQL string to the queue of queries waiting for the connection.
Queued queries run one per second while the connection is ready, and all at once when it connects.
Parameters
- queryString String The SQL to run; anything else is ignored
- callback Function optional, defaults to
nilCalled as forcw.database:RawQuery
cw.database:RawQuery(query, callback, flags, ...)
server gamemodes/catwork/gamemode/core/libraries/sv_database.lua:741Runs an SQL string on the current connection.
The query is queued if the connection is not ready yet. Query and callback errors are printed, not thrown, and the callback is not called when the query fails.
Parameters
- query String The SQL to run
- callback Function optional, defaults to
nilCalled ascallback(result, status, lastID);statusis alwaystrue - flags Number optional, defaults to
nilQuery flags, only used by tmysql4 - ... Any Extra arguments, only passed to tmysql4
See also
Creates a SELECT query object for a table.
See QUERY_CLASS:New for the builder methods and an example.
Parameters
- tableName String Name of the table
Returns
- QUERY_CLASS The query object
cw.database:SetCurrentConnection(id)
server gamemodes/catwork/gamemode/core/libraries/sv_database.lua:586Switches the connection that queries are sent through.
Only MySQLOO supports several connections; other modules always use
'main', as does an unknown ID.
Parameters
- id String ID of a connection made with
cw.database:Connect
cw.database:SetModule(moduleName)
server gamemodes/catwork/gamemode/core/libraries/sv_database.lua:905Sets the database module used by cw.database:Connect.
Parameters
- moduleName String
'sqlite','mysqloo'or'tmysql4'
Internal
This function is internal. It is not part of the public API and may change without notice.
Called every second by the cw.Database.Think timer.
Runs the first queued query if the connection is ready.
cw.database:Truncate(tableName)
server gamemodes/catwork/gamemode/core/libraries/sv_database.lua:569Creates a TRUNCATE TABLE query object for a table.
Parameters
- tableName String Name of the table
Returns
- QUERY_CLASS The query object
Creates an UPDATE query object for a table.
Parameters
- tableName String Name of the table
Returns
- QUERY_CLASS The query object