0 概念
REST2SQL实现连接数据库,数据库的表或视图即可提供REST的GET\POST\PUT\DELETE请求,SQL可执行SQLECT\INSERT\UPDATE\DELETE语句。
0.1 RDB = Relational Database
即关系型数据库(简称 RDB)是一种以关系(即表格)为基础的数据库,它采用了关系代数等数学概念和方法来处理数据。关系型数据库最早由 E.F. Codd 在 1970 年提出,其核心思想是将数据存储在由多个表格组成的关系中,每个表格包含多行数据和多个列(即属性),每个属性只有一种数据类型。
常见的关系型数据库包括 MySQL、Oracle、Microsoft SQL Server、PostgreSQL 和 SQLite 等。
0.2 SQL = Structured Query Language
即结构化查询语言,具有数据操纵和数据定义等多种功能的数据库语言,这种语言具有交互性特点,能为用户提供极大的便利,数据库管理系统应充分利用SQL语言提高计算机应用系统的工作质量与效率。SQL语言不仅能独立应用于终端,还可以作为子语言为其他程序设计提供有效助力,该程序应用中,SQL可与其他程序语言一起优化程序功能,进而为用户提供更多更全面的信息。
SQL语言的组成:
1.一个SQL数据库是表(Table)的集合,它由一个或多个SQL模式定义。
2.一个SQL表由行集构成,一行是列的序列(集合),每列与行对应一个数据项。
3.一个表或者是一个基本表或者是一个视图。基本表是实际存储在数据库的表,而视图是由若干基本表或其他视图构成的表的定义。
4.一个基本表可以跨一个或多个存储文件,一个存储文件也可存放一个或多个基本表。每个存储文件与外部存储上一个物理文件对应。
5.用户可以用SQL语句对视图和基本表进行查询等操作。在用户角度来看,视图和基本表是一样的,没有区别,都是关系(表格)。
6.SQL用户可以是应用程序,也可以是终端用户。SQL语句可嵌入在宿主语言的程序中使用。SQL用户也能作为独立的用户接口,供交互环境下的终端用户使用。
SQL 的 Insert Select Update Delete 对应CRUD
0.3 REST = Representational State Transfer
REST是一种基于HTTP协议的软件架构风格,它提出了一组设计原则和约束条件,用于构建可靠、可扩展、易于维护的分布式系统。REST不是具体的技术规范或协议,而是一种通用的架构设计思想。
RESTful API使用HTTP动词(GET、POST、PUT、DELETE)来表示对资源的操作,这样做更符合HTTP的语义,并且减少了API的复杂度。
0.4 CRUD = Create Read Update Delete
crud是指在做计算处理时的增加(Create)、读取(Read)、更新(Update)和删除(Delete)几个单词的首字母简写。crud主要被用在描述软件系统中数据库或者持久层的基本操作功能。
– | CRUD | SQL | REST |
---|---|---|---|
创建、插入 | Create | Insert | Post |
读取、查询 | Read | Select | Get |
更新 | Update | Update | Put |
删除 | Delete | Delete | Delete |
1 REST设计URL
1.1 URL
http://IP或域名:端口/资源表或视图/查询条件
http://host:port/rest/resource/condition
GET、PUT、DELETE方法可以where条件,同SQL语句的where
GET还可以加orderby排序,同SQL语句select 的 order by
示例 :
http://127.0.0.1:5217/rest/n5217_v/?where=f_gp > '800'&orderby=f_gp desc
1.2 Data
数据提交采用JSON格式。POST和PUT方法需要提交数据
示例1:
{"p_id":5217,"s_mc":"白龙马"}
示例2:
{"F_DM":"005217","N_BS":null,"N_LJ":null,"N_SJ":null,"P_ID":-5217,"S_CYM":null,"S_DP":null,"S_MC":"白龙马"}
2 REST-Methon方法
2.1 GET
Request请求:
url资源后面可以加查询子句,where=过滤条件, orderby=排序条件
以下示例请求都是可以的: (可以浏览器或postman等发起请求)
GET http://127.0.0.1:5217/rest/n5217_v
GET http://127.0.0.1:5217/rest/n5217_v/?where=f_gp > '800'
GET http://127.0.0.1:5217/rest/n5217_v/?orderby=f_gp desc
GET http://127.0.0.1:5217/rest/n5217_v/?where=f_gp > '800'&orderby=f_gp desc
GET http://127.0.0.1:5217/rest/n5217_v/?orderby=f_gp desc&where=f_gp > '800'
GET http://127.0.0.1:5217/rest/n5217_v/?orderby=f_gp desc&where=f_gp > '800' and f_dm is null
CURL请求示例:注意特殊字符需要转义
curl -X GET http://127.0.0.1:5217/rest/n5217_v/?where=f_gp%3E'300900'
Response返回: 结果用JSON,内容包括请求条件和查询结果集。
{"Request":{"Content-Type": "","Data": "","Host": "127.0.0.1:5217","Method": "GET","OrderBy": "f_gp desc","Path": "/rest/n5217_v/","RESTorSQL": "REST","ResName": "n5217_v","Where": "f_gp \u003e '800' and k_dm is null"
},
"Response":[{"F_GP": "835640","F_NEW": "调出","K_DM": null,"K_MC": null,"N_DSP": 17.78,"N_ZZXJ": null,"S_DCR": "2023-10-24","S_MC": "富士达","S_RXR": "2023-04-28"},{"F_GP": "834599","F_NEW": "调出","K_DM": null,"K_MC": null,"N_DSP": 10.13,"N_ZZXJ": null,"S_DCR": "2023-10-27","S_MC": "同力股份","S_RXR": "2023-04-26"},{"F_GP": "834062","F_NEW": null,"K_DM": null,"K_MC": null,"N_DSP": 6.16,"N_ZZXJ": null,"S_DCR": null,"S_MC": "科润智控","S_RXR": "2023-08-29"},{"F_GP": "833533","F_NEW": null,"K_DM": null,"K_MC": null,"N_DSP": 19.61,"N_ZZXJ": null,"S_DCR": null,"S_MC": "骏创科技","S_RXR": "2023-08-23"},{"F_GP": "830839","F_NEW": "新晋","K_DM": null,"K_MC": null,"N_DSP": 10.68,"N_ZZXJ": null,"S_DCR": null,"S_MC": "万通液压","S_RXR": "2023-10-26"}
]}
2.2 POST
Request请求:
Data数据:采用JSON提交 ,Curl的 --Data 项
curl -X POST http://127.0.0.1:5217/rest/atop --data {\"p_id\":800}
Response返回: 结果用JSON,内容包括新插入的结果集。
{"Request":{"Content-Type": "application/x-www-form-urlencoded","Data": {"p_id": 800},"Host": "127.0.0.1:5217","Method": "POST","OrderBy": "","Path": "/rest/atop","RESTorSQL": "REST","ResName": "atop","Where": ""
},
"Response":{"Data":{"p_id": 800
},"Row":{"Insert rowsAffected": 1
}}}
2.3 PUT
Request请求:
url资源后面可以加查询子句,where="定位要更新的资源集。
Data数据:采用JSON提交 ,Curl的 --Data 项
curl -X PUT http://127.0.0.1:5217/rest/atop/?where=p_id=800 --data {\"f_dm\":\"800\"}
Response返回: 结果用JSON, 内容包括被更新后的结果集,更新影响的总行数。
{"Request":{"Content-Type": "application/x-www-form-urlencoded","Data": {"f_dm": "800"},"Host": "127.0.0.1:5217","Method": "PUT","OrderBy": "","Path": "/rest/atop/","RESTorSQL": "REST","ResName": "atop","Where": "p_id=800"
},
"Response":{"Data":{"f_dm": "800"
},"Row":{"Update rowsAffected":1}}}
2.4 DELETE
Request请求:
url资源后面可以加查询子句,where= 定位要更新的资源集。
curl -X DELETE http://127.0.0.1:5217/rest/atop/?where=p_id=800
Response返回: 结果用JSON, 内容包括被删除的结果集,删除影响的总行数。
{"Request":{"Content-Type": "","Data": "","Host": "127.0.0.1:5217","Method": "DELETE","OrderBy": "","Path": "/rest/atop/","RESTorSQL": "REST","ResName": "atop","Where": "p_id=800"
},
"Response":{"Delete rowsAffected": 1
}}
3 URL直接执行SQL设计
URL栏可以直接输入SQL并执行,只支持SELECT,INSERT,UPDATE,DELETE
示例:
Request请求:
http://127.0.0.1:5217/sql/select * from atop
http://127.0.0.1:5217/sql/insert into atop (p_id) values(5217)
http://127.0.0.1:5217/sql/update atop set s_mc = '白龙马'
http://127.0.0.1:5217/sql/delete from atop where p_id = 5217
Response返回结果:
{"Request":{"Content-Type": "","Data": "","Host": "127.0.0.1:5217","Method": "GET","OrderBy": "","Path": "/sql/select * from atop","RESTorSQL": "SQL","ResName": "select * from atop","Where": ""
},
"Response":[{"F_DM": "005217","N_BS": null,"N_LJ": null,"N_SJ": null,"P_ID": -5217,"S_CYM": null,"S_DP": null,"S_MC": "白龙马"}]}