Exemple #1
0
        public bool Update(object key, Encoding keyEncoding = null, params EsentCell[] columns)
        {
            CheckState();
            var byteKey = ValueProcessor.GetBytes(key, keyEncoding);

            Api.MakeKey(Database.Session.JetId, JetId, byteKey, MakeKeyGrbit.NewKey);
            var success = Api.TrySeek(Database.Session.JetId, JetId, SeekGrbit.SeekEQ);

            if (!success)
            {
                return(false);
            }
            Api.JetPrepareUpdate(Database.Session.JetId, JetId, JET_prep.Replace);
            foreach (var column in columns)
            {
                var encoding = column.Encoding ?? Columns[column.ColumnName].Encoding;
                var value    = ValueProcessor.GetBytes(column.Value, encoding);
                if (value != null)
                {
                    Api.SetColumn(Database.Session.JetId, JetId, Columns[column.ColumnName].JetId, value);
                }
            }
            Api.JetUpdate(Database.Session.JetId, JetId);
            return(true);
        }
Exemple #2
0
        public static EsentCell[] Serialize(object obj, EsentTable table)
        {
            var type  = obj.GetType();
            var props = type.GetRuntimeProperties();
            var cells = new List <EsentCell>();

            foreach (var column in table.Columns.Values)
            {
                var prop = props.FirstOrDefault(p => p.Name == column.Name);
                if (column.Options.HasFlag(EsentColumn.Option.Autoincrement) || prop == null)
                {
                    continue;
                }
                if (column.ColumnType == prop.PropertyType)
                {
                    var value = prop.GetValue(obj);
                    cells.Add(new EsentCell(column.Name, value, column.Encoding));
                }
                if (column.ColumnType == typeof(byte[]) && prop.PropertyType != typeof(byte[]))
                {
                    var value = ValueProcessor.GetBytes(prop.GetValue(obj));
                    cells.Add(new EsentCell(column.Name, value, column.Encoding));
                }
            }
            return(cells.ToArray());
        }
Exemple #3
0
        public EsentRow Select(IEnumerable <EsentCell> columns, object key, Encoding keyEncoding = null)
        {
            CheckState();
            var columnArray = columns as EsentCell[] ?? columns.ToArray();
            var byteKey     = ValueProcessor.GetBytes(key, keyEncoding);

            Api.MakeKey(Database.Session.JetId, JetId, byteKey, MakeKeyGrbit.NewKey);
            var success = Api.TrySeek(Database.Session.JetId, JetId, SeekGrbit.SeekEQ);

            if (!success)
            {
                return(null);
            }
            return(RetrieveColumns(columnArray));
        }
Exemple #4
0
        public bool Delete(object key, Encoding keyEncoding = null)
        {
            CheckState();
            var byteKey = ValueProcessor.GetBytes(key, keyEncoding);

            Api.MakeKey(Database.Session.JetId, JetId, byteKey, MakeKeyGrbit.NewKey);
            var success = Api.TrySeek(Database.Session.JetId, JetId, SeekGrbit.SeekEQ);

            if (!success)
            {
                return(false);
            }
            Api.JetDelete(Database.Session.JetId, JetId);
            return(true);
        }
Exemple #5
0
 public void Insert(params EsentCell[] columns)
 {
     CheckState();
     using (Database.Session.BeginTransaction())
     {
         Api.JetPrepareUpdate(Database.Session.JetId, JetId, JET_prep.Insert);
         foreach (var column in columns)
         {
             var encoding = column.Encoding ?? Columns[column.ColumnName].Encoding;
             var value    = ValueProcessor.GetBytes(column.Value, encoding);
             if (value != null)
             {
                 Api.SetColumn(Database.Session.JetId, JetId, Columns[column.ColumnName].JetId, value);
             }
         }
         Api.JetUpdate(Database.Session.JetId, JetId);
     }
 }
        internal static EsentColumn Create <T>(EsentTable table, string name, Encoding encoding, int?max, object defaultValue, Option option)
        {
            var columnType = typeof(T);
            var column     = new EsentColumn
            {
                Table        = table,
                Name         = name,
                ColumnType   = columnType,
                Encoding     = encoding,
                Max          = max,
                DefaultValue = defaultValue,
                JetDef       = { grbit = (ColumndefGrbit)option }
            };
            var defaultValueBytes = ValueProcessor.GetBytes(defaultValue, encoding);
            var defaultValueSize  = defaultValueBytes?.Length ?? 0;

            Api.JetAddColumn(table.Database.Session.JetId, table.JetId, name, column.JetDef, defaultValueBytes, defaultValueSize, out column.JetId);
            return(column);
        }
Exemple #7
0
        public int Update(Func <EsentRow, bool> predicate, Encoding keyEncoding = null, params EsentCell[] columns)
        {
            CheckState();
            var exColumns   = Columns.Select(c => new EsentCell(c.Value.Name, null, c.Value.Encoding));
            var columnArray = exColumns as EsentCell[] ?? exColumns.ToArray();
            var success     = Api.TryMove(Database.Session.JetId, JetId, JET_Move.First, MoveGrbit.None);

            if (!success)
            {
                return(0);
            }
            var count = 0;

            while (true)
            {
                var cells = RetrieveColumns(columnArray);
                if (predicate(cells))
                {
                    count++;
                    Api.JetPrepareUpdate(Database.Session.JetId, JetId, JET_prep.Replace);
                    foreach (var column in columns)
                    {
                        var encoding = column.Encoding ?? Columns[column.ColumnName].Encoding;
                        var value    = ValueProcessor.GetBytes(column.Value, encoding);
                        if (value != null)
                        {
                            Api.SetColumn(Database.Session.JetId, JetId, Columns[column.ColumnName].JetId, value);
                        }
                    }
                    Api.JetUpdate(Database.Session.JetId, JetId);
                }
                if (!Api.TryMove(Database.Session.JetId, JetId, JET_Move.Next, MoveGrbit.None))
                {
                    break;
                }
            }
            return(count);
        }