Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.properties;
import com.codename1.db.Cursor;
import com.codename1.db.Database;
import com.codename1.db.Row;
import com.codename1.io.Log;
import com.codename1.io.Util;
import com.codename1.ui.EncodedImage;
import com.codename1.util.Base64;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* A simple ORM wrapper for property objects. This is a very poor mans ORM that doesn't handle relations
* properly at this time.
*
* @author Shai Almog
*/
public class SQLMap {
private boolean verbose = true;
public static enum SqlType {
SQL_EXCLUDE(null),
SQL_TEXT("TEXT"),
SQL_INTEGER("INTEGER") {
@Override
protected Object getValue(Row row, int index, PropertyBase base) throws IOException {
return row.getInteger(index);
}
},
SQL_BOOLEAN("BOOLEAN") {
@Override
protected Object getValue(Row row, int index, PropertyBase base) throws IOException {
Integer i = row.getInteger(index);
if(i == null) {
return null;
}
return i.intValue() == 1;
}
@Override
protected Object asUpdateInsertValue(Object data, Property p) {
if(data == null) {
return null;
}
return Util.toBooleanValue(data) ? 1 : 0;
}
},
SQL_LONG("INTEGER") {
@Override
protected Object getValue(Row row, int index, PropertyBase base) throws IOException {
return row.getLong(index);
}
},
SQL_DATE("INTEGER") {
@Override
protected Object getValue(Row row, int index, PropertyBase base) throws IOException {
return new Date(row.getLong(index) * 1000);
}
@Override
protected Object asUpdateInsertValue(Object data, Property p) {
if(data == null) {
return null;
}
return ((Date)data).getTime() / 1000;
}
},
SQL_SHORT("INTEGER") {
@Override
protected Object getValue(Row row, int index, PropertyBase base) throws IOException {
return row.getShort(index);
}
},
SQL_FLOAT("REAL") {
@Override
protected Object getValue(Row row, int index, PropertyBase base) throws IOException {
return row.getFloat(index);
}
},
SQL_BLOB("TEXT") {
@Override
protected Object getValue(Row row, int index, PropertyBase base) throws IOException {
String s = row.getString(index);
if(s == null) {
return null;
}
byte[] d = Base64.decode(s.getBytes());
Class t = base.getGenericType();
if(t == EncodedImage.class) {
return EncodedImage.create(d);
}
return d;
}
@Override
protected Object asUpdateInsertValue(Object data, Property p) {
if(data == null) {
return null;
}
Class t = p.getGenericType();
if(t == EncodedImage.class) {
return Base64.encode(((EncodedImage)data).getImageData());
}
return Base64.encode((byte[]) data);
}
},
SQL_DOUBLE("REAL") {
@Override
protected Object getValue(Row row, int index, PropertyBase base) throws IOException {
return row.getDouble(index);
}
};
String dbType;
SqlType(String dbType) {
this.dbType = dbType;
}
protected Object getValue(Row row, int index, PropertyBase base) throws IOException{
return row.getString(index);
}
protected Object asUpdateInsertValue(Object data, Property p) {
return data;
}
}
private Database db;
private SQLMap() {}
/**
* Creates an SQL Map instance to the given database instance
* @param db the database connection instance
* @return an instance of the SQL mapping
*/
public static SQLMap create(Database db) {
SQLMap s = new SQLMap();
s.db = db;
return s;
}
/**
* Sets the primary key for the component
* @param cmp the business object
* @param pk the primary key field
*/
public void setPrimaryKey(PropertyBusinessObject cmp, Property pk) {
cmp.getPropertyIndex().putMetaDataOfClass("cn1$pk", pk.getName());
}
/**
* Sets the primary key for the component and makes it auto-increment
* @param cmp the business object
* @param pk the primary key field
*/
public void setPrimaryKeyAutoIncrement(PropertyBusinessObject cmp, Property pk) {
cmp.getPropertyIndex().putMetaDataOfClass("cn1$pk", pk.getName());
cmp.getPropertyIndex().putMetaDataOfClass("cn1$autoinc", Boolean.TRUE);
}
/**
* Sets the sql type for the column
* @param p the property
* @param type one of the enum values representing supported SQL data types
*/
public void setSqlType(PropertyBase p, SqlType type) {
p.putClientProperty("cn1$colType", type);
}
/**
* Returns the SQL type for the given column
*
* @param p the property
* @return the sql data type
*/
public SqlType getSqlType(PropertyBase p) {
SqlType s = (SqlType)p.getClientProperty("cn1$colType");
if(s == null) {
if(p instanceof Property) {
Class gt = p.getGenericType();
if(gt != null) {
if(gt == Integer.class) {
return SqlType.SQL_INTEGER;
}
if(gt == Boolean.class) {
return SqlType.SQL_BOOLEAN;
}
if(gt == Long.class) {
return SqlType.SQL_LONG;
}
if(gt == Short.class) {
return SqlType.SQL_SHORT;
}
if(gt == Float.class) {
return SqlType.SQL_FLOAT;
}
if(gt == Double.class) {
return SqlType.SQL_DOUBLE;
}
if(gt == Date.class) {
return SqlType.SQL_DATE;
}
if(gt == EncodedImage.class || gt == byte[].class) {
return SqlType.SQL_BLOB;
}
return SqlType.SQL_TEXT;
}
Object val = ((Property)p).get();
if(val != null) {
if(val instanceof Long) {
return SqlType.SQL_LONG;
}
if(val instanceof Integer) {
return SqlType.SQL_INTEGER;
}
if(val instanceof Short) {
return SqlType.SQL_SHORT;
}
if(val instanceof Float) {
return SqlType.SQL_FLOAT;
}
if(val instanceof Double) {
return SqlType.SQL_DOUBLE;
}
if(gt == Date.class) {
return SqlType.SQL_DATE;
}
}
}
return SqlType.SQL_TEXT;
}
return s;
}
/**
* By default the table name matches the property index name unless explicitly modified with this method
* @param cmp the properties business object
* @param name the name of the table
*/
public void setTableName(PropertyBusinessObject cmp, String name) {
cmp.getPropertyIndex().putMetaDataOfClass("cn1$tableName", name);
}
/**
* By default the table name matches the property index name unless explicitly modified with this method
* @param cmp the properties business object
* @return the name of the table
*/
public String getTableName(PropertyBusinessObject cmp) {
String s = (String)cmp.getPropertyIndex().getMetaDataOfClass("cn1$tableName");
if(s != null) {
return s;
}
return cmp.getPropertyIndex().getName();
}
/**
* By default the column name matches the property name unless explicitly modified with this method
* @param prop a property instance, this will apply to all the property instances for the type
* @param name the name of the column
*/
public void setColumnName(PropertyBase prop, String name) {
prop.putClientProperty("cn1$sqlColumn", name);
}
/**
* By default the column name matches the property name unless explicitly modified with this method
* @param prop a property instance, this will apply to all the property instances for the type
* @return the name of the property
*/
public String getColumnName(PropertyBase prop) {
return getColumnNameImpl(prop);
}
private static String getColumnNameImpl(PropertyBase prop) {
String val = (String)prop.getClientProperty("cn1$sqlColumn");
if(val == null) {
return prop.getName();
}
return val;
}
/**
* Creates a table matching the given property component if one doesn't exist yet
* @param cmp the business object
* @return true if the table was created false if it already existed
*/
public boolean createTable(PropertyBusinessObject cmp) throws IOException {
String tableName = getTableName(cmp);
Cursor cr = null;
boolean has = false;
try {
cr = executeQuery("SELECT * FROM sqlite_master WHERE type='table' AND name='" + tableName +"'");
has = cr.next();
} finally {
if(cr != null) {
cr.close();
}
}
if(has) {
return false;
}
StringBuilder createStatement = new StringBuilder("CREATE TABLE ");
createStatement.append(tableName);
createStatement.append(" (");
String pkName = (String)cmp.getPropertyIndex().getMetaDataOfClass("cn1$pk");
boolean autoIncrement = cmp.getPropertyIndex().getMetaDataOfClass("cn1$autoinc") != null;
boolean first = true;
for(PropertyBase p : cmp.getPropertyIndex()) {
SqlType tp = getSqlType(p);
if(tp == SqlType.SQL_EXCLUDE) {
continue;
}
if(!first) {
createStatement.append(",");
}
first = false;
String columnName = getColumnName(p);
createStatement.append(columnName);
createStatement.append(" ");
createStatement.append(tp.dbType);
if(columnName.equalsIgnoreCase(pkName)) {
createStatement.append(" PRIMARY KEY");
if(autoIncrement) {
createStatement.append(" AUTOINCREMENT");
}
}
}
createStatement.append(")");
execute(createStatement.toString());
return true;
}
private void execute(String stmt) throws IOException {
if(verbose) {
Log.p(stmt);
}
db.execute(stmt);
}
private void execute(String stmt, Object[] args) throws IOException {
if(verbose) {
Log.p(stmt);
}
db.execute(stmt, args);
}
private Cursor executeQuery(String stmt, Object[] args) throws IOException {
if(verbose) {
Log.p(stmt);
}
return db.executeQuery(stmt, args);
}
private Cursor executeQuery(String stmt) throws IOException {
if(verbose) {
Log.p(stmt);
}
return db.executeQuery(stmt);
}
/**
* Drop a table matching the given property component
* @param cmp the business object
*/
public void dropTable(PropertyBusinessObject cmp) throws IOException {
String tableName = getTableName(cmp);
execute("Drop table " + tableName);
}
/**
* Adds a new business object into the database
* @param cmp the business component
*/
public void insert(PropertyBusinessObject cmp) throws IOException {
String tableName = getTableName(cmp);
StringBuilder createStatement = new StringBuilder("INSERT INTO ");
createStatement.append(tableName);
createStatement.append(" (");
int count = 0;
ArrayList