# Welcome

{% hint style="danger" %}
**SquishyDatabase** and **SquishyConfiguration** are not linked with this library.

**This** is a **new version** of the libraries combined into **one project**.
{% endhint %}

This library contains useful tools such as **simplified logging**, **test helpers**, a good universal **configuration library** and an interesting **abstract database library**.

This library is used in many of my (Smudgge's) projects, one specifically leaf currently being used on 72 servers (2024).

***

## Intro to Configuration

<pre class="language-java" data-title="Create and load a config file" data-overflow="wrap"><code class="lang-java"><strong>Configuration config = new YamlConfiguration(new File("file.yml"));
</strong>config.setResourcePath("file.yml"); // The name of the file in the resource folder.
config.load();
</code></pre>

{% code title="Read and write" overflow="wrap" %}

```java
String hello = config.getString("hello", "hello world");
config.set("hello", "hello again");
```

{% endcode %}

<pre class="language-java" data-title="Save changes to the file" data-overflow="wrap"><code class="lang-java"><strong>config.save();
</strong></code></pre>

This is quite similar to the spigot configuration classes but designed to be simpler.

***

## Intro to Databases

{% hint style="success" %}
Have you ever had a project that offered different **database types**?

It can be a pain to implement the **same thing for each type of database**. That's why I have created this library, you can now do all your database actions without worrying about doing it for each type! It abstracts the type of database.
{% endhint %}

Below is a step-by-step guide to creating a simple database and querying.

{% tabs %}
{% tab title="Config" %}
{% code title="database.yml" overflow="wrap" %}

```yaml
# Database Library Author: Smudge
# File: database.yml

should_reconnect_every_cycle: true
reconnect_cooldown_millis: 2000
will_reconnect: true
time_between_requests_millis: 100
max_requests_pending: 40

sqlite:
  enabled: true
  path: "plugins/CozyJoinLeave/database.sqlite3"
```

{% endcode %}
{% endtab %}

{% tab title="Create Database" %}
Load the config file and create the database.

{% code title="" overflow="wrap" %}

```java
Configuration databaseConfig = new YamlConfiguration(
    this.getPlugin().getDataFolder(),
    "database.yml"
);
databaseConfig.setResourcePath("database.yml");
databaseConfig.load();

Database database = new DatabaseBuilder(this.databaseConfig).create().connect();
```

{% endcode %}
{% endtab %}

{% tab title="Records" %}
{% code overflow="wrap" %}

```java
public class ExampleRecord implements Record<Example2Record> {

    public static final @NotNull String IDENTIFIER_KEY = "identifier";
    public static final @NotNull String STRING_KEY = "value";

    private final @Field(IDENTIFIER_KEY) @Primary @NotNull String identifier;
    private @Field(STRING_KEY) String string;
    
    public ExampleRecord(@NotNull String identifier) {
        this.identifier = identifier;
    }
    
    @Override
    public @NotNull ConfigurationSection convert() {
        MemoryConfigurationSection section = new MemoryConfigurationSection();
        section.set(STRING_KEY, string);
        return section;
    }

    @Override
    public @NotNull Example2Record convert(
        @NotNull ConfigurationSection section) {
        
        this.string = section.getString(STRING_KEY);
        return this;
    }
}
```

{% endcode %}
{% endtab %}

{% tab title="Tables" %}
Define the table.

{% code overflow="wrap" %}

```java
public class ExampleTable extends Table<ExampleRecord> {

    public static final @NotNull String TABLE_NAME = "example";

    @Override
    public @NotNull String getName() {
        return ExampleTable.TABLE_NAME;
    }

    @Override
    public @NotNull ExampleRecord createEmpty(@NotNull PrimaryFieldMap identifiers) {
        return new ExampleRecord(
            identifiers.getString(ExampleRecord.IDENTIFIER_KEY)
        );
    }
}
```

{% endcode %}

Create the table in the database.

```java
database.createTable(new ExampleTable());
```

{% endtab %}

{% tab title="Query" %}

```java
ExampleRecord record = database.getTable(ExampleTable.class)
    .getFirstRecord();
```

{% endtab %}
{% endtabs %}
