Skip to content

Add select by type#771

Open
ngungbi wants to merge 2 commits intosqlkata:mainfrom
ngungbi:main
Open

Add select by type#771
ngungbi wants to merge 2 commits intosqlkata:mainfrom
ngungbi:main

Conversation

@ngungbi
Copy link
Copy Markdown

@ngungbi ngungbi commented Dec 17, 2025

Add Select<T>() to automatically add all the properties of a class into select component.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a generic Query.Select<T>() API to infer selected columns from a CLR type’s public properties (with optional [Column] attribute mapping), along with tests validating the generated SQL for PostgreSQL.

Changes:

  • Added Query.Select<T>() to build a select list from reflected properties, supporting [Column] for col AS Alias.
  • Added unit tests for selecting by type and for [Column]-driven column aliasing.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
QueryBuilder/Query.Select.cs Introduces Select<T>() implementation using reflection and [Column] attributes.
QueryBuilder.Tests/SelectTests.cs Adds tests to validate generated SQL for Select<T>() with and without [Column].

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +122 to +137
public Query Select<T>() where T : class {
var properties = typeof(T).GetProperties();
var columns = new List<string>();
foreach (var property in properties) {
if (property.GetSetMethod() == null) {
continue;
}

var name = property.Name;
var attribute = property.GetCustomAttribute<ColumnAttribute>();
if (attribute != null) {
name = $"{attribute.Name} as {name}";
}

columns.Add(name);
}
Comment on lines +125 to +128
foreach (var property in properties) {
if (property.GetSetMethod() == null) {
continue;
}
}

public Query Select<T>() where T : class {
var properties = typeof(T).GetProperties();
Comment on lines +122 to +124
public Query Select<T>() where T : class {
var properties = typeof(T).GetProperties();
var columns = new List<string>();
Comment on lines +964 to +971
public void SelectType() {
var q = new Query("Post").Select<Post>();

var pgsql = Compilers.CompileFor(EngineCodes.PostgreSql, q);

Assert.Equal("""
SELECT "Id", "Title", "Published" FROM "Post"
""", pgsql.ToString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants