如何在Firebird 2.5.x中获取服务器名\主机名 [英] How to get the servername\hostname in Firebird 2.5.x

查看:152
本文介绍了如何在Firebird 2.5.x中获取服务器名\主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Firebird 2.5,并且想要检索以下值

I use Firebird 2.5, and I want to retrieve the following values

用户名:
我用SELECT rdb$get_context('SYSTEM', 'CURRENT_USER') FROM ...

Username:
I used SELECT rdb$get_context('SYSTEM', 'CURRENT_USER') FROM ...

数据库名称:
我用SELECT rdb$get_context('SYSTEM', 'DB_NAME') FROM ...

Database name:
I used SELECT rdb$get_context('SYSTEM', 'DB_NAME') FROM ...

但是对于服务器名称,我没有找到任何客户端API,您知道如何使用SELECT语句检索服务器名称.

But for server name, I did not find any client API, do you know how can I retrieve the server name with a SELECT statement.

推荐答案

Firebird中没有内置函数可以通过SQL获取服务器主机名(或主机名,因为服务器可以有多个主机名).

There is nothing built-in in Firebird to obtain the server host name (or host names, as a server can have multiple host names) through SQL.

最接近的方法是通过isc_database_info API函数请求isc_info_firebird_version信息项.返回的版本信息-如果通过TCP/IP连接-包括服务器的主机名.

The closest you can get is by requesting the isc_info_firebird_version information item through the isc_database_info API function. This returns version information that - if connecting through TCP/IP - includes a host name of the server.

但是,由于您这样做的主要原因是要区分SQL中的环境,因此最好寻找其他解决方案.一些想法:

But as your primary reason for this is to discern between environments in SQL, it might be better to look for a different solution. Some ideas:

您可以创建外部表以包含所需的环境信息

You can create an external table to contain the environment information you need

在此示例中,我只是为环境类型输入了一个简短的,固定宽度的名称,但是您可以包括其他信息,只是要知道外部表格式是二进制格式.使用CHAR时,它将看起来像是固定宽度的格式,其中小于声明值的值需要用空格填充.

In this example I just put in a short, fixed width name for the environment types, but you could include other information, just be aware the external table format is a binary format. When using CHAR it will look like a fixed width format, where values shorter than declared need to be padded with spaces.

您可以按照以下步骤操作:

You can follow these steps:

  1. firebird.conf中配置ExternalFileAccess(在此示例中,您需要设置ExternalFileAccess = Restrict D:\data\DB\exttables).

  1. Configure ExternalFileAccess in firebird.conf (for this example, you'd need to set ExternalFileAccess = Restrict D:\data\DB\exttables).

然后您可以将表创建为

create table environment 
  external file 'D:\data\DB\exttables\environment.dat' (
  environment_type CHAR(3) CHARACTER SET ASCII NOT NULL 
)

  • 接下来,创建文件D:\data\DB\exttables\environment.dat,并使用三个字符(例如,用于测试的TST,用于生产的PRO)填充该文件.您也可以插入该值,否则将自动创建外部表文件.如果您想要更多的列或具有不同长度的数据等,则插入可能会更简单.请记住,它是二进制的,但是对所有列使用CHAR会使它看起来像文本.

  • Next, create the file D:\data\DB\exttables\environment.dat and populate it with exactly three characters (eg TST for test, PRO for production, etc). You can also insert the value instead, the external table file will be created automatically. Inserting might be simpler if you want more columns, or data with varying length, etc. Just keep in mind it is binary, but using CHAR for all columns will make it look like text.

    针对每个环境执行此操作,并确保文件是只读文件,以避免意外插入.

    Do this for each environment, and make sure the file is read-only to avoid accidental inserts.

    完成此操作后,您可以使用

    After this is done, you can obtain the environment information using

    select environment_type 
    from environment
    

    您可以为同一主机上的多个数据库共享同一文件,并且外部文件-默认情况下-不包括在gbak备份中(仅当您应用-convert备份选项时才包括).允许在环境之间移动数据库而无需拖动此信息.

    You can share the same file for multiple databases on the same host, and external files are - by default - not included in a gbak backup (they are only included if you apply the -convert backup option), so this would allow moving database between environments without dragging this information along.

    您可以使用适当的编程语言编写UDF(用户定义的函数)或UDR(用户定义的例程),以返回所需的信息并在数据库中定义此函数.然后,Firebird可以从SQL调用此函数.

    You can write an UDF (User-Defined Function) or UDR (User Defined Routine) in a suitable programming language to return the information you want and define this function in your database. Firebird can then call this function from SQL.

    UDF被认为已弃用,如果可能,您应该使用Firebird 3中引入的UDR.

    UDFs are considered deprecated, and you should use UDRs - introduced in Firebird 3 - instead if possible.

    我自己从未写过UDF或UDR,因此无法详细描述.

    I have never written an UDF or UDR myself, so I can't describe it in detail.

    这篇关于如何在Firebird 2.5.x中获取服务器名\主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆