Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="namespaceName"></param>
        /// <param name="path"></param>
        /// <param name="selectedLanguage"></param>
        private void GeneratePersistent(string namespaceName, string path, Language selectedLanguage)
        {
            IDataAccessObjectGenerator generator = null;

            try
            {
                //initialize the generator based on the language

                if (selectedLanguage == Language.CSharp)
                {
                    generator = new CSharpDataAccessObjectGenerator();
                }
                else if (selectedLanguage == Language.VbNet)
                {
                    generator = new VBDataAccessObjectGenerator();
                }


                if (!path.EndsWith(@"\"))
                {
                    path += @"\";
                }

                string[]  tableNames       = new string[listView.CheckedItems.Count];
                ArrayList alColumns        = new ArrayList();
                ArrayList alTableRelations = new ArrayList();

                int current = -1;

                for (int i = 0; i < listView.Items.Count; i++)
                {
                    if (listView.Items[i].Checked)
                    {
                        ++current;
                        tableNames[current] = Utilies.RemoveEmptySpaces(Context.databaseTables[i].EntityName);
                    }
                }


                string currentFilePath = string.Empty;

                for (int i = 0; i < tableNames.Length; i++)
                {
                    alColumns.Clear();
                    alTableRelations.Clear();

                    alColumns.Add(Context.databaseTables[i].Columns);
                    alTableRelations.Add(Context.databaseTables[i].Relations);

                    currentFilePath = path + tableNames[i] + "DataAccess";


                    generator.GeneratePersistentObject(tableNames[i], namespaceName, currentFilePath);
                }
            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 2
0
 private void GetSelectedEntities(ref List <string> listTables, ref List <string> listEntities)
 {
     //get the entity names
     for (int i = 0; i < listView.Items.Count; i++)
     {
         if (listView.Items[i].Checked)
         {
             listEntities.Add(Utilies.RemoveEmptySpaces(GeneratorContext.CurrentDatabaseTables[i].EntityName));
             listTables.Add(GeneratorContext.CurrentDatabaseTables[i].TableName);
         }
     }
 }
Ejemplo n.º 3
0
        private void OnStartLoading()
        {
            try
            {
                //load the table names
                scTableNames = Schema.GetTableList(Context.currentDatabaseServer, Context.currentConnectionString);

                // init tables
                Context.databaseTables = new DatabaseTable[scTableNames.Count];

                for (int i = 0; i < scTableNames.Count; i++)
                {
                    try
                    {
                        //load the column info for each table
                        ColumnInfo[] columns   = Schema.GetColumnInfo(Context.currentDatabaseServer, scTableNames[i], Context.currentConnectionString);
                        string       tableName = Utilies.RemoveEmptySpaces(scTableNames[i]);
                        Context.databaseTables[i] = new DatabaseTable(tableName, columns, new TableRelation[0], tableName);
                    }
                    catch
                    {
                        //remove the table from the list
                        scTableNames.Remove(scTableNames[i]);
                        continue;
                    }
                }

                //invoke the delegate on the UI thread
                FinishedLoading fd = new FinishedLoading(OnFinishedLoading);
                Invoke(fd, new object[1] {
                    string.Empty
                });
            }
            catch (Exception ex)
            {
                FinishedLoading fd = new FinishedLoading(OnFinishedLoading);
                Invoke(fd, new object[1] {
                    ex.Message
                });
            }
        }
Ejemplo n.º 4
0
        private void OnStartLoadingDatabaseSchema()
        {
            List <string> listTables = null;

            try
            {
                //load the table names
                listTables = Schema.GetTableList(GeneratorContext.currentDatabaseServer, GeneratorContext.currentConnectionString);

                GeneratorContext.CurrentDatabaseTables.Clear();

                for (int i = 0; i < listTables.Count; i++)
                {
                    try
                    {
                        //load the column info for current table
                        DatabaseColumn[] columns = Schema.GetColumnInfo(GeneratorContext.currentDatabaseServer, listTables[i], GeneratorContext.currentConnectionString);

                        string tableName = Utilies.RemoveEmptySpaces(listTables[i]);

                        GeneratorContext.CurrentDatabaseTables.Add(new DatabaseTable(tableName, columns, Utilies.GetEntityName(tableName)));
                    }
                    catch
                    {
                        continue;
                    }
                }

                //invoke the delegate on the UI thread
                myView.Invoke(finishedLoadingCallback, new object[1] {
                    string.Empty
                });
            }
            catch (Exception ex)
            {
                myView.Invoke(finishedLoadingCallback, new object[1] {
                    ex.Message
                });
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Generates the mapping files
        /// </summary>
        /// <param name="entityNames"></param>
        /// <param name="tableNames">String array with table names</param>
        /// <param name="alColumns">ArrayList with columns for each table name</param>
        /// <param name="alTableRelations"></param>
        /// <param name="fileName">The file path</param>
        /// <param name="namespaceName">Namespace name</param>
        public void GenerateTableMetadatata(string[] entityNames, string[] tableNames, ArrayList alColumns, ArrayList alTableRelations, string fileName, string namespaceName)
        {
            if (entityNames.Length != alColumns.Count)
            {
                throw new ArgumentException();
            }

            if (!fileName.EndsWith(".cs"))
            {
                fileName = fileName + ".cs";
            }


            FileStream   fs = null;
            StreamWriter sw = null;

            try
            {
                if (File.Exists(fileName))
                {
                    fs = new FileStream(fileName, FileMode.Truncate, FileAccess.Write);
                }
                else
                {
                    fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                }

                sw = new StreamWriter(fs);

                namespaceName = Utilies.RemoveEmptySpaces(namespaceName);

                //write header;
                sw.WriteLine("using System;");
                sw.WriteLine("using System.Data;");
                sw.WriteLine("using voidsoft.DataBlock;");


                sw.WriteLine("");
                sw.WriteLine("namespace " + namespaceName);
                sw.WriteLine("{");

                for (int i = 0; i < entityNames.Length; i++)
                {
                    entityNames[i] = Utilies.RemoveEmptySpaces(entityNames[i].Trim());

                    DatabaseColumn[] column = (DatabaseColumn[])alColumns[i];

                    sw.WriteLine("");
                    sw.WriteLine("      [Serializable()]");
                    sw.WriteLine("		public partial class "+ entityNames[i] + " : TableMetadata");
                    sw.WriteLine("		{");
                    sw.WriteLine("");

                    #region generate the internal enum with myFields

                    sw.WriteLine("                   public enum " + entityNames[i] + "Fields");
                    sw.WriteLine("                   {");

                    for (int ij = 0; ij < column.Length; ij++)
                    {
                        if (ij == column.Length - 1)
                        {
                            sw.WriteLine("                      " + column[ij].Name);
                        }
                        else
                        {
                            sw.WriteLine("                      " + column[ij].Name + ",");
                        }
                    }

                    sw.WriteLine("                  }");

                    #endregion

                    sw.WriteLine("");
                    sw.WriteLine("");


                    sw.WriteLine("			    private DatabaseField[] myFields;");
                    sw.WriteLine("");

                    #region generate ctor

                    sw.WriteLine("		    	public "+ entityNames[i] + "()");
                    sw.WriteLine("			    {");
                    sw.WriteLine("					    myFields = new DatabaseField["+ column.Length + "];");

                    string isPrimaryKey;
                    string isAutoIncremented;


                    for (int x = 0; x < column.Length; x++)
                    {
                        string dataType = column[x].columnDataType;

                        //check the type. If it's a byte[] "rename" it to

                        if (dataType.Trim() == "System.Byte[]")
                        {
                            dataType = "System.Binary";
                        }

                        isPrimaryKey      = column[x].isPrimaryKey.ToString();
                        isAutoIncremented = column[x].isAutoIncremented.ToString();

                        //make it lower case for C#
                        isPrimaryKey      = isPrimaryKey.ToLower();
                        isAutoIncremented = isAutoIncremented.ToLower();

                        sw.WriteLine("                    myFields[" + x + "] = new DatabaseField(DbType." + dataType.Substring(dataType.IndexOf(".") + 1) + ",\"" + column[x].Name + "\"," + isPrimaryKey + "," + isAutoIncremented + "," + "null);");
                    }

                    sw.WriteLine(" ");
                    sw.WriteLine("                        this.currentTableName = \"" + tableNames[i] + "\";");
                    sw.WriteLine("");

                    List <TableRelation> relations = (List <TableRelation>)alTableRelations[i];

                    #region generate relations

                    for (int j = 0; j < relations.Count; j++)
                    {
                        //check the realtion type

                        if (relations[j] is ParentTableRelation)
                        {
                            ParentTableRelation pp = (ParentTableRelation)relations[j];

                            sw.WriteLine("this.listRelations.Add(new ParentTableRelation(\"" + pp.RelatedTableName + "\", \"" + pp.ForeignKeyName + "\"," + "TableRelationCardinality." + pp.RelationCardinality.ToString() + "," + pp.CascadeDeleteChildren.ToString().ToLower() + "));");
                        }
                        else if (relations[j] is ChildTableRelation)
                        {
                            ChildTableRelation ch = (ChildTableRelation)relations[j];
                            sw.WriteLine("this.listRelations.Add(new ChildTableRelation(\"" + ch.RelatedTableName + "\",TableRelationCardinality.OneToOne,\"" + ch.RelatedTableKeyName + "\",\"" + ch.ForeignKeyName + "\"));");
                        }
                        else
                        {
                            //many to many
                            ManyToManyTableRelation mm = (ManyToManyTableRelation)relations[j];
                            sw.WriteLine("this.listRelations.Add(new ManyToManyTableRelation(\"" + mm.RelatedTableName + "\",\"" + mm.IntermediaryTableName + "\",\"" + mm.IntermediaryKeyFieldFromParentTable + "\",\"" + mm.IntermediaryKeyFieldFromChildTable + "\"));");
                        }
                    }


                    sw.WriteLine("");
                    sw.WriteLine("                  }"); //end ctor brace.

                    #endregion

                    #endregion

                    //generate DatabasemyFields

                    sw.WriteLine("");
                    sw.WriteLine("");

                    sw.WriteLine("			public override DatabaseField[] TableFields ");
                    sw.WriteLine("			{");
                    sw.WriteLine("				get{ return myFields;}");
                    sw.WriteLine("				set{myFields = value;}");
                    sw.WriteLine("			}");

                    #region Clone

                    sw.WriteLine("          public " + entityNames[i] + " Clone()");
                    sw.WriteLine("          {");
                    sw.WriteLine("                 return this.Clone<" + entityNames[i] + ">();");
                    sw.WriteLine("          }");

                    #endregion

                    #region generate properties with nullable data types

                    for (int x = 0; x < column.Length; x++)
                    {
                        //generate a empty byte[]
                        if (column[x].columnDataType == "System.Byte[]")
                        {
                            sw.WriteLine("");
                            sw.WriteLine("public " + column[x].columnDataType + " " + column[x].Name);
                            sw.WriteLine("{");
                            sw.WriteLine("    get");
                            sw.WriteLine("    {");
                            sw.WriteLine("         object result = (this.GetField(\"" + column[x].Name + "\")).fieldValue;");
                            sw.WriteLine("         if(result == null)");
                            sw.WriteLine("         {");
                            sw.WriteLine("              return new System.Byte[0];");
                            sw.WriteLine("         }");
                            sw.WriteLine("");
                            sw.WriteLine("          return (" + column[x].columnDataType + ") result;"); // + " (this.GetField(\"" + column[x].Name + "\")).fieldValue;");
                            sw.WriteLine("    }");
                            sw.WriteLine("");
                            sw.WriteLine("    set");
                            sw.WriteLine("    {");
                            sw.WriteLine("          this.SetFieldValue(\"" + column[x].Name + "\", value);");
                            sw.WriteLine("    }");
                            sw.WriteLine("}");
                            sw.WriteLine("");
                        }
                        else if (column[x].columnDataType == "System.String")
                        {
                            sw.WriteLine("");

                            //generate validation attributes
                            if (generateValidationAttributes)
                            {
                                //not null
                                if (!column[x].allowsNull)
                                {
                                    //sw.WriteLine("[EvilAttributes.ValidateRequired(" + column[x].Name + "Null" + ")]");
                                }

                                //also add size check
                                //sw.WriteLine("[EvilAttributes.ValidateMaxLength(" + column[x].fieldLength + ", " + column[x].Name + "MaxLength" + ")]");
                            }

                            sw.WriteLine("public " + column[x].columnDataType + " " + column[x].Name);
                            sw.WriteLine("{");
                            sw.WriteLine("    get");
                            sw.WriteLine("    {");
                            sw.WriteLine("         object result = this.GetField(\"" + column[x].Name + "\").fieldValue; ");
                            sw.WriteLine("         return (result != null) ? result.ToString() : null;");
                            sw.WriteLine("    }");
                            sw.WriteLine("");
                            sw.WriteLine("    set");
                            sw.WriteLine("    {");
                            sw.WriteLine("          this.SetFieldValue(\"" + column[x].Name + "\", value);");
                            sw.WriteLine("    }");
                            sw.WriteLine("}");
                            sw.WriteLine("");
                        }
                        else
                        {
                            //generate nullable types for the rest of the columns

                            if (generateValidationAttributes)
                            {
                                if (!column[x].allowsNull)
                                {
                                    // sw.WriteLine("[EvilAttributes.ValidateRequired(" + column[x].Name + "Null" + ")]");
                                }
                            }


                            if (column[x].allowsNull)
                            {
                                sw.WriteLine("");
                                sw.WriteLine("public " + column[x].columnDataType + "? " + column[x].Name);
                                sw.WriteLine("{");
                                sw.WriteLine("    get");
                                sw.WriteLine("    {");
                                sw.WriteLine("         object result = this.GetField(\"" + column[x].Name + "\").fieldValue; ");

                                sw.WriteLine(" return (result == null || result == DBNull.Value) ? null : (" + column[x].columnDataType + "?) result;");
                                sw.WriteLine("    }");
                                sw.WriteLine("");
                                sw.WriteLine("    set");
                                sw.WriteLine("    {");
                                sw.WriteLine("          this.SetFieldValue(\"" + column[x].Name + "\", value);");
                                sw.WriteLine("    }");
                                sw.WriteLine("}");
                                sw.WriteLine("");
                            }
                            else
                            {
                                sw.WriteLine("");
                                sw.WriteLine("public " + column[x].columnDataType + " " + column[x].Name);
                                sw.WriteLine("{");
                                sw.WriteLine("    get");
                                sw.WriteLine("    {");
                                sw.WriteLine("        return (" + column[x].columnDataType + ") this.GetField(\"" + column[x].Name + "\").fieldValue; ");
                                sw.WriteLine("    }");
                                sw.WriteLine("");
                                sw.WriteLine("    set");
                                sw.WriteLine("    {");
                                sw.WriteLine("          this.SetFieldValue(\"" + column[x].Name + "\", value);");
                                sw.WriteLine("    }");
                                sw.WriteLine("}");
                                sw.WriteLine("");
                            }
                        }
                    }

                    #endregion

                    #region Generate Get methods

                    //generate "Get" methods implementation;

                    for (int j = 0; j < relations.Count; j++)
                    {
                        //check the realtion type

                        if (relations[j] is ParentTableRelation)
                        {
                            ParentTableRelation pp = (ParentTableRelation)relations[j];

                            if (pp.RelationCardinality == RelationCardinality.OneToOne)
                            {
                                sw.WriteLine("public TableMetadata Get" + Utilies.GetEntityName(pp.RelatedTableName) + "()");
                                sw.WriteLine("{");
                                sw.WriteLine("    Type relatedType = Type.GetType(\"" + namespaceName + "." + Utilies.GetEntityName(pp.RelatedTableName) + "\");");
                                sw.WriteLine("    TableMetadata[] result = this.GetRelatedTableData(relatedType);");
                                sw.WriteLine("    return result.Length > 0 ? result[0] : null; ");
                                sw.WriteLine("}");
                            }
                            else if (pp.RelationCardinality == RelationCardinality.OneToMany)
                            {
                                sw.WriteLine("public Array Get" + Utilies.GetEntityName(pp.RelatedTableName) + "()");
                                sw.WriteLine("{");
                                sw.WriteLine("    Type relatedType = Type.GetType(\"" + namespaceName + "." + Utilies.GetEntityName(pp.RelatedTableName) + "\");");
                                sw.WriteLine("    return this.GetRelatedTableData(relatedType);");
                                sw.WriteLine("}");
                            }
                        }
                        else if (relations[j] is ChildTableRelation)
                        {
                            ChildTableRelation pp = (ChildTableRelation)relations[j];
                            sw.WriteLine("public TableMetadata Get" + Utilies.GetEntityName(pp.RelatedTableName) + "()");
                            sw.WriteLine("{");
                            sw.WriteLine("    Type relatedType = Type.GetType(\"" + namespaceName + "." + Utilies.GetEntityName(pp.RelatedTableName) + "\");");
                            sw.WriteLine("    TableMetadata[] result = this.GetRelatedTableData(relatedType);");
                            sw.WriteLine("    return result.Length > 0 ? result[0] : null; ");
                            sw.WriteLine("}");
                        }
                        else
                        {
                            ManyToManyTableRelation pp = (ManyToManyTableRelation)relations[j];
                            sw.WriteLine("public Array Get" + Utilies.GetEntityName(pp.RelatedTableName) + "()");
                            sw.WriteLine("{");
                            sw.WriteLine("    Type relatedType = Type.GetType(\"" + namespaceName + "." + Utilies.GetEntityName(pp.RelatedTableName) + "\");");
                            sw.WriteLine("    Type intermediaryType = Type.GetType(\"" + namespaceName + "." + Utilies.GetEntityName(pp.IntermediaryTableName) + "\");");
                            sw.WriteLine("    return this.GetRelatedTableData(relatedType, intermediaryType);");
                            sw.WriteLine("}");
                        }
                    }

                    #endregion

                    sw.WriteLine("}"); //end class brace
                }


                //end namespace brace
                sw.WriteLine("}");

                sw.Flush();
                sw.Close();
            }
            catch
            {
                throw;
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                }
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Generates the persistent object.
        /// </summary>
        /// <param name="entityName">Name of the object.</param>
        /// <param name="namespaceName">Name of the namespace.</param>
        /// <param name="fileName">Name of the file.</param>
        public void GenerateDataAccessObjects(string entityName,
                                              string namespaceName,
                                              string fileName)
        {
            FileStream   fs = null;
            StreamWriter sw = null;

            try
            {
                entityName    = Utilies.RemoveEmptySpaces(entityName);
                namespaceName = Utilies.RemoveEmptySpaces(namespaceName);

                if (!fileName.EndsWith(".vb"))
                {
                    fileName = fileName + ".vb";
                }


                if (File.Exists(fileName))
                {
                    fs = new FileStream(fileName, FileMode.Truncate, FileAccess.Write);
                }
                else
                {
                    fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                }

                sw = new StreamWriter(fs);

                sw.WriteLine("Imports System");
                sw.WriteLine("Imports System.Data");
                sw.WriteLine("Imports voidsoft.DataBlock");
                sw.WriteLine("Imports System.Collections");
                sw.WriteLine("Imports " + namespaceName);


                sw.WriteLine("Namespace BusinessObjects"); //use BusinessObjects as the namespace name
                sw.WriteLine(" ");
                sw.WriteLine("Public Class " + entityName + "BusinessObject");
                sw.WriteLine("");
                sw.WriteLine("");
                sw.WriteLine("		Private mapped As New "+ entityName + " ()");
                sw.WriteLine("		Private persistent As PersistentObject = Nothing");
                sw.WriteLine("");
                sw.WriteLine("      Public Sub New (database as DatabaseServer,connectionString as String)");
                sw.WriteLine("             persistent = new PersistentObject(database, connectionString, mapped)");
                sw.WriteLine("      End Sub");
                sw.WriteLine("	");
                sw.WriteLine("      Public Sub New (s as Session)");
                sw.WriteLine("             persistent = new PersistentObject(s, mapped)");
                sw.WriteLine("      End Sub");

                sw.WriteLine("      Public Sub New ()");
                sw.WriteLine("              persistent = new PersistentObject(database, mapped)");
                sw.WriteLine("      End Sub");

                #region generate Read Data

                sw.WriteLine("		Public Function GetDataTable() As DataTable");
                sw.WriteLine("			Return persistent.GetDataTable()");
                sw.WriteLine("     End Function");
                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("		Public Function GetDataTable(criteria as QueryCriteria) As DataTable");
                sw.WriteLine("			Return persistent.GetDataTable(criteria)");
                sw.WriteLine("     End Function");
                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("		Public Function GetDataTable(fields() as DatabaseField) As DataTable");
                sw.WriteLine("			Return persistent.GetDataTable(criteria)");
                sw.WriteLine("     End Function");
                sw.WriteLine("");
                sw.WriteLine("");

                //generate GetTableMetadata

                sw.WriteLine("		Public Function Get"+ entityName + "() As " + entityName + "()");
                sw.WriteLine("			Return CType(persistent.GetTableMetadata(), "+ entityName + "()");
                sw.WriteLine("      End Function");
                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("		Public Function Get"+ entityName + "(primaryKeyValue as Object) As " + entityName);
                sw.WriteLine("			Return persistent.GetTableMetadata(primaryKeyValue)");
                sw.WriteLine("      End Function");
                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("		Public Function Get"+ entityName + "(relatedTableName As String, classType As Type, foreignKeyValue As Object) As Array");
                sw.WriteLine("			Return persistent.GetTableMetadata(relatedTableName, classType, foreignKeyValue)");
                sw.WriteLine("      End Function");
                sw.WriteLine("");
                sw.WriteLine("");

                //generate GetFieldList
                sw.WriteLine("		Public Function GetFieldList (criteria As QueryCriteria) As ArrayList");
                sw.WriteLine("			Return persistent.GetFieldList(criteria)");
                sw.WriteLine("      End Function");
                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("		Public Function GetFieldList (field As DatabaseField) As ArrayList");
                sw.WriteLine("			Return persistent.GetFieldList(field)");
                sw.WriteLine("      End Function");
                sw.WriteLine("");
                sw.WriteLine("");

                //generate GetSingleValue
                sw.WriteLine("		Public Function GetValue (criteria As QueryCriteria) As object");
                sw.WriteLine("			Return persistent.GetValue(criteria)");
                sw.WriteLine("      End Function");
                sw.WriteLine("");
                sw.WriteLine("");

                //generate IsUnique
                sw.WriteLine("   Public IsUnique (DatabaseField field, value As Object) As Boolean");
                sw.WriteLine("        	 Return persistent.IsUnique(field, value)");
                sw.WriteLine("	 End Function");
                sw.WriteLine("	 ");
                sw.WriteLine("");
                sw.WriteLine("");


                //generate intrinsec functions
                sw.WriteLine("   Public GetMax (DatabaseField field) As Object");
                sw.WriteLine("        	 Return persistent.GetMax(field)");
                sw.WriteLine("	 End Function");
                sw.WriteLine("	 ");
                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("   Public GetMin (DatabaseField field) As Object");
                sw.WriteLine("        	 Return persistent.GetMin(field)");
                sw.WriteLine("	 End Function");
                sw.WriteLine("	 ");
                sw.WriteLine("");

                sw.WriteLine("   Public GetCount () As Object");
                sw.WriteLine("        	 Return persistent.GetCount()");
                sw.WriteLine("	 End Function");
                sw.WriteLine("	 ");
                sw.WriteLine("");

                #endregion

                //generate Create
                sw.WriteLine("   Public Create (" + entityName + " mappedObject) As Integer");
                sw.WriteLine("        	 Return persistent.Create(mappedObject)");
                sw.WriteLine("	 End Function");
                sw.WriteLine("	 ");


                //generate Update
                sw.WriteLine("   Public Update (" + entityName + " mappedObject) As Integer");
                sw.WriteLine("        	 Return persistent.Update(mappedObject)");
                sw.WriteLine("	 End Function");
                sw.WriteLine("	 ");
                sw.WriteLine("	 ");

                //generate Delete
                sw.WriteLine("   Public Delete (" + entityName + " mappedObject) As Integer");
                sw.WriteLine("        	 Return persistent.Delete(mappedObject)");
                sw.WriteLine("	 End Function");
                sw.WriteLine("	 ");
                sw.WriteLine("	 ");

                sw.WriteLine("   Public Delete (criteria As QueryCriteria) As Integer");
                sw.WriteLine("        	 Return persistent.Delete(criteria)");
                sw.WriteLine("	 End Function");
                sw.WriteLine("	 ");
                sw.WriteLine("	 ");


                sw.WriteLine("	");


                sw.WriteLine("End Class");

                sw.WriteLine("End Namespace"); //end namespace

                sw.Flush();
                sw.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                }

                if (fs != null)
                {
                    fs.Close();
                }
            }
        }
        public void GenerateBusinessObjects(string objectName, string namespaceName, string fileName)
        {
            FileStream   fs = null;
            StreamWriter sw = null;

            try
            {
                objectName    = Utilies.RemoveEmptySpaces(objectName);
                namespaceName = Utilies.RemoveEmptySpaces(namespaceName);

                if (!fileName.EndsWith(".cs"))
                {
                    fileName = fileName + ".cs";
                }

                if (File.Exists(fileName))
                {
                    fs = new FileStream(fileName, FileMode.Truncate, FileAccess.Write);
                }
                else
                {
                    fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                }

                sw = new StreamWriter(fs);

                sw.WriteLine("using System;");
                sw.WriteLine("using System.Data;");
                sw.WriteLine("using voidsoft.DataBlock;");
                sw.WriteLine("using System.Collections;");
                sw.WriteLine("using " + namespaceName + ";");

                sw.WriteLine("namespace " + namespaceName);
                sw.WriteLine("{");
                sw.WriteLine("public partial class " + objectName + "BusinessObject");
                sw.WriteLine("{ ");
                sw.WriteLine("");
                sw.WriteLine("       private " + objectName + " domainObject = new " + objectName + "();");
                sw.WriteLine("       private PersistentObject persistent = null; ");
                sw.WriteLine("");
                sw.WriteLine("");
                sw.WriteLine("      #region Constructors");
                sw.WriteLine("      public " + objectName + "BusinessObject(DatabaseServer database, string connectionString) ");
                sw.WriteLine("      {");
                sw.WriteLine("           persistent = new PersistentObject(database, connectionString, domainObject);");
                sw.WriteLine("      }");
                sw.WriteLine("");
                sw.WriteLine("      ");
                sw.WriteLine("      public " + objectName + "BusinessObject(Session session)");
                sw.WriteLine("      {");
                sw.WriteLine("           persistent = new PersistentObject(session, domainObject);");
                sw.WriteLine("      }");
                sw.WriteLine("");
                sw.WriteLine("      public " + objectName + "BusinessObject()");
                sw.WriteLine("      {");
                sw.WriteLine("           persistent = new PersistentObject(domainObject);");
                sw.WriteLine("      }");
                sw.WriteLine("		#endregion");
                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("       #region generated implementation");

                #region generate Read data

                sw.WriteLine("   public DataTable GetDataTable()");
                sw.WriteLine("	 {");
                sw.WriteLine("        return persistent.GetDataTable();");
                sw.WriteLine("	 }");
                sw.WriteLine("");
                sw.WriteLine("");

/*
 *              sw.WriteLine("   public DataTable GetDataTable(QueryCriteria qc)");
 *              sw.WriteLine("	 {");
 *              sw.WriteLine("        return persistent.GetDataTable(qc);");
 *              sw.WriteLine("	 }");
 *              sw.WriteLine("");
 *              sw.WriteLine("");
 */
                sw.WriteLine("");
                sw.WriteLine("");


                sw.WriteLine("   public " + objectName + "[] Get" + objectName + "()");
                sw.WriteLine("	 {");
                sw.WriteLine("        	 return ("+ objectName + "[])" + "persistent.GetTableMetadata();");
                sw.WriteLine("	 }");
                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("   public " + objectName + " Get" + objectName + "(object primaryKeyValue)");
                sw.WriteLine("   {");
                sw.WriteLine("      	return("+ objectName + ")" + "persistent.GetTableMetadata(primaryKeyValue);");
                sw.WriteLine("   }");
                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("");
                sw.WriteLine("");


                sw.WriteLine("");
                sw.WriteLine("");

                #endregion

                #region generate Create

                sw.WriteLine("	public int Create("+ objectName + " entity)");
                sw.WriteLine("	{");
                sw.WriteLine("		return persistent.Create(entity);");
                sw.WriteLine("	}");
                sw.WriteLine("");
                sw.WriteLine("");

                #endregion

                #region generate Update

                sw.WriteLine("	public int Update("+ objectName + " entity)");
                sw.WriteLine("	{");
                sw.WriteLine("		return persistent.Update(entity);");
                sw.WriteLine("	}");
                sw.WriteLine("");
                sw.WriteLine("");


                #endregion

                #region generate Delete

                sw.WriteLine("	public int Delete("+ objectName + " entity)");
                sw.WriteLine("	{");
                sw.WriteLine("		return persistent.Delete(entity);");
                sw.WriteLine("	}");
                sw.WriteLine("");
                sw.WriteLine("");


                sw.WriteLine("	public int Delete(object id)");
                sw.WriteLine("	{");
                sw.WriteLine("		return persistent.Delete(id);");
                sw.WriteLine("	}");
                sw.WriteLine("");
                sw.WriteLine("");

                #endregion

                sw.WriteLine("       #endregion");

                sw.WriteLine(" ");


                sw.WriteLine("   }"); //end class
                sw.WriteLine("}");    //end namespace.


                sw.Flush();
                sw.Close();
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                }

                if (fs != null)
                {
                    fs.Close();
                }
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="namespaceName"></param>
        /// <param name="path"></param>
        /// <param name="selectedLanguage"></param>
        private void GenerateMappings(string namespaceName, string path, Language selectedLanguage)
        {
            ITableMetadataGenerator generator = null;

            try
            {
                //initialize the generator based on the language

                if (selectedLanguage == Language.CSharp)
                {
                    generator = new CSharpTableMetadataGenerator();
                }
                else if (selectedLanguage == Language.VbNet)
                {
                    generator = new VBTableMetadataGenerator();
                }


                //check path
                if (!path.EndsWith(@"\"))
                {
                    path += @"\";
                }


                string[] entityNames = new string[listView.CheckedItems.Count];
                string[] tableNames  = new string[listView.CheckedItems.Count];

                ArrayList alColumns        = new ArrayList();
                ArrayList alTableRelations = new ArrayList();

                int current = -1;


                //get the entity names
                for (int i = 0; i < listView.Items.Count; i++)
                {
                    if (listView.Items[i].Checked)
                    {
                        ++current;
                        entityNames[current] = Utilies.RemoveEmptySpaces(Context.databaseTables[i].EntityName);
                        tableNames[current]  = Context.databaseTables[i].TableName;
                    }
                }


                string currentFilePath = string.Empty;

                for (int i = 0; i < entityNames.Length; i++)
                {
                    alColumns.Clear();
                    alTableRelations.Clear();

                    foreach (DatabaseTable var in Context.databaseTables)
                    {
                        if (var.EntityName == entityNames[i])
                        {
                            alColumns.Add(var.Columns);
                            alTableRelations.Add(var.Relations);
                            break;
                        }
                    }


                    currentFilePath = path + entityNames[i];

                    generator.GenerateTableMetadatata(new string[1] {
                        entityNames[i]
                    }, new string[1] {
                        tableNames[i]
                    }, alColumns, alTableRelations, currentFilePath, namespaceName);
                }
            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 9
0
        private void AspnetCodeGenerationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AspnetCodeGenerationDialog dialog = null;


            try
            {
                //generate mappings
                menuItemGenerateMappingFiles_Click(null, null);


                //is something selected
                if (listView.CheckedItems.Count != 1)
                {
                    MessageBox.Show("Please choose a single entity to generate ASP.NET code", "DataBlock Modeler", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                if (CheckForPrimaryKeys() == false)
                {
                    return;
                }

                dialog = new AspnetCodeGenerationDialog();

                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    string entityName = string.Empty;
                    string tableName  = string.Empty;

                    ArrayList       alColumns        = new ArrayList();
                    ArrayList       alTableRelations = new ArrayList();
                    TableRelation[] relations        = new TableRelation[0];


                    int current = -1;


                    //get the entity names
                    for (int i = 0; i < listView.Items.Count; i++)
                    {
                        ++current;

                        if (listView.Items[i].Checked)
                        {
                            entityName = Utilies.RemoveEmptySpaces(Context.databaseTables[i].EntityName);
                            tableName  = Context.databaseTables[i].TableName;
                            relations  = Context.databaseTables[i].Relations;

                            break;
                        }
                    }

                    string currentFilePath = string.Empty;

                    AspnetCodeGenerator c = new AspnetCodeGenerator();
                    c.Generate(Context.databaseTables[current].Columns, dialog.NamespaceName, entityName, dialog.GenerateContentPlaceholder, Application.StartupPath + @"\Output", relations);

                    MessageBox.Show("Files were successfully generated.", "DataBlock Modeler", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error occurred while generating the files " + "\n" + ex.Message, "DataBlock Modeler", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (dialog != null)
                {
                    dialog.Dispose();
                }
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Generates the persistent object.
        /// </summary>
        /// <param name="entityName">Name of the object.</param>
        /// <param name="namespaceName">Name of the namespace.</param>
        /// <param name="fileName">Name of the file.</param>
        public void GenerateDataAccessObjects(string entityName, string namespaceName, string fileName)
        {
            FileStream   fs = null;
            StreamWriter sw = null;

            try
            {
                entityName    = Utilies.RemoveEmptySpaces(entityName);
                namespaceName = Utilies.RemoveEmptySpaces(namespaceName);

                if (!fileName.EndsWith(".cs"))
                {
                    fileName = fileName + ".cs";
                }

                if (File.Exists(fileName))
                {
                    fs = new FileStream(fileName, FileMode.Truncate, FileAccess.Write);
                }
                else
                {
                    fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                }

                sw = new StreamWriter(fs);

                sw.WriteLine("using System;");
                sw.WriteLine("using System.Data;");
                sw.WriteLine("using voidsoft.DataBlock;");
                sw.WriteLine("using System.Collections;");
                sw.WriteLine("using " + namespaceName + ";");

                sw.WriteLine("namespace DataAccess"); //use BusinessObjects as the namespace name
                sw.WriteLine("{");
                sw.WriteLine("public class " + entityName + "DataAccess");
                sw.WriteLine("{ ");
                sw.WriteLine("");
                sw.WriteLine("       private " + entityName + " domainObject = new " + entityName + "();");
                sw.WriteLine("       private PersistentObject persistent = null; ");
                sw.WriteLine("");
                sw.WriteLine("");
                sw.WriteLine("      #region Constructors");
                sw.WriteLine("      public " + entityName + "DataAccess(DatabaseServer database, string connectionString) ");
                sw.WriteLine("      {");
                sw.WriteLine("           persistent = new PersistentObject(database, connectionString, domainObject);");
                sw.WriteLine("      }");
                sw.WriteLine("");
                sw.WriteLine("      ");
                sw.WriteLine("      public " + entityName + "DataAccess(Session session)");
                sw.WriteLine("      {");
                sw.WriteLine("           persistent = new PersistentObject(session, domainObject);");
                sw.WriteLine("      }");
                sw.WriteLine("");
                sw.WriteLine("      public " + entityName + "DataAccess()");
                sw.WriteLine("      {");
                sw.WriteLine("           persistent = new PersistentObject(domainObject);");
                sw.WriteLine("      }");
                sw.WriteLine("		#endregion");
                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("       #region generated implementation");

                #region generate Read data

                //generate GetDataTable && GetDataSet
                sw.WriteLine("   public DataTable GetDataTable()");
                sw.WriteLine("	 {");
                sw.WriteLine("        return persistent.GetDataTable();");
                sw.WriteLine("	 }");
                sw.WriteLine("");
                sw.WriteLine("");


                sw.WriteLine("   public DataTable GetDataTable(QueryCriteria qc)");
                sw.WriteLine("	 {");
                sw.WriteLine("        return persistent.GetDataTable(qc);");
                sw.WriteLine("	 }");
                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("   public DataTable GetDataTable(params DatabaseField[] fields)");
                sw.WriteLine("	 {");
                sw.WriteLine("        return persistent.GetDataTable(fields);");
                sw.WriteLine("	 }");
                sw.WriteLine("");
                sw.WriteLine("");

                //generate GetTableMetadata
                sw.WriteLine("   public " + entityName + "[] Get" + entityName + "()");
                sw.WriteLine("	 {");
                sw.WriteLine("        	 return ("+ entityName + "[])" + "persistent.GetTableMetadata();");
                sw.WriteLine("	 }");
                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("   public " + entityName + " Get" + entityName + "(object primaryKeyValue)");
                sw.WriteLine("   {");
                sw.WriteLine("      	return("+ entityName + ")" + "persistent.GetTableMetadata(primaryKeyValue);");
                sw.WriteLine("   }");
                sw.WriteLine("");
                sw.WriteLine("");


                sw.WriteLine("   public " + entityName + "[] Get" + entityName + "(QueryCriteria qc)");
                sw.WriteLine("   {");
                sw.WriteLine("      	return("+ entityName + "[])" + "persistent.GetTableMetadata(qc);");
                sw.WriteLine("   }");
                sw.WriteLine("");
                sw.WriteLine("");


                sw.WriteLine("   public  Array" + " Get" + entityName + "(string relatedTableName, Type classType, object foreignKeyValue)");
                sw.WriteLine("   {");
                sw.WriteLine("     		return persistent.GetTableMetadata(relatedTableName, classType, foreignKeyValue);");
                sw.WriteLine("	 }");
                sw.WriteLine("");
                sw.WriteLine("");

                //Generate ArrayList
                sw.WriteLine("   public ArrayList GetFieldList(QueryCriteria criteria)");
                sw.WriteLine("	 {");
                sw.WriteLine("         return persistent.GetFieldList(criteria);");
                sw.WriteLine("	 }");
                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("   public ArrayList GetFieldList(DatabaseField field)");
                sw.WriteLine("	 {");
                sw.WriteLine("        	 return persistent.GetFieldList(field);");
                sw.WriteLine("	 }");
                sw.WriteLine("");
                sw.WriteLine("");


                //generate GetSingleValue
                sw.WriteLine("   public object GetValue (QueryCriteria criteria)");
                sw.WriteLine("	 {");
                sw.WriteLine("         return persistent.GetValue(criteria);");
                sw.WriteLine("	 }");
                sw.WriteLine("");
                sw.WriteLine("");


                //generate IsUnique
                sw.WriteLine("   public bool IsUnique (DatabaseField field, object value)");
                sw.WriteLine("	 {");
                sw.WriteLine("        	 return persistent.IsUnique(field, value);");
                sw.WriteLine("	 }");
                sw.WriteLine("");
                sw.WriteLine("");

                //generate intrinsec functions
                sw.WriteLine("	public object GetMax(DatabaseField field)");
                sw.WriteLine("	{");
                sw.WriteLine("		return persistent.GetMax(field);");
                sw.WriteLine("	}");
                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("	public object GetMin(DatabaseField field)");
                sw.WriteLine("	{");
                sw.WriteLine("		return persistent.GetMin(field);");
                sw.WriteLine("	}");
                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("	public object GetCount()");
                sw.WriteLine("	{");
                sw.WriteLine("		return persistent.GetCount();");
                sw.WriteLine("	}");
                sw.WriteLine("");
                sw.WriteLine("");

                #endregion

                #region generate Create

                sw.WriteLine("	public int Create("+ entityName + " domainObjectObject)");
                sw.WriteLine("	{");
                sw.WriteLine("		return persistent.Create(domainObjectObject);");
                sw.WriteLine("	}");
                sw.WriteLine("");
                sw.WriteLine("");

                #endregion

                #region generate Update

                sw.WriteLine("	public int Update("+ entityName + " domainObjectObject)");
                sw.WriteLine("	{");
                sw.WriteLine("		return persistent.Update(domainObjectObject);");
                sw.WriteLine("	}");
                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("	public int Update(QueryCriteria criteria)");
                sw.WriteLine("	{");
                sw.WriteLine("		return persistent.Update(criteria);");
                sw.WriteLine("	}");
                sw.WriteLine("");
                sw.WriteLine("");

                #endregion

                #region generate Delete

                sw.WriteLine("	public int Delete("+ entityName + " domainObjectObject)");
                sw.WriteLine("	{");
                sw.WriteLine("		return persistent.Delete(domainObjectObject);");
                sw.WriteLine("	}");
                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("	public int Delete(QueryCriteria criteria)");
                sw.WriteLine("	{");
                sw.WriteLine("		return persistent.Delete(criteria);");
                sw.WriteLine("	}");
                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("	public int Delete(object id)");
                sw.WriteLine("	{");
                sw.WriteLine("		return persistent.Delete(id);");
                sw.WriteLine("	}");
                sw.WriteLine("");
                sw.WriteLine("");

                #endregion

                sw.WriteLine("       #endregion");

                sw.WriteLine(" ");


                sw.WriteLine("   }"); //end class
                sw.WriteLine("}");    //end namespace.


                sw.Flush();
                sw.Close();
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                }

                if (fs != null)
                {
                    fs.Close();
                }
            }
        }