如何执行存储在数据库中的代码?

时间:2021-08-11 07:39:49

How can I store, for example, the body of a method in a database and later run it? (I'm using Delphi XE2; maybe RTTI would help.)

例如,如何在数据库中存储方法体,然后运行它? (我正在使用Delphi XE2;也许RTTI会有所帮助。)

2 个解决方案

#1


6  

RTTI is not a full language interpreter. Delphi is a compiled language. You write it, compile it, and distribute only your binaries. Unless you're Embarcadero, you don't have rights to distribute DCC32 (the command line compiler).

RTTI不是一个完整的语言解释器。 Delphi是一种编译语言。你编写它,编译它,并只分发你的二进制文件。除非您是Embarcadero,否则您无权分发DCC32(命令行编译器)。

However, the JVCL includes a delphi-like language subset wrapped up in a very easy to use Component, called "JvInterpreter". You could write some code (as pascal) and place it in a database. You could then "run that code" (interpreted, not compiled) that you pull from the database. Typically these should be procedures that call methods in your code. YOu have to write some "wrappers" that expose the compiled APIs that you wish to expose to the interpreter (provide access to live data, or database connection objects, or table/query objects). You're thinking that this sounds perfect right? Well, it's a trap.

但是,JVCL包含一个类似于delphi的语言子集,它包含在一个非常容易使用的组件中,称为“JvInterpreter”。您可以编写一些代码(作为pascal)并将其放在数据库中。然后,您可以“运行从数据库中提取的代码”(已解释,未编译)。通常,这些应该是调用代码中的方法的过程。你必须编写一些“包装器”来公开你希望公开给解释器的编译API(提供对实时数据,数据库连接对象或表/查询对象的访问)。你觉得这听起来很完美吗?嗯,这是一个陷阱。

Beware of something called "the configuration complexity clock". YOu've just reached 9 o'clock, and that's where a lot of pain and suffering begins. Just like when you have a problem, and you solve it with regular expressions, and "now you have two problems", adding scripting and DSLs to your app has a way of solving one problem and creating several others.

小心称为“配置复杂性时钟”的东西。你刚刚到了9点,这就是痛苦和痛苦开始的地方。就像你遇到问题,并用正则表达式解决它,“现在你有两个问题”,在你的应用程序中添加脚本和DSL可以解决一个问题并创建其他几个问题。

While I think the "DLL stored in a database blob field" idea is evil, and absurd, I think that wanton addition of scripting and domain-specific languages to applications is also asking for a lot of pain. Ask yourself first if some other simpler solution could work. Then apply the YAGNI principle (You Ain't Gonna Need It) and KISS (keep-it-simple-smartguy).

虽然我认为“存储在数据库blob字段中的DLL”的想法是邪恶的,而且荒谬,但我认为向应用程序添加脚本和特定于域的语言也需要很多痛苦。先问自己一些其他更简单的解决方案是否可行。然后应用YAGNI原则(你不需要它)和KISS(保持它简单 - smartguy)。

Think twice before you implement anything like what you're asking about doing in your question.

在实施任何类似于您在问题中所要求的内容之前,请三思而后行。

#2


2  

Your best Option, IMHO, is using a scripting engine and storing scripts in the database.

您最好的选择,恕我直言,正在使用脚本引擎并在数据库中存储脚本。

Alternatively you could put the code in a dll and put that dll in the database. There is code for loading a dll from a resource into ram and processing it so it can be used as if it was loaded using LoadLibrary, e.g. in dzlib. I don't really know whether works with any dll and in which versions of Windows, but it does with the ones I tried.

或者,您可以将代码放在dll中并将该dll放入数据库中。有一些代码用于将资源从资源加载到ram并对其进行处理,以便可以像使用LoadLibrary加载它一样使用它,例如在dzlib。我真的不知道是否适用于任何DLL以及Windows的哪个版本,但它与我尝试过的版本有关。

#1


6  

RTTI is not a full language interpreter. Delphi is a compiled language. You write it, compile it, and distribute only your binaries. Unless you're Embarcadero, you don't have rights to distribute DCC32 (the command line compiler).

RTTI不是一个完整的语言解释器。 Delphi是一种编译语言。你编写它,编译它,并只分发你的二进制文件。除非您是Embarcadero,否则您无权分发DCC32(命令行编译器)。

However, the JVCL includes a delphi-like language subset wrapped up in a very easy to use Component, called "JvInterpreter". You could write some code (as pascal) and place it in a database. You could then "run that code" (interpreted, not compiled) that you pull from the database. Typically these should be procedures that call methods in your code. YOu have to write some "wrappers" that expose the compiled APIs that you wish to expose to the interpreter (provide access to live data, or database connection objects, or table/query objects). You're thinking that this sounds perfect right? Well, it's a trap.

但是,JVCL包含一个类似于delphi的语言子集,它包含在一个非常容易使用的组件中,称为“JvInterpreter”。您可以编写一些代码(作为pascal)并将其放在数据库中。然后,您可以“运行从数据库中提取的代码”(已解释,未编译)。通常,这些应该是调用代码中的方法的过程。你必须编写一些“包装器”来公开你希望公开给解释器的编译API(提供对实时数据,数据库连接对象或表/查询对象的访问)。你觉得这听起来很完美吗?嗯,这是一个陷阱。

Beware of something called "the configuration complexity clock". YOu've just reached 9 o'clock, and that's where a lot of pain and suffering begins. Just like when you have a problem, and you solve it with regular expressions, and "now you have two problems", adding scripting and DSLs to your app has a way of solving one problem and creating several others.

小心称为“配置复杂性时钟”的东西。你刚刚到了9点,这就是痛苦和痛苦开始的地方。就像你遇到问题,并用正则表达式解决它,“现在你有两个问题”,在你的应用程序中添加脚本和DSL可以解决一个问题并创建其他几个问题。

While I think the "DLL stored in a database blob field" idea is evil, and absurd, I think that wanton addition of scripting and domain-specific languages to applications is also asking for a lot of pain. Ask yourself first if some other simpler solution could work. Then apply the YAGNI principle (You Ain't Gonna Need It) and KISS (keep-it-simple-smartguy).

虽然我认为“存储在数据库blob字段中的DLL”的想法是邪恶的,而且荒谬,但我认为向应用程序添加脚本和特定于域的语言也需要很多痛苦。先问自己一些其他更简单的解决方案是否可行。然后应用YAGNI原则(你不需要它)和KISS(保持它简单 - smartguy)。

Think twice before you implement anything like what you're asking about doing in your question.

在实施任何类似于您在问题中所要求的内容之前,请三思而后行。

#2


2  

Your best Option, IMHO, is using a scripting engine and storing scripts in the database.

您最好的选择,恕我直言,正在使用脚本引擎并在数据库中存储脚本。

Alternatively you could put the code in a dll and put that dll in the database. There is code for loading a dll from a resource into ram and processing it so it can be used as if it was loaded using LoadLibrary, e.g. in dzlib. I don't really know whether works with any dll and in which versions of Windows, but it does with the ones I tried.

或者,您可以将代码放在dll中并将该dll放入数据库中。有一些代码用于将资源从资源加载到ram并对其进行处理,以便可以像使用LoadLibrary加载它一样使用它,例如在dzlib。我真的不知道是否适用于任何DLL以及Windows的哪个版本,但它与我尝试过的版本有关。