Exemplo n.º 1
0
        public void TestContentFreeName()
        {
            string name = "    ";
            SqlDataSchemaAdapter adapter = new SqlDataSchemaAdapter(Host);

            Assert.Throws <ArgumentException>(() => adapter.GetCreateScript(name, false));
        }
Exemplo n.º 2
0
        public void CreateFromOntology()
        {
            Helper.Print("\n---\n--- Starting CreateFromOntology\n---\n");

            DataSchemaAdapter creator = new SqlDataSchemaAdapter(this.Host);
            DataSet           dataSet = new DataSet(this.Catalog);

            string xsdFile = ConfigurationManager.AppSettings["Test.ObjectModelXsd"];

            if (xsdFile == null || xsdFile.Length == 0)
            {
                xsdFile = Path.Combine(TestContext.CurrentContext.TestDirectory, @"Test\ObjectModel.xsd");
            }

            dataSet.ReadXmlSchema(xsdFile);
            dataSet.DataSetName = this.Catalog;

            creator.Create(dataSet, true);

            //
            // Make sure that it has been created
            //
            using (IDbConnection connection = Connection())
            {
                connection.Open();
                using (IDbCommand command = connection.CreateCommand())
                {
                    command.CommandText = String.Format("select count(*) from master.dbo.sysdatabases WHERE name = N'{0}'", this.Catalog);
                    Assertion.AssertEquals("Wrong number of databases found", 1, (int)command.ExecuteScalar());
                }
            }
        }
Exemplo n.º 3
0
        public void TestNullDataSet()
        {
            DataSet ds = null;
            SqlDataSchemaAdapter adapter = new SqlDataSchemaAdapter(Host);

            Assert.Throws <ArgumentException>(() => adapter.GetSchemaScript(ds));
        }
Exemplo n.º 4
0
        public void TestFileNotFound()
        {
            Settings.Connection = new SqlConnection(Settings.ConnectionString);
            Settings.Connection.Open();

            SqlDataSchemaAdapter adapter = new SqlDataSchemaAdapter(Settings.Connection);

            adapter.CreateDatabase("file_not_found.xsd");
        }
Exemplo n.º 5
0
        public void TestNoColumns1()
        {
            DataSet ds = new DataSet("TestNoColumns");

            ds.Tables.Add("empty_table");

            SqlDataSchemaAdapter adapter = new SqlDataSchemaAdapter(Host);

            Assert.Throws <ArgumentException>(() => adapter.GetSchemaScript(ds));
        }
Exemplo n.º 6
0
        public void TestCreate()
        {
            SqlConnection tempConn = null;

            try
            {
                // Create a listener, which outputs to the console screen, and
                // add it to the trace listeners.
                // Trace.Listeners.Add( new TextWriterTraceListener(System.Console.Out) );

                /*
                 * Check to see if we have a connection to the database
                 */
                SqlDataSchemaAdapter adapter = new SqlDataSchemaAdapter(Settings.Connection);

                adapter.CreateDatabase("test.xsd");

                /*
                 * Assure hte database exists
                 */
                tempConn = new SqlConnection(Settings.ConnectionString + ";Initial Catalog=Test-Database");

                tempConn.Open();
                SqlCommand cmd    = new SqlCommand("SELECT count(name) FROM sysobjects where name = 'TestTableOrders'", tempConn);
                int        orders = (int)cmd.ExecuteScalar();
                cmd = new SqlCommand("SELECT count(name)  FROM sysobjects where name = 'TestTable-Customers'", tempConn);
                int customers = (int)cmd.ExecuteScalar();
                tempConn.Close();

                Assertion.Assert("Table 'TestTableOrders' wasn't created", orders == 1);
                Assertion.Assert("Table 'TestTable-Customers' wasn't created", customers == 1);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
                throw;
            }
            finally
            {
                if (tempConn != null)
                {
                    tempConn.Close();
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="force">should the database be overwritten</param>
        public void CreateEmptyDB(bool force)
        {
            Helper.Print("\n---\n--- Starting CreateEmptyDB\n---\n");

            DataSchemaAdapter creator = new SqlDataSchemaAdapter(Host);
            DataSet           dataSet = new DataSet(this.Catalog);

            creator.Create(dataSet, force);

            //
            // Make sure that it has been created
            //
            using (IDbConnection connection = Connection())
            {
                connection.Open();
                using (IDbCommand command = connection.CreateCommand())
                {
                    command.CommandText = String.Format("select count(*) from master.dbo.sysdatabases WHERE name = N'{0}'", this.Catalog);
                    Assertion.AssertEquals("Wrong number of databases found", 1, (int)command.ExecuteScalar());
                }
            }
        }
Exemplo n.º 8
0
        public void TestNoColumns2()
        {
            string name = "TestNoColumns2";
            string file = Path.GetFullPath(Path.ChangeExtension(name, ".xsd"));

            try
            {
                DataSet ds = new DataSet(name);
                ds.Tables.Add("empty_table_1");
                ds.Tables.Add("empty_table_2");
                ds.WriteXmlSchema(file);
                ds.Dispose();
                ds = new DataSet();
                ds.ReadXmlSchema(file);

                SqlDataSchemaAdapter adapter = new SqlDataSchemaAdapter(Host);
                Assert.Throws <ArgumentException>(() => adapter.GetSchemaScript(ds));
            }
            finally
            {
                File.Delete(file);
            }
        }
Exemplo n.º 9
0
        public void TestNoColumns()
        {
            //
            // Check to see if we have a connection to the database
            //
            Settings.Connection.Open();
            SqlDataSchemaAdapter adapter = new SqlDataSchemaAdapter(Settings.Connection);

            //
            // Just making sure that the server doesn't already have a Test-Database
            //
            new SqlCommand("IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'Test-Database') DROP DATABASE [Test-Database]",
                           Settings.Connection)
            .ExecuteNonQuery();

            DataSet   ds = new DataSet("Test-Database");
            DataTable t  = ds.Tables.Add();
            DataTable t2 = ds.Tables.Add();

            ds.WriteXmlSchema("test.xsd");

            adapter.CreateDatabase("test.xsd");
        }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            PrintCopyright();

            if (args.Length == 0)
            {
                PrintHelp();
                return;
            }

            bool bHelp = ParseHelp(args);

            if (bHelp)
            {
                PrintHelp();
                return;
            }

            bool   bDropDB = ParseDropDB(args);
            string xsdFile = ParseXsd(args);

            try
            {
                int    type             = ParseType(args);
                string connectionString = ParseConnectionString(args);

                switch (type)
                {
                case 1:
                    if (bDropDB)
                    {
                        try
                        {
                            Console.Write("Dropping database ");
                            SqlConnection conn = new SqlConnection(connectionString);
                            conn.Open();

                            DataSet ds = new DataSet();
                            ds.ReadXmlSchema(xsdFile);

                            Console.Write(ds.DataSetName + "...");
                            new SqlCommand("DROP DATABASE " + ds.DataSetName, conn).ExecuteNonQuery();
                            conn.Close();
                            Console.WriteLine("done!");
                        }
                        catch (SqlException ex)
                        {
                            Console.WriteLine(ex.Message);
                            Console.WriteLine("Continuing...");
                        }
                    }

                    Console.Write("Creating new database...");
                    SqlDataSchemaAdapter a = new SqlDataSchemaAdapter(connectionString);
                    a.CreateDatabase(xsdFile);

                    Console.WriteLine("done!");
                    break;

                default:
                    Console.WriteLine("Database type is not specified or not supported", type);
                    return;
                }
            }
            catch (FileNotFoundException ex)
            {
                /// Schema file not found
                Console.WriteLine(ex.Message);
                return;
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                /// Database already exists
                Console.WriteLine(ex.Message);
                return;
            }
        }
Exemplo n.º 11
0
        public void TestInvalidConnection()
        {
            Settings.Connection = new SqlConnection(Settings.ConnectionString);

            SqlDataSchemaAdapter adapter = new SqlDataSchemaAdapter(Settings.Connection);
        }
Exemplo n.º 12
0
        public void TestNullConnection()
        {
            Settings.Connection = null;

            SqlDataSchemaAdapter adapter = new SqlDataSchemaAdapter(Settings.Connection);
        }
Exemplo n.º 13
0
        public void TestRelations()
        {
            //
            // Setup a simple foreign key relation
            //
            DataSet    ds     = new DataSet("Test-Database");
            DataTable  master = ds.Tables.Add("Master");
            DataColumn mid    = master.Columns.Add("MasterID", Type.GetType("System.Guid"));

            DataColumn[] keys = new DataColumn[1];
            keys[0]           = mid;
            master.PrimaryKey = keys;
            DataColumn uniqueCol = master.Columns.Add("UniqueText", Type.GetType("System.String"));

            uniqueCol.Unique      = true;
            uniqueCol.AllowDBNull = true;
            uniqueCol.MaxLength   = 80;

            DataTable  detail = ds.Tables.Add("Detail");
            DataColumn did    = detail.Columns.Add("DetailID", Type.GetType("System.Guid"));
            DataColumn mid_fk = detail.Columns.Add("MasterID_FK", Type.GetType("System.Guid"));

            DataColumn[] keys2 = new DataColumn[1];
            keys2[0]          = did;
            detail.PrimaryKey = keys2;

            ds.Relations.Add(mid, mid_fk);

            //
            // Write out the XSD
            //
            ds.WriteXmlSchema("test.xsd");

            //
            // Create a database
            //
            SqlDataSchemaAdapter adapter = new SqlDataSchemaAdapter(Settings.Connection);

            adapter.CreateDatabase("test.xsd");

            //
            //  Check to see if SqlDataSchemaAdapter have created primary relations
            //
            SqlCommand insertCommand = new SqlCommand("USE [Test-Database] INSERT Master (MasterID) VALUES ('{37477C57-447D-4077-B96D-FE49D79045AA}')",
                                                      Settings.Connection);

            int i = insertCommand.ExecuteNonQuery();

            Assertion.Assert("Insert query failed. Expect 1 row affected", i == 1);

            // Try executing the same insert once again
            bool raisedSqlException = false;

            try
            {
                insertCommand.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                Assertion.Assert("Primary key constraint failed. Incompatible SqlException '" + ex.Message + "'", ex.Number == 2627);
                raisedSqlException = true;
            }
            Assertion.Assert("Primary key constraint failed. Expected SqlException", raisedSqlException);

            //
            //  Check to see if SqlDataSchemaAdapter have created the foreign key relations
            //
            insertCommand = new SqlCommand("USE [Test-Database] INSERT Detail (DetailID, MasterID_FK) VALUES ('{37477C58-447D-4077-B96D-FE49D79045AA}', '{37477C57-447D-4077-B96D-FE49D79045AA}')",
                                           Settings.Connection);

            i = insertCommand.ExecuteNonQuery();
            Assertion.Assert("Insert query failed. Expect 1 row affected", i == 1);

            // Try executing the same insert once again
            raisedSqlException = false;
            try
            {
                insertCommand.CommandText = "USE [Test-Database] INSERT Detail (DetailID, MasterID_FK) VALUES ('{37477C59-447D-4077-B96D-FE49D79045AA}', '{DEADBEEF-447D-4077-B96D-FE49D79045AA}')";
                insertCommand.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                Assertion.Assert("Primary key constraint failed. Incompatible SqlException '" + ex.Message + "'", ex.Number == 547);
                raisedSqlException = true;
            }
            Assertion.Assert("Foreign key constraint failed. Expected SqlException", raisedSqlException);
        }