Swiftpack.co - pujiaxin33/SQLiteValueExtension as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by pujiaxin33.
pujiaxin33/SQLiteValueExtension 0.0.7
SQLiteValueExtension for SQLite.swift
⭐️ 11
🕓 42 weeks ago
iOS
.package(url: "https://github.com/pujiaxin33/SQLiteValueExtension.git", from: "0.0.7")

SQLiteValueExtension

Based on the SQLite.swift library, it's easy to store Array, Dictionary or custom data types.

The principle: Convert the Array, Dictionary, or custom type to String type, and then store it. When querying, convert the string to an Array, Dictionary or custom data type.

use

The data type that conforms to the SQLiteValueStringExpressible protocol, it can be stored in the database through SQLite.swift.

Native basic type

The following basic types conform the SQLiteValueStringExpressible protocol.

  • Int,
  • Int64
  • Bool
  • Double
  • Float
  • String
  • Blob
  • Data
  • Date

Array, dictionary

The Array.Element, Dictionary.Key and Dictionary.Value types conform to the SQLiteValueStringExpressible protocol and can be stored in the database through SQLite.swift.

//Expression definition
static let intArray = Expression<[Int]?>("int_array")
static let intStringDict = Expression<[Int:String]?>("int_string_dict")
//Insert
let insert = config.insert(normalInt <- basic.normalInt, intStringDict <- basic.intStringDict)
try connection.run(insert)
//Query
let rows = try connection.prepare(config)
var result = [BasicDataModel](https://raw.github.com/pujiaxin33/SQLiteValueExtension/master/)
for data in rows {
    let basic = BasicDataModel(JSON: [String: Any](https://raw.github.com/pujiaxin33/SQLiteValueExtension/master/))!
    basic.normalInt = data[normalInt]
    basic.intStringDict = data[intStringDict]
    result.append(basic)
}

Custom type

Conform the SQLiteValueStorable protocol and implement related methods.

SQLiteValueStorable inherits the SQLiteValueStringExpressible protocol, and specifies datatypeValue as String in extensioin, for easier to use.

extension BasicInfoModel: SQLiteValueStorable {
    public static func fromStringValue(_ stringValue: String) -> BasicInfoModel {
        return BasicInfoModel(JSONString: stringValue) ?? BasicInfoModel(JSON: [String: Any](https://raw.github.com/pujiaxin33/SQLiteValueExtension/master/))!
    }
    public var stringValue: String {
        return toJSONString() ?? ""
    }
}

Examples of store Array or Dictionary which contains custom data type

//Expression definition
static let modelArray = Expression<[BasicInfoModel]?>("model_array")
static let stringModelDict = Expression<[String:BasicInfoModel]?>("string_model_dict")
//Insert
let insert = config.insert(modelArray <- basic.modelArray, stringModelDict <- basic.stringModelDict)
try connection.run(insert)
//Query
let rows = try connection.prepare(config)
var result = [BasicDataModel](https://raw.github.com/pujiaxin33/SQLiteValueExtension/master/)
for data in rows {
    let basic = BasicDataModel(JSON: [String: Any](https://raw.github.com/pujiaxin33/SQLiteValueExtension/master/))!
    basic.modelArray = data[modelArray]
    basic.intStringDict = data[intStringDict]
    result.append(basic)
}

Added support for basic types

For example, the Float data type:

extension Float: SQLiteValueStringExpressible {
    public static var declaredDatatype: String {Double.declaredDatatype}
    public static func fromDatatypeValue(_ datatypeValue: Double) -> Float {
        return Float(datatypeValue)
    }
    public var datatypeValue: Double {
        return Double(self)
    }
    public static func fromStringValue(_ stringValue: String) -> Float {
        return Float(stringValue) ?? 0
    }
    public var stringValue: String {
        return String(self)
    }
}

If you need to support other basic data types, please submit an Issue or Pull Request.

Install

Cocoapods

pod'SQLiteValueExtension'

SPM

It is supported from version 0.0.6.

For the installation tutorial of Xcode11, please refer to the article: Use Swift Package in Xcode

recommend

GitHub

link
Stars: 11
Last commit: 3 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics