简体   繁体   English

CodeIgniter Active Record中的SQL注释?

[英]SQL Comment in CodeIgniter Active Record?

I'm moving a project to CodeIgniter and want to take advantage of Active Record. 我正在将一个项目移到CodeIgniter,并希望利用Active Record。 However, I haven't found a way to append a SQL comment to the queries. 但是,我还没有找到一种方法来向查询添加SQL注释。 In the existing code the query looks something like this: 在现有代码中,查询看起来像这样:

UPDATE table
SET column = 'value'
WHERE  ID = passed value;
-- Transacted by ' .$_SESSION['Name']

I've search Google and SO and not found the answer to how to add that last "Transacted by" comment without coding the whole thing directly and using $this->db->query('.....') 我搜索谷歌和SO并没有找到答案如何添加最后一个“Transacted by”评论而不直接编码整个事情并使用$this->db->query('.....')

I don't believe this is possible within ActiveRecord, as AR's functionality is a little restrictive. 我认为这在ActiveRecord中是不可能的,因为AR的功能有点限制。 One option that may work is using a custom string for your "where" clause, and sticking your transaction comment at the end of your custom "where" string. 可能有效的一个选项是为“where”子句使用自定义字符串,并在自定义“where”字符串的末尾添加事务注释。 I haven't tested this myself. 我自己没有测试过。

Even if you have to do the query manually, CI provides some nice escaping functions ( $this->db->escape for numbers, $this->db->escape_str for 'where' strings and $this->db->escape_like_str for 'where like' strings) that should make your own queries safe - I know in most of my more complex CI apps I need to write custom queries for about 1/5 of my queries due to the restrictiveness of AR. 即使您必须手动执行查询,CI也提供了一些很好的转义函数( $this->db->escape for numbers, $this->db->escape_str '字符串和$this->db->escape_like_str对于那些应该使你自己的查询安全的“我喜欢的字符串” - 我知道在大多数更复杂的CI应用程序中,由于AR的限制性,我需要为大约1/5的查询编写自定义查询。

Kudos to Darrrrrren for getting me on the right track. 感谢Darrrrrren让我走上正轨。

I defined a variable in my controller like this: $this->sqlStamp = ' /* Executed by ' .strtoupper( $this->router->class .'->' .$this->router->method) .' for ' .strtoupper( $this->session->userdata('displayName') ) .'*/ '; 我在我的控制器中定义了一个变量,如: $this->sqlStamp = ' /* Executed by ' .strtoupper( $this->router->class .'->' .$this->router->method) .' for ' .strtoupper( $this->session->userdata('displayName') ) .'*/ '; $this->sqlStamp = ' /* Executed by ' .strtoupper( $this->router->class .'->' .$this->router->method) .' for ' .strtoupper( $this->session->userdata('displayName') ) .'*/ ';

For Updates and deletes I was able to tack on ->where('1=1'.$this->sqlStamp, NULL, false) . 对于更新和删除,我能够使用->where('1=1'.$this->sqlStamp, NULL, false)

I wasn't able to get a solution to Inserts since they have no where statement and attempts to inject the comments elsewhere didn't seem to work. 我无法获得Inserts的解决方案,因为他们没有where语句,并且尝试在其他地方注入注释似乎不起作用。 I ended up running this before the inserts to give the DBA something to look for: $this->db->get_where('Same_TableName','0=1'.$this->sqlStamp); 我最终在插入之前运行它以给DBA找东西: $this->db->get_where('Same_TableName','0=1'.$this->sqlStamp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM