请选择 进入手机版 | 继续访问电脑版
  • 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

editfmah/sharkorm: Shark ORM for iOS/macOS/tvOS/watchOS

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

editfmah/sharkorm

开源软件地址(OpenSource Url):

https://github.com/editfmah/sharkorm

开源编程语言(OpenSource Language):

C 92.9%

开源软件介绍(OpenSource Introduction):

Shark ORM

Shark allows you to create a model layer in your iOS, macOS or tvOS app naturally, using simple and clean syntax. Shark does as much of the heavy lifting for you, so you don't have to put unnecessary effort into dealing with your data objects.

Its mantra is simple, to be fast, simple and the first choice for any developer.

Getting started

Shark is designed to get your app working quickly, integrated as source code, or as a framework.

Requirements

XCode 9+, iOS8+

Install From Cocoapods

To install it, simply add the following line to your Podfile:

pod "SharkORM"

Install as Framework

Download the source code from the GitHub release, and then within your application, add the following:

import SharkORM

Install as Source

Download the source code from GitHub and add to your target the contents of Core and SQLite, then add SharkORM.h into the bridging header.

Getting help and support

If you are having trouble with your implementation then simply ask a question on Stack Overflow, the team actively monitor SO and will answer your questions as quickly as possible.

If you have found a bug or want to suggest a feature, then feel free to use the issue tracker in GitHub (https://github.com/sharksync/sharkorm/issues) to raise an issue.

Usage

Setting up your project

Once you have added the SharkORM framework into your application, you will need to start it as soon as possible in your application lifecycle. SRKDelegate needs to be set as well, we recommend this is added to your application delegate.

// Swift
class AppDelegate: UIResponder, UIApplicationDelegate, SRKDelegate

Then you need to start SharkORM early on:

// Swift
SharkORM.setDelegate(self)
SharkORM.openDatabaseNamed("MyDatabase")

Objects

SharkORM objects are normal classes with properties defined on them, the ORM then inspects all these classes and mirrors their structure in a SQLite database automatically. If you add or remove columns, then the tables are updated to represent the current structure of the classes.

You can use these classes in much the same way as any other class in the system, and they can be extended with methods and sub classed, and passed around from thread to thread with no problem.

Much like other ORM's, Swift & Objective-C entities have to have their properties defined as dynamic to allow the ORM to install it's own methods for get/set.

// Swift
class Person: SRKObject {

@objc dynamic var name: String?
@objc dynamic var age: Int = 0
@objc dynamic var height: Float = 0

// add a one->many relationship from the Person entity to the Department entity.
@objc dynamic var department: Department?

}

Schemas (Migration)

The schema is automatically maintained from the class signatures and all additions, deletions & type changes are automatically made to the database. Where possible data is retained and converted between types.

If a default value is specified using, defaultValuesForEntity and a property is added, then the column is automatically populated with the default value.

Tables are created automatically by referencing a class which is a subclass of SRKObject.

Excluding properties from the schema.

By default all properties that are @objc dynamic var are picked up by SharkORM and added to the coresponding tables. If you wish to exclude certain properties which are not for persistence then implement the class method ignoredProperties, for which you return an array of string values which match the properties you wish the ORM to ignore.

Example:

// Swift
override class func ignoredProperties() -> [String] {
    return ["height"]
}

Example Object

// Swift
class Person: SRKObject {

@objc dynamic var name: String?
@objc dynamic var age: Int = 0
@objc dynamic var payrollNumber: Int = 0

// add a one->many relationship from the Person entity to the Department entity.
@objc dynamic var department: Department?

}

Initial values

You can initialise an SRKObject with a dictionary, allowing you to populate an object programatically.

Example:

// Swift
let p = Person(dictionary: ["name" : "Shark Developer", "age" : 39])

##Supported Types

Shark supports the following (Objective-c) types: BOOL, bool, int, int64, uint, uint64, float, double, long, long long, unsigned long long, NSString, NSDate, NSData, NSNumber. To use some native Swift types you will need to supply them with a default value as the equivalents are not nullable.

Relationships

SRKObjects can be linked to each other either by directly embedding them to create a one-to-one relationship (dynamic var department : Department?) or for a one-to-many relationship we employ the use of a method which returns either an NSArray<Person*>* or SRKResultSet object.

With the Person object already defined, and with a property department let’s look at the Department class.

Swift

class Department : SRKObject { 
    @objc dynamic var name: String?
    @objc dynamic var location: Location?
}

One-to-One Relationships

This has been created by adding the Department property into the SRKObject class, and once it has been set with a value it can be used as any other property making use of object dot notation.

Swift

let employee = Person()
let section = Department()
employee.department = section

Properties can then be accessed directly, and Shark will automatically retrieve any related objects and allow you to access their properties. For example employee.deparment.location.address will automatically retrieve the required objects to satisfy the statement by loading the related Department and Location objects.

One-to-Many Relationships

You can define to-many relationships by adding methods to the inverse relationship. For example, to relate in a to-many relationship Department and Person we would add the following method to Department.

Swift

func people() -> [Person] {
    return Person.query()
                 .where("department = ?", parameters: [self])
                 .fetch() as! [Person]
}

Indexing Properties

Shark supports indexing by overriding the indexDefinitionForEntity method and returning an SRKIndexDefinition object which describes all of the indexes that need to be maintained on the object.

Swift

override class func indexDefinitionForEntity() -> SRKIndexDefinition? {
    return SRKIndexDefinition(["name","age"])
}

These will automatically be matched to the appropriate query to aid performance. All related object properties are automatically indexed as is required for caching. So there would be no need, for instance, to add in an index for Person.department as it will have already been created.

Default Values

You can specify a set of default values for whenever a new SRKObject is created, by overriding the method defaultValuesForEntity, and returning a dictionary of default values: Swift

override class func defaultValuesForEntity() -> [String : Any] {
    return ["name" : "Billy", "age" : 36]
}

Triggers

Shark objects all have the same methods available for them, to enforce constraints and check validity before or after writes have been made.

entityWillInsert(), entityWillUpdate(), entityWillDelete() returning bool

Objects receive this method before any action has been carried out. In here you can test to see if you wish the operation to continue. If true is returned then the operation is told to continue, but if false is retuned then the transaction is aborted, and the commit returns false.

Swift

override func entityWillDelete() -> Bool {
    return self.people().count == 0;
}

entityDidInsert(), entityDidUpdate(), entityDidDelete()

Objects receive this message after an event has happened and after the transaction is complete.

Printing objects using print(), NSLog or po

We have provided a printable dictionary styled output which, when called, produces output like below.

{
    entity = Person;
    joins =     {
    };
    "pk column" = Id;
    "pk value" = 36664;
    properties =     (
                {
            name = Id;
            type = number;
            value = 36664;
        },
                {
            name = payrollNumber;
            type = number;
            value = 0;
        },
                {
            name = age;
            type = number;
            value = 36;
        },
                {
            name = Name;
            type = unset;
            value = "<null>";
        },
                {
            name = location;
            type = unset;
            value = "<null>";
        },
                {
            name = department;
            type = unset;
            value = "<null>";
        },
                {
            name = seq;
            type = number;
            value = 0;
        }
    );
    relationships =     (
                {
            property = department;
            status = unloaded;
            target = Department;
        },
                {
            property = location;
            status = unloaded;
            target = Location;
        }
    );
}

Writing Objects

Shark looks to simplify the persistence of objects down to a simple method commit. This can be called at any moment and from any thread. If an object contains either a single or multiple related objects within it, then calling commit on the parent object will automatically store all the subsequent objects too.

Swift

// Create a new object
var thisPerson = Person()

// Set some properties
thisPerson.age = 38;
thisPerson.payrollNumber = 123456;
thisPerson.name = "Adrian Herridge";

// Persist the object into the datastore
thisPerson.commit()

Objects are committed immediately and are written to the store in an atomic fashion. They will become immediately queryable upon completion.

.commitOptions (property)

the commitOptions property is present in all SRKObjects, and allows the developer to control on an object-by-object basis how SharkORM behaves when asked to commit certain objects.

The following properties are used to control the logic and add fine grain control:

postCommitBlock Called once a successful commit has completed

postRemoveBlock Called after an object has been removed from the data store

ignoreEntities Allows the developer to specify an array of child/related entities that will not be persisted when the parent object is commited.

commitChildObjects If set, then all child/related entitied will not automatically be commited too.

resetOptionsAfterCommit If set, then all defaults will be restored and all blocks cleared.

raiseErrors If set to false then any errors generated are ignored and not raised with the delegate. Transactions will also not be failed.

triggerEvents If set to true then events are raised for insert,update,delete operations.

Writing in Transactions

For some batch storage situations, it may be better to batch a lot of writes into a single transaction, this will improve speed, and give you atomicity over the persistence to the data store. All changes to all objects will be rolled back upon any raised error within the block. Event triggers will be not be executed until successful completion of the transaction.

You may manually fail a transaction by calling SRKFailTransaction() within the block, allowing developers to abort and rollback based on applicaiton logic.

Swift

SRKTransaction.transaction({ 
    // Create a new object
    var thisPerson = Person()
    thisPerson.name = "Adrian Herridge";
    thisPerson.commit()
    }) { 
        // the rollback on failure 
    }

Querying

To retrieve objects back, we use the SRKQuery object that is associated with every SRKObject class. This then takes optional parameters such as where, limit, orderBy & offset. All of the parameters return the same query object back, enabling the building of a query within a single nested instruction.

The final call to a query object is made using fetch, count, sum, fetchLightweight & fetchAsync which will then execute the query and return the results.

An example to fetch an entire table: Swift

var results : SRKResultSet = Person.query().fetch()

Queries can be built up using a FLUENT interface, so every call except a call to a retrieval method returns itself as a SRKQuery, allowing you to nest your parameters. Swift

var results = Person.query()
                    .where("age = ?", parameters: [35])
                    .limit(99)
                    .order("name")
                    .fetch()

you can also use object dot notation to query related objects via the property path. If we take the example of a Person class which is related to the Department class via the department property.

Swift

Person.query().where("department.name = ?", parameters: ["Test Department"]).fetch()

Where name is within a related object, SharkORM will now automatically re-arrange the query and join the two tables on that relationship and therefore validate that condition.

Supported parameters to SRKQuery

Shark supports the following optional parameters to a query:

where, where(with parameters).

This is the query string supplied to the query, and can contain placeholders as represented by ?. There is no need to encapsulate string parameters with quotation marks as this is automatically dealt with by the bind parameter call in SQLite.

Parameter objects can also be Arrays and Sets for use in subqueries, such as "department IN (?)", parameters: [[1,2,3,4,5]].

limit

Specifies the limit to the number of query results to return

order

Specifies the order by which the SRKResultSet will be returned. These can be chained together to produce multiple vectors. Example, .....order("Name").order(descending: "age").fetch()

offset

Specifies the offset in the values to be retrieved, to allow developers to only retrieve a window of data when required.

batch

This, although it does not affect the query, does allow developers to iterate through a large data set without having the performance and memory issue of dealing with the entire data set. If a batch size of 10 is specified, then the SRKResultSet will perform an entire query, but will only fully retrieve the first 10 objects. Then, it will maintain a window of the batch size when iterating through the results, automatically fetching them in batches. This enables developers to optimise their system without the need to change the way their code is written.

joinTo

Shark allows LEFT JOIN unions to be made, to allow for faster and less nested queries. See Joins for more info.

Other types of Query

In addition to retrieving entire objects there are also additional types of queries which help developers solve other problems.

fetchLightweight

Fetches an object from the store, except it does not retrieve any property values. These are lazily loaded upon access, and can be configured to then be permanently available of freed immediately.

fetchAsync

Performs an asynchronous query on a background thread and then executes the supplied block when the results are complete.

count

Returns a count of the query, the same as COUNT(*) would.

sum

Returns a SUM(field) value from the supplied property name, these can also be compound, such as SUM(property1 + property2).

distinct

Returns an NSArray of the distinct values for a particular column, it is used like distinct("surname").

group

Returns an NSDictionary, which is grouped by the specified property groupBy("surname").

ids

Returns the PK values of the matching objects, this is a faster way to store results for use in a subquery. Although, in practice it is little faster than using lightweight objects.

Joins

Joins represent the most powerful feature of SQL as the way any RDBMS is optimised is not through subqueries, but through joins and null checking.

In Shark, for the time being, all joins are LEFT JOIN. Simply because we have to retrieve whole objects from the originating query class. But joins can be multiple and compound.

Example of a join from [Person] -> [Department] Swift

Person.query()
	  .joinTo(Department, leftParameter: "department", targetParameter: "Id")

But you can also create an [Person]->[Department]->[Location] three way join, using the result of the first join to perform the second. Swift

Person.query()
      .joinTo(Department, leftParameter: "department", targetParameter: "Id")
      .joinTo(Location, leftParameter: "Department.location", targetParameter: "Id")

Once you have performed your join, the results are stored per object in a dictionary joinedResults.

An example of output looks like this.

{
    "Department.Id" = 61;
    "Department.location" = 35;
    "Department.name" = Development;
    "Location.Id" = 35;
    "Location.locationName" = Alton;
}

Removing objects

To remove an object from Shark you simply call remove() on this object, this will delete it form the data store and sterilise it to ensure it cannot be accidentally written back at a later date. To optimise the bulk removal of objects, a query can be combined with a call to removeAll() on the result set to delete many objects at once.

Swift

Person.query()
      .whereWithFormat("age < %@", withParameters: [18])
      .fetch()
      .remove()

The longhand version of this is:

Swift

for person in Person.query().fetch() {
	person.remove()
}

热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap