Skip to content

Database

BastouP411 edited this page Nov 2, 2021 · 6 revisions

This peripheral allows the user to use a SQL database. basic SQL, prepared requests and diverse helpers may be used.

A Database Disk must be inserted inide the databse to function. Every disk holds a separate database and keeps it when transfered to another Database peripheral.

This peripheral uses SQLite databases, and as such it uses the SQL language for requests and prepared requests.

The maximum size of a Database Disk can be set in the server config file.

If you want to learn more about it:

Note: The helpers are a WIP feature so they are not documented yet, even if they are present ingame.

Database

Crafting

Database

To be crafted, the Database requires:

  • 6 stone blocks
  • 1 redstone
  • 1 iron ingot
  • 1 disk drive

Shaped crafting recipe

Database Disk

To be crafted, the Database Disk requires:

  • 1 redstone
  • 3 iron ingots
  • 1 disk

Shaped crafting recipe

Methods

The wraped peripheral will be named database

database.isDiskInserted(): boolean

This checks if Database Disk is inserted inside the Database

database.getDatabaseId(): string

This gets the database id for this disk.

Throws an error if no disk is inserted.

database.getDatabaseName(): string

This gets the database name or nil if no name was set for this disk.

Throws an error if no disk is inserted.

database.setDatabaseName(name: string): string

This sets the database name for this disk.

Throws an error if no disk is inserted.

database.executeSQL(sql: string): table

This executes the provided sql string on the database, and returns a result structure.

Throws an error if no disk is inserted or if there was a problem with the SQL.

Note: This uses the SQLite language

database.prepareStatement(sql: string): table

This prepares the provided sql request for the database, and returns a prepared statement.

Note: This uses the SQLite language and returns a Prepared Statement

Result Structure

This structure is returned by every call to the database.

Parameters:

  1. type: string This is the type of the result. May be "update", "query", or "error"
  2. data: number / table / string the data associated with this response. I repesents the number of updated entries if type = "update", the returned table if type = "query", and a summary od the error if type = "error".

Prepared Statement Structure

This structure is returned by the prepareStatement method.

Methods

The structure will be named statement

statement.setParameter(index: number, value: any): table

This sets the parameter at index in the Prepared Statement to value, and returns the Prepared Statement.

statement.removeParameter(index: number): table

This removes the parameter at index in the Prepared Statement, and returns the Prepared Statement.

statement.execute(index: number, value: any): table

This executes the Prepared Statement, and returns a Result structure.

Throws an error if no disk is inserted or if there was a problem with the SQL.

Events

database_attached

This event is fired when a Database Disk is inserted inside the Database. This returns:

  1. side: string The side the Database is attached to.
  2. uuid: string The id of the Database Disk.
  3. name: string The name of the Database Disk.

database_detached

This event is fired when a Database Disk is removed from the Database. This returns:

  1. side: string The side the Database is attached to.