GithubHelp home page GithubHelp logo

wentaozone / mojo-database Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cpjolicoeur/mojo-database

0.0 2.0 0.0 77 KB

ActiveRecord-like ORM interface for Sqlite in Objective-C for use in iOS applications

License: MIT License

mojo-database's Introduction

Mojo Database

MojoDatabase is an ActiveRecord-like ORM for SQLite written in Objective-C for use in iOS applications.

The goal is to provide a nicer user interface for using SQLite with Objective-C applications via the ActiveRecord pattern.

Installation

To install Mojo Database, simply drag the MojoDatabase directory into your XCode project and choose "copy files to destination" when prompted.

Usage

Mojo Database has two (2) types of interaction with your Objective-C application.

  • The "MojoModel" class will be used as a superclass of your model objects in your application. Generally, these Mojo Models will map directly to a table in your SQLite database.
  • The "AppDatabase" class is where you will define your database properties and schema and setup your migrations for updating the schema.

Creating and Connecting to a SQLite DB

In your application delegate header file, you should should create an instance variable that will be used to referenc e your database in the application.

@class MojoDatabase;

@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
  MojoDatabase *myDatabase;
}

@property (nonatomic, retain) MojoDatabase *myDatabase;

@end

In your application delegate implementation file, you should include the AppDatabase.h header file and create a connection to your DB during the app launch process.

#import "AppDatabase.h"

@implementation MyAppDelegate

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Setup Connection to the database
  self.database = [[[AppDatabase alloc] initWithMigrations] autorelease];
}

-(void)dealloc {
  [database release], database=nil;
  [super dealloc];
}

@end

This will intialize (and create your databse if it didnt already exist) and also update the database to the current version by running any missing migrations.

If desired, you can customize the name of the SQLite database created by changing the kDatabaseName string at the top of the AppDatabase.m file.

Creating Models

Each model you create should map directly to a SQLite table that was created via a migration entry. So, if you had the following migration method in AppDatabase.m:

-(void)createFriendTable {
  [self executeSql:@"CREATE TABLE Friend (primaryKey INTEGER primary key autoincrement, name TEXT, age INTEGER)"];
}

you would also have an Objective-C class called Friend that inherited from MojoModel. The following would be an example of your Friend.h header file:

#import "MojoModel.h"

@interface Friend : MojoMdel {
}

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSNumber *age;

@end

with the following basic Friend.m implementation file:

#import "Friend.h"

@implementation Friend

@synthesize name=_name;
@synthesize age=_age;

-(void)dealloc {
  [_name release], _name=nil;
  [_age release], _age=nil;
  [super dealloc];
}

@end

Creating a new friend object and saving it to your database would be as easy as:

Friend *newFriend = [[Friend alloc] init];
newFriend.name = @"Norman Rockwell";
newFriend.age = [NSNumber numerWithInteger:55];
[newFriend save];  // this will write out the new friend record to the databse

To later on retrieve that Friend object from the database:

NSArray *records = [self findByColumn:@"name" value:@"Norman Rockwell"];
if ( [records count] ) {
  Friend *normanRockwell = [records objectAtIndex:0];
}

Note that findByColumn always returns an NSArray object.

Support

Please use the GitHub Issues tracker for any bug reports or feature requests.

Contributions

If you wish to contribute to Mojo Database, please fork the project and send pull requests.

Credits

Mojo Database is based largely on Dylan Bruzenak's code from the iPhone Advanced Projects book, published by Apress, and was modified and extended by Craig P Jolicoeur.

mojo-database's People

Contributors

cpjolicoeur avatar

Watchers

James Cloos avatar 开水白菜 avatar

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.