# SAP HANA

> 英文标题：SAP HANA
> 官网原文：https://typeorm.io/docs/drivers/sap
> 文档版本：TypeORM v1.1
> 体例：英文原文（EN）与中文译文（ZH）逐块并列，共 31 块

---

<!-- EN -->
# SAP HANA

<!-- ZH -->
# SAP HANA

<!-- EN -->
## Installation

<!-- ZH -->
## 安装

<!-- EN -->
TypeORM relies on `@sap/hana-client` for establishing the database connection:

<!-- ZH -->
TypeORM 依赖 `@sap/hana-client` 来建立数据库连接：

<!-- EN -->
```shell
npm install @sap/hana-client
```

<!-- ZH -->
```shell
npm install @sap/hana-client
```

<!-- EN -->
If you are using TypeORM 0.3.25 or earlier, `hdb-pool` is also required for managing the pool.

<!-- ZH -->
如果你使用的是 TypeORM 0.3.25 或更早版本，还需要 `hdb-pool` 来管理连接池。

<!-- EN -->
## Data Source Options

<!-- ZH -->
## 数据源选项

<!-- EN -->
See [Data Source Options](https://typeorm.io/docs/data-source/data-source-options) for the common data source options.

<!-- ZH -->
通用数据源选项请参阅[数据源选项](https://typeorm.io/docs/data-source/data-source-options)。

<!-- EN -->
* `host` - The hostname of the SAP HANA server. For example, `"localhost"`.

<!-- ZH -->
* `host` - SAP HANA 服务器的主机名。例如 `"localhost"`。

<!-- EN -->
* `port` - The port number of the SAP HANA server. For example, `30015`.

<!-- ZH -->
* `port` - SAP HANA 服务器的端口号。例如 `30015`。

<!-- EN -->
* `username` - The username to connect to the SAP HANA server. For example, `"SYSTEM"`.

<!-- ZH -->
* `username` - 连接 SAP HANA 服务器的用户名。例如 `"SYSTEM"`。

<!-- EN -->
* `password` - The password to connect to the SAP HANA server. For example, `"password"`.

<!-- ZH -->
* `password` - 连接 SAP HANA 服务器的密码。例如 `"password"`。

<!-- EN -->
* `database` - The name of the database to connect to. For example, `"HXE"`.

<!-- ZH -->
* `database` - 要连接的数据库名。例如 `"HXE"`。

<!-- EN -->
* `encrypt` - Whether to encrypt the connection. For example, `true`.

<!-- ZH -->
* `encrypt` - 是否加密连接。例如 `true`。

<!-- EN -->
* `sslValidateCertificate` - Whether to validate the SSL certificate. For example, `true`.

<!-- ZH -->
* `sslValidateCertificate` - 是否校验 SSL 证书。例如 `true`。

<!-- EN -->
* `key`, `cert` and `ca` - Private key, public certificate and certificate authority for the encrypted connection.

<!-- ZH -->
* `key`、`cert` 和 `ca` - 加密连接所用的私钥、公钥证书和证书颁发机构。

<!-- EN -->
* `driver` - Optional explicit `@sap/hana-client` module instance. If omitted, TypeORM loads `@sap/hana-client` automatically.

<!-- ZH -->
* `driver` - 可选的显式 `@sap/hana-client` 模块实例。如果省略，TypeORM 会自动加载 `@sap/hana-client`。

<!-- EN -->
* `pool` — Connection pool configuration object:

<!-- ZH -->
* `pool` — 连接池配置对象：

<!-- EN -->
  <!-- -->

<!-- ZH -->
  <!-- -->

<!-- EN -->
  * `maxConnectedOrPooled` (number) — Max active or idle connections in the pool (default: 10).
  * `maxPooledIdleTime` (seconds) — Time before an idle connection is closed (default: 30).
  * `maxWaitTimeoutIfPoolExhausted` (milliseconds) - Time to wait for a connection to become available (default: 0, no wait). Requires `@sap/hana-client` version `2.27` or later.
  * `pingCheck` (boolean) — Whether to validate connections before use (default: false).
  * `poolCapacity` (number) — Maximum number of connections to be kept available (default: no limit).

<!-- ZH -->
  * `maxConnectedOrPooled`（number）— 池中活跃或空闲连接的最大数量（默认：10）。
  * `maxPooledIdleTime`（秒）— 空闲连接被关闭前的时间（默认：30）。
  * `maxWaitTimeoutIfPoolExhausted`（毫秒）- 等待连接变为可用的时间（默认：0，不等待）。需要 `@sap/hana-client` 版本 `2.27` 或更高。
  * `pingCheck`（boolean）— 使用前是否校验连接（默认：false）。
  * `poolCapacity`（number）— 保持可用的最大连接数（默认：不限制）。

<!-- EN -->
See the official documentation of SAP HANA Client for more details as well as the `extra` properties: [Node.js Connection Properties](https://help.sap.com/docs/SAP_HANA_CLIENT/f1b440ded6144a54ada97ff95dac7adf/4fe9978ebac44f35b9369ef5a4a26f4c.html).

<!-- ZH -->
更多细节以及 `extra` 属性，请参阅 SAP HANA Client 的官方文档：[Node.js Connection Properties](https://help.sap.com/docs/SAP_HANA_CLIENT/f1b440ded6144a54ada97ff95dac7adf/4fe9978ebac44f35b9369ef5a4a26f4c.html)。

<!-- EN -->
## Column Types

<!-- ZH -->
## 列类型

<!-- EN -->
SAP HANA 2.0 and SAP HANA Cloud support slightly different data types. Check the SAP Help pages for more information:

<!-- ZH -->
SAP HANA 2.0 和 SAP HANA Cloud 支持的数据类型略有不同。更多信息请查看 SAP Help 页面：

<!-- EN -->
* [SAP HANA 2.0 Data Types](https://help.sap.com/docs/SAP_HANA_PLATFORM/4fe29514fd584807ac9f2a04f6754767/20a1569875191014b507cf392724b7eb.html?locale=en-US)
* [SAP HANA Cloud Data Types](https://help.sap.com/docs/hana-cloud-database/sap-hana-cloud-sap-hana-database-sql-reference-guide/data-types)

<!-- ZH -->
* [SAP HANA 2.0 Data Types](https://help.sap.com/docs/SAP_HANA_PLATFORM/4fe29514fd584807ac9f2a04f6754767/20a1569875191014b507cf392724b7eb.html?locale=en-US)
* [SAP HANA Cloud Data Types](https://help.sap.com/docs/hana-cloud-database/sap-hana-cloud-sap-hana-database-sql-reference-guide/data-types)

<!-- EN -->
TypeORM's `SapDriver` supports `tinyint`, `smallint`, `integer`, `bigint`, `smalldecimal`, `decimal`, `real`, `double`, `date`, `time`, `seconddate`, `timestamp`, `boolean`, `char`, `nchar`, `varchar`, `nvarchar`, `text`, `alphanum`, `shorttext`, `array`, `varbinary`, `blob`, `clob`, `nclob`, `st_geometry`, `st_point`, `real_vector` and `half_vector`. Some of these data types have been deprecated or removed in SAP HANA Cloud, and will be converted to the closest available alternative when connected to a Cloud database.

<!-- ZH -->
TypeORM 的 `SapDriver` 支持 `tinyint`、`smallint`、`integer`、`bigint`、`smalldecimal`、`decimal`、`real`、`double`、`date`、`time`、`seconddate`、`timestamp`、`boolean`、`char`、`nchar`、`varchar`、`nvarchar`、`text`、`alphanum`、`shorttext`、`array`、`varbinary`、`blob`、`clob`、`nclob`、`st_geometry`、`st_point`、`real_vector` 和 `half_vector`。其中部分数据类型在 SAP HANA Cloud 中已被弃用或移除，连接到 Cloud 数据库时它们会被转换为最接近的可用替代类型。

<!-- EN -->
### Vector Types

<!-- ZH -->
### 向量类型

<!-- EN -->
The `real_vector` and `half_vector` data types were introduced in SAP HANA Cloud (2024Q1 and 2025Q2 respectively), and require a supported version of `@sap/hana-client` as well.

<!-- ZH -->
`real_vector` 和 `half_vector` 数据类型分别在 SAP HANA Cloud（2024Q1 和 2025Q2）中引入，同样需要受支持版本的 `@sap/hana-client`。

<!-- EN -->
For consistency with PostgreSQL's vector support, TypeORM also provides aliases:

<!-- ZH -->
为了与 PostgreSQL 的向量支持保持一致，TypeORM 还提供了别名：

<!-- EN -->
* `vector` (alias for `real_vector`) - stores vectors as 4-byte floats
* `halfvec` (alias for `half_vector`) - stores vectors as 2-byte floats for memory efficiency

<!-- ZH -->
* `vector`（`real_vector` 的别名）- 以 4 字节浮点数存储向量
* `halfvec`（`half_vector` 的别名）- 以 2 字节浮点数存储向量，更节省内存

<!-- EN -->
```typescript
@Entity()
export class Document {
    @PrimaryGeneratedColumn()
    id: number

    // Using SAP HANA native type names
    @Column("real_vector", { length: 1536 })
    embedding: Buffer | number[]

    @Column("half_vector", { length: 768 })
    reduced_embedding: Buffer | number[]

    // Using cross-database aliases (recommended)
    @Column("vector", { length: 1536 })
    universal_embedding: Buffer | number[]

    @Column("halfvec", { length: 768 })
    universal_reduced_embedding: Buffer | number[]
}
```

<!-- ZH -->
```typescript
@Entity()
export class Document {
    @PrimaryGeneratedColumn()
    id: number

    // 使用 SAP HANA 原生类型名
    @Column("real_vector", { length: 1536 })
    embedding: Buffer | number[]

    @Column("half_vector", { length: 768 })
    reduced_embedding: Buffer | number[]

    // 使用跨数据库别名（推荐）
    @Column("vector", { length: 1536 })
    universal_embedding: Buffer | number[]

    @Column("halfvec", { length: 768 })
    universal_reduced_embedding: Buffer | number[]
}
```

<!-- EN -->
By default, the client will return a `Buffer` in the `fvecs`/`hvecs` format, which is more efficient. It is possible to let the driver convert the values to a `number[]` by adding `{ extra: { vectorOutputType: "Array" } }` to the connection options. Check the SAP HANA Client documentation for more information about [REAL_VECTOR](https://help.sap.com/docs/SAP_HANA_CLIENT/f1b440ded6144a54ada97ff95dac7adf/0d197e4389c64e6b9cf90f6f698f62fe.html) or [HALF_VECTOR](https://help.sap.com/docs/SAP_HANA_CLIENT/f1b440ded6144a54ada97ff95dac7adf/8bb854b4ce4a4299bed27c365b717e91.html).

<!-- ZH -->
默认情况下，客户端会以 `fvecs`/`hvecs` 格式返回 `Buffer`，这更高效。通过在连接选项中加入 `{ extra: { vectorOutputType: "Array" } }`，可以让驱动把值转换成 `number[]`。关于 [REAL_VECTOR](https://help.sap.com/docs/SAP_HANA_CLIENT/f1b440ded6144a54ada97ff95dac7adf/0d197e4389c64e6b9cf90f6f698f62fe.html) 或 [HALF_VECTOR](https://help.sap.com/docs/SAP_HANA_CLIENT/f1b440ded6144a54ada97ff95dac7adf/8bb854b4ce4a4299bed27c365b717e91.html) 的更多信息，请查看 SAP HANA Client 文档。

<!-- EN -->
Use the appropriate [vector functions](https://help.sap.com/docs/hana-cloud-database/sap-hana-cloud-sap-hana-database-sql-reference-guide/vector-functions) for similarity searches.

<!-- ZH -->
进行相似性搜索时，请使用相应的[向量函数](https://help.sap.com/docs/hana-cloud-database/sap-hana-cloud-sap-hana-database-sql-reference-guide/vector-functions)。
