Пример #1
0
        /* return all backup of user "clientID"
         * if there are no backups, returns an empty list */
        public static List <BackupRecord> getBackupList(string clientID, string timestamp)
        {
            List <BackupRecord> backupList = new List <BackupRecord>();

            using (var db = new LiteDatabase(DB_PATH))
            {
                // Get a collection (or create, if doesn't exist)
                LiteCollection <BackupRecord> col = db.GetCollection <BackupRecord>(DB_BACKUPS);

                // Index document using document userID property
                col.EnsureIndex(x => x.userID);

                // find all backup records with userID = clientID
                IEnumerable <BackupRecord> results;

                //retreive all backups if timestamp is null
                if (timestamp == null)
                {
                    results = col.Find(x => x.userID.Equals(clientID));
                }
                else
                {
                    results = col.Find(x => x.userID.Equals(clientID) && x.timestamp.Equals(timestamp));
                }

                Console.WriteLine("totale record trovati " + results.LongCount <BackupRecord>());

                //if (results.LongCount<BackupRecord>() <= 0)
                //throw new Exception("No backup found");

                //add backupRecord to list
                for (int i = 0; i < results.LongCount <BackupRecord>(); i++)
                {
                    //string recordString = JsonConvert.SerializeObject(results.ElementAt<BackupRecord>(i));
                    //BackupRecord record = JsonConvert.DeserializeObject<BackupRecord>(recordString);
                    BackupRecord record = results.ElementAt <BackupRecord>(i);
                    backupList.Add(record);

                    //fix for relativePath= null (caused by SQL logic)
                    foreach (myFileInfo info in record.fileInfoList)
                    {
                        if (info.RelativePath == null)
                        {
                            info.RelativePath = "";
                        }
                    }

                    Console.WriteLine("record " + i + " : " + record.userID + " " + record.timestamp);
                }
            }

            return(backupList);
        }
Пример #2
0
        /**
         * Method that a new backup record in the database
         */
        public static bool insertBackupRecord(string client_id, string timestamp, string fileInfoJSONString)
        {
            using (var db = new LiteDatabase(DB_PATH))
            {
                using (db.BeginTrans())
                {
                    // Get a collection (or create, if doesn't exist)
                    var col = db.GetCollection <BackupRecord>(DB_BACKUPS);

                    //insert backup record
                    List <myFileInfo> fileInfoList = JsonConvert.DeserializeObject <List <myFileInfo> >(fileInfoJSONString);
                    var backupRecord = new BackupRecord(client_id, timestamp, fileInfoList);
                    col.Insert(backupRecord);
                }
            }

            return(true);
        }