//////////////////////////FS operations /////////////////////////////////// /// @param [in] id -- get entry using ID directly </param> internal static SqlFsNode getFsNodeByID(SQLiteDatabase db, SqlFsLocker fsLocker, FsID id) { SqlFsNode fsNode = null; string @where = SqlStr.genWhere(new SqlStr.SqlSimpCond(SqlFs.FSBLOCK.fsID.ToString(), "=", id)); Cursor c = null; try { c = db.query(SqlFs.DBNAMES.FsBlock.ToString(), new string[] { SqlFs.FSBLOCK.fsID.ToString(), SqlFs.FSBLOCK.fsType.ToString() }, @where, null, null, null, null); if (c.moveToFirst()) { fsNode = SqlFsNode.getFsNode(db, fsLocker, c); } } catch (Exception e) { SqlFsLog.debug(e); SqlFsErrCode.CurrentError = FsErr.GetFieldError; } finally { SqlFsFunc.close(c); } return(fsNode); }
/// <summary> /// Write a single info /// Update existing entry or add a new one if not present /// </summary> private void __writeInfo(string infoName, string infoVal) { List <object> colsAndValues = new List <object>(); colsAndValues.Add(FSINFO.infoName.ToString()); colsAndValues.Add(infoName); colsAndValues.Add(FSINFO.infoVal.ToString()); colsAndValues.Add(infoVal); ContentValues contVals = SqlStr.genContentValues(colsAndValues); string @where = SqlStr.genWhere(new SqlStr.SqlSimpCond(FSINFO.infoName.ToString(), "=", infoName)); try { // try update first if (db.update(DBNAMES.FsInfo.ToString(), contVals, @where, null) == 0) { // nothing is updated, insert to table db.insert(DBNAMES.FsInfo.ToString(), null, contVals); } } catch (SQLiteException e) { SqlFsLog.debug(e); SqlFsErrCode.CurrentError = FsErr.WriteFsInfoErr; throw e; } }
/// <summary> /// Get a single info /// </summary> /// <returns> info value </returns> private string __getInfo(string infoName) { string value = null; string @where = SqlStr.genWhere(new SqlStr.SqlSimpCond(FSINFO.infoName.ToString(), "=", infoName)); Cursor c = null; try { c = db.query(DBNAMES.FsInfo.ToString(), new string[] { FSINFO.infoVal.ToString() }, @where, null, null, null, null); if (c != null && c.moveToFirst()) { value = c.getString(0); } } catch (SQLiteException e) { SqlFsLog.debug(e); SqlFsErrCode.CurrentError = FsErr.GetFsInfoErr; } finally { SqlFsFunc.close(c); } return(value); }
/// <summary> /// Get data from data block table /// </summary> /// @param [in] dataBlockID -- the data block ID /// </param> /// <returns> true if OK </returns> /// <returns> false if failed </returns> private bool __getData(SQLiteDatabase db, FsID dataBlockID) { string @where = SqlStr.genWhere(new SqlStr.SqlSimpCond(IFileData.IDCOL, "=", dataBlockID)); Cursor c = null; try { c = db.query(IFileData.DTABLENAME.ToString(), null, @where, null, null, null, null); if (c.moveToFirst()) { __getData(c); } } catch (Exception e) { Log.d("IFileData.__getData", e.Message); return(false); } finally { if (c != null) { c.close(); } } return(true); }
/// <summary> /// Check existence of tables /// </summary> /// <returns> true -- all tables present </returns> /// <returns> false -- should create new tables </returns> private bool checkTables() { int tabCount = 0; string @where = SqlStr.genWhere(new SqlStr.SqlSimpCond(DBNAMES.type.ToString(), "=", DBNAMES.table.ToString()) ); Cursor c = db.query(DBNAMES.SQLITE_MASTER.ToString(), new string[] { DBNAMES.name.ToString() }, @where, null, null, null, null); // retrieve all table names and check against our list if (c != null && c.moveToFirst()) { do { string tabName = c.getString(0); foreach (string n in TABLIST) { if (n.Equals(tabName, StringComparison.CurrentCultureIgnoreCase)) { tabCount += 1; break; } } } while (c.moveToNext()); } SqlFsFunc.close(c); bool isCheckingOK = false; do { // not all tables exists, may be some corruption ... if (tabCount < TABLIST.Length) { break; } // may be more checking here ... isCheckingOK = true; } while (false); if (tabCount != 0 && !isCheckingOK) { close(); backup(); open(); } return(isCheckingOK); }
/// @param [in] name -- if null, get all entry </param> /// @param [in] type -- dir, file or any ? </param> private Cursor getEntryByName(string name, SqlFsConst.FSTYPE type) { // conditions List <object> conds = new List <object>(10); conds.Add(new SqlStr.SqlSimpCond(SqlFs.FSBLOCK.fsParent.ToString(), "=", this.ID)); if (name != null) { conds.Add("and"); conds.Add(new SqlStr.SqlSimpCond(SqlFs.FSBLOCK.fsName.ToString(), "=", name)); } if (type != SqlFsConst.FSTYPE.ANY) { conds.Add("and"); conds.Add(new SqlStr.SqlSimpCond(SqlFs.FSBLOCK.fsType.ToString(), "=", type.v())); } string @where = SqlStr.genWhere(conds); // query from DB Cursor c = null; try { c = db.query(SqlFs.DBNAMES.FsBlock.ToString(), new string[] { SqlFs.FSBLOCK.fsID.ToString(), SqlFs.FSBLOCK.fsType.ToString() }, @where, null, null, null, null); } catch (Exception e) { SqlFsLog.debug(e); SqlFsErrCode.CurrentError = SqlFsErrCode.FsErr.NoEntryByName; } // close and set to null if no rows at all if (c != null && c.Count == 0) { c.close(); c = null; } return(c); }
/// <summary> /// Save field to DB using ID /// </summary> private bool __setField(SqlFs.FSBLOCK field, object val) { List <object> colsAndValues = new List <object>(4); switch (field) { case com.sss.sqlfs.SqlFs.FSBLOCK.fsFileSize: case com.sss.sqlfs.SqlFs.FSBLOCK.fsType: case com.sss.sqlfs.SqlFs.FSBLOCK.fsName: case com.sss.sqlfs.SqlFs.FSBLOCK.fsParent: colsAndValues.Add(field.ToString()); colsAndValues.Add(val); break; case com.sss.sqlfs.SqlFs.FSBLOCK.fsChild: sbyte[] blob = idList2Blob((List <FsID>)val); colsAndValues.Add(field.ToString()); colsAndValues.Add(blob); break; } // update last mod time as well colsAndValues.Add(SqlFs.FSBLOCK.fsLastModTime.ToString()); colsAndValues.Add(SqlFsFunc.calToFileTime(new DateTime())); ContentValues contValues = SqlStr.genContentValues(colsAndValues); string @where = SqlStr.genWhere(new SqlStr.SqlSimpCond(SqlFs.FSBLOCK.fsID.ToString(), "=", this.ID)); int rowAffected = 0; try { rowAffected = db.update(SqlFs.DBNAMES.FsBlock.ToString(), contValues, @where, null); } catch (Exception e) { SqlFsLog.debug(e); SqlFsErrCode.CurrentError = FsErr.SetFieldError; } return(rowAffected > 0); }
/// <summary> /// Delete entry in a table using ID /// </summary> /// @param [in] tableName -- table name </param> /// @param [in] idColName -- ID column name </param> /// @param [in] id -- the actual ID </param> internal static bool deleteEntryByID(SQLiteDatabase db, string tableName, string idColName, FsID id) { string @where = SqlStr.genWhere(new SqlStr.SqlSimpCond(idColName, "=", id)); bool isOK = false; try { if (db.delete(tableName, @where, null) > 0) { isOK = true; } } catch (Exception e) { SqlFsLog.debug(e); SqlFsErrCode.CurrentError = FsErr.DeleteFsEntryError; } return(isOK); }
/// /// <summary> /// Save data to data block table /// </summary> /// @param [in] dataBlockID -- the data block ID if exist. Pass 0 if not exist /// </param> /// <returns> dataBlockID if OK </returns> /// <returns> 0 if failed </returns> private FsID __saveData(SQLiteDatabase db, FsID dataBlockID) { ContentValues contValues = __saveData(); try { if (dataBlockID.compare(SqlFsConst.INVALIDID) <= 0) { // save new data if (db.insert(IFileData.DTABLENAME.ToString(), null, contValues) < 0) { dataBlockID = SqlFsConst.INVALIDID; } else { dataBlockID = getLastInsertID(db); } } else { // update data string @where = SqlStr.genWhere(new SqlStr.SqlSimpCond(IFileData.IDCOL, "=", dataBlockID)); if (db.update(IFileData.DTABLENAME.ToString(), contValues, @where, null) == 0) { dataBlockID = SqlFsConst.INVALIDID; } } } catch (Exception e) { Log.d("IFileData.__saveData", e.Message); dataBlockID = SqlFsConst.INVALIDID; } return(dataBlockID); }
/// <summary> /// Get a field from DB using ID /// </summary> private object __getField(SqlFs.FSBLOCK field) { string @where = SqlStr.genWhere(new SqlStr.SqlSimpCond(SqlFs.FSBLOCK.fsID.ToString(), "=", this.ID)); // little adjustment SqlFs.FSBLOCK tField = (field == SqlFs.FSBLOCK.fsChildCount) ? SqlFs.FSBLOCK.fsChild : field; Cursor c = null; object val = null; try { c = db.query(SqlFs.DBNAMES.FsBlock.ToString(), new string[] { tField.ToString() }, @where, null, null, null, null); if (c.moveToFirst() && !c.isNull(0)) { switch (field) { case com.sss.sqlfs.SqlFs.FSBLOCK.fsCreateTime: case com.sss.sqlfs.SqlFs.FSBLOCK.fsLastModTime: val = c.getLong(0); break; case com.sss.sqlfs.SqlFs.FSBLOCK.fsFileSize: case com.sss.sqlfs.SqlFs.FSBLOCK.fsType: val = c.getInt(0); break; case com.sss.sqlfs.SqlFs.FSBLOCK.fsName: val = c.getString(0); break; case com.sss.sqlfs.SqlFs.FSBLOCK.fsParent: val = SqlFsFunc.getID(c, 0); break; case com.sss.sqlfs.SqlFs.FSBLOCK.fsChild: { sbyte[] buf = c.getBlob(0); val = blob2idList(buf); } break; case com.sss.sqlfs.SqlFs.FSBLOCK.fsChildCount: { sbyte[] buf = c.getBlob(0); val = buf.Length / FsID.IDSize; } break; } } else { val = __getDefaultValue(field); } } catch (Exception e) { SqlFsLog.debug(e); SqlFsErrCode.CurrentError = FsErr.GetFieldError; val = __getDefaultValue(field); // return a default value here } finally { SqlFsFunc.close(c); } return(val); }