Exemple #1
0
        private void ProcessAllInternal(IEnumerable <IEntity> data, ProcessMode mode, SqliteTransaction tran, bool inTran)
        {
            string[] columns   = null;
            var      cmd       = new SqliteCommand[4];
            String   tableName = null;

            lock (_dbsync)
                try
                {
                    foreach (IEntity obj in data)
                    {
                        if (cmd[(int)Operation.Insert] == null)
                        {
                            IEntityType type = obj.EntityType;
                            columns = GetColumns(type);
                            String fNames      = "";
                            String fParameters = "";
                            String fFields     = "";
                            foreach (string column in columns)
                            {
                                if (!String.IsNullOrEmpty(fNames))
                                {
                                    fNames      = fNames + ",";
                                    fParameters = fParameters + ",";
                                    fFields     = fFields + ",";
                                }
                                fNames      = fNames + column;
                                fParameters = fParameters + "@" + column;
                                fFields     = fFields + String.Format("[{0}] = @{0}", column);
                            }
                            fNames      = fNames + ",IsTombstone,IsDirty";
                            fParameters = fParameters + ",@IsTombstone,@IsDirty";
                            fFields     = fFields + ",[IsTombstone] = @IsTombstone, [IsDirty] = @IsDirty";

                            tableName = type.TableName;

                            cmd[(int)Operation.Insert] = new SqliteCommand(String.Format("INSERT INTO [_{0}]({1}) VALUES({2})", tableName, fNames, fParameters), ActiveConnection, tran);
                            cmd[(int)Operation.Update] = new SqliteCommand(String.Format("UPDATE [_{0}] SET {1} WHERE [Id] = @Id", tableName, fFields), ActiveConnection, tran);
                            foreach (string column in columns)
                            {
                                cmd[0].Parameters.Add("@" + column, _supportedTypes[GetType(type.GetPropertyType(column))].DbType);
                                cmd[1].Parameters.Add("@" + column, _supportedTypes[GetType(type.GetPropertyType(column))].DbType);
                            }
                            cmd[(int)Operation.Insert].Parameters.Add("@IsTombstone", DbType.Boolean);
                            cmd[(int)Operation.Insert].Parameters.Add("@IsDirty", DbType.Boolean);
                            cmd[(int)Operation.Update].Parameters.Add("@IsTombstone", DbType.Boolean);
                            cmd[(int)Operation.Update].Parameters.Add("@IsDirty", DbType.Boolean);

                            cmd[(int)Operation.Select] = new SqliteCommand(String.Format("SELECT Id FROM [_{0}] WHERE [Id] = @Id", tableName), ActiveConnection, tran);
                            cmd[(int)Operation.Select].Parameters.Add("@Id", DbType.String);

                            cmd[(int)Operation.Delete] = new SqliteCommand(String.Format("DELETE FROM [_{0}] WHERE [Id] = @Id", tableName), ActiveConnection, tran);
                            cmd[(int)Operation.Delete].Parameters.Add("@Id", DbType.String);
                        }

                        //row id
                        DbRef id;
                        if (mode == ProcessMode.InitialLoad || mode == ProcessMode.ServerChanges)
                        {
                            id = DbRef.CreateInstance(tableName, ((ISqliteEntity)obj).EntityId);
                        }
                        else
                        {
                            id = (DbRef)obj.GetValue(obj.EntityType.IdFieldName);
                        }

                        Operation operation = Operation.Insert; //insert
                        if (mode != ProcessMode.InitialLoad)
                        {
                            if (((ISqliteEntity)obj).IsTombstone)
                            {
                                operation = mode == ProcessMode.ServerChanges ? Operation.Delete : Operation.Update;
                            }
                            else
                            {
                                cmd[2].Parameters[0].Value = id;
                                if (cmd[2].ExecuteScalar() != null)
                                {
                                    operation = Operation.Update; //update
                                }
                            }
                        }

                        //assign values
                        if (operation == Operation.Delete) //delete
                        {
                            cmd[(int)operation].Parameters[0].Value = id;
                        }
                        else
                        {
                            int n = 0;
                            foreach (string column in columns)
                            {
                                cmd[(int)operation].Parameters[n].Value = obj.GetValue(column);
                                n++;
                            }
                            cmd[(int)operation].Parameters[n].Value = ((ISqliteEntity)obj).IsTombstone ? 1 : 0; //IsTombstone
                            var entity = (ISqliteEntity)obj;
                            cmd[(int)operation].Parameters[n + 1].Value =
                                mode == ProcessMode.LocalChanges && (entity.IsNew() || entity.IsModified())
                                ? 1 : 0;  //isDirty
                        }

                        if (mode == ProcessMode.LocalChanges && inTran)
                        {
                            CopyTranObject(tran, tableName, id.ToString(), operation);
                        }

                        cmd[(int)operation].ExecuteNonQuery();
                    }
                }
                finally
                {
                    foreach (var c in cmd)
                    {
                        if (c != null)
                        {
                            c.Dispose();
                        }
                    }
                }
        }
Exemple #2
0
 public IDbRef CreateDbRef(string tableName, Guid id)
 {
     return(DbRef.CreateInstance(tableName, id));
 }