greendao introduction

32
GreenDao 綠綠 Heaton

Upload: booch-lin

Post on 10-Feb-2017

2.422 views

Category:

Software


0 download

TRANSCRIPT

Page 1: GreenDao Introduction

GreenDao

綠道Heaton

Page 2: GreenDao Introduction

Outline

• Why GreenDao• Feature• Performance• How to getting start• How to use.

Page 3: GreenDao Introduction

Why GreenDao

• First . Some problem with DB Development.– No object orientation.– Data access sentence is low levely.– Always make same code to write SQL script.– Do you know SQL without 『 ||』 .– I am stupid on SQL tuning

• Second , it is faster than other ORM Lib.

Page 4: GreenDao Introduction

It is popular?

Page 5: GreenDao Introduction

Features

• High Performance– CRD test.

• ORM (Object relation mapping)– Code Generation– Read and write objects – CREATE TABLE done for you – Expressing Queries

• Session cache• Setter ; getter• Create table “table_name”• Return List<Common>

Page 6: GreenDao Introduction

Performance• Test case :

– insert 10000 row– delete 10000 row – query 20000 row

Test 1 Dao Content Provider

Insert 1467 87268 (no transcation)

Load 1608 9797

Delete 108 121

Test 2

Insert 662 87766

Load 1837 12282

delete 137 127

Page 7: GreenDao Introduction

Benchmark

• https://github.com/daj/android-orm-benchmark

• Write 10000• Read 10000

Page 8: GreenDao Introduction

ORM Object/relation mapping (ORM)

Page 9: GreenDao Introduction

A example for presentation

• Implement a social data collector• “Leftpage” as table name for database

“leftpageProvider.db”• Every row data as “CommonData”• Column has – ”DataID” as primary key – “Category” – “Provider” – “DisplayOrder”

Page 10: GreenDao Introduction

Expressing Queries: What difference

• SQL Query all– Cursor c =

mContext.getContentResolver().query(uriLPageProvider , projection , selection, selection args, sort order ) ;

• GreenDao Load all List<commonData> datas =

CommonDataDao.loadAll();

Page 11: GreenDao Introduction

Sqlite review• SQL Query – Provider equal facebook– Order by display order– Limit data count100

• Cursor c = getContentResolver() . query (uriLPageProvider , projection , selection, selection args, sort order ) ;

(String ) selection : StorageField.Provider. + "=?" =(String[])selection args: {“facebook”}

Page 12: GreenDao Introduction

Sort order• SQL Query – Provider equal facebook– Order by display order– Limit data count100

• Cursor c = getContentResolver() . query (uriLPageProvider , projection , selection, selection args, sort order ) ;

(String ) Sort order: StorageField.Displayorder

Page 13: GreenDao Introduction

More detail• SQL Query – Provider equal facebook– Order by display order– Limit data count100

• Cursor c = getContentResolver() . query (uriLPageProvider , projection , selection, selection args, sort order ) ;

(String ) Sort order:“ ASC ” + StorageField.Displayorder + “ LIMIT ” + request

Page 14: GreenDao Introduction

If you are sqlite master• SQL Query – Provider equal facebook– Order by display order– Limit data count100• Select * from leftpage where Provider =

‘facebook’ order asc ‘displayorder’ limit ‘100’

• Then you also load cusor and save to data list

Page 15: GreenDao Introduction

But in GreenDao• SQL Query – Provider equal facebook– Order by display order– Limit data count100

mCommonDataDao.queryBuilder() .where(Properties.Provider.eq(Provider.Facebook)) .orderAsc(Properties.DisplayOrder) .limit(100) .list();

Page 16: GreenDao Introduction

HOW TO GETTING START

Create module

Page 17: GreenDao Introduction

How to getting start?

First time is terrible

Page 18: GreenDao Introduction

Create a module to gen greendao code

• In module gradle– Add Gradle script– compile 'de.greenrobot:greendao-generator:1.3.1

Page 19: GreenDao Introduction

Create main() in our case.public class GreenDao { public static void main(String[] args){ Schema schema = new Schema

(1, "com.acer.android.leftpage.provider");

Entity commonData = schema.addEntity("commonData"); commonData.setTableName("LeftPage"); commonData.addLongProperty("DataID").primaryKey(); commonData.addStringProperty("Category"); commonData.addStringProperty("Provider"); commonData.addLongProperty(“DisplayOrder”); commonData.addContentProvider();DaoGenerator generator = new DaoGenerator(); generator.generateAll

(schema, "../AcerHome/LeftPage/src/main/java/");}

Page 20: GreenDao Introduction

Compile and run to Auto gen Core classes

DaoMaster

DaoSeesion

CommonDataDao

CommonData

Page 21: GreenDao Introduction

In CommonDataDao public static void createTable(SQLiteDatabase db, boolean ifNotExists) { String constraint = ifNotExists? "IF NOT EXISTS ": ""; db.execSQL("CREATE TABLE " + constraint + "'LeftPage' (" + // "'DataID' INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0:

DataID "'Category' TEXT," + // 1: Category "'Provider' TEXT," + // 2: Provider "'DisplayOrder' INTEGER DEFAULT -1," + // 3: DisplayOrder "'Bookmarked' INTEGER DEFAULT 0," + // 4: Bookmarked "'Deleted' INTEGER DEFAULT 0," + // 5: Deleted "'Score' REAL DEFAULT -1," + // 6: Score "'Keywords' TEXT DEFAULT NULL," + // 7: Keywords

Page 22: GreenDao Introduction

CommonData content public Long getDataID() { return DataID; } public void setDataID(Long DataID) { this.DataID = DataID; } …… …… ……

Page 23: GreenDao Introduction

Add it and start to use

• In Used Project gradle– compile 'de.greenrobot:greendao:1.3.7'

Page 24: GreenDao Introduction

HOW TO USEINSERT , DELETE , UPDATE,

Page 25: GreenDao Introduction

Initial

• DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context,”leftpageprovider.db”);

• DaoMaster master = new DaoMaster (helper.getDataBase());

• DaoSession session = master.newSession();• CommonDataDao CommonDataDao=

session.getCommonDataDao();

Page 26: GreenDao Introduction

Query• Query with syntax

– eq , notEq– ge , le , gt , lt– in , between– like

• List<commonData> dataList = CommonDataDao.queryBuilder()

.where(Properties.Provider.eq(Provider.Social)) .orderAsc(mCommonDataDao.Properties.DisplayOrder)

.list();

Page 27: GreenDao Introduction

Raw query SQLiteDatabase db =

daoSession.getDatabase(); Cursor cursor = db.rawQuery("SELECT *FROM

leftpage WHERE .....", null);

Page 28: GreenDao Introduction

Insert• Insert:

– CommonDataDao .insert(new CommonData(DATA_ID, arg1, arg2,x……));

• Bulk Insert – CommonDataDao .insertln(CommonDataList);– CommonDataDao.insertln(CommonData[]);

• InsetOrReplace– CommonDataDao.insertOrReplaceln(CommonDataList)

Note: DATA_ID usually is null .

Page 29: GreenDao Introduction

Delete

• Delete sentence– CommonDataDao.QueryBuilder()

.where(Properties.Provider.eq(facebook))

.buildDelete() .executeDeleteWithoutDetachingEntities();

• Delete all– CommonDataDao.deleteAll();

Page 30: GreenDao Introduction

Update

• Update sentence– CommonDataDao.update(new

CommonData(Data_ID , arg1 , arg2 , …….));• Bulk Update – CommonDataDao.updateln(CommonDataList);– CommonDataDao.updateln(CommonData[]);

Note: DATA_ID must assign what the row you want to update.

Page 32: GreenDao Introduction

Something here AsyncSession asyncSession =

DBHelper.getInstance(this).getAsyncSession(); asyncSession.insert( new CommonData(null, …, …));