What does the code following CREATE FUNCTION being a string imply?

陌路散爱 提交于 2019-12-11 13:02:02

问题


From a great reply:

in PostgreSQL, CREATE FUNCTION is indeed a "SQL statement" but is is merely a "wrapper" to specify a block of code that is executed by something different than the SQL query "engine". Postgres (unlike other DBMS) supports multiple "runtime engines" that can execute the block of code that was passed to the "CREATE FUNCTION" statement - one artifact of that is that the code is actually a string so CREATE FUNCTION only sees a string, nothing else.

What are the consequences of "the code is actually a string so CREATE FUNCTION only sees a string, nothing else"?

Is that considered as dynamic SQL? Does it prevent or introduce SQL injection risk, compared to dynamic SQL?

How is that different from other RDBMS (if any?) where "the code is not a string"?

Thanks.


回答1:


PostgreSQL is highly extensible, and you can for example define your own procedural language to write functions in.

PostgreSQL knows nothing about the language except that it has to call a certain language handler to execute the function.

The way that was chosen to implement this is to simplify pass the code as a string.

This is just an implementation detail and does not make PostgreSQL functions any more or less vulnerable to SQL injection than other RDBMS.

There are several levels on which you have to defend yourself against injection:

  • The function arguments: Here you should choose non-string data types whenever possible.

  • The SQL statements within the function: Here you should avoid dynamic SQL whenever possible, and if you have to use dynamic SQL, you should insert variables using the %L pattern of the format function.

Again, this is the same if function bodies are specified as strings or not.




回答2:


All 3GL+ code is basically a string. The "parameter" passed to CREATE FUNCTION is code (to be executed outide the core SQL engine), which is a string (that's not SQL).

Other RDMS's only support SQL as the function/procedure body.



来源:https://stackoverflow.com/questions/50867972/what-does-the-code-following-create-function-being-a-string-imply

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!