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); }
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()); }
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)); }
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); }
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); }
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); }