Giter VIP home page Giter VIP logo

camembert's Introduction

Camembert

This framework is beging to rewritte, with a new API more safe and more reliable.

Camembert is a toolkit written in swift, for using sqlite3 easier. Is is available for OSX and iOS.

Installation

First you need to add a bridging-header to your project. If it is already set, import Camembert.

#import "Camembert.h"

If you need to add one, follow these instructions:

  • Add libsqlite3.0.tbd and libsqlite3.tbd under Linked Frameworks and Libraries in project settings.
  • Create a new header file.
  • Go into the settings of your project in the category build setting.
  • Search Objective-C Bridging Header with the search bar.
  • Add the following line : headerFileName.h
  • Then import #import "Camembert.h" in your header file.

When the Bridging Header is created, simply copy the files *.m and *.swift from the directory sources.


Usage

First thing to do, is to initialise your sqlite3 database. To do this you must make a call before any other use. The database will be created automatically if it does not exist

Camembert.initDataBase("dataBase.sql")

And voila !, you are ready to use Camembert.

You need now to create a class, matching with your table. For doing that, you MUST inherit of CamembertModel class. And use the typealias for create your rows. You have to provide defaults values.

typealias INTEGER = Int
typealias REAL = Float
typealias TEXT = String
typealias DATE_TIME = NSDate
typealias BIT = Bool

This is an example of a table Book:

class Book :CamembertModel {
    var title :TEXT = ""
    var numberPage :INTEGER = 0
    var currentPage :INTEGER = 0
}

The Book class corresponds to model, and will be associated with the table Book in the database.

Create a new element

var newBook = Book()
newBook.title = "La Fontaine : Fables"
newBook.numberPage = 544
newBook.currentPage = 43
newBook.push()

As you can see for creating a new element, you just need to create a new object, and call the push method. If the table doest not exist yet, it will be created automatly.

Update element

var newBook = Book()
newBook.title = "La Fontaine : Fables"
newBook.numberPage = 544
newBook.currentPage = 43
newBook.push()

//...

newBook.currentPage = 103
newBook.update()

To change something in a existing element, you just need to call the update method. To do this you need to have an object already created. Or you can use the constructor with a specific element based on the ID:

var book = Book(id: 4)
book.currentPage = 103
book.update()

Remove element

var newBook = Book()
newBook.title = "La Fontaine : Fables"
newBook.numberPage = 544
newBook.currentPage = 43
newBook.push()

//...

newBook.remove()

var book = Book(id: 4)
book.remove()

Just call the remove method, for remove the element in the Table.

Get the number of elements in a table

var numberElement :Int = Book.numberElement()
println("number books : \(numberElement)")

Get list of elements in a table

For select elements in the tables, you need to perform a request. For doing this easily you can use the enum Select.

enum Select {
    case SelectAll(OrderOperator, String)
    case CustomRequest(String)
    case Limit(Int, OrderOperator, String)
    case Between(Int, Int, OrderOperator, String)
    case Where(String, Operator, AnyObject, OrderOperator, String)
}
  • SelectAll(OrderOperator, String): will return all element in the table
  • CustomRequest(String): You can use there your own SQL request
  • Limit(Int, OrderOperator, String): will return a limited number of element
  • Between(Int, Int, OrderOperator, String): will return all the element between the interval
  • Where (ColumnName: String, Operator: Larger,equal..etc, Value, OrderOperator, ColumnToOrderBy): will return elements that value of Column specified matches the passed value ("Value")
//display titles of the library (if order by is empty like in example below, order will be done on id column)
for currentElement in Book.select(selectRequest: Select.SelectAll, order: OrderOperator.Ascending, orderby: "") {
  println("current Book's title: \((currentElement as Book).title)")
}


```Swift
//display titles of the library
for currentElement in Book.select(selectRequest: Select.SelectAll, OrderOperator.Ascending, "") {
println("current Book's title: \((currentElement as Book).title)")
}

//reset currentPage
for currentElement in Book.select(selectRequest: Select.CustomRequest("SELECT * FROM Book WHERE currentPage > 0")) {
  (currentElement as Book).currentPage = 0
  (currentElement as Book).update()
}
//How To us extension methods:
var myArray = Array<AnyObject>();
if let m_array = UserModel.select(selectRequest: Select.Where("FirstName", Operator.EqualsTo, "Hello", OrderOperator.Ascending, "LastName")){
    myArray = m_array.Take(10);
}

if let m_array = UserModel.select(selectRequest: Select.Where("FirstName", Operator.EqualsTo, "Hello", OrderOperator.Ascending, "")){
let myUserModel = (m_array.FirstOrDefault() as UserModel);
}

if let m_array = UserModel.select(selectRequest: Select.Where("FirstName", Operator.EqualsTo, "Hello", OrderOperator.Ascending, "")){
let myUserModel = (m_array.LastOrDefault() as UserModel);
}

if var m_array = UserModel.select(selectRequest: Select.Where("FirstName", Operator.EqualsTo, "Hello", OrderOperator.Ascending, "")){
myArray = m_array.TakeRange(1, offset: 10);
}

if var m_array = UserModel.select(selectRequest: Select.Where("FirstName", Operator.EqualsTo, "Hello", OrderOperator.Ascending, "")){
myArray = m_array.Union(Array())
}

let FirstName = (myArray as Array)[0].FirstName;

Get list of table

You get an array of all tables present in your database:

let listTable = Camembert.getListTable()

Camembert, will improve, here's the first version.

Author

Rémi ROBERT, [email protected]

Contributers List

Omar Bizreh, [email protected]

Licence

Camembert is available under the MIT license. See the LICENSE file for more info.

camembert's People

Contributors

omarbizreh avatar remirobert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

camembert's Issues

Issue in getting class name

Hi, I faced an issue in this function:
it is always returning nil when project name includes spaces, for example if my project is called "My App" the return result will be nil. If I know Objective-C I would have helped fixing it 👍

id camembertCreateObject(NSString *nameClass) {
    NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
    NSString *classStringName = [NSString stringWithFormat:@"_TtC%lu%@%lu%@", (unsigned long)appName.length,
                                 appName, (unsigned long)nameClass.length, nameClass];

    Class customClass = NSClassFromString(classStringName);
    if (customClass == nil) {
        return nil;
    }
    return [[customClass alloc] init];
}

Swift 2 compliant

Gents: Good job on this little frame-work. As you know, Swift 2 discontinued "reflect" and introduced "Mirror" class instead. I've changed your wonderful code and made it Swift-2 compliant. If you want, I can submit the code here.

Kind regards.

Framework

it will be awesome to make a framework with all dependencies, for simple integration. I tried, and i didn't get success to build one.

exc_bad_instruction code=exc_i386_invop

getObjectsWithQuery()

Everything is fine, I have created the Model and initialized database in AppDelegate but still it's giving me this error when I try to run a select query to fetch all records from my one and only table.

Please if someone can help me out here.

screen shot 2015-11-10 at 4 55 56 pm

_initWithId(id: Int) returns data, with some offset

I have a model name Ingredient having name, brand, cost
When I _initWithId some ingredient the object that I get is of type "Ingredient" but it's properties are not returned in the correct way. i.e. when I access .name it returns empty, but when access .brand it returns the name and on .cost it returns the brand.. simple it does the following

.name = ""
.brand = .name
.cost = .brand
.cost = (Only God knows)

Does anybody understand this error? Can anyone please help me with it, I have tried to make a breakpoint in CamembertModel.swift and try to track down the error but no luck..

Problem with REAL type

File: Camembert.swift
Method: getObjectsWithQuery

line 97 should be (replace "sqlite3_column_int" with "sqlite3_column_double")
case SQLITE_FLOAT:
currentObject.setValue((Float(sqlite3_column_double(ptrRequest,
CInt(index))) as AnyObject), forKey: columName)

NSDate Issue

Camembert seems to save an NSDate as a string, and when I retrieve the date from the database I expect it to be an NSDate back again, but it's not. How do I solve this problem?

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.