stephencelis / SQLite.swift

A type-safe, Swift-language layer over SQLite3.
MIT License
9.72k stars 1.57k forks source link

fix(LiteralValue): 'LiteralValue' initializer is inaccessible due to 'internal' protection #1223

Open theonetheycallneo opened 1 year ago

theonetheycallneo commented 1 year ago

Issues are used to track bugs and feature requests. Need help or have a general question? Ask on Stack Overflow (tag sqlite.swift).

Build Information

General guidelines

Creating dynamic columns in tables needs a way to create defaultValues:

public struct TableSchema: Codable, Equatable {
  public let schema: String
  public let name: String
  public let strict: Int
  public let num_cols: Int
  public let without_row_id: Int
  public let columns: [String: ColumnSchema]

  public func convertDataType(_ dataType: String) -> ColumnDefinition.Affinity {
    switch dataType {
      case "INTEGER":
        return ColumnDefinition.Affinity.INTEGER
      case "REAL":
        return ColumnDefinition.Affinity.REAL
      case "TEXT":
        return ColumnDefinition.Affinity.TEXT
      case "BLOB":
        return ColumnDefinition.Affinity.BLOB
      default:
        return ColumnDefinition.Affinity.TEXT
    }
  }

  public func createTable(in db: Connection) throws {
    let table = Table(self.name)
    try db.run(table.create(ifNotExists: true, block: { t in
        for column in columns {
            let col = column.value
            let columnDefinition = ColumnDefinition(
              name: col.name,
              type: convertDataType(col.data_type),
              nullable: col.nullable == 1,
              defaultValue: LiteralValue.init(col.default_value ?? "NULL") 
            )

            t.column(columnDefinition)
        }
    }))
  }
}

public enum LiteralValue should have public init

init(_ string: String?) {
        guard let string = string else {
            self = .NULL
            return
        }
        switch string {
        case "NULL": self = .NULL
        case "TRUE": self = .TRUE
        case "FALSE": self = .FALSE
        case "CURRENT_TIME": self = .CURRENT_TIME
        case "CURRENT_TIMESTAMP": self = .CURRENT_TIMESTAMP
        case "CURRENT_DATE": self = .CURRENT_DATE
        default: self = LiteralValue.parse(string)
        }
    }