From 5bcec3a0b1f2ec7609d6d3c8489dd29b494e076e Mon Sep 17 00:00:00 2001 From: Dunak <19434539+dunak-debug@users.noreply.github.com> Date: Tue, 17 Aug 2021 15:18:45 +0200 Subject: [PATCH] New documentation --- .github/workflows/gh-pages.yml | 32 ++++++++ _config.yml | 1 + docs/.pages | 4 + docs/exports/execute.md | 47 ++++++++++++ docs/exports/index.md | 3 + docs/exports/insert.md | 36 +++++++++ docs/exports/prepare.md | 1 + docs/exports/scalar.md | 36 +++++++++ docs/exports/single.md | 36 +++++++++ docs/exports/transaction.md | 102 +++++++++++++++++++++++++ docs/exports/update.md | 36 +++++++++ docs/index.md | 107 ++++++++++++++++++++++++++ docs/issues.md | 7 ++ docs/stylesheets/extra.css | 22 ++++++ mkdocs.yml | 132 +++++++++++++++++++++++++++++++++ requirements.txt | 7 ++ 16 files changed, 609 insertions(+) create mode 100644 .github/workflows/gh-pages.yml create mode 100644 _config.yml create mode 100644 docs/.pages create mode 100644 docs/exports/execute.md create mode 100644 docs/exports/index.md create mode 100644 docs/exports/insert.md create mode 100644 docs/exports/prepare.md create mode 100644 docs/exports/scalar.md create mode 100644 docs/exports/single.md create mode 100644 docs/exports/transaction.md create mode 100644 docs/exports/update.md create mode 100644 docs/index.md create mode 100644 docs/issues.md create mode 100644 docs/stylesheets/extra.css create mode 100644 mkdocs.yml create mode 100644 requirements.txt diff --git a/.github/workflows/gh-pages.yml b/.github/workflows/gh-pages.yml new file mode 100644 index 0000000..14a7929 --- /dev/null +++ b/.github/workflows/gh-pages.yml @@ -0,0 +1,32 @@ +name: build docs +on: + push: + branches: + - docs +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + submodules: "recursive" + fetch-depth: 0 # Fetch all history for .GitInfo and .Lastmod + - name: Setup Python + uses: actions/setup-python@v1 + with: + python-version: '3.7' + architecture: 'x64' + - name: Install dependencies + run: | + python3 -m pip install --upgrade pip # install pip + python3 -m pip install mkdocs # install mkdocs + python3 -m pip install mkdocs-material # install material theme + python3 -m pip install -r ./requirements.txt + + - name: Build site + run: mkdocs build + - name: Deploy + uses: JamesIves/github-pages-deploy-action@4.1.4 + with: + branch: gh-pages + folder: site \ No newline at end of file diff --git a/_config.yml b/_config.yml new file mode 100644 index 0000000..2f7efbe --- /dev/null +++ b/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-minimal \ No newline at end of file diff --git a/docs/.pages b/docs/.pages new file mode 100644 index 0000000..f401242 --- /dev/null +++ b/docs/.pages @@ -0,0 +1,4 @@ +nav: + - index.md + - issues.md + - exports \ No newline at end of file diff --git a/docs/exports/execute.md b/docs/exports/execute.md new file mode 100644 index 0000000..9976446 --- /dev/null +++ b/docs/exports/execute.md @@ -0,0 +1,47 @@ +--- +title: Execute +--- +Generic function that can be utilised for any query, synonymous with fetch. +When selecting data it will return all matching rows and columns, otherwise it will return field data such as insertid, affectedRows, etc. + +!!! info "Example" + === "Lua + === "Async" + ```lua + exports.oxmysql:execute('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, function(result) + if result then + for _, v in pairs(result) do + print(v.identifier, v.firstname, v.lastname) + end + end + end) + ``` + === "Sync" + ```lua + local result = exports.oxmysql:executeSync('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}) + if result then + for _, v in pairs(result) do + print(v.identifier, v.firstname, v.lastname) + end + end + ``` + === "JavaScript + === "Async" + ```js + exports.oxmysql.execute('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], function(result) { + if (result) { + result.forEach((v) => { + console.log(v.identifier, v.firstname, v.lastname) + }) + } + }) + ``` + === "Sync" + ```js + const result = exports.oxmysql.executeSync('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) { + if (result) { + result.forEach((v) => { + console.log(v.identifier, v.firstname, v.lastname) + }) + } + ``` \ No newline at end of file diff --git a/docs/exports/index.md b/docs/exports/index.md new file mode 100644 index 0000000..c74a5ec --- /dev/null +++ b/docs/exports/index.md @@ -0,0 +1,3 @@ +--- +title: Index +--- \ No newline at end of file diff --git a/docs/exports/insert.md b/docs/exports/insert.md new file mode 100644 index 0000000..6148b80 --- /dev/null +++ b/docs/exports/insert.md @@ -0,0 +1,36 @@ +--- +title: Insert +--- +Inserts a new entry into the database and returns the insert id for the row, if valid. + +!!! info "Example" + === "Lua + === "Async" + ```lua + exports.oxmysql:insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', {playerIdentifier, firstName, lastName}, function(id) + if id then + print(id) + end + end) + ``` + === "Sync" + ```lua + local id = exports.oxmysql:insertSync('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', {playerIdentifier, firstName, lastName}) + if id then + print(id) + end + ``` + === "JavaScript + === "Async" + ```js + exports.oxmysql.insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', [playerIdentifier, firstName, lastName], function(id) { + if (id) + console.log(id) + }) + ``` + === "Sync" + ```js + const id = exports.oxmysql.insertSync('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?) ', [playerIdentifier, firstName, lastName]) { + if (id) + console.log(id) + ``` \ No newline at end of file diff --git a/docs/exports/prepare.md b/docs/exports/prepare.md new file mode 100644 index 0000000..30404ce --- /dev/null +++ b/docs/exports/prepare.md @@ -0,0 +1 @@ +TODO \ No newline at end of file diff --git a/docs/exports/scalar.md b/docs/exports/scalar.md new file mode 100644 index 0000000..1f8cce8 --- /dev/null +++ b/docs/exports/scalar.md @@ -0,0 +1,36 @@ +--- +title: Scalar +--- +Returns the first column for a single row. + +!!! info "Example" + === "Lua + === "Async" + ```lua + exports.oxmysql:scalar('SELECT firstname FROM users WHERE identifier = ?', {playerIdentifier}, function(result) + if result then + print(result.firstname) + end + end) + ``` + === "Sync" + ```lua + local result = exports.oxmysql:scalarSync('SELECT firstname FROM users WHERE identifier = ?', {playerIdentifier}) + if result then + print(result.firstname) + end + ``` + === "JavaScript + === "Async" + ```js + exports.oxmysql.scalar('SELECT firstname FROM users WHERE identifier = ?', [playerIdentifier], function(result) { + if (result) + console.log(result.firstname) + }) + ``` + === "Sync" + ```js + const result = exports.oxmysql.scalarSync('SELECT firstname FROM users WHERE identifier = ?', [playerIdentifier]) { + if (result) + console.log(result.firstname) + ``` \ No newline at end of file diff --git a/docs/exports/single.md b/docs/exports/single.md new file mode 100644 index 0000000..87719ce --- /dev/null +++ b/docs/exports/single.md @@ -0,0 +1,36 @@ +--- +title: Single +--- +Returns the columns for a single row. + +!!! info "Example" + === "Lua + === "Async" + ```lua + exports.oxmysql:single('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, function(result) + if result then + print(result.identifier, result.firstname, result.lastname) + end + end) + ``` + === "Sync" + ```lua + local result = exports.oxmysql:singleSync('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}) + if result then + print(result.identifier, result.firstname, result.lastname) + end + ``` + === "JavaScript + === "Async" + ```js + exports.oxmysql.single('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], function(result) { + if (result) + console.log(result.identifier, result.firstname, result.lastname) + }) + ``` + === "Sync" + ```js + const result = exports.oxmysql.singleSync('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) { + if (result) + console.log(result.identifier, result.firstname, result.lastname) + ``` \ No newline at end of file diff --git a/docs/exports/transaction.md b/docs/exports/transaction.md new file mode 100644 index 0000000..77da752 --- /dev/null +++ b/docs/exports/transaction.md @@ -0,0 +1,102 @@ +TODO: update information and formatting + + + + + +# Transactions +A transaction executes multiple queries and commits them only if all succeed. If one fails, none of the queries are committed. The return value is a `boolean`, which is the result of the transaction. + +## Specific Format +When using the `Specific` format you must pass one parameter to the oxmysql export. In this case, the `queries` parameter contains your queries and the SQL variables unique to each query. + +This is useful for transactions where the queries do not share many SQL variables. + +### Usage +Lua +```lua +local queries = { + { query = 'INSERT INTO `test` (id) VALUES (:someid)', values = { ['someid'] = 1 } }, + { query = 'INSERT INTO `test` (id) VALUES (:someid)', values = { ['someid'] = 2 } } +} -- NOTE, the 'values' tables can be named 'parameters' here for MySQL-Async compatibility. + +-- Async +exports.oxmysql:transaction(queries, function(result) + print(result) +end) + +-- Sync +local result = exports.oxmysql:transactionSync(queries) +print(result) +``` + +Javascript +```js +const queries = [ + { query = 'INSERT INTO `test` (id) VALUES (:someid)', values = { someid = 1 } }, + { query = 'INSERT INTO `test` (id) VALUES (:someid)', values = { someid = 2 } } +] // NOTE, the 'values' objects can be named 'parameters' here for MySQL-Async compatibility. + +// Async +exports.oxmysql.transaction(queries, (result) => { + console.log(result) +}) + +// Sync +const result = await exports.oxmysql.transactionSync(queries) +console.log(result) +``` + +### Shared Format +When using the `Shared` format you must pass two parameters to the oxmysql export. The `queries` and the `parameters` those queries will use. +This is useful if your queries use the same SQL variables. + +Lua +```lua +local queries = { + 'INSERT INTO `test` (id, name) VALUES (:someid, :somename)', + 'SET `name` = :newname IN `test` WHERE `id` = :someid' +} + +local parameters = { ['someid'] = 2, ['somename'] = 'John Doe', ['newname'] = 'John Notdoe' } + +-- Async +exports.oxmysql:transaction(queries, parameters, function(result) + print(result) +end) + +-- Sync +local result = exports.oxmysql:transactionSync(queries, parameters) +print(result) +``` + +Javascript +```js +const queries = [ + 'INSERT INTO `test` (id, name) VALUES (:someid, :somename)', + 'SET `name` = :newname IN `test` WHERE `id` = :someid' +] + +const parameters = { someid = 2, somename = 'John Doe', newname = 'John Notdoe' } + +// Async +exports.oxmysql.transaction(queries, parameters, (result) => { + console.log(result) +}) + +// Sync +const result = await exports.oxmysql.transactionSync(queries, parameters) +console.log(result) +``` + +## Transaction Isolation Level + +This can be set through the convar `mysql_transaction_isolation_level` which should be an integer ranging from `1-4`. The default convar value set by oxmysql is `2`. + + +| Convar Value | Result | +| -------------- | ------------------ | +| 1 | Repeatable Read | +| 2 | Read Committed | +| 3 | Read Uncommitted | +| 4 | Serializable | diff --git a/docs/exports/update.md b/docs/exports/update.md new file mode 100644 index 0000000..688cc92 --- /dev/null +++ b/docs/exports/update.md @@ -0,0 +1,36 @@ +--- +title: Update +--- +Updates an entry in the database and returns the number of affected rows. + +!!! info "Example" + === "Lua + === "Async" + ```lua + exports.oxmysql:insert('UPDATE users SET firstname = ? WHERE identifier = ? ', {newName, playerIdentifier}, function(affectedRows) + if affectedRows then + print(affectedRows) + end + end) + ``` + === "Sync" + ```lua + local id = exports.oxmysql:insertSync('UPDATE users SET firstname = ? WHERE identifier = ? ', {newName, playerIdentifier}) + if affectedRows then + print(affectedRows) + end + ``` + === "JavaScript + === "Async" + ```js + exports.oxmysql.insert('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier], function(affectedRows) { + if (affectedRows) + console.log(affectedRows) + }) + ``` + === "Sync" + ```js + const id = exports.oxmysql.insertSync('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier]) { + if (affectedRows) + console.log(affectedRows) + ``` \ No newline at end of file diff --git a/docs/index.md b/docs/index.md new file mode 100644 index 0000000..ae24d46 --- /dev/null +++ b/docs/index.md @@ -0,0 +1,107 @@ +--- +title: Getting Started +--- + +## Installation +- Download the [latest build](https://github.com/overextended/oxmysql/releases/latest) of oxmysql (not the source code). +- Extract the contents of the archive to your resources folder. +- Start the resource near the top of your resources in your `server.cfg`. +- If you have a lot of streamed assets, load them first to prevent timing out the connection. + + +## Configuration +You can change the configuration settings by using convars inside your `server.cfg`. +Reference the following for an idea of how to set your connection options. +You must include one of the following lines, adjusted for your connection and database settings. +``` +set mysql_connection_string "mysql://root:12345@localhost/es_extended?charset=utf8mb4" +set mysql_connection_string "user=root;database=es_extended;password=12345;charset=utf8mb4" +``` +For more optional settings (such as multiple statements) you can reference [pool.d.ts](https://github.com/sidorares/node-mysql2/blob/master/typings/mysql/lib/Pool.d.ts#L10) and [connection.d.ts](https://github.com/sidorares/node-mysql2/blob/master/typings/mysql/lib/Connection.d.ts#L8). + +You can also add the following convars if you require extra information when testing queries. +``` +set mysql_slow_query_warning 100 +set mysql_debug true +``` + + +## Sync vs. async +Asychronous behaviour does not _truly_ exist in any of the scripting runtimes utilised, with tasks being performed in a queue each tick. +Two variants of exports are provided for users, and their names describe their behaviour as part of the active "thread" being executed. + +The following export is "asynchronous" and will execute in order, however the result will be delayed until a result is returned rather than pausing execution of following code. +```lua + print('This print will display first') + exports.oxmysql:scalar('SELECT group FROM users WHERE identifier = ?', {playerIdentifier}, function(result) + print('Group: '..result) + end) + print('This print will display second') +``` + +Conversely, the following export will halt the active thread and wait for it to resolve before continuing. +Furthermore, the result will be returned inline (left-hand assignment) rather than being locally scoped to the callback function. +```lua + print('This print will display first') + local result = exports.oxmysql:scalarSync('SELECT group FROM users WHERE identifier = ?', {playerIdentifier}) + print('Group: '..result) + print('This print will display third') +``` + + +## Benchmark +``` +Lua: Low: 0.2955ms | High: 16.7566ms | Avg: 0.36956378ms | Total: 3695.6378ms (10000 queries) +JS: Low: 0.2831ms | High: 5.1899ms | Avg: 0.33495788ms | Total: 3349.5788ms (10000 queries) +``` +These benchmark _exports_ rather than actual query speed, resulting in extra delays due to serialisation and overhead. Lua generally falls _slightly_ behind, but the amount is negligible. Furthermore we utilise the _sync_ variants for this, so a query will only execute once the previous one has returned. + +Feel free to run your own tests. +#### Lua (5.4) +```lua +local val = 10000 +RegisterCommand('luasync', function() + local queryTimesLocal = {} + local result + for i=1, val do + local time = os.nanotime() + local r = exports.oxmysql:scalarSync('SELECT * from users') + queryTimesLocal[#queryTimesLocal+1] = (os.nanotime() - time) / 1000000 + if i==1 then result = r end + end + local queryMsLow, queryMsHigh, queryMsSum = 1000, 0, 0 + for _, v in pairs(queryTimesLocal) do + queryMsSum = queryMsSum + v + end + for _, v in pairs(queryTimesLocal) do + if v > queryMsHigh then queryMsHigh = v end + end + for _, v in pairs(queryTimesLocal) do + if v < queryMsLow then queryMsLow = v end + end + local averageQueryTime = queryMsSum / #queryTimesLocal + print(json.encode(result)) + print('Low: '.. queryMsLow ..'ms | High: '..queryMsHigh..'ms | Avg: '..averageQueryTime..'ms | Total: '..queryMsSum..'ms ('..#queryTimesLocal..' queries)') +end) +``` + +#### +```js +const val = 10000 +RegisterCommand('jssync', async() => { + const queryTimesLocal = []; + let result + for(let i=0; i < val; i++) { + const startTime = process.hrtime.bigint() + const r = await exports.oxmysql.scalarSync('SELECT * from users') + queryTimesLocal.push(Number(process.hrtime.bigint() - startTime) / 1000000) + if (i === 0) result = 1 + } + const queryMsSum = queryTimesLocal.reduce((a, b) => a + b, 0) + const queryMsHigh = queryTimesLocal.sort((a, b) => b - a)[0] + const queryMsLow = queryTimesLocal.sort((a, b) => a - b)[0] + const averageQueryTime = queryMsSum / queryTimesLocal.length + console.log(result) + console.log('Low: '+ queryMsLow +'ms | High: '+queryMsHigh+'ms | Avg: '+averageQueryTime+'ms | Total: '+queryMsSum+'ms ('+queryTimesLocal.length+' queries)') +}) +``` \ No newline at end of file diff --git a/docs/issues.md b/docs/issues.md new file mode 100644 index 0000000..312852e --- /dev/null +++ b/docs/issues.md @@ -0,0 +1,7 @@ +--- +title: Common issues +--- +### Placeholder +Integer faucibus justo neque. Vivamus vitae posuere tellus. Pellentesque aliquet semper nisi, sit amet pulvinar ligula viverra facilisis. Morbi lacus lacus, finibus quis hendrerit at, gravida quis arcu. +!!! check "Solution" + Proin imperdiet ipsum nisi, et ullamcorper tellus gravida quis. Proin at ipsum et tortor bibendum gravida. \ No newline at end of file diff --git a/docs/stylesheets/extra.css b/docs/stylesheets/extra.css new file mode 100644 index 0000000..c67ed9d --- /dev/null +++ b/docs/stylesheets/extra.css @@ -0,0 +1,22 @@ +.md-content { + --md-typeset-a-color: rgb(83, 108, 238); +} + +.md-typeset { + color-adjust: exact; + font-size: .71rem; + line-height: 1.3; +} + +[data-md-color-scheme="slate"] { + --md-hue: 215; + --md-default-bg-color: rgb(40, 43, 57); +} + +[data-md-color-primary="indigo"] { + --md-primary-fg-color: rgb(55, 70, 149); + --md-primary-fg-color--light: rgb(93, 108, 192); + --md-primary-fg-color--dark: rgb(48, 63, 161); + --md-primary-bg-color: rgb(255, 255, 255); + --md-primary-bg-color--light: rgba(255, 255, 255, 0.7); +} \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml new file mode 100644 index 0000000..4197776 --- /dev/null +++ b/mkdocs.yml @@ -0,0 +1,132 @@ +site_name: OxMySQL +site_description: Documentation +site_author: Overextended + +# Repository +repo_name: OxMySQL +repo_url: https://github.com/overextended/oxmysql +edit_uri: "" + +# Copyright +copyright: Copyright © 2021 Overextended + +# Configuration +docs_dir: docs +site_dir: site +strict: true + +# nav: +# - Getting Started: "index.md" +# - Functions: +# - "functions/index.md" +# - Client: +# - "functions/client/progress.md" +# - "functions/client/playanim.md" +# - "functions/client/playanimadvanced.md" +# - "functions/client/inventorysearch.md" +# - Shared: +# - "functions/shared/checktable.md" +# - "functions/shared/copytable.md" +# - "functions/shared/matchtables.md" +# - "functions/shared/tablecontains.md" +# - Items: +# - items/index.md +# - items/client.md +# - items/server.md + +# Theme +theme: + name: material + + # 404 Page + static_templates: + - 404.html + + # Don't include MKDoc's JavaScript + include_search_page: false + search_index_only: true + + # Default values, taken from mkdocs_theme.yml + language: en + features: + - tabs + - navigation.instant + - navigation.tabs + - navigation.tabs.sticky + - toc.integrate + - navigation.top + palette: + scheme: slate + primary: indigo + accent: indigo + font: + text: Roboto + code: Roboto Code + icon: + logo: material/school + repo: fontawesome/brands/github + favicon: assets/images/favicon.png + +# Plugins +plugins: + - search + - awesome-pages + +extra_css: + - stylesheets/extra.css + +# Social +extra: + social: + - icon: fontawesome/brands/github + link: https://github.com/overextended/ox_inventory + - icon: fontawesome/brands/discord + link: https://discord.io/overextended + +# Extensions +markdown_extensions: + - toc: + toc_depth: 2 + - markdown.extensions.admonition + - markdown.extensions.attr_list + - markdown.extensions.codehilite: + guess_lang: false + linenums: true + - markdown.extensions.def_list + - markdown.extensions.footnotes + - markdown.extensions.meta + - markdown.extensions.toc: + permalink: true + - pymdownx.betterem: + smart_enable: all + - pymdownx.caret + - pymdownx.critic + - pymdownx.details + - pymdownx.emoji: + emoji_index: !!python/name:materialx.emoji.twemoji + emoji_generator: !!python/name:materialx.emoji.to_svg + - pymdownx.inlinehilite + - pymdownx.keys + - pymdownx.magiclink: + provider: "github" + repo_url_shortener: true + repo_url_shorthand: true + social_url_shorthand: true + labels: + { + "commit": "Commit", + "compare": "Compare", + "issue": "Issue", + "pr": "Pull Request", + "mention": "User", + "repo": "Repository", + } + - pymdownx.mark + - pymdownx.smartsymbols + - pymdownx.snippets: + check_paths: true + - pymdownx.superfences + - pymdownx.tabbed + - pymdownx.tasklist: + custom_checkbox: true + - pymdownx.tilde diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1e1aba1 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +# Direct dependencies +mkdocs>=1.1 +Pygments>=2.4 +markdown>=3.2 +pymdown-extensions>=7.0 +mkdocs-material-extensions>=1.0 +mkdocs-awesome-pages-plugin>=2.2.1 \ No newline at end of file