je kunt de volgende methode gebruiken om verbinding te maken met de Oracle-database:(Dit was mogelijk met de hulp van de vapor community.)
-----om het oracle-stuurprogramma te laten werken, heb ik deze methode in Ubuntu gebonden --------------- oracle-client moet zodanig worden geïnstalleerd dat de header en het bibliotheekpad kunnen worden gedefinieerd, je kunt deze krijgen van de orakel-website.
oracle-instantclinet*-basic-*.rpm
oracle-instantclinet*-devel-*.rpm
oracle-instantclinet*-sqlplus-*.rpm
--installeer het aldus gedownloade pakket met het volgende commando
sudo alien -i oracle-instantclinet*-basic-*.rpm
sudo alien -i oracle-instantclinet*-devel-*.rpm
sudo alien -i oracle-instantclinet*-sqlplus-*.rpm
-- Installeer libaio1 in ubuntu
sudo apt install libaio1
-- dit pad zou in ~/.bashrc moeten staan
#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
--download OCILIB-bibliotheek van Githubgit-kloon https://github.com/vrogier/ocilib.git (of download de nieuwste versie / getest op ocilib 4.5.2 )-- extraheer de ocilib-bestands-cd naar de ocilib-map, configureer make en 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
-- gebruik deze configuratie als je met unicodes om moet gaan, over het algemeen heb je dit niet nodig
./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"
-- De bovenstaande methode installeert OCILIB op uw machine.-- Om de OCILIB-bibliotheek in uw Vapor-project te gebruiken Neem het volgende op in uw Package.swift-bestand
// 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"])
]
)
-- Voeg in Routes.swift het volgende toe:
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
}
}
--Om Vapor 3 uit te voeren, moet u de bibliotheekbestanden koppelen
swift build -Xlinker -L/usr/local/lib && ./.build/x86_64-unknown-linux/debug/Run --hostname 0.0.0.0
-- Om unicode-tekens uit een databasekolom weer te geven, moet u mogelijk NLS_LANG instellen in de Vapor-toepassing die de server host.
export NLS_LANG=AMERICAN_AMERICA.AL32UTF8