Skip to content

Commit

Permalink
feat: add oracle’s number as a datatype
Browse files Browse the repository at this point in the history
  • Loading branch information
lovetodream committed Aug 27, 2023
1 parent 8cdde67 commit 167ed3b
Show file tree
Hide file tree
Showing 15 changed files with 453 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Sources/OracleNIO/Data/Bool+OracleCodable.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import NIOCore

extension Bool: OracleEncodable {
public static var oracleType: DBType { .boolean }
public var oracleType: DBType { .boolean }

public func encode<JSONEncoder: OracleJSONEncoder>(
into buffer: inout ByteBuffer,
Expand Down
2 changes: 1 addition & 1 deletion Sources/OracleNIO/Data/ByteBuffer+OracleCodable.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import NIOCore

extension ByteBuffer: OracleEncodable {
public static var oracleType: DBType { .raw }
public var oracleType: DBType { .raw }

/// Encodes `self` into wire data starting from the current `readerIndex`.
///
Expand Down
2 changes: 1 addition & 1 deletion Sources/OracleNIO/Data/Cursor+OracleCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public struct Cursor {
}

extension Cursor: OracleEncodable {
public static var oracleType: DBType { .cursor }
public var oracleType: DBType { .cursor }

public func encode<JSONEncoder: OracleJSONEncoder>(
into buffer: inout ByteBuffer,
Expand Down
4 changes: 2 additions & 2 deletions Sources/OracleNIO/Data/Date+OracleCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import struct Foundation.DateComponents
import struct Foundation.TimeZone

extension Date: OracleEncodable {
public static var oracleType: DBType { .timestampTZ }
public var oracleType: DBType { .timestampTZ }

public func encode<JSONEncoder: OracleJSONEncoder>(
into buffer: inout ByteBuffer,
context: OracleEncodingContext<JSONEncoder>
) {
var length = Self.oracleType.bufferSizeFactor
var length = self.oracleType.bufferSizeFactor
let components = Calendar.current.dateComponents(
[.year, .month, .day, .hour, .minute, .second, .nanosecond],
from: self
Expand Down
2 changes: 1 addition & 1 deletion Sources/OracleNIO/Data/Double+OracleCodable.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import NIOCore

extension Double: OracleEncodable {
public static var oracleType: DBType {
public var oracleType: DBType {
.binaryDouble
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/OracleNIO/Data/Float+OracleCodable.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import NIOCore

extension Float: OracleEncodable {
public static var oracleType: DBType { .binaryFloat }
public var oracleType: DBType { .binaryFloat }

public func encode<JSONEncoder: OracleJSONEncoder>(
into buffer: inout ByteBuffer,
Expand Down
4 changes: 2 additions & 2 deletions Sources/OracleNIO/Data/Int+OracleCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ extension UInt: OracleDecodable {
// MARK: Int

extension Int: OracleEncodable {
public static var oracleType: DBType { .number }
public var oracleType: DBType { .binaryInteger }

public func encode<JSONEncoder: OracleJSONEncoder>(
into buffer: inout ByteBuffer,
context: OracleEncodingContext<JSONEncoder>
) {
// TODO: implement a better version of ByteBuffer.writeOracleNumber
OracleNumeric.encodeNumeric(self, into: &buffer)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/OracleNIO/Data/IntervalDS+OracleCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extension IntervalDS: ExpressibleByFloatLiteral {
}

extension IntervalDS: OracleEncodable {
public static var oracleType: DBType { .intervalDS }
public var oracleType: DBType { .intervalDS }

public func encode<JSONEncoder: OracleJSONEncoder>(
into buffer: inout ByteBuffer,
Expand Down
2 changes: 1 addition & 1 deletion Sources/OracleNIO/Data/LOB+OracleCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public final class LOB {
}

extension LOB: OracleEncodable {
public static var oracleType: DBType { .blob }
public var oracleType: DBType { .blob }

public func encode<JSONEncoder: OracleJSONEncoder>(
into buffer: inout ByteBuffer,
Expand Down
90 changes: 90 additions & 0 deletions Sources/OracleNIO/Data/OracleNumber.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import NIOCore
import struct Foundation.Decimal

public struct OracleNumber:
CustomStringConvertible, CustomDebugStringConvertible,
ExpressibleByStringLiteral, ExpressibleByIntegerLiteral,
ExpressibleByFloatLiteral, Equatable, Hashable, Sendable
{
internal var value: ByteBuffer

public var double: Double? {
var value = self.value.getSlice(
at: 1, length: self.value.readableBytes - 1
)! // skip length
return try? OracleNumeric.parseFloat(from: &value)
}

public var description: String {
if let double = self.double {
return "\(double)"
}
return "<invalid_number>"
}

public var debugDescription: String {
String(describing: value)
}

public init<T: Numeric>(_ value: T) where T: LosslessStringConvertible {
self.init(ascii: value.ascii)
}

public init(stringLiteral value: String) {
self.init(ascii: value.ascii)
}

public init(integerLiteral value: Int) {
self.init(ascii: value.ascii)
}

public init(floatLiteral value: Double) {
self.init(ascii: value.ascii)
}

public init(decimal: Decimal) {
self.init(ascii: decimal.description.ascii)
}


internal init(value: ByteBuffer) {
self.value = value
}

internal init(ascii: [UInt8]) {
var buffer = ByteBuffer()
OracleNumeric.encodeNumeric(ascii, into: &buffer)
self.init(value: buffer)
}

public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.double == rhs.double
}

public func hash(into hasher: inout Hasher) {
hasher.combine(self.double)
}
}

extension OracleNumber: OracleDecodable {
public init<JSONDecoder: OracleJSONDecoder>(
from buffer: inout ByteBuffer,
type: OracleDataType,
context: OracleDecodingContext<JSONDecoder>
) throws {
var bufferWithLength = ByteBuffer(bytes: [UInt8(buffer.readableBytes)])
bufferWithLength.writeBuffer(&buffer)
self.value = bufferWithLength
}
}

extension OracleNumber: OracleEncodable {
public var oracleType: DBType { .number }

public func encode<JSONEncoder: OracleJSONEncoder>(
into buffer: inout ByteBuffer,
context: OracleEncodingContext<JSONEncoder>
) {
buffer.writeImmutableBuffer(self.value)
}
}
Loading

0 comments on commit 167ed3b

Please sign in to comment.