/// <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); }
/// <summary> /// Creates a new instance of <see cref="DbController"/> to the /// given path on the underlying filesystem. /// </summary> /// <param name="config">The initial configurations that will /// be applied to the subsequent databases within the context.</param> /// <remarks> /// If the given path doesn't point to any valid database context /// this will generate it by creating a configuration file which /// will encapsulate all the default configurations and those /// provided in this method. /// </remarks> /// <returns> /// Returns an instance of <see cref="DbController"/> which points /// to the given path. /// </returns> public static DbController Create(IDbConfig config) { StorageType storageType = GetStorageType(config); if (config == null) config = DbConfig.Default; IDbConfig mainConfig; if (storageType == StorageType.File) { string path = config.BasePath() ?? Environment.CurrentDirectory; string configFile = Path.GetFileName(path); // we only allow the file extension .conf string ext = Path.GetExtension(configFile); if (String.Compare(ext, FileExtension, StringComparison.OrdinalIgnoreCase) == 0) { path = Path.GetDirectoryName(path); } else { configFile = DefaultConfigFileName; } // if the directory doesn't exist we will create one... if (path != null && !Directory.Exists(path)) Directory.CreateDirectory(path); mainConfig = GetConfig(config, path, configFile); mainConfig.BasePath(path); } else { mainConfig = config; } var controller = new DbController(mainConfig); if (storageType == StorageType.File) { // done with the main configuration... now look for the databases... string path = config.BasePath(); string[] subDirs = Directory.GetDirectories(path); foreach (string dir in subDirs) { IDbConfig dbConfig = GetConfig(mainConfig, dir, null); if (dbConfig == null) continue; var dbPath = dir.Substring(dir.LastIndexOf(Path.DirectorySeparatorChar) + 1); string name = dbConfig.DatabaseName(); if (name == null) name = dbPath; dbConfig.DatabasePath(dbPath); if (controller.IsDatabaseRegistered(name)) throw new InvalidOperationException("The database '" + name + "' was already registered."); IDatabase database = CreateDatabase(dbConfig, name); controller.databases[name] = database; if (database.Exists) { var callback = new DatabaseShutdownCallback(controller, database); database.Context.OnShutdown += (callback.Execute); } } } return controller; }