当前位置: 首页 > news >正文

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-nativesqlbuilder.dialect.mysql即可.
接着是postgresql,不确定我何时需要构建它.
-史蒂夫

http://www.lryc.cn/news/270710.html

相关文章:

  • 以太网二层交换机实验
  • 启封涂料行业ERP需求分析和方案分享
  • 华为ensp网络设计期末测试题-复盘
  • Dockerfile: WORKDIR vs VOLUME
  • spring ioc源码-refresh();
  • 使用递归实现深拷贝
  • 工程(十七)——自己数据集跑R2live
  • 【python高级用法】迭代器、生成器、装饰器、闭包
  • Nx市工业数据洞察:Flask、MySQL、Echarts的可视化之旅
  • 关于正态分布
  • 每日一练(编程题-C/C++)
  • Unity UnityWebRequest 在Mac上使用报CommectionError
  • WorkPlus为企业打造私有化部署IM解决方案
  • QT上位机开发(抽奖软件)
  • 雨课堂作业整理
  • C#/WPF 只允许一个实例程序运行并将已运行程序置顶
  • 【基础】【Python网络爬虫】【1.认识爬虫】什么是爬虫,爬虫分类,爬虫可以做什么
  • 【算法与数据结构】860、LeetCode柠檬水找零
  • 「Verilog学习笔记」乘法与位运算
  • CSS与JavaScript的简单认识
  • MAC 中多显示器的设置(Parallels Desktop)
  • 迁移到云原生:如何使用微服务迁移应用程序
  • kafka 的零拷贝原理
  • 华为云Stack 8.X流量模型分析(五)
  • 学习动态规划解决不同路径、最小路径和、打家劫舍、打家劫舍iii
  • nodejs微信小程序+python+PHP特困救助供养信息管理系统-计算机毕业设计推荐
  • Vue(二):计算属性与 watch 监听器
  • 25、WEB攻防——通用漏洞SQL读写注入MYSQLMSSQLPostgreSQL
  • 【第5期】前端Vue使用Proxy+Vuex(store、mutations、actions)跨域调通本地后端接口
  • 在Visual Studio(VS)编译器中,Release和Debug区别