示例#1
0
        public void VerifyConstraintsForeignKey()
        {
            List <ConstraintInfo> constraininfo = mssql.getConstraintsFromTable("EmployeeDetails");

            Assert.AreEqual(constraininfo[0].constraintType, ConstraintType.ForeignKey);
            Assert.AreEqual(constraininfo[0].columnName, "EmpID");
            Assert.AreEqual(constraininfo[0].FKcolumnName, "EmpID");
            Assert.AreEqual(constraininfo[0].FKtableName, "Employee");
            Assert.AreEqual(constraininfo[0].constraintName, "FK_EmployeeDetails_Employee");
            Assert.AreEqual(constraininfo[0].Condition, "");
        }
示例#2
0
        public void migrateTables(OracleSQLConnection os, MSSQLConnection ms, CUBRIDConnection cs)
        {
            String cols = "";

            if (ms == null)
            {
                tables = os.GetAllTables();
            }
            else
            {
                tables = ms.getTables();
            }

            for (int i = 0; i < tables.Count; i++)
            {
                cols = "";
                //MessageBox.Show(cols);
                if (ms == null)
                {
                    infos = os.getInfo(tables[i]);
                }
                else
                {
                    infos = ms.getInfo(tables[i]);
                }


                pks = "";

                for (int j = 0; j < infos.Count; j++)
                {
                    // MessageBox.Show(cols);
                    String name = infos[j].columnname;
                    String type = infos[j].datatype + "";
                    if (type == DataType.NUMBER + "")
                    {
                        type = "numeric";
                    }

                    Boolean nullable = infos[j].nullable;
                    Boolean pk       = infos[j].isPrimaryKey;


                    if (pk)
                    {
                        pks  += "[" + name + "],";
                        cols += "[" + name + "] " + type + ",";
                    }
                    else
                    {
                        if (!nullable)
                        {
                            cols += "[" + name + "] " + type + " not null " + ",";
                        }
                        else
                        {
                            cols += "[" + name + "] " + type + ",";
                        }
                    }

                    //  MessageBox.Show(cols);
                }


                if (pks.Length == 0)
                {
                    insert = "Create Table [" + tables[i] + "]( " + cols.Substring(0, cols.Length - 1) +
                             ");\n";
                }

                if (pks.Length > 0)
                {
                    insert = "Create Table [" + tables[i] + "]( " + cols + "Primary key (" +
                             pks.Substring(0, pks.Length - 1) + ")" + ");\n";
                }

                try
                {
                    //   MessageBox.Show(insert);
                    CUBRIDCommand cmd = new CUBRIDCommand(insert, cs);
                    cmd.ExecuteNonQuery();
                    gettableData(tables[i] + "", ms, os, cs);
                }
                catch (Exception e)
                {
                    if ((e + "").Contains("OBJECT"))
                    {
                        int    index      = insert.IndexOf("[");
                        String substring1 = insert.Substring(index + 1);
                        String substring2 = insert.Substring(0, 14);
                        insert = substring2 + "_" + substring1;
                        CUBRIDCommand cmd = new CUBRIDCommand(insert, cs);
                        cmd.ExecuteNonQuery();
                    }
                    else
                    {
                        MessageBox.Show(e + "");
                    }

                    gettableData(tables[i] + "", ms, os, cs);
                }


                pks  = "";
                cols = "";
            }

            for (int i = 0; i < tables.Count; i++)
            {
                if (ms == null)
                {
                    constraints = os.getConstraintsFromTable(tables[i]);
                }
                else
                {
                    constraints = ms.getConstraintsFromTable(tables[i]);
                    // MessageBox.Show(tables[i] + " - " + constraints.Count);
                }

                for (int j = 0; j < constraints.Count; j++)
                {
                    if (!tables[i].ToLower().Equals("object") && !constraints[j].FKtableName.ToLower().Equals("object"))
                    {
                        if ((constraints[j].constraintType + "").Equals("ForeignKey"))
                        {
                            String stat = "Alter table [" + tables[i] + "] add foreign key(" +
                                          constraints[j].columnName +
                                          ")" +
                                          " references [" + constraints[j].FKtableName + "](" +
                                          constraints[j].FKcolumnName + ");\n";
                            altertablestatement += stat;
                        }
                        else if ((constraints[j].constraintType + "").Contains("Unique"))
                        {
                            if (os != null)
                            {
                            }
                            else
                            {
                                String stat = "Alter table [" + tables[i] + "] add unique(" +
                                              constraints[j].columnName +
                                              ");";
                                //    altertablestatement += stat;
                            }
                        }
                        else
                        {
                            String stat = "Alter table [" + tables[i] + "] add constraint " +
                                          constraints[j].constraintName + " check (" + constraints[j].Condition +
                                          ");";
                            altertablestatement += stat;
                            // MessageBox.Show(altertablestatement);
                        }
                    }
                }
            }

//addViews(ms,os,cs);
            //  MessageBox.Show(insert);
            //  MessageBox.Show(altertablestatement);
            try
            {
                // MessageBox.Show(altertablestatement);
                CUBRIDCommand cmd2 = new CUBRIDCommand(altertablestatement, cs);
                cmd2.ExecuteNonQuery();
                addViews(ms, os, cs);
                addSeq(ms, os, cs);
            }
            catch (Exception e)
            {
                if (e.Message.Contains("already defined"))
                {
                    MessageBox.Show(e.Message);
                    altertablestatement = "";
                }
                else
                {
                    MessageBox.Show(e + "");
                }
            }
        }