# Database

To set up the F\&M Garage system, you need to make some changes to your server database.\
Execute the provided queries on your database.

You can either copy the query from here or import the file included in the script.

The file is located at: `/config/database.sql`

{% code title="/config/databse.sql" %}

```sql
CREATE TABLE IF NOT EXISTS `owned_vehicles` (
    `plate` VARCHAR(12) NOT NULL COLLATE 'utf8mb3_general_ci',
    PRIMARY KEY (`plate`) USING BTREE
)
COLLATE='utf8mb3_general_ci'
ENGINE=InnoDB;

ALTER TABLE `owned_vehicles`
    ADD COLUMN IF NOT EXISTS `owner` VARCHAR(60) NULL DEFAULT NULL COLLATE 'utf8mb3_general_ci',
    ADD COLUMN IF NOT EXISTS `vehicle` LONGTEXT NULL DEFAULT NULL COLLATE 'utf8mb3_general_ci',
    ADD COLUMN IF NOT EXISTS `type` VARCHAR(20) NOT NULL DEFAULT 'car' COLLATE 'utf8mb3_general_ci',
    ADD COLUMN IF NOT EXISTS `job` VARCHAR(20) NULL DEFAULT NULL COLLATE 'utf8mb3_general_ci',
    ADD COLUMN IF NOT EXISTS `stored` TINYINT(4) NOT NULL DEFAULT '0',
    ADD COLUMN IF NOT EXISTS `parking` VARCHAR(60) NULL DEFAULT NULL COLLATE 'utf8mb3_general_ci',
    ADD COLUMN IF NOT EXISTS `pound` VARCHAR(60) NULL DEFAULT NULL COLLATE 'utf8mb3_general_ci',
    ADD COLUMN IF NOT EXISTS `glovebox` LONGTEXT NULL DEFAULT NULL COLLATE 'utf8mb3_general_ci',
    ADD COLUMN IF NOT EXISTS `trunk` LONGTEXT NULL DEFAULT NULL COLLATE 'utf8mb3_general_ci',
    ADD COLUMN IF NOT EXISTS `model` TEXT NULL DEFAULT NULL COLLATE 'utf8mb3_general_ci';
```

{% endcode %}

## Important

***

This script uses the `model` column from the `owned_vehicles` table to retrieve the vehicle name.\
If your vehicle shop script doesn't save this information to the database, you should update it accordingly.

## Changing SQL queries

***

To modify the SQL queries, navigate to the `/server/database.lua` file.\
Here, you can change all queries used in the server scripts.

**Note: You cannot change the variables passed to the functions.**

Here is default content of this file.

{% code title="database.lua" %}

```lua
function getVehicleOwnerIdentifier(plate)
    local result = MySQL.single.await('SELECT owner FROM owned_vehicles WHERE plate = ?', {plate})
    return result
end

function storeVehicle(plate, vehProps)
    local result = MySQL.update.await('UPDATE owned_vehicles SET stored = ?, vehicle = ? WHERE plate = ?', {1, json.encode(vehProps), plate})
end

function getPlayerVehicles(identifier, garageType)
    local vehicles = MySQL.query.await('SELECT * FROM owned_vehicles WHERE owner = ? AND type = ? AND stored = ?', {identifier, garageType, 1})
    return vehicles
end

function changeVehicleStatus(plate, status)
    local result = MySQL.update.await('UPDATE owned_vehicles SET stored = ? WHERE plate = ?', {status, plate})
    return result
end

function getPlayerImpound(identifier)
    local impound = MySQL.query.await('SELECT * FROM owned_vehicles WHERE owner = ? AND stored = ?', {identifier, 0})
    return impound
end
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://fmstudio.gitbook.io/fmdocs/fmseries/garage/database.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
