Code Monkey home page Code Monkey logo

dbaccess's Introduction

DBAccess has been depricated in favour of SharkORM, it is open source and API compatible designed to be a drop in replacement , It can be found at https://github.com/sharksync/sharkorm and http://sharkorm.com

DBAccess iOS ORM

[DBAccess] is a fully featured and FREE to use ORM for iOS.

Replace CoreData whilst keeping your existing managed objects, but dump the predicates and long-winded syntax.

Instead use a simple and clean object syntax, with fast and concise inline queries.

DBAccess even has a conversion method to migrate your existing CoreData tables across.

Regularly updated and in constant use within many public applications it thrives on feedback from other developers and is supported by the authors via StackOverflow or directly via email.

It's mantra is simple, to be fast, simple to implement and the first choice for any developer.

Getting started

Integrating [DBAccess] into your project could not be simpler. This guide should be all you need to get you started and wondering how you ever developed iOS apps without it.

Every effort has been made to ensure that you can get working as quickly as possible, from supporting as many data types as possible and working with your existing classes to an absolute bare minimum of configuration required to integrate the framework.

Requirements

| DBAccess Version | Minimum iOS Target | Notes | |:--------------------:|:---------------------------:|:----------------------------:|:-------------------------------------------------------------------------:| | 1.x.x | iOS 7 | Xcode 5 is required. |

###Install From Cocoapods

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

pod "DBAccess"

Usage

Setting up your project

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

// Objective-C
@interface AppDelegate : UIResponder <UIApplicationDelegate, DBDelegate>
// Swift
class AppDelegate: UIResponder, UIApplicationDelegate, DBDelegate

Then you need to start DBAccess early on:

// Objective-C
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [DBAccess setDelegate:self];
    [DBAccess openDatabaseNamed:@"myDatabase"];
    return YES;
}
// Swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
	
	DBAccess.setDelegate(self)
	DBAccess.setPersistSynthesizedProperties(true)
	DBAccess.openDatabaseNamed("myDatabase")
	
	return true
}

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

In Objective-C properties need to be implemented using @dynamic, this is to indicate to the ORM that it will control the fetching and setting of these values from the database, and in Swift the property is defined as var dynamic

####Example Object Objective-C

//  Header File : Person.h
#import <DBAccess/DBAccess.h>

@interface Person : DBObject
@property NSString*         name;
@property int               age;
@property int               payrollNumber;
@end

// Source File : Person.m
#import "Person.h"

@implementation Person
@dynamic name,age,payrollNumber;
@end

Swift

@objc(Person)
class Person: DBObject {
	dynamic var name : String!
	dynamic var age : NSNumber!
	dynamic var payrollNumber : NSNumber!
}

###Creating objects, setting values & persistance Objective-C

// Create a new object
Person* thisPerson = [Person new];

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

// Persist the object into the datastore
[thisPerson commit];

Swift

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

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

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

###Querying objects To retrieve objects back, we use the DBQuery object that is associated with every DBObject 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.

Fetch an entire table

Objective-C

DBResultSet* results = [[Person query] fetch];

Swift

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

Query example with parameters

Objective-C

DBResultSet* results = [[[[[Person query]
                       where:@"age = 35"]
                       limit:99]
                     orderBy:@"name"]
                             fetch];

Swift

var results : DBResultSet = Person.query().whereWithFormat("age = %@", withParameters: [35]).limit(99).orderBy("name").fetch()

###Removing objects Objective-C

for (Person* person in [[Person query] fetch]) {
	[person remove];
}

// or the shorthand is to use the removeAll method on the DBResultSet object
[[[Person query] fetch] removeAll];

Swift

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

// or the shorthand is to use the removeAll method on the DBResultSet object
Person.query().fetch().removeAll()

###Other types of query There are other types of fetches in addition to just fetch, such as count, sum, groupBy and ids

Objective-C

/* count the rows within the Person table */
int count = [[Person query] count];
 
/* add all of the ages together */
double total = [[Person query] sumOf:@"age"];
 
/* group all the people together by the surname property */
NSDictionary* peopleBySurname = [[Person query] groupBy:@"name"];
 
/* get just the primary keys for a query, useful to save memory */
NSArray* ids = [[Person query] ids];

Swift

/* count the rows within the Person table */
var count = Person.query().count()

/* add all of the ages together */
var total = Person.query().sumOf("age")

/* group all the people together by the surname property */
var peopleBySurname = Person.query().groupBy("name")

/* get just the primary keys for a query, useful to save memory */
var ids = Person.query().ids();

Requirements:

  • CocoaPods 0.31

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.