使用服务器端Swift连接到Oracle数据库

时间:2017-03-22 19:06:55

标签: swift oracle vapor perfect kitura

是否有可能在Linux上使用Swift(raw或Swift框架)连接到Oracle数据库?我所做的是尝试构建一个Docker VM,安装Oracle二进制文件,添加OCILIB包然后使用名为SwiftOracle的包连接[尝试],这似乎不受支持(许多构建问题)并且只公开OCILIB C代码Swift使用模块映射和包装器。

我尝试使用Kitura框架,但这似乎都没有用 - Xcode无法编译,因为它无法找到C库或创建模块。

以下是我尝试无效的步骤:

构建Docker VM(包括Oracle二进制文件):https://github.com/wnameless/docker-oracle-xe-11g

下载并安装OCILIB:https://github.com/vrogier/ocilib

添加SwiftOracle包,修复构建问题并尝试构建。

1 个答案:

答案 0 :(得分:0)

您可以使用以下方法连接到oracle数据库:(这可以通过蒸气社区的帮助实现。)

-----为了使oracle驱动程序正常工作,我已在Ubuntu -------------中捆绑了此方法 -需要安装oracle客户端,以便可以定义标头和库路径,您可以从oracle网站获取这些信息。

oracle-instantclinet*-basic-*.rpm
oracle-instantclinet*-devel-*.rpm
oracle-instantclinet*-sqlplus-*.rpm

-使用以下命令安装下载的软件包

sudo alien -i oracle-instantclinet*-basic-*.rpm
sudo alien -i oracle-instantclinet*-devel-*.rpm
sudo alien -i oracle-instantclinet*-sqlplus-*.rpm

-在Ubuntu上安装libaio1

sudo apt install libaio1

-此路径应位于〜/ .bashrc

#oracle home and library path
export ORACLE_HOME=/usr/lib/oracle/12.2/client64
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/oracle/12.2/client64/lib:/usr/local/lib

-从Github下载OCILIB库     git clone https://github.com/vrogier/ocilib.git(或下载最新版本/在ocilib 4.5.2上测试) -将ocilib文件cd提取到ocilib文件夹,配置make并进行install

    tar -zxf ocilib-4.5.2-gnu.tar.gz
    cd ocilib-4.5.2
    ./configure --with-oracle-headers-path=/usr/include/oracle/12.2/client64/ --with-oracle-lib-path=/usr/lib/oracle/12.2/client64/lib CFLAGS="-O2 -m64"
    make
    sudo make install

-如果您需要处理unicode,请使用此配置,通常不需要

./configure --with-oracle-headers-path=/usr/include/oracle/12.2/client64/ --with-oracle-lib-path=/usr/lib/oracle/12.2/client64/lib --with-oracle-charset=wide CFLAGS="-O2 -m64"

-以上方法将OCILIB安装在您的计算机中。 -要在Vapor项目中使用OCILIB库,请在Package.swift文件中包含以下内容

    // swift-tools-version:4.0
    import PackageDescription

    let package = Package(
        name: "myAPIProject",
        dependencies: [
            //  A server-side Swift web framework.
            .package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"),

            //  Swift ORM (queries, models, relations, etc) built on SQLite 3.
            .package(url: "https://github.com/vapor/fluent-sqlite.git", from: "3.0.0"),

            //Oracle wrapper for swift
            .package(url: "https://github.com/h1257977/SwiftOracle.git", from: "0.1.7")
        ],
        targets: [
            .target(name: "App", dependencies: ["FluentSQLite","SwiftOracle", "Vapor"]),
            .target(name: "Run", dependencies: ["App"]),
            .testTarget(name: "AppTests", dependencies: ["App"])
        ]
    )

-在Routes.swift中,包括以下内容:

    import Vapor
    import SwiftOracle

    let service = OracleService(host: "192.168.1.12", port:"1521", service: "orcl")
    let b = Connection(service: service, user:"test", pwd:"oracle")

    final class VReq: Content {
        var name: String? 
        var age: String?

        init (NAME: String, AGE: String) {
        self.name = NAME
        self.age = AGE
    }

    final class VMdata {

            func getData() throws ->  [VReq] {
                try! b.open()
                b.autocommit = true
                let cursor = try! b.cursor()
                try! cursor.execute("select * from userlist")
                //iterates each row in the cursor and maps only the values (keys are unique) from the dictionary of each rows, if its nil it will replace with "null" 
                var items = cursor.map { row in row.dict.mapValues { "\($0 ?? "NULL")" }} //  output as [[String:String]]

                //takes each dictionary in the items array and returns a VReq
                let result = items.map { dict in VReq(NAME: dict["NAME"] ?? "NULL", ADDRESS: dict["ADDRESS"] ?? "NULL", USER_AGE: dict["USER_AGE"] ?? "NULL")}
                return result
                }

    }

    public func routes(_ router: Router) throws {

    router.get("test") { req -> [VReq] in

        let val = VMdata()
        let vdata = try! val.getData()
        return vdata

    }
    }

-要运行Vapor 3,您需要链接库文件

swift build -Xlinker -L/usr/local/lib && ./.build/x86_64-unknown-linux/debug/Run --hostname 0.0.0.0

-要显示任何数据库列中的unicode字符,您可能必须在托管Vapor应用程序的服务器中设置NLS_LANG。

export NLS_LANG=AMERICAN_AMERICA.AL32UTF8