Skip to content

trampster/SqlSrcGen

Repository files navigation

Logo SqlSrcGen

SqlSrcGen is a SQL first, reflection free micro ORM for SQLite using c# source generators. The class definitions and Object Relational Mappings are created automatically from your SQL CREATE TABLE commands.

Advantages

  • No need to manually define c# classes for your tables
  • High performance - mapping code is reflection free and optimized at compile time
  • SQL code is compile time checked
  • AOT friendly - no reflection

Getting Started

  1. Create a .sql file in your project which includes the CREATE TABLE SQL commands defining your database.
CREATE TABLE contact (name Text not null primary key, email Text not null);
  1. Include that .sql file as AdditionalFiles in your .csproj
<ItemGroup>
    <AdditionalFiles Include="SqlSchema.sql" />
</ItemGroup>
  1. Do crud operations on the tables
var database = new Database(databaseName);

// create the table
database.CreateContactTable();

// insert a record
database.InsertContact(new Contact() 
{ 
    Name = "Steve Rogers", 
    Email = "steve@avengers.com"
});

// query all records in the table
var list = new List<Contact>();
database.AllContacts(list);

// get row via primary key, (only generated for tables with a primary key)
var contact = new Contact();
bool found = database.GetContact(contact, "Steve Rogers");

// delete all rows from table
database.DeleteAllContacts();

// delete a row via primary key (only generated for tables with a primary key)
database.DeleteContact("Steve Rogers");

// Begin a transaction
database.BeginTransaction();

// Commit a transaction
database.CommitTransaction();

// Rollback a transaction
database.RollbackTransaction();

Future Work

SqlSrcGen currently only supports basic crud operations generated directly from sql table definitions. Future features include:

  • Custom queries (select, joins etc)

Sponsor this project

 

Packages

No packages published

Languages