duanran3115 2013-08-15 07:21
浏览 23

Web应用程序:将SQL与代码完全分开

This is a design question, I've been thinking about this for a while.

I would like to separate the SQL queries inside independent files or memory locations (for faster access).

The queries will be stored like this:

SELECT .... WHERE col = ?

When the query is needed, it is fetched from it's location.

A (less maintainable) example alternative would be:

$sql = "select ..... where col=". prevent_injection($val) ." and ....";

Benefits:

  • total separation of the queries: the web developers and the DBAs can work specifically to their role
  • cleaner queries

Disadvantages:

  • requires a bit of coordination on what each ? means, which is necessary anyway in a big team
  • sourcing the SQL at runtime may slow things down a bit, but caching can be used

Any feedback on this approach?

  • 写回答

2条回答 默认 最新

  • doy57007 2013-08-15 07:54
    关注

    You should have a look at the MVC (Model View Controller) architectural pattern. It will allow you to achieve your goal: Separation of data, logic and presentation.

    In MVC, your models will handle all SQL related logic, so that the other two layers will never have to deal with queries.

    Have a look at PHP Frameworks which implement the MVC pattern, they will greatly simplify applying this pattern to your project. My personal favourite is Laravel 4, but there are lots of others, such as FuelPHP, CodeIgniter, CakePHP et al.

    Most of these frameworks also have caching mechanisms as a bonus and they are able to utilize other means of speeding up your web application: Redis and Memcache, for example.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么