本文主要是介绍2312d,d的sql构建器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原文
项目
该项目
在我工作
项目中广泛使用
,它允许自动处理
联接方式动态构建SQL
语句.
还会自动直接按表示数据库行
结构序化.它在dconf2022
在线演讲中介绍了:建模一切.
刚刚添加了对sqlite
的支持.该API
还不稳定,但仍非常有用.这是按需构建
,所以虽然有个计划
外表,但满足了我的需要
.
示例(使用sqlite
):
import d2sqlite3;
import std.stdio;
import std.file : exists;
import std.array;
//是的,我知道,在此我需要一个包装
import sqlbuilder.uda;
import sqlbuilder.dataset;
import sqlbuilder.dialect.sqlite;
import sqlbuilder.types;
import d2sqlite3;
struct Author
{@primaryKey @autoIncrement int id;string firstname;string lastname;static @refersTo!Book @mapping("author_id") Relation books;
}
struct Book
{@primaryKey @autoIncrement int id;string title;string description;@mustReferTo!Author("author") int author_id;
}
void main()
{auto shouldInitialize = !exists("books.sqlite");auto db = Database("books.sqlite");if(shouldInitialize){//创建表db.execute(createTableSql!Author);db.execute(createTableSql!Book);//添加一些书籍和作者Author walter = Author( firstname: "Walter", lastname: "Bright");db.create(walter); //按`SQL`插入语句自动序化Author andrei = Author( firstname: "Andrei", lastname: "Alexandrescu");db.create(andrei);db.create(Book(title: "D语言",description: "The OG D manual",author_id: andrei.id));db.create(Book(title: "C++ 设计",description: "The OG C++ template manual",author_id: andrei.id));db.create(Book(title: "D规范",description: "D语言规范",author_id: walter.id));}//按姓名取作者DataSet!Author ads;auto andrei = db.fetchOne(select(ads).where(ads.firstname, " = 'Andrei'"));//根据书籍的数据集来选择DataSet!Book bds;foreach(booktitle, author; db.fetch(select(bds.title, bds.author))){writefln("Found book %s, written by %s %s", booktitle, author.firstname, author.lastname);}auto andreiBooks = db.fetch(select(bds).where(bds.author_id, " = ", andrei.id.param)).array;writeln("Andrei's books: ", andreiBooks);
}
使用mysql
的代码也类似,只需导入mysql-native
和sqlbuilder.dialect.mysql
即可.
接着是postgresql
,不确定我何时需要
构建它.
-史蒂夫
这篇关于2312d,d的sql构建器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!