1. Home
  2. Docs
  3. UniREST Solution 3.5
  4. UniREST Client
  5. Database Operations

Database Operations

The UniRESTClient class has all the methods for calling your server REST APIs and performing the 4 expected operations (read, write, update and delete) in your Database tables.

Those methods have their own specific features and behaviours, but, in general, they need:

  • the API to call;
  • some Database data;

API to call

All the methods require, as the first parameter, the HTTP address of the API to call. The parameter is a string you can manually type but, in order to avoid mistakes, you should use the API class provided by the UniREST Client configuration. The API class contains all your APIs in the form of properties, which name consists of the API Group name and the API name separated by an underscore.

For example…

…if you have created an API like this one…

…your UniRESTClientConfig script should have the API class with that API definition…

…and in your C# Script you should be able to refer to that API address just typing:

API.player_data

Database data

Some methods require, as the second parameter, a specific set of data. This set is defined by a class which replicates the Database table structure the API works with. This means that this parameter must be an instance of that class.
The Database table classes are provided by the UniREST Client configuration under the DB class. The DB class contains all the Database tables as classes, which have the same table name and the table columns (name and data-type) as properties.

For example…

…if you have created a table named players, in the UniRESTClientConfig class you should have this table replicated as a C# serializable class under the DB class…

…and in your C# Script you should be able to instantiate that DB structure just typing:

new DB.Players()

The use of Database data can be requested by methods when:

  • the method is calling your API to write or update your Database table with your game data;
  • the method is calling an API that needs some values for selecting the Database table records to work with.

Lastly, the DB class must be used as data-type to properly get the data received from your Server.

About the DATETIME format

The MySQL DATETIME format is managed, in C#, by the UniREST Client as a string. When you send a date to your Database, you must provide it in the format MySQL expects to receive it.

Often happens that the date format returned by .NET is not perfectly compatible with MySQL. For this reason, in your C# Scripts, you must always provide dates as strings, in the right format.

The UniRESTClient class has a GetDate() method that can help you to manage dates easily.


Operations response

Reading is a two-way Database operation: it performs a reading operation on a Database table and replies with a single data or an array of data.

Writing, updating and deleting are, instead, one-way Database operations: they do something on a Database table and just return true if the operation succeeded or false if failed. However, in the MySQL environment, these operations return some value. This value is contained in the UniREST Server reply and stored in the DBresponse property of the UniRESTClient class:

UniRESTClient.DBresponse;

This property is initialized with an integer value when those operations are correctly executed. The meaning of this value depends on the operation:

OperationDBresponse value
WriteIt’s the ID of the new record written into the table.
UpdateIt’s the number of records that have been updated.
DeleteIt’s the number of records that have been deleted.

Errors management

The Database activity is tracked and provided to the UniRESTClient class every time you call an API. In this way, you can have a minimum control of what’s happening in the server-side when the API works or something goes wrong.

When an error occurs, the methods just return false or a null object, but they don’t block your game execution or throw a warning. Instead, you can have a look of the last launched Database query and its execution result.

That information is stored in the following UniRESTClient class string properties:

UniRESTClient.DBquery;
UniRESTClient.DBerror;

The DBquery property contains the last executed Database query, whereas the DBerror contains the error message thrown by the Database system.


Debug.Log("Last Database query: " + UniRESTClient.XXquery);
if (UniRESTClient.XXerror != "")
{
    Debug.Log("There is an error: " + UniRESTClient.XXerror);
}

Articles