feat: orm-cj 仓颉版 EF Core 风格 ORM 初始版本
- 基于数据模型(POCO + 注解)的映射与增删改查:DbContext/DbSet/ChangeTracker/QueryBuilder - ISqlDialect 方言接口 + PostgreSQL/openGauss 实现,零外部依赖 - @DbContext 宏:纯声明 DbSet 类自动展开为完整 DbContext 子类 - 迁移:Migration/Migrator/ModelSnapshot(快照 JSON)/MiniJson,从模型生成迁移 - 迁移 CLI:add/rm/update/downgrade/list/help,应用内嵌一行 db.cli(args) 接入 - DbContext 便捷方法:databaseExists/hasPendingMigrations/migrate - 57 个单元测试全部通过
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright (c) 2025 SimcuTeam. All rights reserved.
|
||||
* ORM 注解(对齐 .NET EF Core 数据注解特性):
|
||||
* - [Table("xxx")] → @Table["xxx"] 指定表名(target: Type)
|
||||
* - [Column("xxx")] → @Column["xxx"] 指定列名(target: MemberVariable)
|
||||
* - [Key] → @Key 标记主键
|
||||
* - [DatabaseGenerated(...)] → @AutoIncrement 整型主键自增
|
||||
* - [NotMapped] → @Ignore 不映射该字段
|
||||
* - [Required] → @Required 非空列
|
||||
* - [MaxLength(n)] → @MaxLength[Int64] 字符串列最大长度
|
||||
*
|
||||
* 说明:类级注解的 target 是 AnnotationKind.Type(Cangjie 的注解 target 枚举),
|
||||
* 不是 Java/.NET 的 Class。
|
||||
*/
|
||||
|
||||
package simcu::orm.annotations
|
||||
|
||||
/**
|
||||
* 指定实体对应的表名(对齐 .NET TableAttribute)。
|
||||
* 用法:@Table["users"] public class User { ... }
|
||||
*/
|
||||
@Annotation[target: [Type]]
|
||||
public class Table {
|
||||
public let name: String
|
||||
|
||||
public const init(name: String) {
|
||||
this.name = name
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定字段对应的列名(对齐 .NET ColumnAttribute)。
|
||||
* 用法:@Column["user_name"] public var _username: String = ""
|
||||
*/
|
||||
@Annotation[target: [MemberVariable]]
|
||||
public class Column {
|
||||
public let name: String
|
||||
|
||||
public const init(name: String) {
|
||||
this.name = name
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 标记字段为主键(对齐 .NET KeyAttribute)。
|
||||
* 未标注时,名为 _id 的字段默认为主键。
|
||||
* 用法:@Key public var _id: Int64 = 0
|
||||
*/
|
||||
@Annotation[target: [MemberVariable]]
|
||||
public class Key {
|
||||
public const init() {}
|
||||
}
|
||||
|
||||
/**
|
||||
* 标记整型主键自增(对齐 .NET DatabaseGeneratedOption.Identity)。
|
||||
* 名为 _id 的 Int64/Int32 主键默认自增,无需标注。
|
||||
* 用法:@AutoIncrement public var _id: Int64 = 0
|
||||
*/
|
||||
@Annotation[target: [MemberVariable]]
|
||||
public class AutoIncrement {
|
||||
public const init() {}
|
||||
}
|
||||
|
||||
/**
|
||||
* 标记字段不映射到列(对齐 .NET NotMappedAttribute)。
|
||||
* 用法:@Ignore public var _temp: String = ""
|
||||
*/
|
||||
@Annotation[target: [MemberVariable]]
|
||||
public class Ignore {
|
||||
public const init() {}
|
||||
}
|
||||
|
||||
/**
|
||||
* 标记列为非空(对齐 .NET RequiredAttribute),仅影响建表 DDL。
|
||||
* 用法:@Required public var _name: String = ""
|
||||
*/
|
||||
@Annotation[target: [MemberVariable]]
|
||||
public class Required {
|
||||
public const init() {}
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定字符串列最大长度(对齐 .NET MaxLengthAttribute),仅影响建表 DDL。
|
||||
* 用法:@MaxLength[100] public var _name: String = ""
|
||||
*/
|
||||
@Annotation[target: [MemberVariable]]
|
||||
public class MaxLength {
|
||||
public let maxLength: Int64
|
||||
|
||||
public const init(maxLength: Int64) {
|
||||
this.maxLength = maxLength
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user