{"id":27048556,"url":"https://github.com/aschollwoeck/asmerald","last_synced_at":"2026-04-13T13:32:37.034Z","repository":{"id":65130667,"uuid":"556911244","full_name":"aschollwoeck/Asmerald","owner":"aschollwoeck","description":"A library for writing type safe SQL in C#.","archived":false,"fork":false,"pushed_at":"2023-02-17T20:17:42.000Z","size":1178,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-05T04:34:50.676Z","etag":null,"topics":["csharp","postgresql","sql","sqlite"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aschollwoeck.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-10-24T18:48:08.000Z","updated_at":"2024-01-12T18:43:06.000Z","dependencies_parsed_at":"2023-02-16T01:00:56.995Z","dependency_job_id":null,"html_url":"https://github.com/aschollwoeck/Asmerald","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/aschollwoeck%2FAsmerald","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aschollwoeck%2FAsmerald/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aschollwoeck%2FAsmerald/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aschollwoeck%2FAsmerald/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aschollwoeck","download_url":"https://codeload.github.com/aschollwoeck/Asmerald/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247299850,"owners_count":20916193,"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":["csharp","postgresql","sql","sqlite"],"created_at":"2025-04-05T07:15:11.816Z","updated_at":"2026-04-13T13:32:36.996Z","avatar_url":"https://github.com/aschollwoeck.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"﻿# \u003cimg src=\"assets/asmerald_small.png\" alt=\"Asmerald\" width=\"32\"/\u003e Asmerald\nA library for writing type safe SQL statements in code.\n\n[![CI](https://github.com/aschollwoeck/Amorphous/actions/workflows/ci.yml/badge.svg)](https://github.com/aschollwoeck/Amorphous/actions/workflows/ci.yml)\n\u003cimg alt=\"Nuget\" src=\"https://img.shields.io/nuget/v/Asmerald?color=blue\"\u003e\n\nIt provides:\n- type safety where possible\n- support of major database providers\n- low runtime overhead\n\nSee this very good answer on Stackoverflow: https://stackoverflow.com/questions/22860167/what-exactly-does-type-safe-queries-means\n\n## Supported database providers\n|                   |    SQLite |  PostgreSQL |   MySql   |  MariaDb  | MSSql | Oracle  |\n|------------       |---------  |--------   |--------   |--------  |------  |-------- |\n| SQL standard      | ✓       |   ✓     |     ✓   |     ✓   |  ✓  |   ✓   |\n| Stored Procedures |         |   ✓     |     ✓   |     ✓   |  ✓  |   ✓   |\n| Provider specific | ✓       |        |         |        |    |      |\n\nSQL standard = Most common statements such as \"SELECT\", \"WHERE\", \"JOIN\"s, \"HAVING\", etc. (mostly SQL-92)\nProvider specific = Specific database statements (e.g. materialized views), etc.\n\n## Limitations\nBecause of SQL being a declarative language, not everything can be checked in code by a compiler without introducing new complexity or deviating from SQL.\nFor example, there is *no* check in place wether \n- tables of columns mentioned in SELECT, HAVING, GROUP By, etc. statements are added as \"FROM\" or \"JOIN\" statements.\n- ...\n\n## Task list\n- [x] Add PostgreSQL support\n- [x] Add SQL standard support for all database providers\n- [ ] Implement functional tests to verify generated SQL string\n- [ ] Source generators to transform queries during compilation to SQL strings. This approach would not cause any performance impact at runtime at all - type safety for free.\n\n## Example\nSimple query using Asmerald and Dapper:\n```C#\n// Build query with Asmerald\nvar dslCtxt = new SQLiteDSLContext();\nvar ts = dslCtxt\n    .Select(Tbl_Cards.Id(), Tbl_Cards.Name(), Tbl_Cards.Form().As(\"test\"))\n    .From\u003cTbl_Cards\u003e()\n    .Where(Tbl_Cards.Id().Greater(10))\n    .OrderBy(Tbl_Cards.Id())\n    .Limit(50)\n    .QueryBuilder\n    .BuildPreparedStatement();\n\n// Use result as input for Dapper query\nvar res = dbConnection.Query(ts.Statement, param: ts.Parameters)\n    .ToList();\n```\n\nJoins and Where statements:\n```C#\nvar stmt = dslCtxt\n    .Select()\n    .All()\n    .From\u003cTblCards\u003e()\n    .InnerJoin\u003cTblCards, TblPacks\u003e()\n    .On(TblCards.Pack_id(), TblPacks.Id())\n    .Where(TblCards.Attribute().Equal(\"s\")\n        .And(TblCards.Form().Equal(\"c\"))\n        .Group()\n        .And(TblCards.Id().Equal(1)\n        .And(TblCards.Id().Equal(2))))\n    .Limit(20)\n    .Offset(5)\n    .QueryBuilder\n    .BuildPreparedStatement();\n```\n\n## Why?\nDon't limit yourself to abstracted libraries / frameworks and utilize the potential databases provide.\nEnjoy writing SQL - with armor.\n\n**\"Why not use a ORM?\"**\n\nI used ORMs in multiple projects and have not had good experiences using them.\nMost of them forced me to do a \"code first\" approach which never worked for me as I design my database schemes first.\nBesides there were issues with errors during setup I spent hours fixing and figuering out cryptic error messages.\n\nWhen I had adjusted my code to the needs of the ORM and fixed error messages I got problems writing my statements.\nEasy ones are easy but once you get a bit more complex you basically have to write SQL again.\n\nIn the end hand-written SQL queries are required again.\n\nSo let's make sure that we get more safety when writing SQL queries and utilize the power of typed programming languages like C#.\n\n\n## How does it work?\nFor the query itself to the database you need to use an provider - this library only helps you by creating SQL query strings.\nRecommendation for querying data: https://github.com/DapperLib/Dapper\n\n1. Use binary (.exe) of Asmerald.Generate to create classes from database\n2. Include Asmerald library in your project\n\n### Prerequisite\nAsmerald builds upon an existing database scheme and makes the output available for databases providers to query from the database.\n\n\n## Performance impact\nFunction calls and string builder bring overhead to the table. \nIf you are running queries in a hot path - check if it has a major impact on performance.\nShouldn't because of JIT opitmisations but check either way.\n\n## Benchmark\nBenchmarking a simple SQL query which benefits Asmerald\n```SQL\nSELECT Id, Name, Form as 'test' FROM cards WHERE id \u003e @idGr ORDER BY Id LIMIT @limit\n```\nand translated to code with Asmerald\n```C#\ndslCtxt\n.Select(TblCards.Id(), TblCards.Name(), TblCards.Form().As(\"test\"))\n.From\u003cTblCards\u003e()\n.Where(TblCards.Id().Greater(10))\n.OrderBy(TblCards.Id())\n.Limit(50)\n.QueryBuilder\n.BuildPreparedStatement();\n```\nRunning those queries against a SQLite database in combination with Dapper returns following results:\n\n|      Method |     Mean |   Error |  StdDev | Ratio |\n|------------ |---------:|--------:|--------:|------:|\n| DapperRaw | 179.4 us | 1.48 us | 1.38 us |  1.00 |\n| Asmerald | 191.4 us | 2.28 us | 2.02 us |  1.07 |\n\nAsmerald adds about 7% overhead.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faschollwoeck%2Fasmerald","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faschollwoeck%2Fasmerald","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faschollwoeck%2Fasmerald/lists"}