{"id":18462689,"url":"https://github.com/extism/extism-sqlite-host-function","last_synced_at":"2025-04-15T19:54:18.959Z","repository":{"id":139443797,"uuid":"593733740","full_name":"extism/extism-sqlite-host-function","owner":"extism","description":"Demo of using a host function to expose a SQL database to your plugin","archived":false,"fork":false,"pushed_at":"2024-01-30T23:25:36.000Z","size":12,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-15T19:54:12.535Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","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/extism.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,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-01-26T18:08:36.000Z","updated_at":"2025-02-18T11:24:26.000Z","dependencies_parsed_at":"2024-01-31T00:48:53.324Z","dependency_job_id":null,"html_url":"https://github.com/extism/extism-sqlite-host-function","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/extism%2Fextism-sqlite-host-function","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/extism%2Fextism-sqlite-host-function/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/extism%2Fextism-sqlite-host-function/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/extism%2Fextism-sqlite-host-function/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/extism","download_url":"https://codeload.github.com/extism/extism-sqlite-host-function/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249145296,"owners_count":21219966,"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":[],"created_at":"2024-11-06T09:04:04.716Z","updated_at":"2025-04-15T19:54:18.940Z","avatar_url":"https://github.com/extism.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Extism Host Function SQL Demo\n\n\u003e Warning: This example is pre-1.0 Extism. If you'd like to update it, please do! If you're not sure how\n\u003e please reach out to us in Discord.\n\nThis is a simple demo of using a host function to expose a SQL database to your plugin. In this case our host\nis python and our database is a sqlite database. Our plugin is written in Rust.\n\n## Build and Run\n\n```bash\npoetry install\ncd plugin/ \u0026\u0026 cargo build --target wasm32-unknown-unknown --release \u0026\u0026 cd ..\npoetry run python main.py\n```\n\n## How it works\n\nThe host holds a read-only connection to the sqlite database of accounts:\n\n```python\ndb_conn = sqlite3.connect(\"file:accounts.db?mode=ro\", uri=True)\n```\n\nWe can create a host function called `execute_sql` that allows our plugin to execute sql on it.\n\n\u003e **Note**: If you want more control or security, you should consider giving your plugin a more granular interface instead of letting it make raw queries.\n\n\n```python\nfunctions = [\n    Function(\n        \"execute_sql\",\n        [ValType.I64],\n        [ValType.I64],\n        execute_sql,\n        db_conn,\n    )\n]\n\nplugin = context.plugin(config, functions=functions)\n```\n\nThe `execute_sql` function takes the db_conn as userdata so it can be used by the implementation. You can think of this like a closure. The function signature has a pointer (i64) input and a pointer (i64) output.\n\n* The `input` is a pointer to a UTF-8 SQL string\n* The `output` is a pointer to a UTF-8 JSON string of results\n\nWe can implement it like this:\n\n\n```python\n@host_fn\ndef execute_sql(plugin, input, output, db_conn):\n    # Get the SQL query from the input, it's raw utf-8 bytes\n    mem = plugin.memory_at_offset(input[0])\n    query = str(plugin.memory(mem)[:], 'UTF-8')\n\n    # execute the query and serialize it as json bytes\n    results = db_conn.execute(query).fetchall()\n    out = json.dumps(results).encode()\n\n    # allocate output memory\n    mem = plugin.alloc(len(out))\n    plugin.memory(mem)[:] = out\n    output[0].value = mem.offset\n```\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fextism%2Fextism-sqlite-host-function","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fextism%2Fextism-sqlite-host-function","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fextism%2Fextism-sqlite-host-function/lists"}