Skip to content

gunivan/cooq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

  • The main idea started from: jOOQ, typedquery
  • Useful library to make typed-safe query to database, currently support PostgreSQL and MSSQL.
  • UI tool to generate mapped class from database.
  • Use CooQ with existing ORM project, the only thing need to do is generate classes and make query with syntax as below.

Background

  • Already work with ADO.NET

Generate Database Objects Class

  • Currently supports 2 database types - MSSQL and PostgreSQL. I use velocity to generate class by template. - You can extend as you want.

  • To generate template, you should open file CooQGenerate.exe then fill specific required information to connect to your database, then click Generate button.

  • Init CooQ with Connection String

  • You must init some information for CooQ once.

//for PostgreSQl
CooQ.Query.Init(new CooQ.ConnectionSetting()
  {
    Server = "localhost",
    Database = "database_name",
    Username = "postgres",
    Password = "root",
    Port = 5432,
    Type = CooQ.Types.DatabaseType.POSTGRESQL
});
//for MSSQL
CooQ.Query.Init(new CooQ.ConnectionSetting()
  {
    Server = @".\SQLEXPRESS",
    Database = "database_name",
    Type = CooQ.Types.DatabaseType.MSSQL
});
  • Make Query. Note that your database naming convention should be lowercase with _, for example your table name is TableTest then it should be table_test in your database.

Suppose you create a table in your database named table_test and your generated class is:

//we have 2 mapped class after generated:
Table.TableTest.cs
Record.TableTestRecord.cs
Supposed that table_test have 2 columns named column_a, column_b.
//We have two mapped columns after generated:
TableTest.TABLETEST.COLUMNA
TableTest.TABLETEST.COLUMNB
Then, you make a first query as below:
  • Select Query
IQueryable<Record> res = CooQ.Query.Select(TableTest.TABLETEST)
       .From(TableTest.TABLETEST)
       .Execute()
       .GetRows();

foreach (TableTestRecord record in res)
 {
   //your code here...
   Debug.Print("Column A is:" + record.ColumnA);
 }
  • Insert Query
Query.Insert(TableTest.TABLETEST)
      .Set(TableTest.TABLETEST.COLUMNA, "value 1")
      .Set(TableTest.TABLETEST.COLUMNB, "value 2");
  • Update Query
Query.Update(TableTest.TABLETEST)
      .Set(TableTest.TABLETEST.COLUMNA, "value 1")
      .Set(TableTest.TABLETEST.COLUMNB, "value 2");
  • Delete Query
Query.Delete(TableTest.TABLETEST)
        .Where(TableTest.TABLETEST.COLUMNA = "value 1");

License

This article, along with any associated source code and files, is licensed under The MIT License

About

A typed-safe query with idea of jOOQ, UI to generate mapping code for database, build dynamic query with avoid SQL injection.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages