public void Create_All_Test()
        {
            string fileName = DateTime.Now.Ticks.ToString() + ".kpsdb";
            string folderName = DateTime.Now.Ticks.ToString();
            var provider = new Provider(fileName, folderName);

            var sampleData = CreateDummyData();
            ISyncItemServices services = new SyncItemServices(provider);
            var returnData = services.CreateSyncItem(sampleData);

            using (var connection = provider.GetConnection())
            {
                connection.Open();
                using (var command = new SqliteCommand(string.Format(
                    @"SELECT Id, FileKey, CloudAssetId,
                CloudLastUpdatedDate, CloudCreatedDate, IsDeleted,
                Size FROM {0} WHERE Id={1}", SyncItem.TableName, returnData.Id), connection))
                {
                    var reader = command.ExecuteReader();
                    SyncItem outputData = new SyncItem();

                    while (reader.Read())
                    {
                        outputData.Id = reader.GetInt64(0);
                        outputData.FileKey = reader.GetString(1);
                        outputData.CloudAssetId = reader.GetString(2);
                        outputData.CloudLastUpdated = new DateTime(reader.GetInt64(3));
                        outputData.CloudCreated = new DateTime(reader.GetInt64(4));
                        outputData.IsDeleted = reader.GetBoolean(5);
                        outputData.Size = reader.GetInt64(6);
                    }
                    Assert.AreEqual(outputData.Id, returnData.Id);
                    Assert.AreEqual(outputData.FileKey, returnData.FileKey);
                    Assert.AreEqual(outputData.CloudAssetId, returnData.CloudAssetId);
                    Assert.AreEqual(outputData.CloudLastUpdated, returnData.CloudLastUpdated);
                    Assert.AreEqual(outputData.CloudCreated, returnData.CloudCreated);
                    Assert.AreEqual(outputData.IsDeleted, returnData.IsDeleted);
                    Assert.AreEqual(outputData.Size, returnData.Size);
                }
                connection.Close();
            }
            File.Delete(provider.DbFile);
            Directory.Delete(folderName, true);
        }
Example #2
0
        public SyncItem CreateSyncItem(SyncItem newItem)
        {
            using (SqliteConnection con = _provider.GetConnection())
            {
                con.Open();

                using (SqliteCommand cmd = new SqliteCommand(con))
                {
                    cmd.CommandText = string.Format(
                        @"INSERT INTO {0}(
                        Id,
                        FileKey, 
                        CloudAssetId, 
                        CloudLastUpdatedDate, 
                        CloudCreatedDate,
                        IsDeleted,
                        Size) 
                        VALUES(
                        @Id,
                        @FileKey, 
                        @CloudAssetId,
                        @CloudLastUpdatedDate, 
                        @CloudCreatedDate,
                        @IsDeleted,
                        @Size)", SyncItem.TableName);
                    cmd.Prepare();

                    cmd.Parameters.AddWithValue("@Id", newItem.Id);
                    cmd.Parameters.AddWithValue("@FileKey", newItem.FileKey);
                    cmd.Parameters.AddWithValue("@CloudAssetId", newItem.CloudAssetId);
                    cmd.Parameters.AddWithValue("@CloudLastUpdatedDate", newItem.CloudLastUpdated.Ticks);
                    cmd.Parameters.AddWithValue("@CloudCreatedDate", newItem.CloudCreated.Ticks);
                    cmd.Parameters.AddWithValue("@IsDeleted", newItem.IsDeleted);
                    cmd.Parameters.AddWithValue("@Size", newItem.Size);


                    cmd.ExecuteNonQuery();
                }

                con.Close();
            }
            return(newItem);
        }
        public SyncItem CreateSyncItem(SyncItem newItem)
        {
            using(SqliteConnection con = _provider.GetConnection())
            {
                con.Open();

                using (SqliteCommand cmd = new SqliteCommand(con))
                {
                    cmd.CommandText = string.Format(
                        @"INSERT INTO {0}(
                        Id,
                        FileKey,
                        CloudAssetId,
                        CloudLastUpdatedDate,
                        CloudCreatedDate,
                        IsDeleted,
                        Size)
                        VALUES(
                        @Id,
                        @FileKey,
                        @CloudAssetId,
                        @CloudLastUpdatedDate,
                        @CloudCreatedDate,
                        @IsDeleted,
                        @Size)", SyncItem.TableName);
                    cmd.Prepare();

                    cmd.Parameters.AddWithValue("@Id", newItem.Id);
                    cmd.Parameters.AddWithValue("@FileKey", newItem.FileKey);
                    cmd.Parameters.AddWithValue("@CloudAssetId", newItem.CloudAssetId);
                    cmd.Parameters.AddWithValue("@CloudLastUpdatedDate", newItem.CloudLastUpdated.Ticks);
                    cmd.Parameters.AddWithValue("@CloudCreatedDate", newItem.CloudCreated.Ticks);
                    cmd.Parameters.AddWithValue("@IsDeleted", newItem.IsDeleted);
                    cmd.Parameters.AddWithValue("@Size", newItem.Size);

                    cmd.ExecuteNonQuery();
                }

                con.Close();
            }
            return newItem;
        }
 public IList<SyncItem> GetAll()
 {
     List<SyncItem> returnData = new List<SyncItem>();
     using (var conn = _provider.GetConnection())
     {
         conn.Open();
         using (SqliteCommand command =
             new SqliteCommand(
                 string.Format(
                     @"SELECT Id, FileKey, CloudAssetId,
         CloudLastUpdatedDate, CloudCreatedDate, IsDeleted,
         Size FROM {0}", SyncItem.TableName), conn))
         {
             var reader = command.ExecuteReader();
             while (reader.Read())
             {
                 SyncItem item = new SyncItem();
                 item.Id = reader.GetInt32(0);
                 item.FileKey = reader.GetString(1);
                 item.CloudAssetId = reader.GetString(2);
                 item.CloudLastUpdated = new DateTime(reader.GetInt64(3));
                 item.CloudCreated = new DateTime(reader.GetInt64(4));
                 item.IsDeleted = reader.GetBoolean(5);
                 returnData.Add(item);
             }
         }
         conn.Close();
     }
     return returnData;
 }
        public SyncItem UpdateSyncItem(SyncItem updatedItem)
        {
            using(SqliteConnection con = _provider.GetConnection())
            {
                con.Open();

                using (SqliteCommand cmd = new SqliteCommand(con))
                {
                    cmd.CommandText = string.Format(@"UPDATE {0} SET
                        FileKey = @FileKey,
                        CloudAssetId = @CloudAssetId,
                        CloudLastUpdatedDate = @CloudLastUpdatedDate,
                        CloudCreatedDate = @CloudCreatedDate,
                        IsDeleted = @IsDeleted,
                        Size = @Size WHERE
                        Id = {1}", SyncItem.TableName, updatedItem.Id);
                    cmd.Prepare();
                    cmd.Parameters.AddWithValue("@FileKey", updatedItem.FileKey);
                    cmd.Parameters.AddWithValue("@CloudAssetId", updatedItem.CloudAssetId);
                    cmd.Parameters.AddWithValue("@CloudLastUpdatedDate", updatedItem.CloudLastUpdated.Ticks);
                    cmd.Parameters.AddWithValue("@CloudCreatedDate", updatedItem.CloudCreated.Ticks);
                    cmd.Parameters.AddWithValue("@IsDeleted", updatedItem.IsDeleted);
                    cmd.Parameters.AddWithValue("@Size", updatedItem.Size);
                    cmd.ExecuteNonQuery();
                }

                con.Close();
            }
            return updatedItem;
        }
 public SyncItem Save(SyncItem item)
 {
     if (item.Id < 0)
     {
         return CreateSyncItem(item);
     }
     else
     {
         return UpdateSyncItem(item);
     }
 }
 public SyncItem GetById(long id)
 {
     SyncItem item = new SyncItem();
     using (var conn = _provider.GetConnection())
     {
         conn.Open();
         using (SqliteCommand command =
             new SqliteCommand(
                 string.Format(
                     @"SELECT Id, FileKey, CloudAssetId,
         CloudLastUpdatedDate, CloudCreatedDate, IsDeleted,
         Size FROM {0} WHERE Id={1}", SyncItem.TableName, id), conn))
         {
             var reader = command.ExecuteReader();
             while (reader.Read())
             {
                 item.Id = reader.GetInt64(0);
                 item.FileKey = reader.GetString(1);
                 item.CloudAssetId = reader.GetString(2);
                 item.CloudLastUpdated = new DateTime(reader.GetInt64(3));
                 item.CloudCreated = new DateTime(reader.GetInt64(4));
                 item.IsDeleted = reader.GetBoolean(5);
                 item.Size = reader.GetInt64(6);
             }
         }
         conn.Close();
     }
     return item;
 }
        private void InsertDummyData(SyncItem dummyData, SqliteCommand cmd)
        {
            cmd.CommandText = string.Format(
                @"INSERT INTO {0}(
                        FileKey,
                        CloudAssetId,
                        CloudLastUpdatedDate,
                        CloudCreatedDate,
                        IsDeleted,
                        Size)
                        VALUES(
                        @FileKey,
                        @CloudAssetId,
                        @CloudLastUpdated,
                        @CloudCreated,
                        @IsDeleted,
                        @Size)", SyncItem.TableName);
            cmd.Prepare();

            cmd.Parameters.AddWithValue("@FileKey", dummyData.FileKey);
            cmd.Parameters.AddWithValue("@CloudAssetId", dummyData.CloudAssetId);
            cmd.Parameters.AddWithValue("@CloudLastUpdated", dummyData.CloudLastUpdated.Ticks);
            cmd.Parameters.AddWithValue("@CloudCreated", dummyData.CloudCreated.Ticks);
            cmd.Parameters.AddWithValue("@IsDeleted", dummyData.IsDeleted);
            cmd.Parameters.AddWithValue("@Size", dummyData.Size);

            cmd.ExecuteNonQuery();
        }
 private SyncItem CreateDummyData()
 {
     var dummy = new SyncItem
     {
             Id = DateTime.Now.Ticks,
             FileKey = Guid.NewGuid().ToString(),
             CloudAssetId = Guid.NewGuid().ToString(),
             CloudCreated = DateTime.Now,
             CloudLastUpdated = DateTime.Now,
             IsDeleted = false,
             Size = 1024
     };
     return dummy;
 }