Esempio n. 1
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;
 }
Esempio n. 2
0
        private EntitiesGenerator.Definitions.DataTable ConvertToDefinition(SQLiteConnection connection, string tableName, string sql)
        {
            string commandText = string.Format("PRAGMA table_info('{0}')", tableName);
            System.Data.DataTable tableInfo = ExecuteForDataTable(connection, commandText);

            EntitiesGenerator.Definitions.DataTable dataTableDefinition = new EntitiesGenerator.Definitions.DataTable();
            dataTableDefinition.TableName = tableName;
            dataTableDefinition.SourceName = tableName;
            List<System.Data.DataColumn> primaryKeys = new List<System.Data.DataColumn>(tableInfo.PrimaryKey);
            foreach (System.Data.DataRow dataRow in tableInfo.Rows)
            {
                string columnName = dataRow["name"] as string;
                string type = dataRow["type"] as string;

                EntitiesGenerator.Definitions.DataColumn dataColumnDefinition = new EntitiesGenerator.Definitions.DataColumn();
                dataColumnDefinition.ColumnName = columnName;
                dataColumnDefinition.SourceName = columnName;
                dataColumnDefinition.AllowDBNull = Convert.ToInt32(dataRow["notnull"]) == 0 ? true : false;
                string expression = @"\W\[{0}\]\s[^,]*AUTOINCREMENT|\W{0}\s[^,]*AUTOINCREMENT";
                bool autoIncrement = new Regex(string.Format(expression, columnName.ToUpper())).IsMatch(sql);
                dataColumnDefinition.AutoIncrement = autoIncrement;
                dataColumnDefinition.DataType = type;
                dataColumnDefinition.Type = GetWellKnownDataTypeName(type);
                dataColumnDefinition.PrimaryKey = Convert.ToInt32(dataRow["pk"]) == 0 ? false : true;
                dataTableDefinition.Columns.Add(dataColumnDefinition);
            }
            return dataTableDefinition;
        }
Esempio n. 3
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;
 }
Esempio n. 4
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;
        }
Esempio n. 5
0
        private void newTable(object sender, EventArgs e)
        {
            TreeNode definitionNode = this.treeView1.Nodes[0];
            if (definitionNode != null)
            {
                Definition definition = definitionNode.Tag as Definition;
                if (definition != null)
                {
                    string tableName = GenerateNewTableName();
                    EntitiesGenerator.Definitions.DataTable table = new EntitiesGenerator.Definitions.DataTable(tableName);
                    string columnName = GenerateNewColumnName();
                    EntitiesGenerator.Definitions.DataColumn column = new EntitiesGenerator.Definitions.DataColumn(columnName);
                    table.Columns.Add(column);
                    definition.Tables.Add(table);

                    // Create new table tree node.
                    TreeNode tableNode = CreateDataTableNode(table);
                    TreeNode columnNode = CreateDataColumnNode(column);
                    tableNode.Nodes.Add(columnNode);
                    definitionNode.Nodes.Add(tableNode);
                    if (!definitionNode.IsExpanded)
                    {
                        definitionNode.Expand();
                    }
                    if (!tableNode.IsExpanded)
                    {
                        tableNode.Expand();
                    }
                    this.treeView1.SelectedNode = tableNode;

                    this.modified = true;
                }
            }
        }
Esempio n. 6
0
        private void newColumn(object sender, EventArgs e)
        {
            if (this.dataGridView1.DataSource != null)
            {
                List<EntitiesGenerator.Definitions.DataColumn> columns = this.dataGridView1.DataSource as List<EntitiesGenerator.Definitions.DataColumn>;
                if (columns != null)
                {
                    string columnName = GenerateNewColumnName();
                    EntitiesGenerator.Definitions.DataColumn column = new EntitiesGenerator.Definitions.DataColumn(columnName);
                    columns.Add(column);

                    RefreshDataGridView(this.dataGridView1.Tag as EntitiesGenerator.Definitions.DataTable);
                    SelectDataGridViewRow(column);

                    // TODO: refresh tree view.
                    if (this._showColumnNodes)
                    {
                        RefreshTreeNode(this.dataGridView1.Tag as EntitiesGenerator.Definitions.DataTable);
                    }

                    this.modified = true;
                }
            }
        }