weed3 2.5.解决数据库关键字问题 - noear/weed3 GitHub Wiki

Weed3 一个微型ORM框架(只有0.1Mb哦)

源码:https://github.com/noear/weed3 源码:https://gitee.com/noear/weed3

weed3提供了字段和对象格式化支持,通过DbContext进行设定
//以mysql为例
DbContext db = new DbContext(...).fieldFormatSet("`%`")//设定字段格式符
                                 .objectFormatSet("`%`");//设定对象格式符
//%号为占位符,`%`表过你的字段会转为:`字段名`

字段格式符对会对: .set(..), .select(..), .orderBy(..), .groupBy(..) 里的字段进行处理

对对象格式符对会: .table(..), innerJoin(..), leftJoin(..), rightJoin(..) 里的表名进行处理

如果不设置,则需要自己手动处理关键字;手动处理与自动处理并不冲突。
//手动处理
db.table("`user`").where("`count`<10 AND sex=1").count();
格式化是由IDbFormater来处理的,如果觉得里面的实现不好,还可以自己写一个替换它:)
IDbFormater df = new DfNew(); //DfNew 算是自己写的
db.formaterSet(df); //搞定

//附:
public interface IDbFormater {
    /** 字段格式符设置 */
    void fieldFormatSet(String format);
    /** 对象格式符设置 */
    void objectFormatSet(String format);

    /** 格式化字段(用于:set(..,v)) */
    String formatField(String name);
    /** 格式化多列(用于:select(..) orderBy(..) groupBy(..)) */
    String formatColumns(String columns);
    /** 格式化条件(用于:where(..) and(..) or(..))  */
    String formatCondition(String condition);
    /** 格式化对象(用于:from(..), join(..)) */
    String formatObject(String name);
}
下一篇:2.6.三大接口盘点(table,call,sql)