Package: ayradb
Python AyraDB client libraryThis module provides a simple interface to perform actions on an AyraDB installation.InitializationThe AyraDB class is the main interface to an AyraDB installation.In order to instantiate an AyraDB object, a validhostmust be provided.from ayradb import AyraDB

db: AyraDB = AyraDB(ip="192.168.0.1")All possible operations a user can perform are described below.Create a tableAyraDB allows users to create three different types of tables:FIXED_LENGTH,PADDED,NOSQLConvenient APIs to create tables of each type are available.Fixed lenght tablesForfixed_lengthtables, maximum key column size, maximum table size, and at least one field must be provided.
New fields can be added later.from ayradb import Table, Column

table = db.table("new_fixed_length_table")
fields = [{Column.NAME: "field0", Column.MAX_LENGTH: 1024},
           {Column.NAME: "field1", Column.MAX_LENGTH: 1024},
           {Column.NAME: "field2", Column.MAX_LENGTH: 1024}]

max_key_column_size_byte = 1024
rct = table.create(Table.TYPE_FIXED_LENGTH, columns=fields, key_max_size=max_key_column_size_byte).wait_response()
if rct.success is True:
    print("New fixed length table created!")
else:
    print(f"Error code: {rct.error_code}")PaddedForpaddedtables, at least one field must be provided, and new fields can be added later.from ayradb import Table, Column

table = db.table("new_padded_table")
fields = [{Column.NAME: "field0"},
            {Column.NAME: "field1"},
            {Column.NAME: "field2"}]
max_key_column_size_byte = 1024
rct = table.create(Table.TYPE_PADDED, columns=fields, key_max_size=max_key_column_size_byte).wait_response()
if rct.success is True:
    print("New padded table created!")
else:
    print(f"Error code: {rct.error_code}")NosqlNosqltables do not have a fixed structure, so thefieldsargument is omitted.from ayradb import Table, Column

table = db.table("new_nosql_table")
rct = table.create(Table.TYPE_NOSQL).wait_response()
if rct.success is True:
    print("New nosql table created!")
else:
    print(f"Error code: {rct.error_code}")Insert a recordIf the new record is not already contained in table, then it is inserted, otherwise it is updated.table = db.table("new_fixed_length_table")
key_value = 'key_column'
fields = {
            "field0": b'value0',
            "field1": b'value1',
            "field2": b'value2',
         }
ri = table.upsert_record(key_value, fields).wait_response()
if ri.success is True:
    print("New record inserted!")
else:
    print(f"Error code: {ri.error_code}")Thefieldsargument must be specified, and thefield valueargument must be in the form of a byte Array.Read a recordRead can be limited to a subset of fields with thefieldsargument.table = db.table("new_fixed_length_table")
key_value = "key_column"
field_names = ["field0", "field2"]
rr = table.read_record(key_value, fields=field_names).wait_response()
if rr.success is True:
    for key, value in rr.content.items():
        print("Field name: " + key)
        print("Field value: " + value.decode())
else:
    print(f"Error code: {rr.error_code}")If thefieldsargument is set tonull, all fields are retrieved.table = db.table("new_fixed_length_table")
key_value = "key_column"
rr = table.read_record(key_value, fields=[]).wait_response()
if rr.success is True:
    for key, value in rr.content.items():
        print("Field name: " + key)
        print("Field value: " + value.decode())
else:
    print(f"Error code: {rr.error_code}")Delete a recordtable = db.table("new_fixed_length_table")
key_value = "key_column"
rd = table.delete_record(key_value).wait_response()
if rd.success is True: 
      print("Record successfully deleted!")
else:
    print(f"Error code: {rd.error_code}")Deletion inNosqltables can be limited to a subset of fields with thefieldsargument.table = db.table("new_nosql_table")
key_value = "key_column_ns"
field_names = ["field0ns", "field1ns"]
rd = table.delete_record(key_value, fields=field_names).wait_response()
if rd.success is True:
    print("Record fields successfully deleted!")
else:
    print(f"Error code: {rd.error_code}")The operation completes even if a record with the provided key does not exist in the table.Retrieve table structureRetrieve table structure retrieves the structures of the record of a table.table = db.table("new_fixed_length_table")
rts = table.get_structure().wait_response()
if rts.success is True:
    for i in range(len(rts.structure)):
        field = rts.structure[i]
        print(f"Position: {i}, Field name: {field.get('column_label')}, Field max length: {int(field.get('column_max_net_length',-1))} bytes")
else:
    print(f"Error code: {rts.error_code}")Truncate a tableTruncate table deletes all the records of an existing table, leaving the structure of the table intact and usable.table = db.table("new_fixed_length_table")
rt = table.truncate().wait_response()
if rt.success is True:
    print("Table is now empty!")
else:
    print(f"Error code: {rt.error_code}")Delete a tableDelete table destroys an existing table.table = db.table("new_fixed_length_table")
rd = table.drop().wait_response()
if rd.success is True:
    print("Table destroyed!")
else:
    print(f"Error code: {rd.error_code}")SQL queriesAyraDB provides an API to perform SQL-like queries on a single table.
AyraDB SQL queries can contain the following clauses:WHEREGROUP_BYHAVINGORDER_BYLIMITDISTINCTIMPORTANT: the semicolon (';') at the end of the query is mandatory.query = db.query('SELECT * FROM %s WHERE field="condition";', % table_name)
stream = query.execute().stream
while not stream.is_closed():
   responses = stream.take_when_available()
       for response in responses:
           for record in response.content:
               # Store or use the recordConnection handlingWhen all operations are completed, it's best practice to close the connection with the following command.db.close_connection()No operations are allowed after connection is closed.
A "No Connection Error" (error code: 99) is returned when an action fails because of a broken connection.
In such event a new AyraDB object must be referenced (see:Initialization) before performing a new action.Error handlingWhen an action fails, a response object is returned to the user.Successwill returnfalsein case of a failure.The response object contains an error code.A list of all possible errors, divided by action, is provided below.Error 99: No Connection ErrorError 100: Internal ErrorCreate a tableError -1: Field max lenght requiredError -2: Key max lenght requiredError -3: Not enough nodes providedError 1:  Table already existsInsert a recordError -1: Invalid field nameError 1:  Table not foundError 2:  Field not foundError 3:  Field too longRead a recordError -1: Invalid field nameError 1: Table not foundError 2: Record not foundDelete a recordError -1: Invalid field nameError 1: Table not foundError 2: Field not foundRetrieve table structureError -1: Invalid field nameError 1: Table not foundTruncate a tableError 1: Table not foundDelete a tableError -1: Invalid field nameError 1: Table not found