SlideShare a Scribd company logo
SQLite & ORM Binding - Part I
SQLite Mapping
✦SQLite property objects can be mapped to SQLite tables
✦I also had to moved a couple of properties to getters/
setters as at the time I built the app the API didn’t allow
excluding an entry
✦I abstracted the mapping further by creating my own
persistence class
© Codename One 2017 all rights reserved
public class AppStorage {
private static SQLMap smap;
private static Database db;
private static AppStorage instance = new AppStorage();
private EventDispatcher dispatcher = new EventDispatcher();
private AppStorage() {}
public static AppStorage getInstance() {
initStorage();
return instance;
}
private static void initStorage() {
if(smap == null) {
try {
db = Display.getInstance().openOrCreate("RestDb.sql");
smap = SQLMap.create(db);
AppSettings app = new AppSettings();
Dish dish = new Dish();
smap.setPrimaryKey(app, app.id);
smap.setPrimaryKey(dish, dish.id);
smap.setSqlType(dish.price, SQLMap.SqlType.SQL_DOUBLE);
smap.createTable(app);
smap.createTable(dish);
} catch(IOException err) {
Log.e(err);
}
}
}
AppStorage
public class AppStorage {
private static SQLMap smap;
private static Database db;
private static AppStorage instance = new AppStorage();
private EventDispatcher dispatcher = new EventDispatcher();
private AppStorage() {}
public static AppStorage getInstance() {
initStorage();
return instance;
}
private static void initStorage() {
if(smap == null) {
try {
db = Display.getInstance().openOrCreate("RestDb.sql");
smap = SQLMap.create(db);
AppSettings app = new AppSettings();
Dish dish = new Dish();
smap.setPrimaryKey(app, app.id);
smap.setPrimaryKey(dish, dish.id);
smap.setSqlType(dish.price, SQLMap.SqlType.SQL_DOUBLE);
smap.createTable(app);
smap.createTable(dish);
} catch(IOException err) {
Log.e(err);
}
}
}
AppStorage
public class AppStorage {
private static SQLMap smap;
private static Database db;
private static AppStorage instance = new AppStorage();
private EventDispatcher dispatcher = new EventDispatcher();
private AppStorage() {}
public static AppStorage getInstance() {
initStorage();
return instance;
}
private static void initStorage() {
if(smap == null) {
try {
db = Display.getInstance().openOrCreate("RestDb.sql");
smap = SQLMap.create(db);
AppSettings app = new AppSettings();
Dish dish = new Dish();
smap.setPrimaryKey(app, app.id);
smap.setPrimaryKey(dish, dish.id);
smap.setSqlType(dish.price, SQLMap.SqlType.SQL_DOUBLE);
smap.createTable(app);
smap.createTable(dish);
} catch(IOException err) {
Log.e(err);
}
}
}
AppStorage
public class AppStorage {
private static SQLMap smap;
private static Database db;
private static AppStorage instance = new AppStorage();
private EventDispatcher dispatcher = new EventDispatcher();
private AppStorage() {}
public static AppStorage getInstance() {
initStorage();
return instance;
}
private static void initStorage() {
if(smap == null) {
try {
db = Display.getInstance().openOrCreate("RestDb.sql");
smap = SQLMap.create(db);
AppSettings app = new AppSettings();
Dish dish = new Dish();
smap.setPrimaryKey(app, app.id);
smap.setPrimaryKey(dish, dish.id);
smap.setSqlType(dish.price, SQLMap.SqlType.SQL_DOUBLE);
smap.createTable(app);
smap.createTable(dish);
} catch(IOException err) {
Log.e(err);
}
}
}
AppStorage
public void insert(PropertyBusinessObject d) {
try {
smap.insert(d);
} catch(IOException err) {
Log.e(err);
ToastBar.showErrorMessage("Error saving: " + err);
}
}
public void update(PropertyBusinessObject d) {
try {
smap.update(d);
} catch(IOException err) {
Log.e(err);
ToastBar.showErrorMessage("Error updating storage: " + err);
}
}
public void addDeleteListener(ActionListener<ActionEvent> onDelete) {
dispatcher.addListener(onDelete);
}
public void removeDeleteListener(ActionListener<ActionEvent> onDelete) {
dispatcher.removeListener(onDelete);
}
AppStorage
public void insert(PropertyBusinessObject d) {
try {
smap.insert(d);
} catch(IOException err) {
Log.e(err);
ToastBar.showErrorMessage("Error saving: " + err);
}
}
public void update(PropertyBusinessObject d) {
try {
smap.update(d);
} catch(IOException err) {
Log.e(err);
ToastBar.showErrorMessage("Error updating storage: " + err);
}
}
public void addDeleteListener(ActionListener<ActionEvent> onDelete) {
dispatcher.addListener(onDelete);
}
public void removeDeleteListener(ActionListener<ActionEvent> onDelete) {
dispatcher.removeListener(onDelete);
}
AppStorage
public void delete(PropertyBusinessObject d) {
try {
smap.delete(d);
dispatcher.fireActionEvent(new ActionEvent(d));
} catch(IOException err) {
Log.e(err);
ToastBar.showErrorMessage("Error deleting: " + err);
}
}
public AppSettings fetchAppSettings() {
try {
AppSettings a = new AppSettings();
a.name.set(null);
a.tagline.set(null);
List<PropertyBusinessObject> lp = smap.select(a, null, true, 1000, 0);
if(lp.size() == 0) {
a = new AppSettings();
a.id.set("1");
insert(a);
return a;
}
return (AppSettings)lp.get(0);
} catch(Exception err) {
Log.e(err);
ToastBar.showErrorMessage("Error loading AppSettings: " + err);
return null;
}
AppStorage
public void delete(PropertyBusinessObject d) {
try {
smap.delete(d);
dispatcher.fireActionEvent(new ActionEvent(d));
} catch(IOException err) {
Log.e(err);
ToastBar.showErrorMessage("Error deleting: " + err);
}
}
public AppSettings fetchAppSettings() {
try {
AppSettings a = new AppSettings();
a.name.set(null);
a.tagline.set(null);
List<PropertyBusinessObject> lp = smap.select(a, null, true, 1000, 0);
if(lp.size() == 0) {
a = new AppSettings();
a.id.set("1");
insert(a);
return a;
}
return (AppSettings)lp.get(0);
} catch(Exception err) {
Log.e(err);
ToastBar.showErrorMessage("Error loading AppSettings: " + err);
return null;
}
AppStorage
public void delete(PropertyBusinessObject d) {
try {
smap.delete(d);
dispatcher.fireActionEvent(new ActionEvent(d));
} catch(IOException err) {
Log.e(err);
ToastBar.showErrorMessage("Error deleting: " + err);
}
}
public AppSettings fetchAppSettings() {
try {
AppSettings a = new AppSettings();
a.name.set(null);
a.tagline.set(null);
List<PropertyBusinessObject> lp = smap.select(a, null, true, 1000, 0);
if(lp.size() == 0) {
a = new AppSettings();
a.id.set("1");
insert(a);
return a;
}
return (AppSettings)lp.get(0);
} catch(Exception err) {
Log.e(err);
ToastBar.showErrorMessage("Error loading AppSettings: " + err);
return null;
}
AppStorage
public List<PropertyBusinessObject> fetchDishes() {
try {
Dish d = new Dish();
List<PropertyBusinessObject> lp = smap.select(d, d.name, true, 1000, 0);
// workaround for null images
for(PropertyBusinessObject p : lp) {
Dish dd = (Dish)p;
if(dd.getFullSize() == null) {
dd.setFullSize(Resources.getGlobalResources().getImage("food1.jpg"));
}
}
return lp;
} catch(Exception err) {
Log.e(err);
ToastBar.showErrorMessage("Error loading dishes: " + err);
return null;
}
}
AppStorage
public List<PropertyBusinessObject> fetchDishes() {
try {
Dish d = new Dish();
List<PropertyBusinessObject> lp = smap.select(d, d.name, true, 1000, 0);
// workaround for null images
for(PropertyBusinessObject p : lp) {
Dish dd = (Dish)p;
if(dd.getFullSize() == null) {
dd.setFullSize(Resources.getGlobalResources().getImage("food1.jpg"));
}
}
return lp;
} catch(Exception err) {
Log.e(err);
ToastBar.showErrorMessage("Error loading dishes: " + err);
return null;
}
}
AppStorage
Local & Remote DB
✦Keeping both a local and remote DB is risky but since
we currently have the assumption of one user per
restaurant it’s lower
✦We don’t really support working offline, this is mostly
meant for faster loading and reduced server load
© Codename One 2017 all rights reserved

More Related Content

Similar to SQLite and ORM Binding - Part 1.pdf

Capture image on eye blink
Capture image on eye blinkCapture image on eye blink
Capture image on eye blinkInnovationM
 
#18.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
#18.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...#18.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
#18.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...탑크리에듀(구로디지털단지역3번출구 2분거리)
 
Beautiful java script
Beautiful java scriptBeautiful java script
Beautiful java scriptÜrgo Ringo
 
Neues aus dem Tindergarten: Auswertung "privater" APIs mit Apache Ignite
Neues aus dem Tindergarten: Auswertung "privater" APIs mit Apache IgniteNeues aus dem Tindergarten: Auswertung "privater" APIs mit Apache Ignite
Neues aus dem Tindergarten: Auswertung "privater" APIs mit Apache IgniteQAware GmbH
 
Androidaop 170105090257
Androidaop 170105090257Androidaop 170105090257
Androidaop 170105090257newegg
 
Creating custom views
Creating custom viewsCreating custom views
Creating custom viewsMu Chun Wang
 
Silex meets SOAP & REST
Silex meets SOAP & RESTSilex meets SOAP & REST
Silex meets SOAP & RESTHugo Hamon
 
(PHPers Wrocław #5) How to write valuable unit test?
(PHPers Wrocław #5) How to write valuable unit test?(PHPers Wrocław #5) How to write valuable unit test?
(PHPers Wrocław #5) How to write valuable unit test?RST Software Masters
 
Crossing platforms with JavaScript & React
Crossing platforms with JavaScript & React Crossing platforms with JavaScript & React
Crossing platforms with JavaScript & React Robert DeLuca
 
Sustaining Test-Driven Development
Sustaining Test-Driven DevelopmentSustaining Test-Driven Development
Sustaining Test-Driven DevelopmentAgileOnTheBeach
 
Node Anti-Patterns and Bad Practices
Node Anti-Patterns and Bad PracticesNode Anti-Patterns and Bad Practices
Node Anti-Patterns and Bad PracticesPedro Teixeira
 
Engineering JavaScript
Engineering JavaScriptEngineering JavaScript
Engineering JavaScriptJim Purbrick
 
Promises are so passé - Tim Perry - Codemotion Milan 2016
Promises are so passé - Tim Perry - Codemotion Milan 2016Promises are so passé - Tim Perry - Codemotion Milan 2016
Promises are so passé - Tim Perry - Codemotion Milan 2016Codemotion
 
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy ApplicationsAdding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy ApplicationsSam Hennessy
 

Similar to SQLite and ORM Binding - Part 1.pdf (20)

Capture image on eye blink
Capture image on eye blinkCapture image on eye blink
Capture image on eye blink
 
Android workshop
Android workshopAndroid workshop
Android workshop
 
Using the Windows 8 Runtime from C++
Using the Windows 8 Runtime from C++Using the Windows 8 Runtime from C++
Using the Windows 8 Runtime from C++
 
An intro to cqrs
An intro to cqrsAn intro to cqrs
An intro to cqrs
 
20150319 testotipsio
20150319 testotipsio20150319 testotipsio
20150319 testotipsio
 
#18.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
#18.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...#18.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
#18.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
 
Beautiful java script
Beautiful java scriptBeautiful java script
Beautiful java script
 
Neues aus dem Tindergarten: Auswertung "privater" APIs mit Apache Ignite
Neues aus dem Tindergarten: Auswertung "privater" APIs mit Apache IgniteNeues aus dem Tindergarten: Auswertung "privater" APIs mit Apache Ignite
Neues aus dem Tindergarten: Auswertung "privater" APIs mit Apache Ignite
 
Androidaop 170105090257
Androidaop 170105090257Androidaop 170105090257
Androidaop 170105090257
 
Creating custom views
Creating custom viewsCreating custom views
Creating custom views
 
Silex meets SOAP & REST
Silex meets SOAP & RESTSilex meets SOAP & REST
Silex meets SOAP & REST
 
(PHPers Wrocław #5) How to write valuable unit test?
(PHPers Wrocław #5) How to write valuable unit test?(PHPers Wrocław #5) How to write valuable unit test?
(PHPers Wrocław #5) How to write valuable unit test?
 
Crossing platforms with JavaScript & React
Crossing platforms with JavaScript & React Crossing platforms with JavaScript & React
Crossing platforms with JavaScript & React
 
Introduccion a Jasmin
Introduccion a JasminIntroduccion a Jasmin
Introduccion a Jasmin
 
Sustaining Test-Driven Development
Sustaining Test-Driven DevelopmentSustaining Test-Driven Development
Sustaining Test-Driven Development
 
Advanced redux
Advanced reduxAdvanced redux
Advanced redux
 
Node Anti-Patterns and Bad Practices
Node Anti-Patterns and Bad PracticesNode Anti-Patterns and Bad Practices
Node Anti-Patterns and Bad Practices
 
Engineering JavaScript
Engineering JavaScriptEngineering JavaScript
Engineering JavaScript
 
Promises are so passé - Tim Perry - Codemotion Milan 2016
Promises are so passé - Tim Perry - Codemotion Milan 2016Promises are so passé - Tim Perry - Codemotion Milan 2016
Promises are so passé - Tim Perry - Codemotion Milan 2016
 
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy ApplicationsAdding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
 

More from ShaiAlmog1

The Duck Teaches Learn to debug from the masters. Local to production- kill ...
The Duck Teaches  Learn to debug from the masters. Local to production- kill ...The Duck Teaches  Learn to debug from the masters. Local to production- kill ...
The Duck Teaches Learn to debug from the masters. Local to production- kill ...ShaiAlmog1
 
create-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdfcreate-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdfShaiAlmog1
 
create-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdfcreate-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdfShaiAlmog1
 
create-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdfcreate-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdfShaiAlmog1
 
create-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdfcreate-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdfShaiAlmog1
 
create-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdfcreate-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdfShaiAlmog1
 
create-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdfcreate-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdfShaiAlmog1
 
create-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdfcreate-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdfShaiAlmog1
 
create-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdfcreate-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdfShaiAlmog1
 
create-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdfcreate-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdfShaiAlmog1
 
create-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdfcreate-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdfShaiAlmog1
 
create-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdfcreate-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdfShaiAlmog1
 
create-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdfcreate-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdfCreating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdfCreating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdfCreating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdfCreating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdfCreating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdfCreating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdfCreating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdfShaiAlmog1
 

More from ShaiAlmog1 (20)

The Duck Teaches Learn to debug from the masters. Local to production- kill ...
The Duck Teaches  Learn to debug from the masters. Local to production- kill ...The Duck Teaches  Learn to debug from the masters. Local to production- kill ...
The Duck Teaches Learn to debug from the masters. Local to production- kill ...
 
create-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdfcreate-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdf
 
create-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdfcreate-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdf
 
create-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdfcreate-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdf
 
create-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdfcreate-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdf
 
create-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdfcreate-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdf
 
create-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdfcreate-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdf
 
create-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdfcreate-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdf
 
create-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdfcreate-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdf
 
create-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdfcreate-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdf
 
create-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdfcreate-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdf
 
create-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdfcreate-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdf
 
create-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdfcreate-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdf
 
Creating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdfCreating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdf
 
Creating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdfCreating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdf
 
Creating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdfCreating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdf
 
Creating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdfCreating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdf
 
Creating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdfCreating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdf
 
Creating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdfCreating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdf
 
Creating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdfCreating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdf
 

Recently uploaded

WSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptxWSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptxJennifer Lim
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1DianaGray10
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIES VE
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...FIDO Alliance
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutesconfluent
 
PLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. StartupsPLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. StartupsStefano
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeCzechDreamin
 
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfFIDO Alliance
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxAbida Shariff
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024Stephanie Beckett
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyJohn Staveley
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoTAnalytics
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityScyllaDB
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Patrick Viafore
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfFIDO Alliance
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Julian Hyde
 
AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101vincent683379
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfFIDO Alliance
 
Buy Epson EcoTank L3210 Colour Printer Online.pptx
Buy Epson EcoTank L3210 Colour Printer Online.pptxBuy Epson EcoTank L3210 Colour Printer Online.pptx
Buy Epson EcoTank L3210 Colour Printer Online.pptxEasyPrinterHelp
 
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FIDO Alliance
 

Recently uploaded (20)

WSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptxWSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
PLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. StartupsPLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. Startups
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through Observability
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
 
AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
Buy Epson EcoTank L3210 Colour Printer Online.pptx
Buy Epson EcoTank L3210 Colour Printer Online.pptxBuy Epson EcoTank L3210 Colour Printer Online.pptx
Buy Epson EcoTank L3210 Colour Printer Online.pptx
 
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
 

SQLite and ORM Binding - Part 1.pdf

  • 1. SQLite & ORM Binding - Part I
  • 2. SQLite Mapping ✦SQLite property objects can be mapped to SQLite tables ✦I also had to moved a couple of properties to getters/ setters as at the time I built the app the API didn’t allow excluding an entry ✦I abstracted the mapping further by creating my own persistence class © Codename One 2017 all rights reserved
  • 3. public class AppStorage { private static SQLMap smap; private static Database db; private static AppStorage instance = new AppStorage(); private EventDispatcher dispatcher = new EventDispatcher(); private AppStorage() {} public static AppStorage getInstance() { initStorage(); return instance; } private static void initStorage() { if(smap == null) { try { db = Display.getInstance().openOrCreate("RestDb.sql"); smap = SQLMap.create(db); AppSettings app = new AppSettings(); Dish dish = new Dish(); smap.setPrimaryKey(app, app.id); smap.setPrimaryKey(dish, dish.id); smap.setSqlType(dish.price, SQLMap.SqlType.SQL_DOUBLE); smap.createTable(app); smap.createTable(dish); } catch(IOException err) { Log.e(err); } } } AppStorage
  • 4. public class AppStorage { private static SQLMap smap; private static Database db; private static AppStorage instance = new AppStorage(); private EventDispatcher dispatcher = new EventDispatcher(); private AppStorage() {} public static AppStorage getInstance() { initStorage(); return instance; } private static void initStorage() { if(smap == null) { try { db = Display.getInstance().openOrCreate("RestDb.sql"); smap = SQLMap.create(db); AppSettings app = new AppSettings(); Dish dish = new Dish(); smap.setPrimaryKey(app, app.id); smap.setPrimaryKey(dish, dish.id); smap.setSqlType(dish.price, SQLMap.SqlType.SQL_DOUBLE); smap.createTable(app); smap.createTable(dish); } catch(IOException err) { Log.e(err); } } } AppStorage
  • 5. public class AppStorage { private static SQLMap smap; private static Database db; private static AppStorage instance = new AppStorage(); private EventDispatcher dispatcher = new EventDispatcher(); private AppStorage() {} public static AppStorage getInstance() { initStorage(); return instance; } private static void initStorage() { if(smap == null) { try { db = Display.getInstance().openOrCreate("RestDb.sql"); smap = SQLMap.create(db); AppSettings app = new AppSettings(); Dish dish = new Dish(); smap.setPrimaryKey(app, app.id); smap.setPrimaryKey(dish, dish.id); smap.setSqlType(dish.price, SQLMap.SqlType.SQL_DOUBLE); smap.createTable(app); smap.createTable(dish); } catch(IOException err) { Log.e(err); } } } AppStorage
  • 6. public class AppStorage { private static SQLMap smap; private static Database db; private static AppStorage instance = new AppStorage(); private EventDispatcher dispatcher = new EventDispatcher(); private AppStorage() {} public static AppStorage getInstance() { initStorage(); return instance; } private static void initStorage() { if(smap == null) { try { db = Display.getInstance().openOrCreate("RestDb.sql"); smap = SQLMap.create(db); AppSettings app = new AppSettings(); Dish dish = new Dish(); smap.setPrimaryKey(app, app.id); smap.setPrimaryKey(dish, dish.id); smap.setSqlType(dish.price, SQLMap.SqlType.SQL_DOUBLE); smap.createTable(app); smap.createTable(dish); } catch(IOException err) { Log.e(err); } } } AppStorage
  • 7. public void insert(PropertyBusinessObject d) { try { smap.insert(d); } catch(IOException err) { Log.e(err); ToastBar.showErrorMessage("Error saving: " + err); } } public void update(PropertyBusinessObject d) { try { smap.update(d); } catch(IOException err) { Log.e(err); ToastBar.showErrorMessage("Error updating storage: " + err); } } public void addDeleteListener(ActionListener<ActionEvent> onDelete) { dispatcher.addListener(onDelete); } public void removeDeleteListener(ActionListener<ActionEvent> onDelete) { dispatcher.removeListener(onDelete); } AppStorage
  • 8. public void insert(PropertyBusinessObject d) { try { smap.insert(d); } catch(IOException err) { Log.e(err); ToastBar.showErrorMessage("Error saving: " + err); } } public void update(PropertyBusinessObject d) { try { smap.update(d); } catch(IOException err) { Log.e(err); ToastBar.showErrorMessage("Error updating storage: " + err); } } public void addDeleteListener(ActionListener<ActionEvent> onDelete) { dispatcher.addListener(onDelete); } public void removeDeleteListener(ActionListener<ActionEvent> onDelete) { dispatcher.removeListener(onDelete); } AppStorage
  • 9. public void delete(PropertyBusinessObject d) { try { smap.delete(d); dispatcher.fireActionEvent(new ActionEvent(d)); } catch(IOException err) { Log.e(err); ToastBar.showErrorMessage("Error deleting: " + err); } } public AppSettings fetchAppSettings() { try { AppSettings a = new AppSettings(); a.name.set(null); a.tagline.set(null); List<PropertyBusinessObject> lp = smap.select(a, null, true, 1000, 0); if(lp.size() == 0) { a = new AppSettings(); a.id.set("1"); insert(a); return a; } return (AppSettings)lp.get(0); } catch(Exception err) { Log.e(err); ToastBar.showErrorMessage("Error loading AppSettings: " + err); return null; } AppStorage
  • 10. public void delete(PropertyBusinessObject d) { try { smap.delete(d); dispatcher.fireActionEvent(new ActionEvent(d)); } catch(IOException err) { Log.e(err); ToastBar.showErrorMessage("Error deleting: " + err); } } public AppSettings fetchAppSettings() { try { AppSettings a = new AppSettings(); a.name.set(null); a.tagline.set(null); List<PropertyBusinessObject> lp = smap.select(a, null, true, 1000, 0); if(lp.size() == 0) { a = new AppSettings(); a.id.set("1"); insert(a); return a; } return (AppSettings)lp.get(0); } catch(Exception err) { Log.e(err); ToastBar.showErrorMessage("Error loading AppSettings: " + err); return null; } AppStorage
  • 11. public void delete(PropertyBusinessObject d) { try { smap.delete(d); dispatcher.fireActionEvent(new ActionEvent(d)); } catch(IOException err) { Log.e(err); ToastBar.showErrorMessage("Error deleting: " + err); } } public AppSettings fetchAppSettings() { try { AppSettings a = new AppSettings(); a.name.set(null); a.tagline.set(null); List<PropertyBusinessObject> lp = smap.select(a, null, true, 1000, 0); if(lp.size() == 0) { a = new AppSettings(); a.id.set("1"); insert(a); return a; } return (AppSettings)lp.get(0); } catch(Exception err) { Log.e(err); ToastBar.showErrorMessage("Error loading AppSettings: " + err); return null; } AppStorage
  • 12. public List<PropertyBusinessObject> fetchDishes() { try { Dish d = new Dish(); List<PropertyBusinessObject> lp = smap.select(d, d.name, true, 1000, 0); // workaround for null images for(PropertyBusinessObject p : lp) { Dish dd = (Dish)p; if(dd.getFullSize() == null) { dd.setFullSize(Resources.getGlobalResources().getImage("food1.jpg")); } } return lp; } catch(Exception err) { Log.e(err); ToastBar.showErrorMessage("Error loading dishes: " + err); return null; } } AppStorage
  • 13. public List<PropertyBusinessObject> fetchDishes() { try { Dish d = new Dish(); List<PropertyBusinessObject> lp = smap.select(d, d.name, true, 1000, 0); // workaround for null images for(PropertyBusinessObject p : lp) { Dish dd = (Dish)p; if(dd.getFullSize() == null) { dd.setFullSize(Resources.getGlobalResources().getImage("food1.jpg")); } } return lp; } catch(Exception err) { Log.e(err); ToastBar.showErrorMessage("Error loading dishes: " + err); return null; } } AppStorage
  • 14. Local & Remote DB ✦Keeping both a local and remote DB is risky but since we currently have the assumption of one user per restaurant it’s lower ✦We don’t really support working offline, this is mostly meant for faster loading and reduced server load © Codename One 2017 all rights reserved