Exemplo n.º 1
0
 public EntitiesGenerator.Definitions.Definition ReadFile(System.IO.FileInfo fileInfo)
 {
     if (fileInfo.Exists)
     {
         Definition definition = new Definition();
         System.IO.StreamReader  streamReader  = fileInfo.OpenText();
         string textLine = streamReader.ReadLine();
         while (textLine != null)
         {
             string text = textLine.TrimStart();
             if (text.StartsWith("CREATE TABLE ", StringComparison.CurrentCultureIgnoreCase))
             {
                 string tableText;
                 string tableName = ReadTableName(text, streamReader, out tableText);
                 EntitiesGenerator.Definitions.DataTable table = new EntitiesGenerator.Definitions.DataTable(tableName);
                 ProcessColumns(table, tableText);
             }
             textLine = streamReader.ReadLine();
         }
         return definition;
     }
     else
     {
         return null;
     }
 }
Exemplo n.º 2
0
 private Definition GenerateDefinitionFromAssembly(string fileName)
 {
     Definition difinition = new Definition();
     Assembly assembly = Assembly.LoadFile(fileName);
     Type[] types = assembly.GetTypes();
     foreach (var type in types)
     {
         Definitions.DataTable table = new Definitions.DataTable();
         table.TableName = type.Name;
         table.SourceName = type.Name;
         if ((type.GetCustomAttributes(typeof(TableAttribute), true) != null) && (type.GetCustomAttributes(typeof(TableAttribute), true).Length > 0))
         {
             table.SourceName = (type.GetCustomAttributes(typeof(TableAttribute), true)[0] as TableAttribute).Name;
         }
         foreach (var property in type.GetProperties())
         {
             Type propertyType = property.PropertyType;
             if (propertyType.IsGenericType)
             {
                 propertyType = propertyType.GetGenericArguments()[0];
             }
             string typeName = TypeManager.GetWellKnownDataTypeName(propertyType);
             if (!string.IsNullOrEmpty(typeName))
             {
                 Definitions.DataColumn column = new Definitions.DataColumn
                 {
                     ColumnName = property.Name,
                     SourceName = property.Name,
                     Type = typeName,
                     DataType = typeName
                 };
                 if ((property.GetCustomAttributes(typeof(ColumnAttribute), false) != null) && (property.GetCustomAttributes(typeof(ColumnAttribute), false).Length > 0))
                 {
                     ColumnAttribute columnAttribute = property.GetCustomAttributes(typeof(ColumnAttribute), false)[0] as ColumnAttribute;
                     column.SourceName = columnAttribute.Name;
                     column.PrimaryKey = columnAttribute.IsPrimaryKey;
                     column.AllowDBNull = columnAttribute.CanBeNull;
                     if (!string.IsNullOrWhiteSpace(columnAttribute.DbType))
                     {
                         column.DataType = columnAttribute.DbType;
                     }
                 }
                 table.Columns.Add(column);
             }
         }
         difinition.Tables.Add(table);
     }
     return difinition;
 }
Exemplo n.º 3
0
 private Definition CreateTemplateDefinition()
 {
     Definition definition = new Definition();
     definition.Namespace = "General.Data.Entities";
     definition.ProjectLocation = Application.StartupPath;
     //EntitiesGenerator.Definitions.DataTable table = new EntitiesGenerator.Definitions.DataTable("Table1");
     //table.Columns.Add(new EntitiesGenerator.Definitions.DataColumn("Column1"));
     //definition.Tables.Add(table);
     return definition;
 }
Exemplo n.º 4
0
 private TreeNode CreateDefinitionNode(Definition definition)
 {
     TreeNode treeNode = new TreeNode();
     treeNode.Text = "Tables";
     treeNode.ImageKey = "Microsoft.VisualStudio.DataTools.Database.ico";
     treeNode.SelectedImageKey = treeNode.ImageKey;
     treeNode.Tag = definition;
     return treeNode;
 }
Exemplo n.º 5
0
 private Definition ConvertToDefinitionFromSchema(SQLiteConnection connection, System.Data.DataTable schema)
 {
     // TODO:
     Definition definition = new Definition();
     if (schema != null)
     {
         foreach (System.Data.DataRow dataRow in schema.Rows)
         {
             string tableName = dataRow["tbl_name"] as string;
             string sql = dataRow["sql"] as string;
             if (!string.IsNullOrEmpty(sql))
             {
                 EntitiesGenerator.Definitions.DataTable dataTableDefinition = ConvertToDefinition(connection, tableName, sql);
                 if (dataTableDefinition != null)
                 {
                     definition.Tables.Add(dataTableDefinition);
                 }
             }
         }
     }
     return definition;
 }
Exemplo n.º 6
0
 private Definition ConvertToDefinition(System.Data.DataSet dataSet)
 {
     Definition definition = new Definition();
     if (dataSet != null)
     {
         foreach (System.Data.DataTable dataTable in dataSet.Tables)
         {
             EntitiesGenerator.Definitions.DataTable dataTableDefinition = new EntitiesGenerator.Definitions.DataTable();
             dataTableDefinition.TableName = dataTable.TableName;
             dataTableDefinition.SourceName = dataTable.TableName;
             List<System.Data.DataColumn> primaryKeys = new List<System.Data.DataColumn>(dataTable.PrimaryKey);
             foreach (System.Data.DataColumn dataColumn in dataTable.Columns)
             {
                 EntitiesGenerator.Definitions.DataColumn dataColumnDefinition = new EntitiesGenerator.Definitions.DataColumn();
                 dataColumnDefinition.ColumnName = dataColumn.ColumnName;
                 dataColumnDefinition.SourceName = dataColumn.ColumnName;
                 dataColumnDefinition.AllowDBNull = dataColumn.AllowDBNull;
                 dataColumnDefinition.AutoIncrement = dataColumn.AutoIncrement;
                 dataColumnDefinition.DataType = TypeManager.GetWellKnownDataTypeName(dataColumn.DataType);
                 dataColumnDefinition.Type = TypeManager.GetWellKnownDataTypeName(dataColumn.DataType);
                 dataColumnDefinition.PrimaryKey = primaryKeys.Contains(dataColumn);
                 dataTableDefinition.Columns.Add(dataColumnDefinition);
             }
             definition.Tables.Add(dataTableDefinition);
         }
     }
     return definition;
 }
Exemplo n.º 7
0
 private System.Data.DataSet ConvertToDataSet(Definition definition, bool sourceName)
 {
     System.Data.DataSet dataSet = new System.Data.DataSet();
     if (definition != null)
     {
         foreach (EntitiesGenerator.Definitions.DataTable dataTableDefinition in definition.Tables)
         {
             System.Data.DataTable dataTable = new System.Data.DataTable();
             dataTable.TableName = sourceName ? dataTableDefinition.SourceName : dataTableDefinition.TableName;
             List<System.Data.DataColumn> primaryKeys = new List<System.Data.DataColumn>();
             foreach (EntitiesGenerator.Definitions.DataColumn dataColumnDefinition in dataTableDefinition.Columns)
             {
                 System.Data.DataColumn dataColumn = new System.Data.DataColumn();
                 dataColumn.ColumnName = sourceName ? dataColumnDefinition.SourceName : dataColumnDefinition.ColumnName;
                 dataColumn.AllowDBNull = dataColumnDefinition.AllowDBNull;
                 dataColumn.AutoIncrement = dataColumnDefinition.AutoIncrement;
                 if (sourceName)
                 {
                     Type dataType = TypeManager.GetWellKnownDataType(dataColumnDefinition.DataType);
                     if (dataType != null)
                     {
                         dataColumn.DataType = dataType;
                     }
                 }
                 else
                 {
                     Type dataType = TypeManager.GetWellKnownDataType(dataColumnDefinition.Type);
                     if (dataType != null)
                     {
                         dataColumn.DataType = dataType;
                     }
                 }
                 if (dataColumnDefinition.PrimaryKey)
                 {
                     primaryKeys.Add(dataColumn);
                 }
                 dataTable.Columns.Add(dataColumn);
             }
             dataTable.PrimaryKey = primaryKeys.ToArray();
             dataSet.Tables.Add(dataTable);
         }
     }
     return dataSet;
 }
Exemplo n.º 8
0
        private void AppendDefinition(Definition definition)
        {
            if (definition != null)
            {
                if (this.definition != null)
                {
                    // TODO:
                    foreach (EntitiesGenerator.Definitions.DataTable dataTable in definition.Tables)
                    {
                        this.definition.Tables.Add(dataTable);
                    }
                }
                else
                {
                    this.definition = definition;
                }

                RefreshDefinition();
            }
        }
Exemplo n.º 9
0
        private bool OpenXmlFile(System.IO.FileInfo fileInfo)
        {
            try
            {
                System.IO.FileStream fileStream = fileInfo.OpenRead();
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(Definition));
                this.definition = xmlSerializer.Deserialize(fileStream) as Definition;
                fileStream.Close();

                // refresh tree view.
                RefreshDefinition();

                UpdateCurrentFile(fileInfo);
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Open", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }
Exemplo n.º 10
0
 private void OpenSqlFile(System.IO.FileInfo fileInfo)
 {
     SqlFileReader reader = new SqlFileReader();
     this.definition = reader.ReadFile(fileInfo);
     if (this.definition != null)
     {
         RefreshDefinition();
     }
     else
     {
         string message = "Reading sql caused an exception, please check the file \"{0}\".";
         MessageBox.Show(message, "Open", MessageBoxButtons.OK, MessageBoxIcon.Stop);
     }
 }
Exemplo n.º 11
0
        private static EntitiesGenerator.Definitions.Definition GetDefinitionFromSharePoint(string connectionString)
        {
            EntitiesGenerator.Definitions.Definition definition = new EntitiesGenerator.Definitions.Definition();
            SharePointConnection sharePointConnection = new SharePointConnection(connectionString);
            System.Data.DataTable tables = sharePointConnection.GetTables();
            SelectTableForm selectTableForm = new SelectTableForm();
            foreach (System.Data.DataRow dataRow in tables.Rows)
            {
                string tableName = dataRow["Title"] as string;
                ListViewItem item = new ListViewItem();
                item.Text = tableName;
                item.Tag = dataRow;
                item.ImageIndex = 0;
                item.Checked = true;
                selectTableForm.AddListViewItem(item);
            }
            if (selectTableForm.ShowDialog() == DialogResult.OK)
            {
                System.Windows.Forms.ListView.CheckedListViewItemCollection checkedItems = selectTableForm.GetCheckedItems();
                foreach (ListViewItem item in checkedItems)
                {
                    System.Data.DataRow dataRow = item.Tag as System.Data.DataRow;
                    string listUrl = (dataRow["RootFolderUrl"] as string);
                    EntitiesGenerator.Definitions.DataTable dataTable = new EntitiesGenerator.Definitions.DataTable();
                    dataTable.SourceName = listUrl;
                    dataTable.TableName = dataRow["Title"] as string;
                    System.Data.DataTable tableColumns = sharePointConnection.GetTable(listUrl);
                    foreach (System.Data.DataRow tableColumn in tableColumns.Rows)
                    {
                        EntitiesGenerator.Definitions.DataColumn dataColumn = new EntitiesGenerator.Definitions.DataColumn();
                        string columnName = tableColumn["InternalName"] as string;
                        Type dataType = tableColumn["FieldType"] as Type;
                        dataColumn.ColumnName = columnName;
                        dataColumn.SourceName = columnName;
                        dataColumn.DataType = TypeManager.GetWellKnownDataTypeName(dataType);
                        dataColumn.Type = dataColumn.DataType;
                        dataColumn.AllowDBNull = !((bool)tableColumn["Required"]);
                        dataColumn.PrimaryKey = (dataColumn.ColumnName == "ID");
                        //dataColumn.Length = GetDataColumnLength(column);
                        dataTable.Columns.Add(dataColumn);

                    }
                    definition.Tables.Add(dataTable);
                }
            }
            return definition;
        }
Exemplo n.º 12
0
        private void NewDefinition()
        {
            this.definition = CreateTemplateDefinition();

            RefreshDefinition();
        }
Exemplo n.º 13
0
        private void ImportFromSharePoint(string connectionString)
        {
            EntitiesGenerator.Definitions.Definition definition = GetDefinitionFromSharePoint(connectionString);
            if ((definition != null) && (definition.Tables.Count > 0))
            {
                bool useNamingConvetion = false;
                string message = "Do you want to apply Naming Convention to Table Name(s) and Column Name(s)?";
                if (MessageBox.Show(message, "Naming Convention", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    useNamingConvetion = true;
                }
                if (this.definition != null)
                {
                    // TODO:
                    foreach (EntitiesGenerator.Definitions.DataTable dataTable in definition.Tables)
                    {
                        if (useNamingConvetion)
                        {
                            dataTable.TableName = DatabaseReader.GetConventionName(dataTable.TableName);
                            foreach (EntitiesGenerator.Definitions.DataColumn dataColumn in dataTable.Columns)
                            {
                                dataColumn.ColumnName = DatabaseReader.GetConventionName(dataColumn.ColumnName);
                            }
                        }
                        this.definition.Tables.Add(dataTable);
                    }
                }
                else
                {
                    if (useNamingConvetion)
                    {
                        foreach (EntitiesGenerator.Definitions.DataTable dataTable in definition.Tables)
                        {
                            dataTable.TableName = DatabaseReader.GetConventionName(dataTable.TableName);
                            foreach (EntitiesGenerator.Definitions.DataColumn dataColumn in dataTable.Columns)
                            {
                                dataColumn.ColumnName = DatabaseReader.GetConventionName(dataColumn.ColumnName);
                            }
                        }
                    }
                    this.definition = definition;
                }

                RefreshDefinition();
            }
        }