示例#1
0
        /// <summary>
        /// Creates a new database with the given name and with initial
        /// configurations.
        /// </summary>
        /// <param name="config">The configurations specific for the dataabse
        /// to create. These will be merged with existing context configurations.</param>
        /// <param name="name">The name of the database to create.</param>
        /// <param name="adminUser">The name of the administrator for the database,</param>
        /// <param name="adminPass">The password used to identify the administrator.</param>
        /// <returns>
        /// Returns a <see cref="DbSystem"/> instance used to access the database created.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// If the <paramref name="name"/> of the database is <b>null</b>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// If a database with the given <paramref name="name"/> already exists.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// If an error occurred while initializing the database.
        /// </exception>
        public DbSystem CreateDatabase(IDbConfig config, string name, string adminUser, string adminPass)
        {
            if (name == null)
                throw new ArgumentNullException("name");

            if (config == null)
                config = DbConfig.Default;

            if (DatabaseExists(config, name))
                throw new ArgumentException("A database '" + name + "' already exists.");

            config.Parent = Config;

            StorageType storageType = GetStorageType(config);

            if (storageType == StorageType.File) {
                // we ensure that the BasePath points to where we want it to point
                string path = Path.Combine(config.BasePath(), name);
                if (Directory.Exists(path))
                    throw new ApplicationException("Database path '" + name + "' already exists: try opening");

                Directory.CreateDirectory(path);

                config.SetValue(ConfigKeys.DatabasePath, name);

                string configFile = Path.Combine(path, DefaultConfigFileName);
                //TODO: support multiple formats?
                config.Save(configFile);
            }

            IDatabase database = CreateDatabase(config, name);

            try {
                database.Create(adminUser, adminPass);
                database.Init();

                var callback = new DatabaseShutdownCallback(this, database);
                database.Context.OnShutdown += (callback.Execute);
            } catch (Exception e) {
                database.Context.Logger.Error(this, "Database create failed");
                database.Context.Logger.Error(this, e);
                throw new InvalidOperationException(e.Message, e);
            }
            // Return the DbSystem object for the newly created database.
            databases[name] = database;
            return new DbSystem(this, name, config, database);
        }