Detailed Explanation of Table Contract Self-writing Tool Class

Table contract

This is a Solidity smart contract on Table. This smart contract defines some important entities and methods that can be used to create, manipulate and manage tabular data.

The first is TableFactorythe contract , which is used to create and open tables. which contains:

  • openTable(string)The method is used to open an existing form and return the form instanceTable
  • createTable(string, string, string)The method is used to create a new table and needs to pass in three parameters: table name, primary key name, storage engine type

Next is a Conditioncontract for setting query conditions. which contains:

  • Equal to (Equal)
  • Not equal to (Not Equal)
  • Greater than (Greater Than)
  • Greater than or equal to (Greater or Equal)
  • Less than (Less Than)
  • Less than or equal to (Less or Equal)
  • Limit the number of returned records (limit)

Then there is a Entrycontract , which represents a row of data records in the table. It provides methods to get and set various types of data values ​​such as integers, unsigned integers, addresses, strings, etc.

Finally, a Tablecontract , including the following methods:

  • select(string, Condition)The method is used to select the data records in the table according to the query conditions
  • insert(string, Entry)The method is used to insert a new data record into the table
  • update(string, Entry, Condition)The method is used to update the data records in the table according to the given query conditions
  • remove(string, Condition)The method is used to delete data records in the table according to the given query conditions
  • newEntry()Method used to create a new data record instance
  • newCondition()method is used to create a new query condition instance

There is also a KVTableFactorycontract for creating and opening key-value tables. which contains:

  • openTable(string)The method is used to open an existing key-value table and return the table instanceKVTable
  • createTable(string, string, string)The method is used to create a new key-value table and needs to pass in three parameters: table name, primary key name, storage engine type

The last is a KVTablecontract , which represents a row of data records in the key-value table. It provides methods for getting and setting key-value pair data, where key is of type string and value is of type Entry.

pragma solidity ^0.4.24;

contract TableFactory {
    
    
    function openTable(string) public view returns (Table); //open table
    function createTable(string, string, string) public returns (int256); //create table
}

//select condition
contract Condition {
    
    
    function EQ(string, int256) public view;
    function EQ(string, string) public view;
    function EQ(string, address) public view;

    function NE(string, int256) public view;
    function NE(string, string) public view;

    function GT(string, int256) public view;
    function GE(string, int256) public view;

    function LT(string, int256) public view;
    function LE(string, int256) public view;

    function limit(int256) public view;
    function limit(int256, int256) public view;
}

//one record
contract Entry {
    
    
    function getInt(string) public view returns (int256);
    function getUInt(string) public view returns (uint256);
    function getAddress(string) public view returns (address);
    function getBytes64(string) public view returns (bytes1[64]);
    function getBytes32(string) public view returns (bytes32);
    function getString(string) public view returns (string);

    function set(string, int256) public;
    function set(string, uint256) public;
    function set(string, string) public;
    function set(string, address) public;
}

//record sets
contract Entries {
    
    
    function get(int256) public view returns (Entry);
    function size() public view returns (int256);
}

//Table main contract
contract Table {
    
    
    function select(string, Condition) public view returns (Entries);
    function insert(string, Entry) public returns (int256);
    function update(string, Entry, Condition) public returns (int256);
    function remove(string, Condition) public returns (int256);

    function newEntry() public view returns (Entry);
    function newCondition() public view returns (Condition);
}

contract KVTableFactory {
    
    
    function openTable(string) public view returns (KVTable);
    function createTable(string, string, string) public returns (int256);
}

//KVTable per permiary key has only one Entry
contract KVTable {
    
    
    function get(string) public view returns (bool, Entry);
    function set(string, Entry) public returns (int256);
    function newEntry() public view returns (Entry);
}

Write Table contract tool class

The tool class written in Solidity language can perform data operations based on the given Tablecontract , including CRUD operations on the data in the table.

The utility class contains the following methods:

  • constructor: Constructor, used to initialize the tool class instance, need to pass in TableFactorythe instance , table name and primary key name. Open a table instance with table name and primary key name.
  • createTable: Create a new table, create a new table in the current database, need to pass in the storage engine type, and return the ID of the newly created table.
  • selectByID: Query table data according to the given primary key ID, and return the first row of data records of the query result.
  • selectByCondition: Query table data according to the given query conditions, and return all data records that meet the conditions.
  • insertEntry: To insert a new data record into the table, you need to pass in the data record instance and return the ID of the newly added record.
  • updateByID: Update the data records in the table according to the given primary key ID, need to pass in the data record instance to be updated, and return the number of affected records.
  • removeByID: Delete data records in the table according to the given primary key ID, and return the number of affected records.

It should be noted that before the method is called, it is necessary to ensure that the correct table instance has been opened, and the correct table name and primary key name have been passed in.

pragma solidity ^0.8.0;

import "./Table.sol";

contract TableUtils {
    
    
    TableFactory private tableFactory;
    Table private table;
    string private tableName;
    string private primaryKey;

    // 构造函数,用于初始化工具类实例
    constructor(TableFactory factory, string memory name, string memory key) {
    
    
        tableFactory = factory;
        tableName = name;
        primaryKey = key;

        // 打开表格实例
        table = tableFactory.openTable(tableName);
    }

    // 获取当前打开的表格实例
    function getTable() external view returns (Table) {
    
    
        return table;
    }

    // 在当前数据库中创建一张新表格
    function createTable(string memory engineType) external returns (int256) {
    
    
        return tableFactory.createTable(tableName, primaryKey, engineType);
    }

    // 根据给定的主键 ID 查询表格数据,返回查询结果的第一行数据记录
    function selectByID(int256 id) external view returns (Entry memory) {
    
    
        Condition condition = table.newCondition();
        condition.EQ(primaryKey, id);

        Entries entries = table.select("", condition);
        require(entries.size() == 1, "Record not found");

        return entries.get(0);
    }

    // 根据给定的查询条件查询表格数据,返回符合条件的所有数据记录
    function selectByCondition(Condition condition) external view returns (Entries memory) {
    
    
        return table.select("", condition);
    }

    // 向表格中插入一条新的数据记录,需要传入数据记录实例,返回新增记录的 ID
    function insertEntry(Entry memory entry) external returns (int256) {
    
    
        return table.insert("", entry);
    }

    // 根据给定的主键 ID 更新表格中的数据记录,需要传入需要更新的数据记录实例,返回影响的记录数
    function updateByID(int256 id, Entry memory entry) external returns (int256) {
    
    
        Condition condition = table.newCondition();
        condition.EQ(primaryKey, id);

        return table.update("", entry, condition);
    }

    // 根据给定的主键 ID 删除表格中的数据记录,返回影响的记录数
    function removeByID(int256 id) external returns (int256) {
    
    
        Condition condition = table.newCondition();
        condition.EQ(primaryKey, id);

        return table.remove("", condition);
    }
}

TableUtils use

Example using TableUtilsthe utility class. First, it needs to be instantiated in the contract and passed in the correct parameters:

pragma solidity ^0.8.0;

import "./TableFactory.sol";
import "./TableUtils.sol";

contract MyContract {
    
    
    TableFactory private tableFactory;
    TableUtils private utils;

    // 构造函数,用于初始化合约实例和工具类实例
    constructor(TableFactory factory) {
    
    
        tableFactory = factory;
        utils = new TableUtils(tableFactory, "my_table", "id");
    }

    // 创建新表格,并返回新建表的 ID
    function createTable() external returns (int256) {
    
    
        return utils.createTable("BASIC");
    }

    // 向表格中插入一条新的数据记录,并返回新增记录的 ID
    function insertData(string memory name, uint256 value) external returns (int256) {
    
    
        Entry memory entry = utils.getTable().newEntry();

        entry.set("name", name);
        entry.set("value", int256(value));

        return utils.insertEntry(entry);
    }

    // 根据给定的主键 ID 更新表格中的数据记录,需要传入需要更新的数据记录实例,返回影响的记录数
    function updateData(int256 id, string memory name, uint256 value) external returns (int256) {
    
    
        Entry memory entry = utils.getTable().newEntry();

        entry.set("name", name);
        entry.set("value", int256(value));

        return utils.updateByID(id, entry);
    }

    // 根据给定的主键 ID 删除表格中的数据记录,返回影响的记录数
    function removeData(int256 id) external returns (int256) {
    
    
        return utils.removeByID(id);
    }

    // 根据给定的主键 ID 查询表格中的数据记录,并返回查询结果
    function getDataByID(int256 id) external view returns (string memory, uint256) {
    
    
        Entry memory entry = utils.selectByID(id);

        return (entry.getString("name"), uint256(entry.getInt("value")));
    }

    // 根据给定的查询条件查询表格中的数据记录,并返回查询结果
    function getDataByCondition() external view returns (string[] memory, uint256[] memory) {
    
    
        Condition condition = utils.getTable().newCondition();
        Entries entries = utils.selectByCondition(condition);

        string[] memory names = new string[](uint256(entries.size()));
        uint256[] memory values = new uint256[](uint256(entries.size()));

        for (int256 i = 0; i < entries.size(); i++) {
    
    
            Entry memory entry = entries.get(i);

            names[uint256(i)] = entry.getString("name");
            values[uint256(i)] = uint256(entry.getInt("value"));
        }

        return (names, values);
    }
}

In this example, we first create a contract MyContractcalled , initialize it in the constructor and pass in an TableFactoryinstance of . Then, create a table my_tablenamed and specify the primary key named id.

Next, some methods are defined for performing CRUD operations on the tabular data. For example, insertDatathe method can insert a new data record into the table and return the ID of the newly added data; getDataByIDthe method can query the data records in the table according to the given primary key ID and return the query result.

It should be noted that all methods related to the operation of table data need to be called through TableUtilsthe tool class instance. In each method, you need to first get the correct table instance, and create, set, and read data record instances as needed. Finally, use the data record instance as a parameter to call the corresponding method for data manipulation.

Guess you like

Origin blog.csdn.net/weixin_46532941/article/details/129889082