[英]How do I “generate” a web API from a database?
I'd like to automatically generate a web API from a database. 我想从数据库自动生成Web API。 I can easily query the database with SQL, but obviously this won't do for a public API.
我可以使用SQL轻松查询数据库,但显然这不适用于公共API。 I can also write the server-side code manually, but this will take a very long time, since the database is fairly large.
我也可以手动编写服务器端代码,但这需要很长时间,因为数据库相当大。 Also, writing it manually would be a very mechanical and repetitive task, so I don't see why a program can't do it for me.
此外,手动编写它将是一个非常机械和重复的任务,所以我不明白为什么程序不能为我做。
For example, let's say I have a table in my database called "Announcements". 例如,假设我的数据库中有一个名为“Announcements”的表。 I'm looking for a way to (given that database table), automatically generate the server-side code that will allow someone to create new announcements, requiring the correct fields.
我正在寻找一种方法(给定数据库表),自动生成服务器端代码,允许某人创建新的公告,需要正确的字段。 Done manually, this could look like a PHP file called "create_announcement.php", which requires all the fields that exist in the database, like the announcement title, body, and author.
手动完成,这可能看起来像一个名为“create_announcement.php”的PHP文件,它需要数据库中存在的所有字段,如公告标题,正文和作者。 Of course, this could also be done in a cleaner way by following some design model (like REST), but this is meant as an example.
当然,这也可以通过遵循一些设计模型(如REST)以更清洁的方式完成,但这只是一个例子。
Other things I'm considering are: 我正在考虑的其他事情是:
So what kind of tools should I look into? 那么我应该研究什么样的工具呢? What is the general approach people take when confronted with this problem?
人们在面对这个问题时采取的一般方法是什么?
The comments above have listed out some great existing software, you've really got to figure out whether or not you want to reinvent this concept. 上面的评论列出了一些很棒的现有软件,你真的要弄清楚你是否想重新发明这个概念。
If you write this yourself you can still save a lot of time vs writing each page & action individually, but the validation you're talking about is probably going to eat up all of your time if you don't already have that functionality built into these models, if they exist. 如果你自己写这篇文章,你仍然可以节省大量时间,而不是单独编写每个页面和操作,但是如果你还没有内置的功能,那么你所谈论的验证可能会耗费你所有的时间。这些模型,如果存在的话。
Something simple could take in a table name, action name, id number from the URL and spit out basic stuff. 简单的东西可以从URL中获取表名,操作名,ID号并吐出基本内容。
<?
$allowed_tables = array('posts','comments','people');
$table = $_GET['table'];
$page = (int) $_GET['page'];
$per_page = 100;
$offset = ($page - 1) * $per_page;
if (!$table || !in_array($table, $allowed_tables)) {
die('Invalid table.');
}
$rs = mysql_query("SELECT * FROM `$table` ORDER BY `id` DESC LIMIT $per_page, $offset");
$data = array();
if ($rs && mysql_num_rows($rs)) {
while ($row = mysql_fetch_array($rs,MYSQL_ASSOC)) {
$data[] = $row;
}
}
print json_decode($data);
?>
You could definitely make some serious progress in a couple of hours, adding in fields to specifically show or hide, you could even deal with the validation on a simple basis (required fields would be simple) for now just to get it rolling. 你肯定可以在几个小时内取得一些重大进展,在字段中添加专门的显示或隐藏,你甚至可以在简单的基础上处理验证(必填字段很简单),现在只是为了让它滚动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.