Stackoverflow is always a good place to get ideas to write posts. Today i answer to this question. The original question is about using Room Library. But i want to change the question: how can i persiste a byte array using Kripton ORM Library?
With kripton, you can easly manage byte[]
fields. Just to show you how to do:
I define a bean FileBean binded to a table file_bean
:
1 2 3 4 5 6 7 8 9 |
@BindTable public class FileBean { public long id; public Date date; public String title; public String text; public String address; public byte[] image; } |
A DAO definition:
1 2 3 4 5 6 7 8 9 |
@BindDao(FileBean.class) public interface FileBeanDao { @BindSqlInsert long insert(FileBean bean); @BindSqlSelect(where="id=${id}") List selectById(long id); } |
And a data source interface:
1 2 3 4 |
@BindDataSource(daoSet={FileBeanDao.class}, fileName = "example.db") public interface ExampleDataSource { } |
After project configuration to use Kripton and its annotation processor in gradle config file:
1 2 3 4 5 6 7 8 9 |
dependencies { ... // annotation processors annotationProcessor"com.abubusoft:kripton-processor:3.2.0" ... // dependencies implementation "com.abubusoft:kripton-android-library:3.2.0" ... } |
Kripton will generate for you a DAO implementation like this (Javadoc too):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
/** * <p> * DAO implementation for entity <code>FileBean</code>, based on interface <code>FileBeanDao</code> * </p> * * @see FileBean * @see FileBeanDao * @see FileBeanTable */ public class FileBeanDaoImpl extends AbstractDao implements FileBeanDao { public FileBeanDaoImpl(BindExampleDataSource dataSet) { super(dataSet); } /** * <p>SQL insert:</p> * <pre>INSERT INTO file_bean (date, title, text, address, image) VALUES (${bean.date}, ${bean.title}, ${bean.text}, ${bean.address}, ${bean.image})</pre> * * <p><code>bean.id</code> is automatically updated because it is the primary key</p> * * <p><strong>Inserted columns:</strong></p> * <dl> * <dt>date</dt><dd>is mapped to <strong>${bean.date}</strong></dd> * <dt>title</dt><dd>is mapped to <strong>${bean.title}</strong></dd> * <dt>text</dt><dd>is mapped to <strong>${bean.text}</strong></dd> * <dt>address</dt><dd>is mapped to <strong>${bean.address}</strong></dd> * <dt>image</dt><dd>is mapped to <strong>${bean.image}</strong></dd> * </dl> * * @param bean * is mapped to parameter <strong>bean</strong> * * @return <strong>id</strong> of inserted record */ @Override public long insert(FileBean bean) { ContentValues contentValues=contentValues(); contentValues.clear(); if (bean.date!=null) { contentValues.put("date", DateUtils.write(bean.date)); } else { contentValues.putNull("date"); } if (bean.title!=null) { contentValues.put("title", bean.title); } else { contentValues.putNull("title"); } if (bean.text!=null) { contentValues.put("text", bean.text); } else { contentValues.putNull("text"); } if (bean.address!=null) { contentValues.put("address", bean.address); } else { contentValues.putNull("address"); } if (bean.image!=null) { contentValues.put("image", bean.image); } else { contentValues.putNull("image"); } // log for insert -- BEGIN StringBuffer _columnNameBuffer=new StringBuffer(); StringBuffer _columnValueBuffer=new StringBuffer(); String _columnSeparator=""; for (String columnName:contentValues.keySet()) { _columnNameBuffer.append(_columnSeparator+columnName); _columnValueBuffer.append(_columnSeparator+":"+columnName); _columnSeparator=", "; } Logger.info("INSERT INTO file_bean (%s) VALUES (%s)", _columnNameBuffer.toString(), _columnValueBuffer.toString()); // log for content values -- BEGIN Object _contentValue; for (String _contentKey:contentValues.keySet()) { _contentValue=contentValues.get(_contentKey); if (_contentValue==null) { Logger.info("==> :%s = <null>", _contentKey); } else { Logger.info("==> :%s = '%s' (%s)", _contentKey, StringUtils.checkSize(_contentValue), _contentValue.getClass().getCanonicalName()); } } // log for content values -- END // log for insert -- END long result = database().insert("file_bean", null, contentValues); bean.id=result; return result; } /** * <h2>Select SQL:</h2> * * <pre>SELECT id, date, title, text, address, image FROM file_bean WHERE id=${id}</pre> * * <h2>Projected columns:</h2> * <dl> * <dt>id</dt><dd>is associated to bean's property <strong>id</strong></dd> * <dt>date</dt><dd>is associated to bean's property <strong>date</strong></dd> * <dt>title</dt><dd>is associated to bean's property <strong>title</strong></dd> * <dt>text</dt><dd>is associated to bean's property <strong>text</strong></dd> * <dt>address</dt><dd>is associated to bean's property <strong>address</strong></dd> * <dt>image</dt><dd>is associated to bean's property <strong>image</strong></dd> * </dl> * * <h2>Query's parameters:</h2> * <dl> * <dt>${id}</dt><dd>is binded to method's parameter <strong>id</strong></dd> * </dl> * * @param id * is binded to <code>${id}</code> * @return collection of bean or empty collection. */ @Override public List<FileBean> selectById(long id) { StringBuilder _sqlBuilder=getSQLStringBuilder(); _sqlBuilder.append("SELECT id, date, title, text, address, image FROM file_bean"); // generation CODE_001 -- BEGIN // generation CODE_001 -- END ArrayList<String> _sqlWhereParams=getWhereParamsArray(); // manage WHERE arguments -- BEGIN // manage WHERE statement String _sqlWhereStatement=" WHERE id=?"; _sqlBuilder.append(_sqlWhereStatement); // manage WHERE arguments -- END // build where condition _sqlWhereParams.add(String.valueOf(id)); String _sql=_sqlBuilder.toString(); String[] _sqlArgs=_sqlWhereParams.toArray(new String[_sqlWhereParams.size()]); Logger.info(_sql); // log for where parameters -- BEGIN int _whereParamCounter=0; for (String _whereParamItem: _sqlWhereParams) { Logger.info("==> param%s: '%s'",(_whereParamCounter++), StringUtils.checkSize(_whereParamItem)); } // log for where parameters -- END try (Cursor cursor = database().rawQuery(_sql, _sqlArgs)) { Logger.info("Rows found: %s",cursor.getCount()); LinkedList<FileBean> resultList=new LinkedList<FileBean>(); FileBean resultBean=null; if (cursor.moveToFirst()) { int index0=cursor.getColumnIndex("id"); int index1=cursor.getColumnIndex("date"); int index2=cursor.getColumnIndex("title"); int index3=cursor.getColumnIndex("text"); int index4=cursor.getColumnIndex("address"); int index5=cursor.getColumnIndex("image"); do { resultBean=new FileBean(); resultBean.id=cursor.getLong(index0); if (!cursor.isNull(index1)) { resultBean.date=DateUtils.read(cursor.getString(index1)); } if (!cursor.isNull(index2)) { resultBean.title=cursor.getString(index2); } if (!cursor.isNull(index3)) { resultBean.text=cursor.getString(index3); } if (!cursor.isNull(index4)) { resultBean.address=cursor.getString(index4); } if (!cursor.isNull(index5)) { resultBean.image=cursor.getBlob(index5); } resultList.add(resultBean); } while (cursor.moveToNext()); } return resultList; } } } |
Kritpon, how you can see from generated code, natively persists byte[]
fields into BLOB
type columns. No special operation or configuration is required.
Quite easy no? For any question does not esitate to comment this post.
For more information about Kripton Persistence Library:
- https://github.com/xcesco/kripton
- https://github.com/xcesco/kripton/wiki