Categorygithub.com/suifengpiao14/sqlbuilder
modulepackage
0.1.18
Repository: https://github.com/suifengpiao14/sqlbuilder.git
Documentation: pkg.go.dev

# README

SQL拼接

从2个角度看问题: column 是从模型、表的角度出发,对表进行增改珊查操作,以及建表等操作,将表拆分成列,在此基础上以列为最小基本单元组合 params 是从接口操作角度出发,对接口字段拆分成字段,在此基础上以字段为最小基本单元组合

二者区别: 表模型角度:增改删查是一体的,是一个模型的基本操作,要求每个操作对应的column是固定不变的 params 角度:新增、修改、删除、查询 是各自独立的操作,之间没有关系,不能融合在一起,每个操作的column可以不一样 表模型和接口模型不是一一对应,包含或者被包含关系,它们可以互相交叉,一个接口操作多个表的部分数据,一个表可以提供多个接口数据操作 二者联系:

  1. 表模型的列和接口模型的字段是同一个东西,2种不同的叫法。列更偏向描述业务模型中的属性,定义是什么,接口字段更偏向描述这个属性的特征检测,如手机号——phone, 表模型确定名称:phone,接口代码更偏向确保这个字段的值符合手机号规则,确定其是否可以为空等、根据具体业务验证否唯一等逻辑,有时在不同的接口(场景下)其验证规则可能不一样(比如要求唯一:新增时,表内不存在即可,更新时要求表内除了当前记录外不存在才行)
  2. 最简单、通用的 增加、修改、删除、查询操作按照表模型全量字段进行,不做任何验证、检测,这样的接口非常通用,但是其业务约束力差

根据以上分析,表模型定义了对象及其属性,负责对象、属性命名,及其存储。接口模型定义了对象在特定场景(指定接口)部分属性的特征检测,这些属性既有通用属性检测(手机号格式),又有具体业务属性检测(是否可空、是否唯一、在定义的枚举值范围内、字符串、整型)等

表、接口都是解决业务问题,特定业务本身具有一定的内涵属性,比如唯一标识,只能新增、不可修改,条件查询时支持全匹配。手机号、邮箱号原本就对应固定格式,删除字段新增默认非删除,查询接口都要携带非删除条件等。这些在属性可以分层固定下来,以便减轻具体业务开发时的心智负担

经过上述分析,分类总结如下:

  1. 表模型: 固定字段名称、标题、基于表类型、大小等增加验证,支持新增、修改、查询操作,表模型只负责字段名、基于表的数据验证以及SQL的生成,不负责数据、where部分
  2. 业务模型:定义业务含义,增加业务验证,值赋值部分数据校验,不负责动态数据、Where条件
  3. 接口模型:结合业务验证参数、动态配置字段、查询条件等,将接口模型定义为业务场景,业务场景只负责验证数据,动态配置写入、查询字段以及where条件,不负责生成SQL

关于表模型使用 接口-类方式还是直接使用类-实例方式: 接口-类:优点是能创建多个实例,提高复用率;缺点是实例是最后提供的,类继承后其方法还是作用在父类上,无法读取子类数据,父类(中间件)所有的操作结果都需要存储在已有的实例上 类-实例:优点是灵活性强,通过重写属性值函数实现复用,缺点是只存在一个实例,不能复用

所以最终只能选择 接口-类-实例方式,并且需要选择一个底层实例,收集所有中间件生成的动态数据及条件集合

说明: 目前代码设计比较混乱.主要因为对整个事物的认知不够,没有形成整体思维、无法形成整体解决方案,目前属于收集各种场景状态下,逐步优化、重构的过程中.目前又个计划方案: 将apply,scene,valueFn,whereFn 合并成可定制化的基本单元,然后再支持特定场景下选择指定单元执行,可选择的力度可以达到基本单元中的每个函数.如validate 单独抽离部分函数执行就是一个很好的应用案例,比如ValueFn.Value 函数,第二、三个参数排除当前函数,就是对常规流程的定制化案例.后续会逐步优化、重构 统一管理这种定制化设计(目前是固定的)

# Functions

No description provided by the author
No description provided by the author
Deprecated ApplyFnUniqueField 单列唯一索引键,新增场景中间件.
No description provided by the author
No description provided by the author
No description provided by the author
ConvertEnumKeyType 转换枚举值类型.
DocNameWrapArrFn 将文档列名称前增加数组符号.
No description provided by the author
FieldFilterExclude 从fields 集合中筛选出和subFileds差集.
FieldFilterInclude 从fields 集合中筛选出和subFileds交集.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NewEnumTitleField 根据enum field 生成title列.
No description provided by the author
NewField 生成列,使用最简单版本,只需要提供获取值的函数,其它都使用默认配置,同时支持修改(字段名、标题等这些会在不同的层级设置).
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
OrderFieldFn 给定列按指定值顺序排序.
StructToFields 将结构体结构的fields 转换成 数组类型 结构体格式的feilds 方便编程引用, 数组类型fields 方便当作数据批量处理,常用于生产文档、ddl等场景,支持对象属性、数组类型定制化.
No description provided by the author
No description provided by the author
TryConvert2Expressions 业务where 条件判断,优先判断是否符可以转换为条件,可以直接应用.
No description provided by the author
No description provided by the author
TryParseExpressions 尝试解析where条件.
UniqueueArray 数组元素去重.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ValueFnDBSchemaFormat 根据DB类型要求,转换数据类型.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Constants

No description provided by the author
No description provided by the author
枚举值可以为空,通常用于选择条件.
No description provided by the author
No description provided by the author
列取名为deletedAt 为删除列.
列取名为id.
标记为删除场景下,可以更新数据库字段(如操作人 ,Field_name_deletedAt 自带该标签功能).
标记为pageIndex列.
标记为pageSize列.
标记为updateLimit列.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
字段递减.
No description provided by the author
最终状态(所有场景执行完成后再执行的场景 ,有时需要清除公共库设置的所有场景,只有在这里清除最保险).
字段递增.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
输出文档类型使用.
输出文档类型使用.
输出文档类型使用.
输出文档类型使用.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
不存在新增,存在更新,使用最新数据覆盖.
只新增说明使用最早数据.
只更新说明不存在时不处理.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
解码前格式化层.
验证层.
格式化层.
DB验证层.
只用于 insert values,update set 部分,不用于where部分.
赋值后格式化层,用于重置或者解码等场景.
赋值层.

# Variables

ApplyFnIncrease 字段值递增中间件.
No description provided by the author
No description provided by the author
No description provided by the author
ApplyFnValueFnSetIfEmpty 数据库值为空则修改,否则不修改,用于update.
No description provided by the author
No description provided by the author
ApplyFnWhereFindInColumnSet 传入的值在列字段集合内.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Dialect 设定驱动,方便直接使用.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
FieldName2DBColumnName 将接口字段转换为数据表字段列名称.
GlobalFnFormatFieldName 全局函数钩子,统一修改字段列名称,比如统一增加列前缀F.
GlobalFnFormatTableName 全局函数钩子,统一修改表名称,比如统一增加表前缀t_.
No description provided by the author
No description provided by the author
获取转换成db数据格式之前的原始数据.
验证数据时执行的函数.
where 场景下执行的函数.
No description provided by the author
No description provided by the author
No description provided by the author
此处暂时使用mysql的.
ValueFnDecodeComma 参数中,拼接的字符串解码成数组.
No description provided by the author
ValueFnFormatArray 格式化数组,只有一个元素时,直接返回当前元素,常用于where in 条件.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
屏蔽新增、修改 数据.
ValueFnTrimBlankSpace 删除字符串前后空格.
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
Compiler.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Field 供中间件插入数据时,定制化值类型 如 插件为了运算方便,值声明为float64 类型,而数据库需要string类型此时需要通过匿名函数修改值.
No description provided by the author
No description provided by the author
InsertParam 供子类复用,修改数据.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
基于数据表填充对应数据,同时也可以基于此生成SQL DDL.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

AttributeI 领域对象属性接口.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
Between 介于2者之间(包含上下边界,对于不包含边界情况,可以修改值范围或者直接用表达式),3个元素时为: col1<12<col2 格式.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Ilike 不区分大小写like语句.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
SchemaType 重新声明类型后,使用时IDE能自动识别到常量定义.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author