Exemplo n.º 1
0
        private void tbxFKTable_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            tbxFKCol.Items.Clear();
            tbxFKColTitle.Items.Clear();
            if (sender == null)
            {
                return;
            }
            ComboBox cb = sender as ComboBox;

            if (cb.SelectedValue == null)
            {
                return;
            }
            ZippyCoder.Entity.Table table = _Project.FindTable(cb.SelectedValue.ToString());
            if (table != null)
            {
                foreach (ZippyCoder.Entity.Col col in table.Cols)
                {
                    tbxFKCol.Items.Add(col.Name);
                    tbxFKColTitle.Items.Add(col.Name);
                }
                tbxFKCol.SelectedValue = _Col.RefCol;
            }
            //MessageBox.Show(cb.SelectedValue.ToString());
        }
Exemplo n.º 2
0
        public void UpdateColNode(ZippyCoder.Entity.Col col)
        {
            _Modified = true;
            ChangeTitle(false);
            if (_CurrentTreeViewItem.Tag is ZippyCoder.Entity.Col)
            {
                _CurrentTreeViewItem.Tag    = col;
                _CurrentTreeViewItem.Header = col.Title + "[" + col.Name + "]";
            }
            else if (_CurrentTreeViewItem.Tag is ZippyCoder.Entity.Table)
            {
                ZippyCoder.Entity.Table table = _CurrentTreeViewItem.Tag as ZippyCoder.Entity.Table;
                //int insPos = table.Cols.Count;
                //if (insPos >= 9)   //懒人列
                //{
                //    insPos = insPos - 8;
                //}
                //table.Cols.Insert(insPos, col);
                table.Cols.Add(col);

                TreeViewItem tviCol = new TreeViewItem();
                _CurrentTreeViewItem.Items.Add(tviCol);
                //_CurrentTreeViewItem.Items.Insert(insPos, tviCol);

                _CurrentTreeViewItem.IsExpanded = true;
                tviCol.Tag    = col;
                tviCol.Header = col.Title + "[" + col.Name + "]";
            }
        }
Exemplo n.º 3
0
        private void lvTable_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox lbi = e.Source as ListBox;

            currentTable = lbi.SelectedItem as ZippyCoder.Entity.Table;
            if (currentTable == null)
            {
                return;
            }
            lvCol.ItemsSource = currentTable.Cols;
        }
Exemplo n.º 4
0
 private void BindFKCol(ComboBox cbx, string tableName, string colName)
 {
     ZippyCoder.Entity.Table table = _Project.FindTable(tableName);
     if (table != null)
     {
         foreach (ZippyCoder.Entity.Col col in table.Cols)
         {
             cbx.Items.Add(col.Name);
         }
         cbx.SelectedValue = colName;
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// 生成代码
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="ttFile">tt模板路径</param>
        /// <param name="outputFileName">生成的文件的名称</param>
        /// <param name="fileNamePattern">需要替换的文件名</param>
        /// <param name="fileNamesDel">需要去掉的文件名前缀</param>
        /// <param name="outputPath">输出路径</param>
        /// <param name="nameSpace">命名空间</param>
        public static void CreateCode(ZippyCoder.Entity.Project project, ZippyCoder.Entity.Table table, string ttFile, string outputFileName, string fileNamePattern, string fileNamesDel, string outputPath, bool multiDir)
        {
            if (T4Errors != null)
            {
                T4Errors.Clear();
            }
            ZippyT4Host host = new ZippyT4Host();

            host.Project = project;
            host.Table   = table;

            Engine engine = new Engine();

            host.TemplateFile = ttFile;


            string input  = File.ReadAllText(ttFile);
            string output = engine.ProcessTemplate(input, host);

            string[] fileNameDels = fileNamesDel.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string fileNameDel in fileNameDels)
            {
                if (outputFileName.StartsWith(fileNameDel))
                {
                    outputFileName = outputFileName.Remove(0, fileNameDel.Length);
                }
            }
            //大写文件名,java 会敏感,与类对应。
            outputFileName = ZippyCoder.Helper.ToJavaClassName(outputFileName);

            if (multiDir)
            {
                if (table != null)
                {
                    outputPath = Path.Combine(outputPath, table.Name);
                    if (!System.IO.Directory.Exists(outputPath))
                    {
                        System.IO.Directory.CreateDirectory(outputPath);
                    }
                }
            }

            outputFileName = string.Format(fileNamePattern, outputFileName);
            var utf8WithoutBom = new System.Text.UTF8Encoding(false);

            File.WriteAllText(Path.Combine(outputPath, outputFileName + host.FileExtension), output, utf8WithoutBom);

            T4Errors = host.Errors;
        }
Exemplo n.º 6
0
        public void InitClipBoard()
        {
            IDataObject iobj     = Clipboard.GetDataObject();
            object      colObj   = iobj.GetData(typeof(ZippyCoder.Entity.Col));
            object      tableObj = iobj.GetData(typeof(ZippyCoder.Entity.Table));

            if (colObj != null)
            {
                _ClipboardCol = colObj as ZippyCoder.Entity.Col;
            }
            if (tableObj != null)
            {
                _ClipboardTable = tableObj as ZippyCoder.Entity.Table;
            }
        }
Exemplo n.º 7
0
 private void CopyNode()
 {
     if (_CurrentNode == Nodes.Table)
     {
         _ClipboardTable = _CurrentTreeViewItem.Tag as ZippyCoder.Entity.Table;
         //_ClipboardTable = table;
         DataObject dataobj = new DataObject(typeof(ZippyCoder.Entity.Table), _ClipboardTable);
         Clipboard.SetDataObject(dataobj);
     }
     else if (_CurrentNode == Nodes.Col)
     {
         _ClipboardCol = _CurrentTreeViewItem.Tag as ZippyCoder.Entity.Col;
         //_ClipboardCol = col;
         DataObject dataobj = new DataObject(typeof(ZippyCoder.Entity.Col), _ClipboardCol);
         Clipboard.SetDataObject(dataobj);
     }
 }
Exemplo n.º 8
0
        private void PasteNode()
        {
            _Modified = true;
            ChangeTitle(false);

            if (_CurrentNode == Nodes.Project && (_ClipboardTable != null)) //table 直接加入,因为带有下面的列数据
            {
                operation             = ChangeTypes.None;
                _uiProject.Visibility = Visibility.Hidden;
                _uiTable.Visibility   = Visibility.Hidden;
                _uiCol.Visibility     = Visibility.Hidden;
                ZippyCoder.Entity.Table table = _ClipboardTable;
                table.Name = table.Name;
                TreeViewItem tviTable = new TreeViewItem();
                _CurrentTreeViewItem.Items.Add(tviTable);
                tviTable.Tag    = table;
                tviTable.Header = table.Title + "[" + table.Name + "]";

                foreach (ZippyCoder.Entity.Col col in table.Cols)
                {
                    TreeViewItem tviCol = new TreeViewItem();
                    tviTable.Items.Add(tviCol);
                    tviTable.IsExpanded = true;
                    tviCol.Tag          = col;
                    tviCol.Header       = col.Title + "[" + col.Name + "]";
                }

                _Project.Tables.Add(table);
            }
            else if (_CurrentNode == Nodes.Table && (_ClipboardCol != null))  //列加入的时候,确认一下
            {
                operation = ChangeTypes.AddCol;
                ZippyCoder.Entity.Col col = _ClipboardCol;
                col.Name       = col.Name;
                _uiCol.Project = _Project;
                _uiCol.Col     = col;
                Navigate(_uiCol);
            }
        }
Exemplo n.º 9
0
        public void UpdateTableNode(ZippyCoder.Entity.Table table)
        {
            _Modified = true;
            ChangeTitle(false);
            if (_CurrentTreeViewItem.Tag is ZippyCoder.Entity.Table) //表示修改
            {
                _CurrentTreeViewItem.Tag    = table;
                _CurrentTreeViewItem.Header = table.Title + "[" + table.Name + "]";
            }
            else //新建的时候加入默认列
            {
                _Project.Tables.Add(table);

                TreeViewItem tviTable = new TreeViewItem();
                _CurrentTreeViewItem.Items.Add(tviTable);
                _CurrentTreeViewItem.IsExpanded = true;

                tviTable.Tag    = table;
                tviTable.Header = table.Title + "[" + table.Name + "]";

                //CreateDefaultCol(tviTable, table);
            }
        }
Exemplo n.º 10
0
        private void DeleteNode()
        {
            if (MessageBox.Show("是否删除当前节点?", "请确认", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No)
            {
                return;
            }
            _Modified = true;
            ChangeTitle(false);
            if (_CurrentNode == Nodes.Table)
            {
                ZippyCoder.Entity.Table table = _CurrentTreeViewItem.Tag as ZippyCoder.Entity.Table;

                operation = ChangeTypes.DropTable;
                WriteSqlLog(null, null); //没有col,但有当前 table,通过 ChangeTypes.DropTable 删除表

                TreeViewItem parentTV = _CurrentTreeViewItem.Parent as TreeViewItem;
                parentTV.Items.Remove(_CurrentTreeViewItem);
                _Project.Tables.Remove(table);

                ShowInfo("删除成功!");
            }
            else if (_CurrentNode == Nodes.Col)
            {
                ZippyCoder.Entity.Col col = _CurrentTreeViewItem.Tag as ZippyCoder.Entity.Col;

                operation = ChangeTypes.DropCol;
                WriteSqlLog(col, null);  //删除列

                TreeViewItem parentTV = _CurrentTreeViewItem.Parent as TreeViewItem;
                parentTV.Items.Remove(_CurrentTreeViewItem);
                ZippyCoder.Entity.Table table = parentTV.Tag as ZippyCoder.Entity.Table;
                table.Cols.Remove(col);

                ShowInfo("删除成功!");
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// 新建懒人列
        /// </summary>
        /// <param name="tviTable"></param>
        /// <param name="table"></param>
        public static void CreateLazyCols(TreeNode tviTable, ZippyCoder.Entity.Table table)
        {
            TreeNode tviCol;

            ZippyCoder.Entity.Col col;

            col = new ZippyCoder.Entity.Col();
            table.Cols.Add(col);
            col.Name           = table.Name + "ID";
            col.IsPK           = true;
            col.IsNull         = false;
            col.Unique         = true;
            col.AutoIncrease   = true;
            col.CssClass       = "textBox";
            col.CssClassLength = "w1";
            col.DataType       = System.Data.SqlDbType.BigInt;
            col.Title          = table.Title + "编号";
            col.UIColType      = ZippyCoder.Entity.UIColTypes.Detailable | ZippyCoder.Entity.UIColTypes.Listable | ZippyCoder.Entity.UIColTypes.Sortable;
            tviCol             = new TreeNode();
            tviCol.Text        = table.Title + "编号[" + table.Name + "ID]";
            tviCol.Tag         = col;
            tviTable.Nodes.Add(tviCol);


            col = new ZippyCoder.Entity.Col();
            table.Cols.Add(col);
            col.Name           = "TenantID";
            col.IsPK           = false;
            col.IsNull         = false;
            col.Unique         = false;
            col.AutoIncrease   = false;
            col.IsIndex        = true;
            col.Default        = "'00000000-0000-0000-0000-000000000000'";
            col.CssClass       = "";
            col.CssClassLength = "";
            col.DataType       = System.Data.SqlDbType.UniqueIdentifier;
            col.Title          = "租户";
            col.UIColType      = ZippyCoder.Entity.UIColTypes.Detailable;
            tviCol             = new TreeNode();
            tviCol.Text        = "租户[TenantID]";
            tviCol.Tag         = col;
            tviTable.Nodes.Add(tviCol);


            col = new ZippyCoder.Entity.Col();
            table.Cols.Add(col);
            col.Name         = table.Name + "Type";
            col.DataType     = System.Data.SqlDbType.Int;
            col.Default      = "1";
            col.Title        = table.Title + "类型";
            col.EnumType     = table.Name + "Types";
            col.ResourceType = "Resources.X";
            col.UIColType    = ZippyCoder.Entity.UIColTypes.Detailable | ZippyCoder.Entity.UIColTypes.Queryable | ZippyCoder.Entity.UIColTypes.Editable | ZippyCoder.Entity.UIColTypes.Listable;
            col.RenderType   = ZippyCoder.Entity.RenderTypes.CheckBoxList;
            tviCol           = new TreeNode();
            tviCol.Text      = col.Title + "[" + col.Name + "]";
            tviCol.Tag       = col;
            tviTable.Nodes.Add(tviCol);

            col = new ZippyCoder.Entity.Col();
            table.Cols.Add(col);
            col.Name         = table.Name + "Status";
            col.DataType     = System.Data.SqlDbType.Int;
            col.Default      = "1";
            col.Title        = table.Title + "状态";
            col.EnumType     = table.Name + "Status";
            col.ResourceType = "Resources.X";
            col.UIColType    = ZippyCoder.Entity.UIColTypes.Detailable | ZippyCoder.Entity.UIColTypes.Queryable | ZippyCoder.Entity.UIColTypes.Editable | ZippyCoder.Entity.UIColTypes.Listable;
            col.RenderType   = ZippyCoder.Entity.RenderTypes.RadioButtonList;
            tviCol           = new TreeNode();
            tviCol.Text      = col.Title + "[" + col.Name + "]";
            tviCol.Tag       = col;
            tviTable.Nodes.Add(tviCol);

            col = new ZippyCoder.Entity.Col();
            table.Cols.Add(col);
            col.Name           = "DisplayOrder";
            col.CssClass       = "textBox";
            col.CssClassLength = "w1";
            col.DataType       = System.Data.SqlDbType.Int;
            col.Default        = "0";
            col.Title          = "排列顺序";
            col.UIColType      = ZippyCoder.Entity.UIColTypes.Detailable | ZippyCoder.Entity.UIColTypes.Editable | ZippyCoder.Entity.UIColTypes.Sortable;
            tviCol             = new TreeNode();
            tviCol.Text        = "排列顺序[DisplayOrder]";
            tviCol.Tag         = col;
            tviTable.Nodes.Add(tviCol);

            col = new ZippyCoder.Entity.Col();
            table.Cols.Add(col);
            col.Name           = "CreateDate";
            col.AutoIncrease   = false;
            col.CssClass       = "textBox";
            col.CssClassLength = "w3";
            col.DataType       = System.Data.SqlDbType.DateTime;
            col.Default        = "(GetDate())";
            col.Title          = "创建时间";
            col.RenderType     = ZippyCoder.Entity.RenderTypes.TextBox;
            col.UIColType      = ZippyCoder.Entity.UIColTypes.Detailable | ZippyCoder.Entity.UIColTypes.Listable | ZippyCoder.Entity.UIColTypes.Queryable | ZippyCoder.Entity.UIColTypes.Sortable;
            tviCol             = new TreeNode();
            tviCol.Text        = "创建时间[CreateDate]";
            tviCol.Tag         = col;
            tviTable.Nodes.Add(tviCol);

            col = new ZippyCoder.Entity.Col();
            table.Cols.Add(col);
            col.Name           = "Creator";
            col.CssClass       = "textBox";
            col.CssClassLength = "w1";
            col.DataType       = System.Data.SqlDbType.UniqueIdentifier;
            //col.Default = "0";
            col.Title     = "创建人";
            col.UIColType = ZippyCoder.Entity.UIColTypes.Detailable;
            tviCol        = new TreeNode();
            tviCol.Text   = "创建人[Creator]";
            tviCol.Tag    = col;
            tviTable.Nodes.Add(tviCol);

            col = new ZippyCoder.Entity.Col();
            table.Cols.Add(col);
            col.Name           = "UpdateDate";
            col.AutoIncrease   = false;
            col.CssClass       = "textBox";
            col.CssClassLength = "w3";
            col.DataType       = System.Data.SqlDbType.DateTime;
            col.Title          = "更新时间";
            col.RenderType     = ZippyCoder.Entity.RenderTypes.TextBox;
            col.UIColType      = ZippyCoder.Entity.UIColTypes.Detailable;
            tviCol             = new TreeNode();
            tviCol.Text        = "更新时间[UpdateDate]";
            tviCol.Tag         = col;
            tviTable.Nodes.Add(tviCol);

            col = new ZippyCoder.Entity.Col();
            table.Cols.Add(col);
            col.Name           = "Updater";
            col.CssClass       = "textBox";
            col.CssClassLength = "w1";
            col.DataType       = System.Data.SqlDbType.UniqueIdentifier;
            //col.Default = "0";
            col.Title     = "更新人";
            col.UIColType = ZippyCoder.Entity.UIColTypes.Detailable;
            tviCol        = new TreeNode();
            tviCol.Text   = "更新人[Updater]";
            tviCol.Tag    = col;
            tviTable.Nodes.Add(tviCol);
        }
Exemplo n.º 12
0
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(ddlDatabase.Text))
            {
                return;
            }

            project.Namespace = ddlDatabase.Text;
            project.Title     = ddlDatabase.Text;

            string conStr1 = "Server={0};Port={1};Uid={2};Pwd={3};Database={4}";

            string conStr = string.Format(conStr1, tbxServer.Text, tbxPort.Text, tbxUserName.Text, tbxPassword.Text, ddlDatabase.Text);

            MessageBox.Show(conStr);

            MySqlConnection con = new MySqlConnection(conStr);

            try
            {
                con.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            List <string> tableNames = new List <string>();
            MySqlCommand  cmd        = new MySqlCommand("show tables", con);
            IDataReader   reader     = cmd.ExecuteReader();

            while (reader.Read())
            {
                tableNames.Add(reader.GetString(0));
            }
            reader.Close();

            #region 查询列结构
            foreach (string strTableName in tableNames)
            {
                ZippyCoder.Entity.Table table = new ZippyCoder.Entity.Table();
                table.Name  = strTableName;
                table.Title = strTableName;
                project.Tables.Add(table);


                string       sql            = "SHOW COLUMNS FROM " + strTableName;
                MySqlCommand cmdColProperty = new MySqlCommand(sql, con);
                IDataReader  readerCol      = cmdColProperty.ExecuteReader();

                List <ZippyCoder.Entity.Col> fkCols = new List <ZippyCoder.Entity.Col>();

                while (readerCol.Read())
                {
                    object _colName = readerCol.GetValue(0);
                    object _mysqlType = readerCol.GetValue(1);
                    object _canNull = readerCol.GetValue(2);
                    object _isKey = readerCol.GetValue(3);
                    object _defVal = readerCol.GetValue(4);
                    object _extra = readerCol.GetValue(5);
                    string colName = _colName == null ? "" : _colName.ToString();
                    string mysqlType = _mysqlType == null ? "" : _mysqlType.ToString();
                    string canNull = _canNull == null ? "" : _canNull.ToString();
                    string isKey = _isKey == null ? "" : _isKey.ToString();
                    string defVal = _defVal == null ? "" : _defVal.ToString();
                    string extra = _extra == null ? "" : _extra.ToString();
                    string dataType = string.Empty, dataLen = string.Empty;
                    var    match      = System.Text.RegularExpressions.Regex.Match(mysqlType, @"([\w]+).*?([\d\,]+).*");
                    var    matchCount = match.Groups.Count;
                    if (matchCount > 1)
                    {
                        dataType = match.Groups[1].Value;
                    }
                    if (matchCount > 2)
                    {
                        dataLen = match.Groups[2].Value;
                    }

                    if (string.IsNullOrEmpty(dataType))
                    {
                        dataType = mysqlType;
                    }



                    ZippyCoder.Entity.Col col = table.Exists(colName);
                    if (col == null)
                    {
                        col = new ZippyCoder.Entity.Col();
                        table.Cols.Add(col);
                        col.Parent = table;
                    }
                    col.Name         = colName;
                    col.Title        = colName;
                    col.DataType     = ZippyCoder.TypeConverter.ToSqlDbType(dataType);
                    col.Default      = (defVal == "NULL" ? "" : defVal);
                    col.Length       = dataLen;
                    col.IsPK         = (isKey != null && isKey.ToLower() == "pri");
                    col.IsNull       = (canNull != null && canNull.ToLower() == "yes");
                    col.AutoIncrease = (extra != null && extra.ToLower() == "auto_increment");
                }
                readerCol.Close();
            }

            #endregion

            con.Close();

            _Owner.Project = project;
            _Owner.UpdateUI();
        }
Exemplo n.º 13
0
 private void CreateTable()
 {
     ZippyCoder.Entity.Table table = new ZippyCoder.Entity.Table();
     _uiTable.Table = table;
     Navigate(_uiTable);
 }
Exemplo n.º 14
0
        private void ZippyCommands_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (e.Command.Equals(ZippyCommands.CopyNode))
            {
                CopyNode();
            }
            else if (e.Command.Equals(ZippyCommands.PasteNode))
            {
                PasteNode();
            }
            else if (e.Command.Equals(ZippyCommands.DeleteNode))
            {
                DeleteNode();
            }
            else if (e.Command.Equals(ZippyCommands.CreateCols))
            {
                Helper.CreateLazyCols(_CurrentTreeViewItem, (ZippyCoder.Entity.Table)_CurrentTreeViewItem.Tag);
            }
            else if (e.Command.Equals(ZippyCommands.NodeDown))
            {
                TreeViewItem myParent = _CurrentTreeViewItem.Parent as TreeViewItem;

                int index = 0;
                for (; index < myParent.Items.Count; index++)
                {
                    if (_CurrentTreeViewItem == myParent.Items[index])
                    {
                        break;
                    }
                }
                myParent.Items.Remove(_CurrentTreeViewItem);
                myParent.Items.Insert(index + 1, _CurrentTreeViewItem);

                if (myParent.Tag is ZippyCoder.Entity.Project) //表移动
                {
                    _Project.Tables.Remove(_CurrentTreeViewItem.Tag as ZippyCoder.Entity.Table);
                    _Project.Tables.Insert(index + 1, _CurrentTreeViewItem.Tag as ZippyCoder.Entity.Table);
                }
                else
                {
                    ZippyCoder.Entity.Table table = myParent.Tag as ZippyCoder.Entity.Table;

                    table.Cols.Remove(_CurrentTreeViewItem.Tag as ZippyCoder.Entity.Col);
                    table.Cols.Insert(index + 1, _CurrentTreeViewItem.Tag as ZippyCoder.Entity.Col);
                }
                _CurrentTreeViewItem.IsSelected = true;
                ShowInfo("下移成功。");
            }
            else if (e.Command.Equals(ZippyCommands.NodeUp))
            {
                TreeViewItem myParent = _CurrentTreeViewItem.Parent as TreeViewItem;

                int index = 0;
                for (; index < myParent.Items.Count; index++)
                {
                    if (_CurrentTreeViewItem == myParent.Items[index])
                    {
                        break;
                    }
                }
                myParent.Items.Remove(_CurrentTreeViewItem);
                myParent.Items.Insert(index - 1 >= 0 ? index - 1 : 0, _CurrentTreeViewItem);

                if (myParent.Tag is ZippyCoder.Entity.Project) //表移动
                {
                    _Project.Tables.Remove(_CurrentTreeViewItem.Tag as ZippyCoder.Entity.Table);
                    _Project.Tables.Insert(index - 1 >= 0 ? index - 1 : 0, _CurrentTreeViewItem.Tag as ZippyCoder.Entity.Table);
                }
                else
                {
                    ZippyCoder.Entity.Table table = myParent.Tag as ZippyCoder.Entity.Table;

                    table.Cols.Remove(_CurrentTreeViewItem.Tag as ZippyCoder.Entity.Col);
                    table.Cols.Insert(index - 1 >= 0 ? index - 1 : 0, _CurrentTreeViewItem.Tag as ZippyCoder.Entity.Col);
                }
                _CurrentTreeViewItem.IsSelected = true;
                ShowInfo("上移成功。");
            }
        }
Exemplo n.º 15
0
        public static void WriteSqlServerChangeLog(System.IO.StreamWriter sw, ZippyCoder.Entity.Table table, ZippyCoder.Entity.Col col, ChangeTypes changeType, string oldColName)
        {
            if (sw == null)
            {
                return;
            }
            if (table == null)
            {
                return;
            }
            switch (changeType)
            {
            case ChangeTypes.AddCol:
                if (col != null)
                {
                    sw.Write("ALTER TABLE [" + table.Name + "] ");
                    sw.Write("ADD [" + col.Name + "] ");

                    if (!string.IsNullOrEmpty(col.Length))
                    {
                        sw.Write("(" + col.Length + ") ");
                    }
                    if (col.AutoIncrease)
                    {
                        sw.Write("IDENTITY(1,1) ");
                    }
                    if (col.IsPK)
                    {
                        sw.Write("PRIMARY KEY ");
                    }
                    if (col.Unique)
                    {
                        sw.Write("UNIQUE ");
                    }
                    if (col.IsNull)
                    {
                        sw.Write("NULL ");
                    }
                    else
                    {
                        sw.Write("NOT NULL ");
                    }

                    if (!string.IsNullOrEmpty(col.Default))
                    {
                        sw.Write("DEFAULT (" + col.Default + ") ");
                    }
                    if ((!string.IsNullOrEmpty(col.RefTable)) && (!string.IsNullOrEmpty(col.RefCol)))
                    {
                        sw.Write("REFERENCES [" + col.RefTable + "]([" + col.RefCol + "])");
                    }
                    sw.WriteLine();
                    sw.WriteLine("GO");
                    sw.WriteLine("----------------------------------");
                }
                break;

            case ChangeTypes.AlterCol:
                if (col != null)
                {
                    if (col.Name == oldColName)
                    {
                        sw.Write("ALTER TABLE [" + table.Name + "] ");
                        sw.Write("ALTER COLUMN [" + col.Name + "] ");
                        sw.Write(col.DataType.ToString());
                        if (!string.IsNullOrEmpty(col.Length))
                        {
                            sw.Write("(" + col.Length + ") ");
                        }
                        if (col.AutoIncrease)
                        {
                            sw.Write("IDENTITY(1,1) ");
                        }
                        if (col.IsPK)
                        {
                            sw.Write("PRIMARY KEY ");
                        }
                        if (col.Unique)
                        {
                            sw.Write("UNIQUE ");
                        }
                        if (col.IsNull)
                        {
                            sw.Write("NULL ");
                        }
                        else
                        {
                            sw.Write("NOT NULL ");
                        }

                        if (col.Default != "")
                        {
                            sw.Write("DEFAULT (" + col.Default + ") ");
                        }
                        if (col.RefTable != null && col.RefTable != "" && col.RefCol != null && col.RefCol != "")
                        {
                            sw.Write("REFERENCES [" + col.RefTable + "]([" + col.RefCol + "])");
                        }
                        sw.WriteLine();
                        sw.WriteLine("GO");
                        sw.WriteLine("----------------------------------");
                    }
                    else
                    {
                        sw.Write("ALTER TABLE [" + table.Name + "] ");
                        sw.Write("DROP COLUMN [" + oldColName + "] ");
                        sw.WriteLine();
                        sw.WriteLine("GO");
                        sw.WriteLine("----------------------------------");
                        sw.Write("ALTER TABLE [" + table.Name + "] ");
                        sw.Write("ADD [" + col.Name + "] ");
                        sw.Write(col.DataType.ToString() + " ");
                        if (!string.IsNullOrEmpty(col.Length))
                        {
                            sw.Write("(" + col.Length + ") ");
                        }
                        if (col.AutoIncrease)
                        {
                            sw.Write("IDENTITY(1,1) ");
                        }
                        if (col.IsPK)
                        {
                            sw.Write("PRIMARY KEY ");
                        }
                        if (col.Unique)
                        {
                            sw.Write("UNIQUE ");
                        }
                        if (col.IsNull)
                        {
                            sw.Write("NULL ");
                        }
                        else
                        {
                            sw.Write("NOT NULL ");
                        }

                        if (!string.IsNullOrEmpty(col.Default))
                        {
                            sw.Write("DEFAULT (" + col.Default + ") ");
                        }
                        if ((!string.IsNullOrEmpty(col.RefTable)) && (!string.IsNullOrEmpty(col.RefCol)))
                        {
                            sw.Write("REFERENCES [" + col.RefTable + "]([" + col.RefCol + "])");
                        }
                        sw.WriteLine();
                        sw.WriteLine("GO");
                        sw.WriteLine("----------------------------------");
                    }
                }
                break;

            case ChangeTypes.DropCol:
                if (col != null)
                {
                    sw.Write("ALTER TABLE [" + table.Name + "] ");
                    sw.Write("DROP COLUMN [" + col.Name + "] ");
                    sw.WriteLine();
                    sw.WriteLine("GO");
                    sw.WriteLine("----------------------------------");
                }
                break;

            case ChangeTypes.DropTable:
                sw.Write("DROP TABLE [" + table.Name + "] ");
                sw.WriteLine();
                sw.WriteLine("GO");
                sw.WriteLine("----------------------------------");
                break;
            }
            sw.Write("");
        }
Exemplo n.º 16
0
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(ddlDatabase.Text))
            {
                return;
            }

            project.Namespace = ddlDatabase.Text;
            project.Title     = ddlDatabase.Text;

            string conStr1 = "Persist Security Info=False;User ID={1};Password={2};Server={0};Initial Catalog={3}";
            string conStr2 = "Persist Security Info=False;Integrated Security=true;Server={0};Initial Catalog={1}";

            string conStr = "";

            if (string.IsNullOrEmpty(tbxUserName.Text.Trim()))
            {
                conStr = string.Format(conStr2, tbxServer.Text, ddlDatabase.Text);
            }
            else
            {
                conStr = string.Format(conStr1, tbxServer.Text, tbxUserName.Text, tbxPassword.Text, ddlDatabase.Text);
            }

            SqlConnection con = new SqlConnection(conStr);

            try
            {
                con.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            List <string> tableNames = new List <string>();
            SqlCommand    cmd        = new SqlCommand("select name from sysobjects where xtype='U' order by name", con);
            IDataReader   reader     = cmd.ExecuteReader();

            while (reader.Read())
            {
                tableNames.Add(reader.GetString(0));
            }
            reader.Close();

            foreach (string strTableName in tableNames)
            {
                ZippyCoder.Entity.Table table = new ZippyCoder.Entity.Table();
                table.Name  = strTableName;
                table.Title = strTableName;
                project.Tables.Add(table);

                string sql = @"SELECT 
INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME, 
INFORMATION_SCHEMA.COLUMNS.DATA_TYPE,
INFORMATION_SCHEMA.COLUMNS.COLUMN_DEFAULT, 
INFORMATION_SCHEMA.COLUMNS.CHARACTER_MAXIMUM_LENGTH, 
INFORMATION_SCHEMA.COLUMNS.NUMERIC_PRECISION, 
INFORMATION_SCHEMA.COLUMNS.NUMERIC_SCALE, 
INFORMATION_SCHEMA.COLUMNS.IS_NULLABLE, 
COLUMNPROPERTY(OBJECT_ID('" + strTableName + @"'), INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME, 'IsIdentity') AS IsIdentity,
INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_TYPE
FROM INFORMATION_SCHEMA.COLUMNS LEFT JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ON INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME = INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE.COLUMN_NAME AND INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE.TABLE_NAME LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS ON INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE.CONSTRAINT_NAME = INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_NAME AND INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE.TABLE_NAME = INFORMATION_SCHEMA.TABLE_CONSTRAINTS.TABLE_NAME WHERE INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = '" + strTableName + "'";


                //System.Diagnostics.Debug.WriteLine(sql);

                //return;


                SqlCommand  cmdColProperty = new SqlCommand(sql, con);
                IDataReader readerCol      = cmdColProperty.ExecuteReader();

                List <ZippyCoder.Entity.Col> fkCols = new List <ZippyCoder.Entity.Col>();

                while (readerCol.Read())
                {
                    string colName            = readerCol.GetValue(0).ToString();
                    ZippyCoder.Entity.Col col = table.Exists(colName);
                    if (col == null)
                    {
                        col = new ZippyCoder.Entity.Col();
                        table.Cols.Add(col);
                        col.Parent = table;
                    }
                    col.Name     = colName;
                    col.Title    = colName;
                    col.DataType = ZippyCoder.TypeConverter.ToSqlDbType(readerCol.GetValue(1).ToString());
                    col.Default  = readerCol.GetValue(2).ToString();
                    if (col.DataType == SqlDbType.VarChar || col.DataType == SqlDbType.NVarChar || col.DataType == SqlDbType.Char || col.DataType == SqlDbType.NChar)
                    {
                        col.Length = readerCol.GetValue(3).ToString();
                    }
                    else if (col.DataType == SqlDbType.Decimal)
                    {
                        col.Length = "(" + readerCol.GetValue(4).ToString() + "," + readerCol.GetValue(5).ToString() + ")";
                    }
                    if ((readerCol.GetValue(6).ToString().ToUpper() == "NO"))
                    {
                        col.IsNull = false;
                    }
                    if (readerCol.GetValue(7).ToString() == "1")
                    {
                        col.AutoIncrease = true;
                    }
                    if (readerCol.GetValue(8).ToString() == "PRIMARY KEY")
                    {
                        col.IsPK = true;
                    }
                    if (readerCol.GetValue(8).ToString() == "UNIQUE")
                    {
                        col.Unique = true;
                    }
                    if (readerCol.GetValue(8).ToString() == "FOREIGN KEY") //将有外键约束的列记录下来,待查。
                    {
                        fkCols.Add(col);
                    }
                }
                readerCol.Close();
            }

            con.Close();

            _Owner.Project = project;
            _Owner.UpdateUI();
        }