Esempio n. 1
0
        public void CreateBasicTableColumnIndex3OnXp()
        {
            var tablecreate = new JET_TABLECREATE { szTableName = "table" };

            string directory = SetupHelper.CreateRandomDirectory();
            string database = Path.Combine(directory, "test.db");

            using (var instance = new Instance("XpCreateBasicTableColumnIndex3"))
            {
                instance.Parameters.Recovery = false;
                instance.Parameters.NoInformationEvent = true;
                instance.Parameters.MaxTemporaryTables = 0;
                instance.Init();
                using (var session = new Session(instance))
                {
                    JET_DBID dbid;
                    Api.JetCreateDatabase(session, database, String.Empty, out dbid, CreateDatabaseGrbit.None);
                    using (var transaction = new Transaction(session))
                    {
                        Api.JetCreateTableColumnIndex3(session, dbid, tablecreate);
                        Assert.AreNotEqual(JET_TABLEID.Nil, tablecreate.tableid);
                        Assert.AreEqual(tablecreate.cCreated, 1);
                        Api.JetCloseTable(session, tablecreate.tableid);
                        transaction.Commit(CommitTransactionGrbit.LazyFlush);
                    }
                }
            }
        }
Esempio n. 2
0
        public void each_up()
        {
            var tempDir = Path.Combine(Environment.CurrentDirectory, "with_a_dummy_instance");
            var tempPath = Path.Combine(tempDir, "test.db");
            if(Directory.Exists(tempDir)) Directory.Delete(tempDir, true);

            Instance = new Instance(Guid.NewGuid().ToString());
            Instance.Init();
        }
Esempio n. 3
0
        /// <summary>
        /// Dump the meta-data of the table.
        /// </summary>
        /// <param name="args">Arguments for the command.</param>
        private void DumpMetaData(string[] args)
        {
            if (args.Length != 1)
            {
                throw new ArgumentException("specify the database", "args");
            }

            string database = args[0];

            using (var instance = new Instance("dumpmetadata"))
            {
                instance.Parameters.Recovery = false;
                instance.Init();

                using (var session = new Session(instance))
                {
                    JET_DBID dbid;
                    Api.JetAttachDatabase(session, database, AttachDatabaseGrbit.ReadOnly);
                    Api.JetOpenDatabase(session, database, null, out dbid, OpenDatabaseGrbit.ReadOnly);

                    foreach (string table in Api.GetTableNames(session, dbid))
                    {
                        Console.WriteLine(table);
                        foreach (ColumnInfo column in Api.GetTableColumns(session, dbid, table))
                        {
                            Console.WriteLine("\t{0}", column.Name);
                            Console.WriteLine("\t\tColtyp:     {0}", column.Coltyp);
                            Console.WriteLine("\t\tColumnid:   {0}", column.Columnid);
                            if (JET_coltyp.LongText == column.Coltyp || JET_coltyp.Text == column.Coltyp)
                            {
                                Console.WriteLine("\t\tCode page:  {0}", column.Cp);
                            }

                            Console.WriteLine("\t\tMax length: {0}", column.MaxLength);
                            Console.WriteLine("\t\tGrbit:      {0}", column.Grbit);
                        }

                        foreach (IndexInfo index in Api.GetTableIndexes(session, dbid, table))
                        {
                            Console.WriteLine("\t{0}", index.Name);
                            Console.WriteLine("\t\tGrbit:          {0}", index.Grbit);
                            Console.WriteLine("\t\tCultureInfo:    {0}", index.CultureInfo);
                            Console.WriteLine("\t\tCompareOptions: {0}", index.CompareOptions);

                            foreach (IndexSegment segment in index.IndexSegments)
                            {
                                Console.WriteLine("\t\t\t{0}", segment.ColumnName);
                                Console.WriteLine("\t\t\t\tColtyp:      {0}", segment.Coltyp);
                                Console.WriteLine("\t\t\t\tIsAscending: {0}", segment.IsAscending);
                                Console.WriteLine("\t\t\t\tIsASCII:     {0}", segment.IsASCII);
                            }
                        }
                    }
                }
            }
        }
        public override void Initialise()
        {
            instance = new Instance(string.Empty);
            instance.Parameters.MaxVerPages = 1024;
            instance.Init();

            using (var session = new Session(instance))
                if (!fileSystem.FileExists(GetDatabaseFileName()))
                    CreateDatabaseAndTables(session, GetDatabaseFileName());
        }
Esempio n. 5
0
 /// <summary>
 /// Creates the message store.
 /// </summary>
 /// <param name="database">The database.</param>
 public static void CreateDatabase()
 {
     using (var instance = new Instance("createdatabase"))
     {
         instance.Parameters.CircularLog = true;
         instance.Init();
         using (var session = new Session(instance))
         {
             JET_DBID dbid;
             Api.JetCreateDatabase(session, DatabaseName, null, out dbid, CreateDatabaseGrbit.OverwriteExisting);
         }
     }
 }
Esempio n. 6
0
        public void IinsertAndReadOneMessage()
        {
            TestHelper.CreateDatabase("EsentTestsDummy");
            using (var instance = new Instance("MyInstanceName"))
            {
                instance.Parameters.CircularLog = true;
                instance.Init();
                using (var session = new Session(instance))
                {
                    JET_DBID dbid;
                    Api.JetAttachDatabase(session, DatabaseName, AttachDatabaseGrbit.None);
                    Api.JetOpenDatabase(session, DatabaseName, null, out dbid, OpenDatabaseGrbit.None);

                    JET_TABLEID tableid;

                    if (Api.TryOpenTable(session, dbid, "EsentTestsDummymessages", OpenTableGrbit.None, out tableid))
                    {
                        IDictionary<string, JET_COLUMNID> columnids = Api.GetColumnDictionary(session, tableid);
                        JET_COLUMNID columnidMessage = columnids["message"];
                        JET_COLUMNID columnidMetaData = columnids["metadata"];

                        Assert.IsInstanceOfType(columnidMessage, typeof(JET_COLUMNID));
                        Assert.IsInstanceOfType(columnidMetaData, typeof(JET_COLUMNID));
                        AddMessage(session, tableid, ref columnidMessage, ref columnidMetaData);

                        var results = TestHelper.DumpByIndex(session, tableid, null, columnids);
                        Assert.IsNotNull(results);
                        Assert.IsTrue(results.Count == 1);
                        Assert.AreEqual("Hi this is the message", results[0]);
                    }
                    else
                    {
                        using (var table = new Table(session, dbid, "EsentTestsDummymessages", OpenTableGrbit.None))
                        {
                            IDictionary<string, JET_COLUMNID> columnids = Api.GetColumnDictionary(session, table);
                            JET_COLUMNID columnidMessage = columnids["message"];
                            JET_COLUMNID columnidMetaData = columnids["metadata"];

                            Assert.IsInstanceOfType(columnidMessage, typeof(JET_COLUMNID));
                            Assert.IsInstanceOfType(columnidMetaData, typeof(JET_COLUMNID));
                            AddMessage(session, table, ref columnidMessage, ref columnidMetaData);

                            var results = TestHelper.DumpByIndex(session, table, null, columnids);
                            Assert.IsNotNull(results);
                            Assert.IsTrue(results.Count == 1);
                            Assert.AreEqual("Hi this is the message", results[0]);
                        }
                    }
                }
            }
        }
Esempio n. 7
0
 public void CreateInstanceInitTerm()
 {
     string dir = SetupHelper.CreateRandomDirectory();
     using (var instance = new Instance("theinstance"))
     {
         instance.Parameters.LogFileDirectory = dir;
         instance.Parameters.SystemDirectory = dir;
         instance.Parameters.TempDirectory = dir;
         instance.Parameters.NoInformationEvent = true;
         instance.Init();
         instance.Term();
         Directory.Delete(dir, true);    // only works if the instance is terminated
     }
 }
Esempio n. 8
0
 /// <summary>
 /// Creates the message store.
 /// </summary>
 /// <param name="database">The database.</param>
 public static void CreateDatabaseAndActorStore(string messageTypeName)
 {
     using (var instance = new Instance("createdatabase"))
     {
         instance.Parameters.CircularLog = true;
         instance.Init();
         using (var session = new Session(instance))
         {
             JET_DBID dbid;
             Api.JetCreateDatabase(session, DatabaseName, null, out dbid, CreateDatabaseGrbit.OverwriteExisting);
             CreateMessageTable(messageTypeName, session, dbid);
         }
     }
 }
        public void CreateIndexesOnXp()
        {
            string directory = SetupHelper.CreateRandomDirectory();
            string database = Path.Combine(directory, "test.db");

            using (var instance = new Instance("XPcreateindexes"))
            {
                instance.Parameters.Recovery = false;
                instance.Parameters.NoInformationEvent = true;
                instance.Parameters.MaxTemporaryTables = 0;
                instance.Parameters.TempDirectory = directory;
                instance.Init();
                using (var session = new Session(instance))
                {
                    JET_DBID dbid;
                    Api.JetCreateDatabase(session, database, String.Empty, out dbid, CreateDatabaseGrbit.None);
                    using (var transaction = new Transaction(session))
                    {
                        JET_TABLEID tableid;
                        Api.JetCreateTable(session, dbid, "table", 0, 100, out tableid);
                        JET_COLUMNID columnid;
                        Api.JetAddColumn(
                            session,
                            tableid,
                            "column1",
                            new JET_COLUMNDEF { coltyp = JET_coltyp.Long },
                            null,
                            0,
                            out columnid);

                        var indexcreates = new[]
                        {
                            new JET_INDEXCREATE
                            {
                                szKey = "+column1\0",
                                cbKey = 10,
                                szIndexName = "index1",
                                pidxUnicode = new JET_UNICODEINDEX { lcid = 1033 },
                            },
                        };

                        Api.JetCreateIndex2(session, tableid, indexcreates, indexcreates.Length);
                        transaction.Commit(CommitTransactionGrbit.LazyFlush);
                    }
                }
            }

            Cleanup.DeleteDirectoryWithRetry(directory);
        }
 public override void CreateDatabase()
 {
     try {
         using (Instance instance = new Instance("newdb")) {
             instance.Init();
             using (Session session = new Session(instance)) {
                 JET_DBID dbid;
                 Api.JetCreateDatabase(session, filename, null, out dbid, CreateDatabaseGrbit.None);
             }
         }
     } catch {
         // TODO: Wrap database already exists exception
         throw;
     }
 }
Esempio n. 11
0
        public void CreateInstanceInit()
        {
            string dir = SetupHelper.CreateRandomDirectory();
            using (var instance = new Instance("createinit"))
            {
                instance.Parameters.LogFileDirectory = dir;
                instance.Parameters.SystemDirectory = dir;
                instance.Parameters.TempDirectory = dir;
                instance.Parameters.LogFileSize = 512; // 512Kb
                instance.Parameters.NoInformationEvent = true;
                instance.Init();
            }

            Cleanup.DeleteDirectoryWithRetry(dir);
        }
Esempio n. 12
0
 public void CreateInstanceInitTerm()
 {
     string dir = SetupHelper.CreateRandomDirectory();
     using (var instance = new Instance("initterm"))
     {
         instance.Parameters.LogFileDirectory = dir;
         instance.Parameters.SystemDirectory = dir;
         instance.Parameters.TempDirectory = dir;
         instance.Parameters.LogFileSize = 256; // 256Kb
         instance.Parameters.NoInformationEvent = true;
         instance.Init();
         instance.Term();
         Cleanup.DeleteDirectoryWithRetry(dir);    // only works if the instance is terminated
     }
 }
Esempio n. 13
0
        public void AttachDatabaseWithAsciiPath2()
        {
            using (var instance = new Instance("asciidbattach2"))
            {
                SetupHelper.SetLightweightConfiguration(instance);
                instance.Parameters.CreatePathIfNotExist = true;
                instance.Init();
                using (var session = new Session(instance))
                {
                    JET_DBID dbid;
                    Api.JetCreateDatabase(session, this.database, String.Empty, out dbid, CreateDatabaseGrbit.None);
                    Api.JetCloseDatabase(session, dbid, CloseDatabaseGrbit.None);
                    Api.JetDetachDatabase(session, this.database);

                    Api.JetAttachDatabase2(session, this.database, 512, AttachDatabaseGrbit.None);
                }
            }
        }
 private Instance CreateInstance()
 {
     var instance = new Instance(databasePath)
     {
         Parameters =
         {
             CreatePathIfNotExist = true,
             TempDirectory = Path.Combine(fullPath, "temp"),
             SystemDirectory = Path.Combine(fullPath, "system"),
             LogFileDirectory = Path.Combine(fullPath, "logs"),
             Recovery = true,
             CircularLog = true,
             LogFileSize = 1024,
         },
     };
     instance.Init();
     return instance;
 }
Esempio n. 15
0
        protected void ExecuteInDatabase(Action<Session, JET_DBID> action)
        {
            var instance = new Instance(databasePath);
            try
            {
                instance.Parameters.CircularLog = true;
                instance.Parameters.CreatePathIfNotExist = true;
                instance.Parameters.TempDirectory = Path.Combine(Path.GetDirectoryName(databasePath), "temp");
                instance.Parameters.SystemDirectory = Path.Combine(Path.GetDirectoryName(databasePath), "system");
                instance.Parameters.LogFileDirectory = Path.Combine(Path.GetDirectoryName(databasePath), "logs");
                instance.Init();

                using (var session = new Session(instance))
                {
                    Api.JetAttachDatabase(session, databasePath, AttachDatabaseGrbit.None);
                    try
                    {
                        using (var tx = new Transaction(session))
                        {

                            JET_DBID dbid;
                            Api.JetOpenDatabase(session, databasePath, "", out dbid, OpenDatabaseGrbit.None);
                            try
                            {
                                action(session, dbid);
                                tx.Commit(CommitTransactionGrbit.None);
                            }
                            finally
                            {
                                Api.JetCloseDatabase(session, dbid, CloseDatabaseGrbit.None);
                            }
                        }
                    }
                    finally
                    {
                        Api.JetDetachDatabase(session, databasePath);
                    }
                }
            }
            finally
            {
                instance.Term();
            }
        }
Esempio n. 16
0
        public void CreateDatabaseWithUnicodePath()
        {
            if (!EsentVersion.SupportsUnicodePaths)
                return;

            using (var instance = new Instance("unicode"))
            {
                instance.Parameters.MaxTemporaryTables = 0;
                instance.Parameters.Recovery = false;
                instance.Parameters.CreatePathIfNotExist = true;
                instance.Init();
                using (var session = new Session(instance))
                {
                    JET_DBID dbid;
                    Api.JetCreateDatabase(session, this.database, String.Empty, out dbid, CreateDatabaseGrbit.None);
                    Assert.IsTrue(File.Exists(this.database));
                }
            }
        }
Esempio n. 17
0
        public Instance CreateInstance(bool delete = true)
        {
            if (delete && Directory.Exists(_path))
                Directory.Delete(_path, true);

            if (!Directory.Exists(_path))
                Directory.CreateDirectory(_path);

            var instance = new Instance(Guid.NewGuid().ToString());

            _configurator.ConfigureInstance(instance, _path);

            instance.Init();

            if (!delete)
                return instance;

            CreateSchema(instance);

            return instance;
        }
Esempio n. 18
0
 public void EsentExceptionIsThrownOnApiError()
 {
     using (var instance = new Instance("EsentExceptionHasErrorCode"))
     {
         SetupHelper.SetLightweightConfiguration(instance);
         instance.Init();
         using (var session = new Session(instance))
         {
             try
             {
                 // The session shouldn't be in a transaction so this will
                 // generate an error.
                 Api.JetCommitTransaction(session, CommitTransactionGrbit.None);
                 Assert.Fail("Should have thrown an exception");
             }
             catch (EsentErrorException ex)
             {
                 Assert.AreEqual(JET_err.NotInTransaction, ex.Error);
             }
         }
     }
 }
Esempio n. 19
0
        public void AttachDatabaseWithUnicodePath()
        {
            if (!EsentVersion.SupportsUnicodePaths)
            {
                return;
            }

            using (var instance = new Instance("unicodedbattach"))
            {
                SetupHelper.SetLightweightConfiguration(instance);
                instance.Parameters.CreatePathIfNotExist = true;
                instance.Init();
                using (var session = new Session(instance))
                {
                    JET_DBID dbid;
                    Api.JetCreateDatabase(session, this.database, String.Empty, out dbid, CreateDatabaseGrbit.None);
                    Api.JetCloseDatabase(session, dbid, CloseDatabaseGrbit.None);
                    Api.JetDetachDatabase(session, this.database);

                    Api.JetAttachDatabase(session, this.database, AttachDatabaseGrbit.None);
                }
            }
        }
Esempio n. 20
0
        public void CreateDatabase2WithUnicodePath()
        {
            if (!EsentVersion.SupportsUnicodePaths)
            {
                return;
            }

            using (var instance = new Instance("unicodedbcreate"))
            {
                SetupHelper.SetLightweightConfiguration(instance);
                instance.Parameters.CreatePathIfNotExist = true;
                instance.Init();
                using (var session = new Session(instance))
                {
                    JET_DBID dbid;
                    Api.JetCreateDatabase2(session, this.database, 512, out dbid, CreateDatabaseGrbit.None);
                    Assert.IsTrue(File.Exists(this.database));
                }
            }
        }
Esempio n. 21
0
 public void CreateInstanceWithDisplayName()
 {
     using (var instance = new Instance(Guid.NewGuid().ToString(), "Friendly Display Name"))
     {
         instance.Parameters.MaxTemporaryTables = 0;
         instance.Parameters.Recovery = false;
         instance.Init();
     }
 }
Esempio n. 22
0
 /// <summary>
 /// Create an instance and abandon it. Garbage collection should
 /// be able to finalize the instance.
 /// </summary>
 private static void CreateOneInstance()
 {
     var instance = new Instance("finalize_me");
     instance.Parameters.NoInformationEvent = true;
     instance.Parameters.Recovery = false;
     instance.Parameters.MaxTemporaryTables = 0;
     instance.Init();
 }
Esempio n. 23
0
        public void VerifyInstanceDoesNotCallJetTermWhenJetInitFails()
        {
            var mocks = new MockRepository();
            var mockApi = mocks.StrictMock<IJetApi>();
            using (new ApiTestHook(mockApi))
            {
                var jetInstance = new JET_INSTANCE { Value = (IntPtr) 0x1 };

                Expect.Call(
                    mockApi.JetCreateInstance2(
                        out Arg<JET_INSTANCE>.Out(jetInstance).Dummy,
                        Arg<string>.Is.Anything,
                        Arg<string>.Is.Anything,
                        Arg<CreateInstanceGrbit>.Is.Anything))
                    .Return((int) JET_err.Success);
                Expect.Call(
                    mockApi.JetInit2(
                        ref Arg<JET_INSTANCE>.Ref(Is.Equal(jetInstance), JET_INSTANCE.Nil).Dummy,
                        Arg<InitGrbit>.Is.Anything))
                    .Return((int) JET_err.OutOfMemory);
                mocks.ReplayAll();

                try
                {
                    using (var instance = new Instance("test"))
                    {
                        instance.Init();
                        Assert.Fail("Expected an EsentErrorException");
                    }
                }
                catch (EsentErrorException)
                {
                    // expected
                }

                mocks.VerifyAll();
            }
        }
Esempio n. 24
0
 public void JetInstanceThrowsExceptionWhenInstanceIsClosed()
 {
     var instance = new Instance("theinstance");
     instance.Parameters.NoInformationEvent = true;
     instance.Parameters.Recovery = false;
     instance.Parameters.MaxTemporaryTables = 0;
     instance.Init();
     instance.Term();
     JET_INSTANCE x = instance.JetInstance;
 }
Esempio n. 25
0
 public void InitThrowsExceptionWhenInstanceIsDisposed()
 {
     var instance = new Instance("theinstance");
     instance.Dispose();
     instance.Init();
 }
Esempio n. 26
0
        public void TestJetGetInstanceInfoWithUnicodePath()
        {
            if (!EsentVersion.SupportsUnicodePaths)
            {
                return;
            }

            const string InstanceName = "unicodegetinstanceinfo";
            using (var instance = new Instance(InstanceName))
            {
                // Don't turn off logging -- JetGetInstanceInfo only returns information for
                // databases that have logging on.
                instance.Parameters.LogFileSize = 384; // 384Kb
                instance.Parameters.NoInformationEvent = true;
                instance.Parameters.MaxTemporaryTables = 0;
                instance.Parameters.CreatePathIfNotExist = true;
                instance.Parameters.LogFileDirectory = this.directory;
                instance.Parameters.SystemDirectory = this.directory;
                instance.Init();
                using (var session = new Session(instance))
                {
                    JET_DBID dbid;
                    Api.JetCreateDatabase(session, this.database, String.Empty, out dbid, CreateDatabaseGrbit.None);
                    int numInstances;
                    JET_INSTANCE_INFO[] instances;
                    Api.JetGetInstanceInfo(out numInstances, out instances);

                    Assert.AreEqual(1, numInstances);
                    Assert.AreEqual(numInstances, instances.Length);
                    Assert.AreEqual(InstanceName, instances[0].szInstanceName);

                    Assert.AreEqual(1, instances[0].cDatabases);
                    Assert.AreEqual(instances[0].cDatabases, instances[0].szDatabaseFileName.Count);
                    Assert.AreEqual(Path.GetFullPath(this.database), instances[0].szDatabaseFileName[0]);
                }
            }
        }
Esempio n. 27
0
        public void GetDatabaseInfoOnXp()
        {
            string directory = SetupHelper.CreateRandomDirectory();
            string database = Path.Combine(directory, "test.db");

            using (var instance = new Instance("XPJetGetDatabaseInfo"))
            {
                SetupHelper.SetLightweightConfiguration(instance);
                instance.Init();
                using (var session = new Session(instance))
                {
                    JET_DBID dbid;
                    Api.JetCreateDatabase(session, database, String.Empty, out dbid, CreateDatabaseGrbit.None);

                    JET_DBINFOMISC dbinfomisc;
                    Api.JetGetDatabaseInfo(session, dbid, out dbinfomisc, JET_DbInfo.Misc);
                    Assert.AreEqual(SystemParameters.DatabasePageSize, dbinfomisc.cbPageSize);
                }
            }

            Cleanup.DeleteDirectoryWithRetry(directory);
        }
Esempio n. 28
0
        public void CreateTableColumnIndex3OnXp()
        {
            var columncreates = new JET_COLUMNCREATE[]
            {
                new JET_COLUMNCREATE()
                {
                    szColumnName = "col1_short",
                    coltyp = JET_coltyp.Short,
                    cbMax = 2,
                },
                new JET_COLUMNCREATE()
                {
                    szColumnName = "col2_longtext",
                    coltyp = JET_coltyp.LongText,
                    cp = JET_CP.Unicode,
                },
            };

            const string Index1Name = "firstIndex";
            const string Index1Description = "+col1_short\0-col2_longtext\0";

            const string Index2Name = "secondIndex";
            const string Index2Description = "+col2_longtext\0-col1_short\0";

            var indexcreates = new JET_INDEXCREATE[]
            {
                  new JET_INDEXCREATE
                {
                    szIndexName = Index1Name,
                    szKey = Index1Description,
                    cbKey = Index1Description.Length + 1,
                    grbit = CreateIndexGrbit.None,
                    ulDensity = 99,
                },
                new JET_INDEXCREATE
                {
                    szIndexName = Index2Name,
                    szKey = Index2Description,
                    cbKey = Index2Description.Length + 1,
                    grbit = CreateIndexGrbit.None,
                    ulDensity = 79,
                },
            };

            var tablecreate = new JET_TABLECREATE()
            {
                szTableName = "tableBigBang",
                ulPages = 23,
                ulDensity = 75,
                cColumns = columncreates.Length,
                rgcolumncreate = columncreates,
                rgindexcreate = indexcreates,
                cIndexes = indexcreates.Length,
                cbSeparateLV = 100,
                cbtyp = JET_cbtyp.Null,
                grbit = CreateTableColumnIndexGrbit.None,
            };

            string directory = SetupHelper.CreateRandomDirectory();
            string database = Path.Combine(directory, "test.db");

            using (var instance = new Instance("XpCreateTableColumnIndex3"))
            {
                instance.Parameters.Recovery = false;
                instance.Parameters.NoInformationEvent = true;
                instance.Parameters.MaxTemporaryTables = 0;
                instance.Init();
                using (var session = new Session(instance))
                {
                    JET_DBID dbid;
                    Api.JetCreateDatabase(session, database, String.Empty, out dbid, CreateDatabaseGrbit.None);
                    using (var transaction = new Transaction(session))
                    {
                        Api.JetCreateTableColumnIndex3(session, dbid, tablecreate);
                        Assert.AreNotEqual(JET_TABLEID.Nil, tablecreate.tableid);

                        // 1 table, 2 columns, 2 indices = 5 objects.
                        Assert.AreEqual(tablecreate.cCreated, 5);
                        Assert.AreNotEqual(tablecreate.rgcolumncreate[0].columnid, JET_COLUMNID.Nil);
                        Assert.AreNotEqual(tablecreate.rgcolumncreate[1].columnid, JET_COLUMNID.Nil);

                        Api.JetCloseTable(session, tablecreate.tableid);
                        transaction.Commit(CommitTransactionGrbit.LazyFlush);
                    }
                }
            }
        }
Esempio n. 29
0
        private void EnsureDatabaseIsCreatedAndAttachToDatabase()
        {
            using (var session = new Session(instance))
            {
                try
                {
                    Api.JetAttachDatabase(session, database, AttachDatabaseGrbit.None);
                    return;
                }
                catch (EsentErrorException e)
                {
                    if (e.Error == JET_err.DatabaseDirtyShutdown)
                    {
                        try
                        {
                            using (var recoverInstance = new Instance("Recovery instance for: " + database))
                            {
                                recoverInstance.Init();
                                using (var recoverSession = new Session(recoverInstance))
                                {
                                    ConfigureInstance(recoverInstance.JetInstance);
                                    Api.JetAttachDatabase(recoverSession, database,
                                                          AttachDatabaseGrbit.DeleteCorruptIndexes);
                                    Api.JetDetachDatabase(recoverSession, database);
                                }
                            }
                        }
                        catch (Exception)
                        {
                        }

                        Api.JetAttachDatabase(session, database, AttachDatabaseGrbit.None);
                        return;
                    }
                    if (e.Error != JET_err.FileNotFound)
                        throw;
                }

                new SchemaCreator(session).Create(database);
                Api.JetAttachDatabase(session, database, AttachDatabaseGrbit.None);
            }
        }
Esempio n. 30
0
        private bool EnsureDatabaseIsCreatedAndAttachToDatabase()
        {

            int maxSize = 0;
            try
            {
                string value;
                if (ValidateLicense.CurrentLicense.Attributes.TryGetValue("maxSizeInMb", out value))
                {
                    if (value != "unlimited")
                    {
                        maxSize = (int)((long.Parse(value) * 1024 * 1024) / SystemParameters.DatabasePageSize);
                    }
                }
                using (var session = new Session(instance))
                {
                    Api.JetAttachDatabase2(session, database, maxSize, AttachDatabaseGrbit.None);
                }
                return false;
            }
            catch (EsentErrorException e)
            {
                switch (e.Error)
                {
                    case JET_err.SecondaryIndexCorrupted:
                        Output("Secondary Index Corrupted detected, attempting to compact...");
                        Api.JetTerm2(instance, TermGrbit.Complete);
                        Compact(configuration, (sesid, snp, snt, data) =>
                        {
                            Output(string.Format("{0}, {1}, {2}, {3}", sesid, snp, snt, data));
                            return JET_err.Success;
                        });
                        CreateInstance(out instance, uniquePrefix + "-" + database);
                        Api.JetInit(ref instance);
                        using (var session = new Session(instance))
                        {
                            Api.JetAttachDatabase2(session, database, maxSize, AttachDatabaseGrbit.None);
                        }
                        return false;
                    case JET_err.DatabaseDirtyShutdown:
                        try
                        {
                            Api.JetTerm2(instance, TermGrbit.Complete);
                            using (var recoverInstance = new Instance("Recovery instance for: " + database))
                            {
                                new TransactionalStorageConfigurator(configuration, this).ConfigureInstance(recoverInstance.JetInstance, path);
                                recoverInstance.Init();
                                using (var recoverSession = new Session(recoverInstance))
                                {
                                    Api.JetAttachDatabase(recoverSession, database,
                                                          AttachDatabaseGrbit.DeleteCorruptIndexes);
                                    Api.JetDetachDatabase(recoverSession, database);
                                }
                            }
                        }
                        catch (Exception e2)
                        {
                            log.WarnException("Could not recover database " + database + ", will try opening it one last time. If that doesn't work, try using esentutl", e2);
                        }
                        CreateInstance(out instance, uniquePrefix + "-" + database);
                        Api.JetInit(ref instance);
                        using (var session = new Session(instance))
                        {
                            Api.JetAttachDatabase2(session, database, maxSize, AttachDatabaseGrbit.None);
                        }
                        return false;
                }
                if (e.Error != JET_err.FileNotFound)
                    throw;
            }

            using (var session = new Session(instance))
            {
                new SchemaCreator(session).Create(database);
                Api.JetAttachDatabase2(session, database, maxSize, AttachDatabaseGrbit.None);
                return true;
            }
        }