public void TestNonFluentUpdateInterface() { using (Connection connection = Esent.CreateDatabase(this.database)) using (Transaction transaction = connection.BeginTransaction()) { Table table = connection.CreateTable("mytable"); table.CreateColumn(new ColumnDefinition("autoinc", ColumnType.Int32) { IsAutoincrement = true }); table.CreateColumn(new ColumnDefinition("myint", ColumnType.Int32)); table.CreateColumn(new ColumnDefinition("mystring", ColumnType.Text) { MaxSize = 200 }); Record record = table.NewRecord(); record["myint"] = 5; record["mystring"] = "hello"; record.Save(); transaction.Commit(); } using (Connection connection = Esent.OpenDatabase(this.database, DatabaseOpenMode.ReadOnly)) { Table table = connection.OpenTable("mytable"); Record record = table.First(); Assert.AreEqual(1, record["autoinc"]); Assert.AreEqual(5, record["myint"]); Assert.AreEqual("hello", record["mystring"]); } }
public void EsentOpenDatabasePassesOpenModeToConnectionFactory() { Expect.Call(this.mockConnectionManager.AttachDatabase("mydatabase.edb", DatabaseOpenMode.ReadOnly)).Return(this.dummyConnection); this.mocks.ReplayAll(); Esent.OpenDatabase("mydatabase.edb", DatabaseOpenMode.ReadOnly); this.mocks.VerifyAll(); }
public void TestFluentUpdateInterface() { using (Connection connection = Esent.CreateDatabase(this.database)) { connection.UsingTransaction(() => { Table table = connection.CreateTable("mytable") .CreateColumn(DefinedAs.Int32Column("autoinc").AsAutoincrement()) .CreateColumn(DefinedAs.Int32Column("myint")) .CreateColumn(DefinedAs.TextColumn("mystring").WithMaxSize(200)); table.NewRecord() .SetColumn("myint", 5) .SetColumn("mystring", "hello") .Save(); }); } using (Connection connection = Esent.OpenDatabase(this.database, DatabaseOpenMode.ReadOnly)) { Table table = connection.OpenTable("mytable"); Record record = table.First(); Assert.AreEqual(1, record["autoinc"]); Assert.AreEqual(5, record["myint"]); Assert.AreEqual("hello", record["mystring"]); } }
public void EsentOpenDatabaseUsesOpenModeReadWriteAsDefault() { Expect.Call(this.mockConnectionManager.AttachDatabase("mydatabase.edb", DatabaseOpenMode.ReadWrite)).Return(this.dummyConnection); this.mocks.ReplayAll(); Esent.OpenDatabase("mydatabase.edb"); this.mocks.VerifyAll(); }
/// <summary> /// /// </summary> /// <returns></returns> public int GetTotalNumberRecords() { using (Connection connection = Esent.OpenDatabase(OutputPath + @"\data.edb")) using (Table table = connection.OpenTable("Data")) { return((from data in table select data).Count()); } }
public void VerifyReadOnlyConnectionCannotCreateTable() { using (Connection connection = Esent.CreateDatabase(this.database)) using (Connection readOnlyConnection = Esent.OpenDatabase(this.database, DatabaseOpenMode.ReadOnly)) { readOnlyConnection.CreateTable("foo"); } }
public void VerifyOpenDatabaseReadOnlyReturnsReadOnlyConnection() { using (Connection connection = Esent.CreateDatabase(this.database)) using (Connection readOnlyConnection = Esent.OpenDatabase(this.database, DatabaseOpenMode.ReadOnly)) { Assert.IsInstanceOfType(readOnlyConnection, typeof(ReadOnlyConnection)); } }
public void VerifyReadOnlyConnectionReturnsReadOnlyTable() { using (Connection connection = Esent.CreateDatabase(this.database)) using (Connection readOnlyConnection = Esent.OpenDatabase(this.database, DatabaseOpenMode.ReadOnly)) { connection.CreateTable("foo"); Table table = readOnlyConnection.OpenTable("foo"); Assert.IsInstanceOfType(table, typeof(ReadOnlyTable)); } }
public void VerifyReadOnlyTableCannotCreateColumn() { using (Connection connection = Esent.CreateDatabase(this.database)) using (Connection readOnlyConnection = Esent.OpenDatabase(this.database, DatabaseOpenMode.ReadOnly)) { connection.CreateTable("foo"); Table table = readOnlyConnection.OpenTable("foo"); table.CreateColumn(DefinedAs.AsciiTextColumn("col")); } }
public void VerifyReadOnlyTableCannotCreateNewRecord() { using (Connection connection = Esent.CreateDatabase(this.database)) using (Connection readOnlyConnection = Esent.OpenDatabase(this.database, DatabaseOpenMode.ReadOnly)) { connection.CreateTable("foo"); Table table = readOnlyConnection.OpenTable("foo"); readOnlyConnection.UsingTransaction(() => table.NewRecord()); } }
static void DumpRecords() { using (Connection connection = Esent.OpenDatabase(Database)) using (Table table = connection.OpenTable("mytable")) { foreach (Record record in table) { Console.WriteLine("{0} : {1}", record["myid"], record["mydata"]); } } }
/// <summary> /// Perform the specified action on a read-only record. /// </summary> /// <param name="action">The action to perform</param> private void WithReadOnlyRecord(Action <Record> action) { using (Connection connection = Esent.CreateDatabase(this.database)) using (Connection readOnlyConnection = Esent.OpenDatabase(this.database, DatabaseOpenMode.ReadOnly)) using (Table table = connection.CreateTable("foo").CreateColumn(DefinedAs.AsciiTextColumn("text"))) using (Table readOnlyTable = readOnlyConnection.OpenTable("foo")) { connection.UsingTransaction(() => table.NewRecord().SetColumn("text", Any.String).Save()); Record record = readOnlyTable.First(); action(record); } }
public void TestFluentUpdateInterfaceWithRollback() { using (Connection connection = Esent.CreateDatabase(this.database)) { Table table = null; connection.UsingLazyTransaction(() => { table = connection.CreateTable("mytable") .CreateColumn(DefinedAs.Int32Column("autoinc").AsAutoincrement()) .CreateColumn(DefinedAs.Int32Column("myint")) .CreateColumn(DefinedAs.TextColumn("mystring").WithMaxSize(200)); }); connection.UsingLazyTransaction(() => table.NewRecord() .SetColumn("myint", 5) .SetColumn("mystring", "hello") .Save()); try { connection.UsingLazyTransaction(() => { table.First() .SetColumn("myint", 100) .SetColumn("mystring", "somethingelse") .Save(); throw new OutOfMemoryException("cancel this update"); }); } catch (OutOfMemoryException) { // we expected this } } using (Connection connection = Esent.OpenDatabase(this.database, DatabaseOpenMode.ReadOnly)) { Table table = connection.OpenTable("mytable"); Record record = table.First(); Assert.AreEqual(1, record["autoinc"]); Assert.AreEqual(5, record["myint"]); Assert.AreEqual("hello", record["mystring"]); } }
static void InsertRecords() { using (Connection connection = Esent.OpenDatabase(Database)) using (Table table = connection.OpenTable("mytable")) { connection.UsingLazyTransaction(() => { for (int i = 0; i < 10; ++i) { table.NewRecord() .SetColumn("myid", i) .SetColumn("mydata", "some data") .Save(); } }); } }
static void DeleteRecords() { using (Connection connection = Esent.OpenDatabase(Database)) using (Table table = connection.OpenTable("mytable")) { IEnumerable <Record> recordsToDelete = from record in table where (0 == ((int)record["myid"] % 2)) select record; connection.UsingLazyTransaction(() => { foreach (Record record in recordsToDelete) { record.Delete(); } }); } }
static void UpdateRecords() { using (Connection connection = Esent.OpenDatabase(Database)) using (Table table = connection.OpenTable("mytable")) { connection.UsingLazyTransaction(() => { Record[] records = table.ToArray(); records[0]["mydata"] = "some other data"; records[1]["mydata"] = "even different data"; records[2]["mydata"] = "this update will be lost"; records[0].Save(); records[1].Save(); // records[2] isn't saved so the update will be cancelled when the transaction commits }); } }
public void CreateTwoConnections() { using (Connection connection = Esent.CreateDatabase(this.database)) using (Connection anotherconnection = Esent.OpenDatabase(this.database)) using (Table anothertable = anotherconnection.CreateTable("table")) { Transaction transaction = anotherconnection.BeginTransaction(); anothertable.CreateColumn(new ColumnDefinition("column", ColumnType.Bool)); Record anotherrecord = anothertable.NewRecord(); anotherrecord["column"] = 1; anotherrecord.Save(); transaction.Commit(); using (Table table = connection.OpenTable("table")) { Record record = table.First(); Assert.AreEqual(true, record["column"]); } } }
public void Setup() { this.connection = Esent.OpenDatabase(database); this.transaction = this.connection.BeginTransaction(); this.table = this.connection.OpenTable(tablename); }
///// <summary> ///// ///// </summary> ///// <param name="directory"></param> ///// <param name="path"></param> //public void Delete(string directory, string path) //{ // File.Delete(path); // File.Delete(System.IO.Path.Combine(directory, String.Format(CultureInfo.InvariantCulture, "{0}.chk", "edb"))); // foreach (string file in Directory.GetFiles(directory, String.Format(CultureInfo.InvariantCulture, "{0}*.log", "edb"))) // { // File.Delete(file); // } // foreach (string file in Directory.GetFiles(directory, String.Format(CultureInfo.InvariantCulture, "{0}*.jrs", "edb"))) // { // File.Delete(file); // } //} /// <summary> /// /// </summary> public string CreateDatabase() { try { if (File.Exists(OutputPath + @"\data.edb") == false) { _connection = Esent.CreateDatabase(OutputPath + @"\data.edb"); _table = _connection.CreateTable("Data"); _connection.UsingLazyTransaction(() => { _table.CreateColumn(new ColumnDefinition("Id", ColumnType.Int32)); // Filterable _table.CreateColumn(new ColumnDefinition("Type", ColumnType.Text)); // Filterable _table.CreateColumn(new ColumnDefinition("HostName", ColumnType.Text)); // Filterable _table.CreateColumn(new ColumnDefinition("IpAddress", ColumnType.Text)); // Filterable _table.CreateColumn(new ColumnDefinition("MacAddress", ColumnType.Text)); // Filterable _table.CreateColumn(new ColumnDefinition("Service", ColumnType.Text)); // Filterable _table.CreateColumn(new ColumnDefinition("Protocol", ColumnType.Text)); // Filterable _table.CreateColumn(new ColumnDefinition("Port", ColumnType.Text)); // Filterable _table.CreateColumn(new ColumnDefinition("Text", ColumnType.Text)); // Filterable _table.CreateColumn(new ColumnDefinition("PluginId", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("PluginName", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("PluginFamily", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("Severity", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("Solution", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("RiskFactor", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("Description", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("PluginPublicationDate", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("VulnPublicationDate", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("PatchPublicationDate", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("Synopsis", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("PluginOutput", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("PluginVersion", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("SeeAlso", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("CvssVector", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("CvssBaseScore", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("CvssTemporalScore", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("Cve", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("Bid", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("Xref", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("State", ColumnType.Text)); // Filterable _table.CreateColumn(new ColumnDefinition("Product", ColumnType.Text)); // Filterable _table.CreateColumn(new ColumnDefinition("Version", ColumnType.Text)); // Filterable _table.CreateColumn(new ColumnDefinition("ExtraInfo", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("ExploitabilityEase", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("ExploitAvailable", ColumnType.Text)); // Filterable _table.CreateColumn(new ColumnDefinition("ExploitFrameworkCanvas", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("ExploitFrameworkMetasploit", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("ExploitFrameworkCore", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("MetasploitName", ColumnType.Text)); _table.CreateColumn(new ColumnDefinition("CanvasPackage", ColumnType.Text)); }); _transaction = _connection.BeginTransaction(); } else { _connection = Esent.OpenDatabase(OutputPath + @"\data.edb"); _table = _connection.OpenTable("Data"); _transaction = _connection.BeginTransaction(); } return(string.Empty); } catch (Exception ex) { return(ex.Message); } }
/// <summary> /// Opens an existing database and returns a connection to it. /// </summary> /// <param name="database">The database to open.</param> /// <returns>A connection to the database.</returns> public static Connection OpenDatabase(string database) { return(Esent.OpenDatabase(database, DatabaseOpenMode.ReadWrite)); }