{"id":15144144,"url":"https://github.com/cupidonsauce173/mypigsql","last_synced_at":"2025-04-19T14:08:34.440Z","repository":{"id":43762442,"uuid":"454621995","full_name":"CupidonSauce173/MyPigSQL","owner":"CupidonSauce173","description":"Async MySQL Library for PocketMine-MP","archived":false,"fork":false,"pushed_at":"2023-05-26T02:16:29.000Z","size":134,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-29T08:25:02.240Z","etag":null,"topics":["async","asynchronous","multithread","multithreading","mysql","php","pmmp","pmmp-plugin","sql","threading"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CupidonSauce173.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-02-02T02:39:11.000Z","updated_at":"2023-02-15T23:51:00.000Z","dependencies_parsed_at":"2023-01-18T14:45:56.494Z","dependency_job_id":null,"html_url":"https://github.com/CupidonSauce173/MyPigSQL","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CupidonSauce173%2FMyPigSQL","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CupidonSauce173%2FMyPigSQL/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CupidonSauce173%2FMyPigSQL/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CupidonSauce173%2FMyPigSQL/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CupidonSauce173","download_url":"https://codeload.github.com/CupidonSauce173/MyPigSQL/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246390896,"owners_count":20769475,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","asynchronous","multithread","multithreading","mysql","php","pmmp","pmmp-plugin","sql","threading"],"created_at":"2024-09-26T10:22:04.747Z","updated_at":"2025-03-30T22:31:36.203Z","avatar_url":"https://github.com/CupidonSauce173.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003cimg width=\"150\" src=\"MyPigSQL.png\" /\u003e\n\u003c/p\u003e\n\u003ch1 align=\"center\"\u003e MyPigSQL \u003c/h1\u003e\n\u003cp align=\"center\"\u003eJoin my discord: https://discord.gg/2QAPHbqrny \u003c/p\u003e\n\u003cp align=\"center\"\u003e Async MySQL Library for PocketMine-MP \u003c/p\u003e\n\n### Current  Bugs\n\n- There are `zombie SQLRequest` that can stay in the batches but they won't be executed multiple times and are extremely\n  rare.\n\nSee [this merge](https://github.com/CupidonSauce173/MyPigSQL/pull/3) for a full update from `2.0.0-beta` to `3.0.0-beta`\n.\n\n## Library\n\n| **Feature**                 | **State** | \n| --------------------------- |:----------:|\n| MultiThreaded System        | ✔️ |\n| SQLConnString Object        | ✔️ |\n| SQLRequest Object           | ✔️ |\n| SQLRequestExceptions        | ✔️ |\n| SQLRequest Completions      | ✔️ |\n| DispatchBatchThread         | ✔️ |\n| DispatchBatchPool           | 🛠️ |\n\n### Prerequisites\n\n- Working MySQL Server.\n\n### Why is this necessary?\n\nUnlike other SQL libraries for PMMP, you don't need to specify your queries in another file. You can create your queries\non the fly while keeping a strict environment. This library also makes it possible to use multiple connections so you\ncan interact with multiple databases \u0026 tables. The creation of the SQLConnStrings \u0026 SQLRequests are very similar to how\nyou create packets in PMMP.\n\nNote that every queries are prepared and you **must** supply the data types \u0026 the data variables (as array) when\ncreating a new SQLRequest.\n\n### SQLConnString Creation\n\nIn order to send requests to the MySQL servers, you will surely need to create the SQLConnStrings which are the\ninformations needed for the DispatchBatchThread to execute the queries.\n\n```php\n# This is how to create a new SQLConnString from the SQLConnString::create() function.\n$sqlConn = SQLConnString::create(\n        'MainDatabase', # Name of the SQLConnString.\n        '127.0.0.1', # host/address of the server.\n        'mainUser', # Username to connect.\n        'mainUserPassword', # User password.\n        'notifications', # Database used.\n        3306, # Port of the server, set to 3306 by defaulté\n        true); # If has to validate. Is set to true by default.\n\n# This is how to create it from the functions within it.\n$connString = new SQLConnString();\n$connString-\u003esetName('MainDatabase');\n$connString-\u003esetAddress('127.0.0.1');\n$connString-\u003esetUsername('mainUser');\n$connString-\u003esetPassword('mainUserPassword');\n$connString-\u003esetDatabase('notifications');\n$connString-\u003esetPort(3589); # 3306 is the default port if none is supplied.\n$connString-\u003evalidate();\n```\n\nThere are quite a few variables you can give to the SQLConnString object. Here's a table of the different variables.\n\n| **Property** | **DataType** | **Description** | \n| ------------ | :---------- | :------------- | \n| $name | string | The name of the connection. | \n| $address | string | The host/address of the server server. | \n| $username | string | The username used to connect. | \n| $password | string | The password of the user. | \n| $database | string | The database used. | \n| $port | int | The port of the server. |\n\n#### SQLConnString::validate();\n\nThis function will validate the connection when created. If there is a problem, the function will throw a\nSQLRequestException with the message from the mysqli connection error. It is not obligated to validate a connection if\nyou know that it will work but note that if you use SQLConnString::create(), you must specify at the end with a false.\n\n#### How to work with SQLConnString?\n\nWhen you successfully created your SQLConnString object \u0026 validated (optional), you will need to register it to the list\nof SQLConnStrings, for that, you simple need to use one line of code, here it is :\n\n```php\nMyPigSQL::registerStringConn($connString); # Returns true if successful\n```\n\nThis function will throw a SQLRequestException if you try to register two SQLConnStrings with the same\nname (`$connString-\u003egetName()`)\nIf for some reason you need to also unregister it, you can use this function:\n\n```php\nMyPigSQL::unregisterStringConn($connString); # Returns true if successful\n```\n\nHere is the list of functions related to the SQLConnString object.\n\n```php\nMyPigSQL::getSQLConnStringByName($connName); # Returns SQLConnString object or throws SQLRequestException.\nMyPigSQL::unregisterStringConn($SQLConnString); # Unregisters the SQLConnString object (must be supplied with the object).\nMyPigSQL::unregisterStringConnByName($SQLConnStringName); # Unregisters the SQLConnString object by name.\nMyPigSQL::registerStringConn($connString); # Registers the SQLConnString (must be supplied with the object).\n\n# ['address' =\u003e $address, 'username' =\u003e $user, 'password' =\u003e $password, 'database' =\u003e $database, 'port' =\u003e $port]\nMyPigSQL::validateConnString($connStringData); # Will validate the data to create a SQLConnString.\n```\n\n### SQLRequest Creation\n\nWhen you are done with creating \u0026 registering your SQLConnStrings, you will finally be able to create new SQLRequests.\nThe way you make them is exactly the same as how you create SQLConnStrings. You can use `SQLRequest::create()`\nor `$request = new SQLRequest()`. Here are two examples:\n\n```php\n# This is how to create a new Utils from the Utils::create() function.\n$requestTwo = SQLRequest::create(\n        'SELECT * FROM PlayerData WHERE xuid = ?',\n        's',\n        [$player-\u003egetUniqueId()-\u003etoString()],\n        self::getSQLConnStringByName('MainDatabase'),\n        function (array $data) use ($player){\n            $player-\u003esendMessage(\"Welcome {$player-\u003egetName()}, you have {$data['kills'] kills!}\");\n        }\n);\n# This is how to create it from the functions within it.\n$request = new SQLRequest();\n$request-\u003esetQuery(\"SELECT * FROM FriendRequests WHERE id = ?\");\n$request-\u003esetDataTypes('s');\n$request-\u003esetDataKeys([$player-\u003egetUniqueId()-\u003etoString()]);\n$request-\u003esetConnString(MyPigSQL::getSQLConnStringByName('MainDatabase'));\n$request-\u003esetCallable(function(array $data) use ($player){\n        $player-\u003esendMessage-\u003e('Wow, this is very fine! Here is when the relation has been created: ' . $data['reg_date']);\n});\n\n# Here is a real-world example of a fully implemented command using MyPigSQL.\n    /**\n     * @param CommandSender $sender\n     * @param string $commandLabel\n     * @param array $args\n     * @throws SQLRequestException\n     */\n    public function execute(CommandSender $sender, string $commandLabel, array $args): void\n    {\n        if (!$sender-\u003ehasPermission(DefaultPermissions::ROOT_OPERATOR)) {\n            $sender-\u003esendMessage(Translation::getMessage('noPermission'));\n            return;\n        }\n        if (!isset($args[1])) {\n            $sender-\u003esendMessage(Translation::getMessage('usageMessage', [\n                'usage' =\u003e $this-\u003egetUsage()\n            ]));\n            return;\n        }\n        if (!is_numeric($args[1])) {\n            $sender-\u003esendMessage(Translation::getMessage('notNumeric'));\n            return;\n        }\n\n        $player = $this-\u003egetCore()-\u003egetServer()-\u003egetPlayerExact($args[0]);\n        MyPigSQL::addQueryToBatch(SQLRequest::create(\n            'UPDATE players SET shards = shards + ? WHERE username = ?',\n            'ss',\n            [$args[1], $args[0]],\n            MyPigSQL::getSQLConnStringByName('mainDB'),\n            function(?array $data) use ($sender, $player, $args){\n                $name = $args[0];\n                if($player instanceof CorePlayer){\n                    $player-\u003eaddShards((int)$args[1]);\n                    $name = $player-\u003egetName();\n                }\n                $sender-\u003esendMessage(Translation::getMessage('addShardsSuccess', [\n                    'amount' =\u003e TF::GREEN . '' . $args[1],\n                    'name' =\u003e TF::GOLD . $name\n                ]));\n            }\n        ));\n    }\n```\n\nThere are quite a few variables you can give to the SQLRequest object. Here's a table of the different variables.\n\n| **Property** | **DataType**   | **Description** | \n| ------------ | :------------- | :------------- |\n| $query | string | The query string. | \n| $dataTypes | string | The variable types used in the prepared statement. | \n| $dataKeys | array |The variables used in the prepared statement. | \n| $connString | SQLConnString | The SQLConnString object used for the request. | \n| $validated | bool | If the request has been sent to the MySQL server. | \n| $id | string | The id of the request. |\n\nTo register your query in the next batch of queries to be sent, you need to use this simple line of code:\n\n```php\nMyPigSQL::addQueryToBatch($request); # NOTE: MIGHT BE CHANGED TO addRequestToBatch().\n```\n\nHere is a list of the other functions related to SQLRequests.\n\n```php\nMyPigSQL::addQueryToBatch($request); # Will register the request to the batch.\nMyPigSQL::removeQueryFromBatch($id, $batch); # Will unset the request from the batch, must be supplied with the request's id \u0026 batch.\nMyPigSQL::getQueryFromBatch($id); # Will return the Utils object from the batch, must be supplied with the request's id.\n```\n\n### DispatchBatchPool\n\n`New Since 3.0.0-beta`\nThis class extends `Pool` and has, for now, no methods. But the end goal will be to let users create their own batches\nof requests and send them over a custom `DispatchBatchPool ` where they will be able to set a batch, the amount\nof `DispatchBatchThread` they want in them and other few options. The batch will be divided by the amount\nof `DispatchBathThread` the user set.\n\n### Notes\n\n1. `MyPigSQL::addQueryToBatch($request);` might be changed to `MyPigSQL::addRequestToBatch($request);`.\n2. `SQLRequest::setDataKeys($dataList);` might be changed to `SQLRequest::setDataInput($dataList);`.\n3. You can, if you do not want to register a new SQLConnString, create an SQLConnStrig without\n   a `$name` (`SQLConnString::setName($name)`) by making it `null` when creating it.\n4. A thread for each SQLConnStrings might be implemented to speed up the execution of the batch.\n\n### Why this?\n\nI made this plugin in my spare time because I didn't want to use another library for my network and because I am\nplanning to connect all my other systems (PigNotify, PigFriend \u0026 more to come) \"together\". My only problem was the use\nof threads for each plugins so this library fixes the issue by using one thread for all other systems.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcupidonsauce173%2Fmypigsql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcupidonsauce173%2Fmypigsql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcupidonsauce173%2Fmypigsql/lists"}