public static void OpenDatabase( PSCmdletBase cmdlet, string fileName, bool structureDB, bool repositoryDB, bool resultsDB) { // check input try { string absolutePath = System.IO.Path.GetFullPath(fileName); cmdlet.WriteVerbose(cmdlet, absolutePath); if (System.IO.File.Exists(absolutePath)) { string connectionString = "Data Source='" + absolutePath + "';Version=3;Max Pool Size=100;UseUTF16Encoding=True;"; cmdlet.WriteVerbose(cmdlet, connectionString); using (SQLiteConnection conn = new SQLiteConnection(connectionString)) { conn.Open(); IDatabase database = new Database( ((DatabaseFileCmdletBase)cmdlet).Name, fileName, conn); // check structure DB // check repository DB // check data DB conn.Close(); if (structureDB) { TestData.CurrentStructureDB = database; } if (repositoryDB) { TestData.CurrentRepositoryDB = database; } if (resultsDB) { TestData.CurrentResultsDB = database; } SQLiteData.Databases.Add(database); cmdlet.WriteObject(cmdlet, database); } } } catch (Exception eOpenDB) { cmdlet.WriteError( cmdlet, "Unable to open the database. " + eOpenDB.Message, "OpenDBFailed", ErrorCategory.InvalidOperation, true); } }
public static void CreateDatabase( //PSCmdletBase cmdlet, //CommonCmdletBase cmdlet, DatabaseFileCmdletBase cmdlet, string fileName, bool structureDB, bool repositoryDB, bool resultsDB) { // check input try { string absolutePath = System.IO.Path.GetFullPath(fileName); cmdlet.WriteVerbose(cmdlet, absolutePath); SQLiteConnection.CreateFile(absolutePath); if (System.IO.File.Exists(absolutePath)) { string connectionString = "Data Source='" + absolutePath + "';Version=3;Max Pool Size=100;UseUTF16Encoding=True;"; cmdlet.WriteVerbose(cmdlet, connectionString); using (SQLiteConnection conn = new SQLiteConnection(connectionString)) { conn.Open(); // 20120918 if (null == cmdlet.Name || 0 == cmdlet.Name.Length) { cmdlet.Name = fileName; } IDatabase database = new Database( ((DatabaseFileCmdletBase)cmdlet).Name, absolutePath, conn); // create tables if (repositoryDB) { runSQLCommand( cmdlet, database, "CREATE TABLE TestBuckets (BucketName TEXT, Id INTEGER PRIMARY KEY, BucketTag TEXT, Description TEXT);"); runSQLCommand( cmdlet, database, "CREATE TABLE TestConstants (Id INTEGER PRIMARY KEY, ConstantName TEXT, ConstantType TEXT, ConstantValue BLOB, ConstantTag TEXT, Description TEXT, BucketId NUMERIC);"); } if (structureDB) { runSQLCommand( cmdlet, database, "CREATE TABLE TestCases (AfterCode TEXT, BeforeCode TEXT, MainCode TEXT, Id INTEGER PRIMARY KEY, TestCaseName TEXT, TestCaseNumber TEXT, TestCaseTag TEXT, Description TEXT);"); } if (resultsDB || structureDB) { runSQLCommand( cmdlet, database, "CREATE TABLE TestSuites (Id INTEGER PRIMARY KEY, SuiteId TEXT, SuiteName TEXT, StatusId NUMERIC, Description TEXT);"); runSQLCommand( cmdlet, database, "CREATE TABLE TestScenarios (Id INTEGER PRIMARY KEY, SuiteId TEXT, ScenarioId TEXT, ScenarioName TEXT, StatusId NUMERIC, Description TEXT);"); } if (resultsDB) { runSQLCommand( cmdlet, database, "CREATE TABLE TestResults (Id INTEGER PRIMARY KEY, TestResultId TEXT, TestResultName TEXT, " + "StatusId NUMERIC, ScriptName TEXT, LineNumber NUMERIC, " + //"Position NUMERIC, Error BLOB, Code TEXT, Description TEXT, Parameters BLOB, " + "Position NUMERIC, Error TEXT, Code TEXT, Description TEXT, Parameters BLOB, " + //"SuiteId TEXT, ScenarioId TEXT, Timestamp TEXT, TimeSpent NUMERIC, Generated NUMERIC, Screenshot TEXT);"); "SuiteId TEXT, ScenarioId TEXT, Timestamp TEXT, TimeSpent NUMERIC, Generated NUMERIC, Screenshot BLOB);"); runSQLCommand( cmdlet, database, "CREATE TABLE TestResultDetails (Id INTEGER PRIMARY KEY, TestResultId TEXT, TestResultDetailName TEXT, " + //"TestResultDetail BLOB, Timestamp TEXT);"); "TestResultDetail TEXT, Timestamp TEXT);"); } if (structureDB) { runSQLCommand( cmdlet, database, "CREATE INDEX TestCaseName ON TestCases(TestCaseName ASC);"); runSQLCommand( cmdlet, database, "CREATE INDEX TestCaseNumber ON TestCases(TestCaseNumber ASC);"); } if (repositoryDB) { runSQLCommand( cmdlet, database, "CREATE INDEX TestConstantName ON TestConstants(ConstantName ASC);"); } if (resultsDB) { runSQLCommand( cmdlet, database, "CREATE INDEX TestResultId ON TestResults(TestResultId ASC);"); runSQLCommand( cmdlet, database, "CREATE INDEX TestResultName ON TestResults(TestResultName ASC);"); runSQLCommand( cmdlet, database, "CREATE INDEX TestResultDetailName ON TestResultDetails(TestResultDetailName ASC);"); } cmdlet.WriteVerbose(cmdlet, "closing the connection"); conn.Close(); if (structureDB) { cmdlet.WriteVerbose(cmdlet, "setting the current structure DB"); database.IsStructureDB = true; TestData.CurrentStructureDB = database; } if (repositoryDB) { cmdlet.WriteVerbose(cmdlet, "setting the current repository DB"); database.IsRepositoryDB = true; TestData.CurrentRepositoryDB = database; } if (resultsDB) { cmdlet.WriteVerbose(cmdlet, "setting the current results DB"); database.IsResultsDB = true; TestData.CurrentResultsDB = database; } cmdlet.WriteVerbose(cmdlet, "adding the database to the colleciton"); SQLiteData.Databases.Add(database); cmdlet.WriteVerbose(cmdlet, "outputting the database"); cmdlet.WriteObject(cmdlet, database); } } } catch (Exception eCreateDB) { cmdlet.WriteError( cmdlet, "Unable to create a database with structure. " + eCreateDB.Message, "CreateDBFailed", ErrorCategory.InvalidOperation, true); } // create structure // http://sqlite.phxsoftware.com/forums/p/134/465.aspx#465 // http://sqlite.phxsoftware.com/forums/t/76.aspx }