flutter Moor数据库

2020-11-07  本文已影响0人  liboxiang

https://moor.simonbinder.eu/docs/getting-started/

moor.png

Expression<D>

GenerationContext的创建和sql命令的拼接在以下方法中,以下方法一般在go()方法中调用

  GenerationContext constructQuery() {
    final ctx = GenerationContext.fromDb(database);

    // whether we need to insert a space before writing the next component
    var needsWhitespace = false;

    void writeWithSpace(Component /*?*/ component) {
      if (component == null) return;

      if (needsWhitespace) ctx.writeWhitespace();
      component.writeInto(ctx);
      needsWhitespace = true;
    }

    writeStartPart(ctx);
    needsWhitespace = true;

    writeWithSpace(whereExpr);
    writeWithSpace(_groupBy);
    writeWithSpace(orderByExpr);
    writeWithSpace(limitExpr);

    ctx.buffer.write(';');

    return ctx;
  }
}

join

https://www.w3schools.com/sql/sql_join.asp
如下所示,

MyDatabase()
                    .select(MyDatabase().todos)
                    .join(
                      [
                        leftOuterJoin(
                          MyDatabase().categories,
                          MyDatabase().categories.id.equalsExp(MyDatabase().todos.category),
                        ),
                         // crossJoin(
                        //   MyDatabase().categories,
                        // ),
                      ],
                    )
                    .get()
                    .then((value) {
                      print(value);
                      value.forEach((e){
                        print(e.readTable(MyDatabase().todos)?.toJson());
                        print(e.readTable(MyDatabase().categories)?.toJson());
                      });
                    });
上一篇下一篇

猜你喜欢

热点阅读