# SQL

## Hooks

<details>

<summary>swatch_mysql_config_loaded</summary>

Called as soon as all config entries has been loaded from database.

</details>

<details>

<summary>swatch_mysql_connected</summary>

Called as soon a mysql database connected. Called instantly with sqlite. Rather use [#swatch\_mysql\_config\_loaded](#swatch_mysql_config_loaded "mention")

</details>

## Functions

{% hint style="warning" %}
Use below functions only after [#swatch\_mysql\_connected](#swatch_mysql_connected "mention") hook
{% endhint %}

<details>

<summary>sWatch.query</summary>

Executes sql query

#### Arguments

1. **(String)** query

</details>

<details>

<summary>sWatch.selectQuery</summary>

#### Arguments

1. **(String)** query
2. **(Function)** callback (optional)

#### Returns

1. **(Callback)(Table)** data - inside callback

#### Example

Internal code to load config from database

```
local query = "SELECT * FROM `swatch_config`"
sWatch.selectQuery(query, function(data)
    data = data or {}
    for _, row in pairs(data) do
        configCache[row.key] = {}
        configCache[row.key].value = dataTypesLoad[row.data_type](row.value)
    end
end)
```

</details>

<details>

<summary>sWatch.sqlEscape</summary>

Uses for sql flatfile `sql.SQLStr` and `mysql:escape` for mysql.

#### Arguments

1. **(String)** valueToEscape

#### Returns

1. **(String)** escapedValue

</details>
