예제 #1
0
        public override void GenerateContent(DALEntity entity, String outputFilePath)
        {
            /****************************
            *  // Create File
            ****************************/
            string filename      = entity.EntityName;
            string textBuffer    = String.Empty;
            string tempSprocName = String.Empty;
            string tempParamName = String.Empty;
            int    count         = 0;
            int    listcount     = 0;

            /****************************
            *  // In the MySQL Schema there is no DB schema prefix, thus we can consider the database name the schema
            ****************************/
            entity.SchemaName = entity.DatabaseName;

            string path = String.Format(@"{0}\\{1}.php", outputFilePath, filename);

            using (StreamWriter sw = new StreamWriter(path, false))
            {
                /****************************
                *  // Initial Comments
                ****************************/
                System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
                FileVersionInfo            fvi      = FileVersionInfo.GetVersionInfo(assembly.Location);
                string version = fvi.FileVersion;

                textBuffer  = "<?php\n/*\n";
                textBuffer += "Author:\t\t\tThis code was generated by DALGen version " + version + " available at https://github.com/H0r53/DALGen \n";
                textBuffer += "Date:\t\t\t" + DateTime.Now.ToShortDateString() + "\n";
                textBuffer += "Description:\tCreates the DAL class for  " + entity.EntityName + " table and respective stored procedures\n";
                textBuffer += "\n";
                textBuffer += "*/\n";
                textBuffer += "\n";
                if (!String.IsNullOrWhiteSpace(entity.NamespaceName))
                {
                    textBuffer += "namespace DALGen\\" + entity.EntityName + ";\n";
                }
                sw.WriteLine(textBuffer);


                /****************************
                *  // Begin class definition and define properties
                ****************************/
                textBuffer  = "\n";
                textBuffer += "class " + UpperFirst(entity.EntityName) + " {\n";
                textBuffer += "\n";
                textBuffer += "\t// This is for local purposes only! In hosted environments the db_settings.php file should be outside of the webroot, such as: include(\"/outside-webroot/db_settings.php\");\n";
                textBuffer += "\tprotected static function getDbSettings() { return \"DAL/db_localsettings.php\"; }\n";
                textBuffer += "\n";
                textBuffer += "\t/******************************************************************/\n";
                textBuffer += "\t// Properties\n";
                textBuffer += "\t/******************************************************************/\n";
                textBuffer += "\n";
                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += "\tprotected $" + attribute.AttributeName + ";\n";
                }
                sw.WriteLine(textBuffer);

                /****************************
                *  // Create Constructors
                ****************************/

                textBuffer  = "\n";
                textBuffer += "\t/******************************************************************/\n";
                textBuffer += "\t// Constructors\n";
                textBuffer += "\t/******************************************************************/\n";
                textBuffer += "\tpublic function __construct() {\n";
                textBuffer += "\t\t$argv = func_get_args();\n";
                textBuffer += "\t\tswitch( func_num_args() ) {\n";
                textBuffer += "\t\t\tcase 0:\n";
                textBuffer += "\t\t\t\tself::__constructBase();\n";
                textBuffer += "\t\t\t\tbreak;\n";
                textBuffer += "\t\t\tcase 1:\n";
                textBuffer += "\t\t\t\tself::__constructPK( $argv[0] );\n";
                textBuffer += "\t\t\t\tbreak;\n";

                listcount   = entity.Attributes.Count;
                textBuffer += "\t\t\tcase " + listcount + ":\n";
                textBuffer += "\t\t\t\tself::__constructFull( ";
                for (count = 0; count < listcount; count++)
                {
                    textBuffer += "$argv[" + count + "]";
                    if (count < listcount - 1)
                    {
                        textBuffer += ", ";
                    }
                    else
                    {
                        textBuffer += " );\n";
                    }
                }
                textBuffer += "\t\t}\n";
                textBuffer += "\t}\n";
                sw.WriteLine(textBuffer);

                /****************************
                *  // Base Constructor
                ****************************/

                textBuffer  = "\n";
                textBuffer += "\tpublic function __constructBase() {\n";
                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += "\t\t$this->" + attribute.AttributeName + " = " + GetDataTypeDefaultValue(attribute.DataType, "\"\"") + ";\n";
                }
                textBuffer += "\t}\n";
                sw.WriteLine(textBuffer);

                /****************************
                *  // PK Constructor
                ****************************/

                textBuffer  = "\n";
                textBuffer += "\tpublic function __constructPK($paramId) {\n";
                textBuffer += "\t\t$this->load($paramId);\n";
                textBuffer += "\t}\n";
                sw.WriteLine(textBuffer);

                /****************************
                *  // Full Constructor
                ****************************/

                textBuffer  = "\n";
                textBuffer += "\tpublic function __constructFull(";
                count       = 1;
                listcount   = entity.Attributes.Count;
                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += "$param" + UpperFirst(attribute.AttributeName);
                    if (count++ < listcount)
                    {
                        textBuffer += ",";
                    }
                    else
                    {
                        textBuffer += ") {\n";
                    }
                }
                ;
                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += "\t\t$this->" + attribute.AttributeName + " = $param" + UpperFirst(attribute.AttributeName) + ";\n";
                }
                ;
                textBuffer += "\t}\n";
                sw.WriteLine(textBuffer);

                /****************************
                *  // Accessors / Mutators
                ****************************/

                textBuffer  = "\n";
                textBuffer += "\t/******************************************************************/\n";
                textBuffer += "\t// Accessors / Mutators\n";
                textBuffer += "\t/******************************************************************/\n";
                textBuffer += "\n";
                foreach (var attribute in entity.Attributes)
                {
                    // Accessor
                    textBuffer += "\tpublic function get" + UpperFirst(attribute.AttributeName) + "(){\n";
                    textBuffer += "\t\treturn $this->" + attribute.AttributeName + ";\n";
                    textBuffer += "\t}\n";

                    // Mutator
                    textBuffer += "\tpublic function set" + UpperFirst(attribute.AttributeName) + "($value){\n";
                    textBuffer += "\t\t$this->" + attribute.AttributeName + " = $value;\n";
                    textBuffer += "\t}\n";
                }
                ;
                textBuffer += "\n";
                sw.WriteLine(textBuffer);

                /****************************
                *  // Methods
                ****************************/

                // Skip procedure creation if no primary key is selected
                if (entity.Attributes.Where(x => x.IsPrimaryKey).Count() < 1)
                {
                    sw.Close();
                    return;
                }

                /****************************
                *  // Public Methods
                ****************************/

                textBuffer  = "\t/******************************************************************/\n";
                textBuffer += "\t// Public Methods\n";
                textBuffer += "\t/******************************************************************/\n";
                sw.WriteLine(textBuffer);

                /****************************
                *  // Load
                ****************************/

                textBuffer  = "\n";
                textBuffer += "\tpublic function load($paramId) {\n";
                textBuffer += "\t\tinclude(self::getDbSettings());\n";
                textBuffer += "\t\t$conn = new mysqli($servername, $username, $password, $dbname);\n";
                textBuffer += "\t\t$stmt = $conn->prepare('CALL usp_" + entity.EntityName + "_Load(?)');\n";
                textBuffer += "\t\t$stmt->bind_param('i', $paramId);\n";
                textBuffer += "\t\t$stmt->execute();\n";
                textBuffer += "\n";
                textBuffer += "\t\t$result = $stmt->get_result();\n";
                textBuffer += "\t\tif (!$result) die($conn->error);\n";
                textBuffer += "\n";
                textBuffer += "\t\twhile ($row = $result->fetch_assoc()) {\n";

                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += "\t\t $this->set" + UpperFirst(attribute.AttributeName) + "($row['" + attribute.AttributeName + "']);\n";
                }

                textBuffer += "\t\t}\n";
                textBuffer += "\t}\n";
                sw.WriteLine(textBuffer);

                /****************************
                *  // Save
                ****************************/

                textBuffer  = "\n";
                textBuffer += "\tpublic function save() {\n";
                textBuffer += "\t\tif ($this->get" + UpperFirst(entity.Attributes.Where(x => x.IsPrimaryKey).FirstOrDefault().AttributeName) + "() == 0)\n";
                textBuffer += "\t\t\t$this->insert();\n";
                textBuffer += "\t\telse\n";
                textBuffer += "\t\t\t$this->update();\n";
                textBuffer += "\t}\n";
                sw.WriteLine(textBuffer);

                /****************************
                *  // Private Methods
                ****************************/

                textBuffer  = "\t/******************************************************************/\n";
                textBuffer += "\t// Private Methods\n";
                textBuffer += "\t/******************************************************************/\n";
                textBuffer += "\n";
                sw.WriteLine(textBuffer);

                /****************************
                *  // Insert
                ****************************/

                textBuffer  = "\n";
                textBuffer += "\tprivate function insert() {\n";
                textBuffer += "\t\tinclude(self::getDbSettings());\n";
                textBuffer += "\t\t$conn = new mysqli($servername, $username, $password, $dbname);\n";
                textBuffer += "\t\t$stmt = $conn->prepare('CALL usp_" + entity.EntityName + "_Add(";
                listcount   = entity.Attributes.Where(x => !x.IsPrimaryKey).ToList().Count;
                for (count = 0; count < listcount; count++)
                {
                    textBuffer += "?";
                    if (count < listcount - 1)
                    {
                        textBuffer += ",";
                    }
                    else
                    {
                        textBuffer += ")');\n";
                    }
                }
                count = 1;
                foreach (var attribute in entity.Attributes.Where(x => !x.IsPrimaryKey).ToList())
                {
                    textBuffer += "\t\t$arg" + count++ + " = $this->get" + UpperFirst(attribute.AttributeName) + "();\n";
                }
                textBuffer += "\t\t$stmt->bind_param('";
                foreach (var attribute in entity.Attributes.Where(x => !x.IsPrimaryKey).ToList())
                {
                    textBuffer += GetPHPBindParam(attribute.DataType);
                }
                textBuffer += "',";
                count       = 1;
                foreach (var attribute in entity.Attributes.Where(x => !x.IsPrimaryKey).ToList())
                {
                    textBuffer += "$arg" + count;
                    if (count++ < listcount)
                    {
                        textBuffer += ",";
                    }
                    else
                    {
                        textBuffer += ");\n";
                    }
                }
                textBuffer += "\t\t$stmt->execute();\n";
                textBuffer += "\n";
                textBuffer += "\t\t$result = $stmt->get_result();\n";
                textBuffer += "\t\tif (!$result) die($conn->error);\n";
                textBuffer += "\t\twhile ($row = $result->fetch_assoc()) {\n";
                textBuffer += "\t\t\t// By default, the DALGen generated INSERT procedure returns the scope identity as id\n";
                textBuffer += "\t\t\t$this->load($row['id']);\n";
                textBuffer += "\t\t}\n";
                textBuffer += "\t}\n";
                sw.WriteLine(textBuffer);

                /****************************
                *  // Update
                ****************************/

                textBuffer  = "\n";
                textBuffer += "\tprivate function update() {\n";
                textBuffer += "\t\tinclude(self::getDbSettings());\n";
                textBuffer += "\t\t$conn = new mysqli($servername, $username, $password, $dbname);\n";
                textBuffer += "\t\t$stmt = $conn->prepare('CALL usp_" + entity.EntityName + "_Update(";
                listcount   = entity.Attributes.Count;
                for (count = 0; count < listcount; count++)
                {
                    textBuffer += "?";
                    if (count < listcount - 1)
                    {
                        textBuffer += ",";
                    }
                    else
                    {
                        textBuffer += ")');\n";
                    }
                }
                count = 1;
                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += "\t\t$arg" + count++ + " = $this->get" + UpperFirst(attribute.AttributeName) + "();\n";
                }
                textBuffer += "\t\t$stmt->bind_param('";
                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += GetPHPBindParam(attribute.DataType);
                }
                textBuffer += "',";
                count       = 1;
                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += "$arg" + count;
                    if (count++ < listcount)
                    {
                        textBuffer += ",";
                    }
                    else
                    {
                        textBuffer += ");\n";
                    }
                }
                textBuffer += "\t\t$stmt->execute();\n";
                textBuffer += "\t}\n";
                sw.WriteLine(textBuffer);

                /****************************
                *  // Define SetNullValue function (for use with Search method)
                ****************************/

                textBuffer  = "\tprivate static function setNullValue($value){\n";
                textBuffer += "\t\tif ($value == \"\")\n";
                textBuffer += "\t\t\treturn null;\n";
                textBuffer += "\t\telse\n";
                textBuffer += "\t\t\treturn $value;\n";
                textBuffer += "\t}\n";
                sw.WriteLine(textBuffer);

                /****************************
                *  // Static Methods
                ****************************/

                textBuffer  = "\t/******************************************************************/\n";
                textBuffer += "\t// Static Methods\n";
                textBuffer += "\t/******************************************************************/\n";
                textBuffer += "\n";
                sw.WriteLine(textBuffer);

                /****************************
                *  // Load All
                ****************************/

                textBuffer  = "\n";
                textBuffer += "\tpublic static function loadall() {\n";
                textBuffer += "\t\tinclude(self::getDbSettings());\n";
                textBuffer += "\t\t$conn = new mysqli($servername, $username, $password, $dbname);\n";
                textBuffer += "\t\t$stmt = $conn->prepare('CALL usp_" + entity.EntityName + "_LoadAll()');\n";
                textBuffer += "\t\t$stmt->execute();\n";
                textBuffer += "\n";
                textBuffer += "\t\t$result = $stmt->get_result();\n";
                textBuffer += "\t\tif (!$result) die($conn->error);\n";
                textBuffer += "\t\tif ($result->num_rows > 0) {\n";
                textBuffer += "\t\t\t$arr = array();\n";
                textBuffer += "\t\t\twhile ($row = $result->fetch_assoc()) {\n";

                textBuffer += "\t\t\t\t$" + LowerFirst(entity.EntityName) + " = new " + UpperFirst(entity.EntityName) + "(";
                count       = 0;
                listcount   = entity.Attributes.Count;
                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += "$row['" + attribute.AttributeName + "']";
                    if (++count < listcount)
                    {
                        textBuffer += ",";
                    }
                    else
                    {
                        textBuffer += ");\n";
                    }
                }
                textBuffer += "\t\t\t\t$arr[] = $" + LowerFirst(entity.EntityName) + ";\n";
                textBuffer += "\t\t\t}\n";
                textBuffer += "\t\t\treturn $arr;\n";
                textBuffer += "\t\t}\n";
                textBuffer += "\t\telse {\n";
                textBuffer += "\t\t\treturn array();\n";
                textBuffer += "\t\t}\n";
                textBuffer += "\t}\n";
                sw.WriteLine(textBuffer);

                /****************************
                *  // Remove
                ****************************/

                textBuffer  = "\n";
                textBuffer += "\tpublic static function remove($paramId) {\n";
                textBuffer += "\t\tinclude(self::getDbSettings());\n";
                textBuffer += "\t\t$conn = new mysqli($servername, $username, $password, $dbname);\n";
                textBuffer += "\t\t$stmt = $conn->prepare('CALL usp_" + entity.EntityName + "_Delete(?)');\n";
                textBuffer += "\t\t$stmt->bind_param('i', $paramId);\n";
                textBuffer += "\t\t$stmt->execute();\n";
                textBuffer += "\t}\n";
                sw.WriteLine(textBuffer);

                /****************************
                *  // Search
                ****************************/

                textBuffer  = "\n";
                textBuffer += "\tpublic static function search(";
                count       = 0;
                listcount   = entity.Attributes.Count;
                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += "$param" + UpperFirst(attribute.AttributeName);
                    if (++count < listcount)
                    {
                        textBuffer += ",";
                    }
                    else
                    {
                        textBuffer += ") {\n";
                    }
                }

                textBuffer += "\t\tinclude(self::getDbSettings());\n";
                textBuffer += "\t\t$conn = new mysqli($servername, $username, $password, $dbname);\n";
                textBuffer += "\t\t$stmt = $conn->prepare('CALL usp_" + entity.EntityName + "_Search(";
                listcount   = entity.Attributes.Count;
                for (count = 0; count < listcount; count++)
                {
                    textBuffer += "?";
                    if (count < listcount - 1)
                    {
                        textBuffer += ",";
                    }
                    else
                    {
                        textBuffer += ")');\n";
                    }
                }
                count = 1;
                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += "\t\t$arg" + count++ + " = " + UpperFirst(entity.EntityName) + "::setNullValue($param" + UpperFirst(attribute.AttributeName) + ");\n";
                }
                textBuffer += "\t\t$stmt->bind_param('";
                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += GetPHPBindParam(attribute.DataType);
                }
                textBuffer += "',";
                count       = 1;
                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += "$arg" + count;
                    if (count++ < listcount)
                    {
                        textBuffer += ",";
                    }
                    else
                    {
                        textBuffer += ");\n";
                    }
                }
                textBuffer += "\t\t$stmt->execute();\n";
                textBuffer += "\n";
                textBuffer += "\t\t$result = $stmt->get_result();\n";
                textBuffer += "\t\tif (!$result) die($conn->error);\n";
                textBuffer += "\t\tif ($result->num_rows > 0) {\n";
                textBuffer += "\t\t\t$arr = array();\n";
                textBuffer += "\t\t\twhile ($row = $result->fetch_assoc()) {\n";

                textBuffer += "\t\t\t\t$" + LowerFirst(entity.EntityName) + " = new " + UpperFirst(entity.EntityName) + "(";
                count       = 0;
                listcount   = entity.Attributes.Count;
                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += "$row['" + attribute.AttributeName + "']";
                    if (++count < listcount)
                    {
                        textBuffer += ",";
                    }
                    else
                    {
                        textBuffer += ");\n";
                    }
                }
                textBuffer += "\t\t\t\t$arr[] = $" + LowerFirst(entity.EntityName) + ";\n";
                textBuffer += "\t\t\t}\n";
                textBuffer += "\t\t\treturn $arr;\n";
                textBuffer += "\t\t}\n";
                textBuffer += "\t\telse {\n";
                textBuffer += "\t\t\treturn array();\n";
                textBuffer += "\t\t}\n";
                textBuffer += "\t}\n";
                textBuffer += "}";
                sw.WriteLine(textBuffer);

                sw.Close();
            }
        }
예제 #2
0
        public override void GenerateContent(DALEntity entity, String outputFilePath)
        {
            /****************************
            *  // Create File
            ****************************/
            string filename      = "MySQL_" + entity.EntityName;
            string textBuffer    = String.Empty;
            string tempSprocName = String.Empty;
            string tempParamName = String.Empty;
            int    count         = 0;

            /****************************
            *  // In the MySQL Schema there is no DB schema prefix, thus we can consider the database name the schema
            ****************************/
            entity.SchemaName = entity.DatabaseName;

            string path = String.Format(@"{0}\\{1}.sql", outputFilePath, filename);

            using (StreamWriter sw = new StreamWriter(path, false))
            {
                /****************************
                *  // Initial Comments
                ****************************/
                System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
                FileVersionInfo            fvi      = FileVersionInfo.GetVersionInfo(assembly.Location);
                string version = fvi.FileVersion;

                textBuffer  = "/*\n";
                textBuffer += "Author:\t\t\tThis code was generated by DALGen version " + version + " available at https://github.com/H0r53/DALGen \n";
                textBuffer += "Date:\t\t\t" + DateTime.Now.ToShortDateString() + "\n";
                textBuffer += "Description:\tCreates the " + entity.EntityName + " table and respective stored procedures\n";
                textBuffer += "\n";
                textBuffer += "*/\n";
                sw.WriteLine(textBuffer);

                /****************************
                *  // Generate DB Schema
                ****************************/

                // Set Database Instance
                textBuffer  = "\n";
                textBuffer += "USE " + entity.DatabaseName + ";\n";
                textBuffer += "\n";
                sw.WriteLine(textBuffer);


                // Create Table
                textBuffer  = "\n";
                textBuffer += "--------------------------------------------------------------\n";
                textBuffer += "-- Create table\n";
                textBuffer += "--------------------------------------------------------------\n";
                textBuffer += "\n";
                sw.WriteLine(textBuffer);

                textBuffer  = "\n";
                textBuffer += "CREATE TABLE `" + entity.SchemaName + "`.`" + entity.EntityName + "` (\n";
                count       = 0;
                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += attribute.AttributeName + " " + GetDataTypeString(attribute.DataType, attribute.AttributeSize);
                    textBuffer += (attribute.AutoIncrement) ? " AUTO_INCREMENT" : "";
                    if (++count < entity.Attributes.Count)
                    {
                        textBuffer += ",\n";
                    }
                }

                // Add contraints
                count = 0;
                var listcount = entity.Attributes.Where(x => x.IsPrimaryKey || x.IsForeignKey).ToList().Count;
                foreach (var attribute in entity.Attributes.Where(x => x.IsPrimaryKey || x.IsForeignKey).ToList())
                {
                    if (count < listcount)
                    {
                        textBuffer += ",";
                    }
                    textBuffer += "\n";

                    if (attribute.IsPrimaryKey)
                    {
                        textBuffer += "CONSTRAINT pk_" + entity.EntityName + "_" + attribute.AttributeName
                                      + " PRIMARY KEY (" + attribute.AttributeName + ")\n";
                    }
                    else if (attribute.IsForeignKey)
                    {
                        textBuffer += "CONSTRAINT fk_" + entity.EntityName + "_" + attribute.AttributeName + "_" + attribute.ReferenceEntity + "_"
                                      + attribute.ReferenceAttribute + " FOREIGN KEY (" + attribute.AttributeName + ") REFERENCES "
                                      + attribute.ReferenceEntity + " (" + attribute.ReferenceAttribute + ")\n";
                    }
                    ++count;
                }

                textBuffer += ");\n";
                sw.WriteLine(textBuffer);

                /****************************
                *  // Generate Sprocs
                ****************************/
                textBuffer  = "\n";
                textBuffer += "--------------------------------------------------------------\n";
                textBuffer += "-- Create default SCRUD sprocs for this table\n";
                textBuffer += "--------------------------------------------------------------\n";
                textBuffer += "\n";
                sw.WriteLine(textBuffer);

                // Load
                tempSprocName = "`" + entity.SchemaName + "`.`usp_" + entity.EntityName + "_Load`";
                textBuffer    = "DELIMITER //\n";
                textBuffer   += "CREATE PROCEDURE " + tempSprocName + "\n";
                textBuffer   += "(\n";

                count = 0;
                foreach (var pk in entity.Attributes.Where(x => x.IsPrimaryKey).ToList())
                {
                    tempParamName = "param" + pk.AttributeName + " " + GetDataTypeString(pk.DataType, pk.AttributeSize);
                    textBuffer   += "\t IN " + tempParamName;
                    if (++count < entity.Attributes.Where(x => x.IsPrimaryKey).ToList().Count)
                    {
                        textBuffer += ",";
                    }
                    textBuffer += "\n";
                }

                textBuffer += ")\n";
                textBuffer += "BEGIN\n";
                textBuffer += "\tSELECT\n";

                count = 0;
                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += "\t\t`" + entity.EntityName + "`.`" + attribute.AttributeName + "` AS " + "`" + attribute.AttributeName + "`";
                    if (++count < entity.Attributes.Count)
                    {
                        textBuffer += ",";
                    }
                    textBuffer += "\n";
                }
                textBuffer += "\tFROM `" + entity.EntityName + "`\n";
                textBuffer += "\tWHERE ";

                count     = 0;
                listcount = entity.Attributes.Where(x => x.IsPrimaryKey).ToList().Count;
                foreach (var pk in entity.Attributes.Where(x => x.IsPrimaryKey).ToList())
                {
                    if (count++ != 0)
                    {
                        textBuffer += "\t\tAND ";
                    }
                    else
                    {
                        textBuffer += "\t\t";
                    }

                    tempParamName = "param" + pk.AttributeName;
                    textBuffer   += "`" + entity.EntityName + "`.`" + pk.AttributeName + "` = " + tempParamName;

                    if (count == listcount)
                    {
                        textBuffer += ";";
                    }
                    textBuffer += "\n";
                }
                textBuffer += "END //\n";
                textBuffer += "DELIMITER ;\n";
                sw.WriteLine(textBuffer);


                // LoadAll
                tempSprocName = "`" + entity.SchemaName + "`.`usp_" + entity.EntityName + "_LoadAll`";
                textBuffer    = "DELIMITER //\nCREATE PROCEDURE " + tempSprocName + "()\n";
                textBuffer   += "BEGIN\n";
                textBuffer   += "\tSELECT\n";

                count = 0;
                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += "\t\t`" + entity.EntityName + "`.`" + attribute.AttributeName + "` AS " + "`" + attribute.AttributeName + "`";
                    if (++count < entity.Attributes.Count)
                    {
                        textBuffer += ",";
                    }
                    textBuffer += "\n";
                }
                textBuffer += "\tFROM `" + entity.EntityName + "`;\n";
                textBuffer += "END //\nDELIMITER ;\n";
                sw.WriteLine(textBuffer);

                // Add
                tempSprocName = "`" + entity.SchemaName + "`.`usp_" + entity.EntityName + "_Add`";
                textBuffer    = "DELIMITER //\nCREATE PROCEDURE " + tempSprocName + "\n";
                textBuffer   += "(\n";

                count = 0;
                foreach (var pk in entity.Attributes.Where(x => !x.IsPrimaryKey).ToList())
                {
                    tempParamName = " IN param" + pk.AttributeName + " " + GetDataTypeString(pk.DataType, pk.AttributeSize);
                    textBuffer   += "\t" + tempParamName;
                    if (++count < entity.Attributes.Where(x => !x.IsPrimaryKey).ToList().Count)
                    {
                        textBuffer += ",";
                    }
                    textBuffer += "\n";
                }

                textBuffer += ")\n";
                textBuffer += "BEGIN\n";
                textBuffer += "\tINSERT INTO `" + entity.EntityName + "` (";

                count = 0;
                foreach (var attribute in entity.Attributes.Where(x => !x.IsPrimaryKey).ToList())
                {
                    textBuffer += attribute.AttributeName;
                    if (++count < entity.Attributes.Where(x => !x.IsPrimaryKey).ToList().Count)
                    {
                        textBuffer += ",";
                    }
                    else
                    {
                        textBuffer += ")\n";
                    }
                }
                textBuffer += "\tVALUES (";
                count       = 0;
                foreach (var pk in entity.Attributes.Where(x => !x.IsPrimaryKey).ToList())
                {
                    tempParamName = "param" + pk.AttributeName;
                    textBuffer   += tempParamName;
                    if (++count < entity.Attributes.Where(x => !x.IsPrimaryKey).ToList().Count)
                    {
                        textBuffer += ", ";
                    }
                    else
                    {
                        textBuffer += ");\n";
                    }
                }
                textBuffer += "\t-- Return last inserted ID as result\n";
                textBuffer += "\tSELECT LAST_INSERT_ID() as id;\n";
                textBuffer += "END //\n";
                textBuffer += "DELIMITER ;\n\n";
                sw.WriteLine(textBuffer);


                // Update
                tempSprocName = "`" + entity.SchemaName + "`.`usp_" + entity.EntityName + "_Update`";
                textBuffer    = "DELIMITER //\nCREATE PROCEDURE " + tempSprocName + "\n";
                textBuffer   += "(\n";

                count = 0;
                foreach (var pk in entity.Attributes)
                {
                    tempParamName = "IN param" + pk.AttributeName + " " + GetDataTypeString(pk.DataType, pk.AttributeSize);
                    textBuffer   += "\t" + tempParamName;
                    if (++count < entity.Attributes.Count)
                    {
                        textBuffer += ",";
                    }
                    textBuffer += "\n";
                }

                textBuffer += ")\n";
                textBuffer += "BEGIN\n";
                textBuffer += "\tUPDATE `" + entity.EntityName + "`\n";
                textBuffer += "\tSET ";
                count       = 0;
                foreach (var pk in entity.Attributes.Where(x => !x.IsPrimaryKey).ToList())
                {
                    tempParamName = "param" + pk.AttributeName;
                    textBuffer   += pk.AttributeName + " = " + tempParamName;
                    if (++count < entity.Attributes.Where(x => !x.IsPrimaryKey).ToList().Count)
                    {
                        textBuffer += "\n\t\t,";
                    }
                    else
                    {
                        textBuffer += "\n";
                    }
                }

                textBuffer += "\tWHERE";
                count       = 0;
                listcount   = entity.Attributes.Where(x => x.IsPrimaryKey).ToList().Count;
                foreach (var pk in entity.Attributes.Where(x => x.IsPrimaryKey).ToList())
                {
                    if (count++ != 0)
                    {
                        textBuffer += "\t\tAND ";
                    }
                    else
                    {
                        textBuffer += "\t\t";
                    }

                    tempParamName = "param" + pk.AttributeName;
                    textBuffer   += "`" + entity.EntityName + "`.`" + pk.AttributeName + "` = " + tempParamName;

                    if (count == listcount)
                    {
                        textBuffer += ";";
                    }
                    textBuffer += "\n";
                }

                textBuffer += "END //\n";
                textBuffer += "DELIMITER ;\n\n";
                sw.WriteLine(textBuffer);

                // Delete
                tempSprocName = "`" + entity.SchemaName + "`.`usp_" + entity.EntityName + "_Delete`";
                textBuffer    = "DELIMITER //\nCREATE PROCEDURE " + tempSprocName + "\n";
                textBuffer   += "(\n";

                count = 0;
                foreach (var pk in entity.Attributes.Where(x => x.IsPrimaryKey))
                {
                    tempParamName = "IN param" + pk.AttributeName + " " + GetDataTypeString(pk.DataType, pk.AttributeSize);
                    textBuffer   += "\t" + tempParamName;
                    if (++count < entity.Attributes.Where(x => x.IsPrimaryKey).ToList().Count)
                    {
                        textBuffer += ",";
                    }
                    textBuffer += "\n";
                }

                textBuffer += ")\n";
                textBuffer += "BEGIN\n";
                textBuffer += "\tDELETE FROM `" + entity.EntityName + "`\n";
                textBuffer += "\tWHERE";
                count       = 0;
                listcount   = entity.Attributes.Where(x => x.IsPrimaryKey).ToList().Count;
                foreach (var pk in entity.Attributes.Where(x => x.IsPrimaryKey).ToList())
                {
                    if (count++ != 0)
                    {
                        textBuffer += "\t\tAND ";
                    }
                    else
                    {
                        textBuffer += "\t\t";
                    }

                    tempParamName = "param" + pk.AttributeName;
                    textBuffer   += "`" + entity.EntityName + "`.`" + pk.AttributeName + "` = " + tempParamName;

                    if (count == listcount)
                    {
                        textBuffer += ";";
                    }
                    textBuffer += "\n";
                }

                textBuffer += "END //\n";
                textBuffer += "DELIMITER ;\n\n";
                sw.WriteLine(textBuffer);


                // Search
                tempSprocName = "`" + entity.SchemaName + "`.`usp_" + entity.EntityName + "_Search`";

                textBuffer  = "DELIMITER //\nCREATE PROCEDURE " + tempSprocName + "\n";
                textBuffer += "(\n";

                count = 0;
                foreach (var pk in entity.Attributes)
                {
                    tempParamName = "IN param" + pk.AttributeName + " " + GetDataTypeString(pk.DataType, pk.AttributeSize);
                    textBuffer   += "\t" + tempParamName;
                    if (++count < entity.Attributes.Count)
                    {
                        textBuffer += ",";
                    }
                    textBuffer += "\n";
                }

                textBuffer += ")\n";
                textBuffer += "BEGIN\n";
                textBuffer += "\tSELECT\n";

                count = 0;
                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += "\t\t`" + entity.EntityName + "`.`" + attribute.AttributeName + "` AS " + "`" + attribute.AttributeName + "`";
                    if (++count < entity.Attributes.Count)
                    {
                        textBuffer += ",";
                    }
                    textBuffer += "\n";
                }

                textBuffer += "\tFROM `" + entity.EntityName + "`\n";
                textBuffer += "\tWHERE\n";
                count       = 0;
                listcount   = entity.Attributes.Count;
                foreach (var pk in entity.Attributes)
                {
                    if (count++ != 0)
                    {
                        textBuffer += "\t\tAND ";
                    }
                    else
                    {
                        textBuffer += "\t\t";
                    }

                    tempParamName = "param" + pk.AttributeName;

                    if (IsDateDataType(pk.DataType) && pk.DataType != DataType.DATE)
                    {
                        // For general purposes, any Date comparisons will be based on the calendar date and not the time element
                        textBuffer += "COALESCE(CAST(" + entity.EntityName + ".`" + pk.AttributeName + "` AS DATE), CAST(NOW() AS DATE)) = COALESCE(CAST(";
                        textBuffer += tempParamName + " AS DATE),CAST(" + entity.EntityName + ".`" + pk.AttributeName + "` AS DATE), CAST(NOW() AS DATE))";
                    }
                    else
                    {
                        textBuffer += "COALESCE(" + entity.EntityName + ".`" + pk.AttributeName + "`," + GetDataTypeDefaultValue(pk.DataType) + ") = COALESCE(";
                        textBuffer += tempParamName + "," + entity.EntityName + ".`" + pk.AttributeName + "`," + GetDataTypeDefaultValue(pk.DataType) + ")";
                    }

                    if (count == listcount)
                    {
                        textBuffer += ";";
                    }
                    textBuffer += "\n";
                }
                textBuffer += "END //\n";
                textBuffer += "DELIMITER ;\n\n";
                sw.WriteLine(textBuffer);

                sw.Close();
            }
        }
예제 #3
0
        public override void GenerateContent(DALEntity entity, String outputFilePath)
        {
            /****************************
            *  // Create File
            ****************************/
            string filename      = "TSQL_" + entity.EntityName;
            string textBuffer    = String.Empty;
            string tempSprocName = String.Empty;
            string tempParamName = String.Empty;
            int    count         = 0;

            /****************************
            *  // Set full schema now to simplify generation logic later
            ****************************/
            //entity.SchemaName = (String.IsNullOrWhiteSpace(entity.SchemaName) ? entity.DatabaseName : entity.DatabaseName + "." + entity.SchemaName);

            string path = String.Format(@"{0}\\{1}.sql", outputFilePath, filename);

            using (StreamWriter sw = new StreamWriter(path, false))
            {
                /****************************
                *  // Initial Comments
                ****************************/
                System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
                FileVersionInfo            fvi      = FileVersionInfo.GetVersionInfo(assembly.Location);
                string version = fvi.FileVersion;

                textBuffer  = "/*\n";
                textBuffer += "Author:\t\t\tThis code was generated by DALGen version " + version + " available at https://github.com/H0r53/DALGen \n";
                textBuffer += "Date:\t\t\t" + DateTime.Now.ToShortDateString() + "\n";
                textBuffer += "Description:\tCreates the " + entity.EntityName + " table and respective stored procedures\n";
                textBuffer += "\n";
                textBuffer += "*/\n";
                sw.WriteLine(textBuffer);

                /****************************
                *  // Generate DB Schema
                ****************************/

                // Set Database Instance
                textBuffer  = "\n";
                textBuffer += "USE " + entity.DatabaseName + ";\n";
                textBuffer += "\n";
                sw.WriteLine(textBuffer);


                // Create Table
                textBuffer  = "\n";
                textBuffer += "--------------------------------------------------------------\n";
                textBuffer += "-- Create table\n";
                textBuffer += "--------------------------------------------------------------\n";
                textBuffer += "\n";
                sw.WriteLine(textBuffer);

                textBuffer  = "\n";
                textBuffer += "CREATE TABLE [" + entity.SchemaName + "].[" + entity.EntityName + "] (\n";
                count       = 0;
                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += attribute.AttributeName + " " + GetDataTypeString(attribute.DataType, attribute.AttributeSize);
                    textBuffer += (attribute.AutoIncrement) ? " IDENTITY(1,1)" : "";
                    if (++count < entity.Attributes.Count)
                    {
                        textBuffer += ",";
                    }
                    textBuffer += "\n";
                }

                // Add contraints
                foreach (var attribute in entity.Attributes.Where(x => x.IsPrimaryKey || x.IsForeignKey).ToList())
                {
                    if (attribute.IsPrimaryKey)
                    {
                        textBuffer += "CONSTRAINT pk_" + entity.EntityName + "_" + attribute.AttributeName
                                      + " PRIMARY KEY (" + attribute.AttributeName + ")\n";
                    }
                    else if (attribute.IsForeignKey)
                    {
                        textBuffer += "CONSTRAINT fk_" + entity.EntityName + "_" + attribute.AttributeName + "_" + attribute.ReferenceEntity + "_"
                                      + attribute.ReferenceAttribute + " FOREIGN KEY (" + attribute.AttributeName + ") REFERENCES "
                                      + attribute.ReferenceEntity + " (" + attribute.ReferenceAttribute + ")\n";
                    }
                }

                textBuffer += ");\nGO\n";
                sw.WriteLine(textBuffer);

                /****************************
                *  // Generate Sprocs
                ****************************/
                textBuffer  = "\n";
                textBuffer += "--------------------------------------------------------------\n";
                textBuffer += "-- Create default SCRUD sprocs for this table\n";
                textBuffer += "--------------------------------------------------------------\n";
                textBuffer += "\n";
                sw.WriteLine(textBuffer);

                // Get
                tempSprocName = "[" + entity.SchemaName + "].[usp_" + entity.EntityName + "_Load]";
                textBuffer    = "\n";
                textBuffer   += "IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID('" + tempSprocName + "') AND sysstat & 0xf = 4)\n";
                textBuffer   += "\tDROP PROCEDURE " + tempSprocName + ";\n";
                textBuffer   += "GO\n";
                sw.WriteLine(textBuffer);

                textBuffer  = "CREATE PROCEDURE " + tempSprocName + "\n";
                textBuffer += "(\n";

                count = 0;
                foreach (var pk in entity.Attributes.Where(x => x.IsPrimaryKey).ToList())
                {
                    tempParamName = "@param" + pk.AttributeName + " " + GetDataTypeString(pk.DataType, pk.AttributeSize);
                    textBuffer   += "\t" + tempParamName;
                    if (++count < entity.Attributes.Where(x => x.IsPrimaryKey).ToList().Count)
                    {
                        textBuffer += ",";
                    }
                    textBuffer += "\n";
                }

                textBuffer += ")\n";
                textBuffer += "AS\n";
                textBuffer += "BEGIN\n";
                textBuffer += "\tSET NOCOUNT ON\n";
                textBuffer += "\tDECLARE @Err INT\n\n";
                textBuffer += "\tSELECT\n";

                count = 0;
                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += "\t\t[" + entity.EntityName + "].[" + attribute.AttributeName + "] AS " + "[" + attribute.AttributeName + "]";
                    if (++count < entity.Attributes.Count)
                    {
                        textBuffer += ",";
                    }
                    textBuffer += "\n";
                }
                textBuffer += "\tFROM [" + entity.EntityName + "]\n";
                textBuffer += "\tWHERE\n";

                count = 0;
                foreach (var pk in entity.Attributes.Where(x => x.IsPrimaryKey).ToList())
                {
                    if (count++ != 0)
                    {
                        textBuffer += "\t\tAND ";
                    }
                    else
                    {
                        textBuffer += "\t\t";
                    }

                    tempParamName = "@param" + pk.AttributeName;
                    textBuffer   += "[" + entity.EntityName + "].[" + pk.AttributeName + "] = " + tempParamName + "\n";
                }

                textBuffer += "\n\tSET @Err = @@Error\n";
                textBuffer += "\n\tRETURN @Err\n";
                textBuffer += "END\n";
                textBuffer += "GO\n\n";
                sw.WriteLine(textBuffer);


                // GetAll
                tempSprocName = "[" + entity.SchemaName + "].[usp_" + entity.EntityName + "_LoadAll]";
                textBuffer    = "\n";
                textBuffer   += "IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID('" + tempSprocName + "') AND sysstat & 0xf = 4)\n";
                textBuffer   += "\tDROP PROCEDURE " + tempSprocName + ";\n";
                textBuffer   += "GO\n";
                sw.WriteLine(textBuffer);

                textBuffer  = "CREATE PROCEDURE " + tempSprocName + "\n";
                textBuffer += "AS\n";
                textBuffer += "BEGIN\n";
                textBuffer += "\tSET NOCOUNT ON\n";
                textBuffer += "\tDECLARE @Err INT\n\n";
                textBuffer += "\tSELECT\n";

                count = 0;
                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += "\t\t[" + entity.EntityName + "].[" + attribute.AttributeName + "] AS " + "[" + attribute.AttributeName + "]";
                    if (++count < entity.Attributes.Count)
                    {
                        textBuffer += ",";
                    }
                    textBuffer += "\n";
                }
                textBuffer += "\tFROM [" + entity.EntityName + "]\n";
                textBuffer += "\n\tSET @Err = @@Error\n";
                textBuffer += "\n\tRETURN @Err\n";
                textBuffer += "END\n";
                textBuffer += "GO\n\n";
                sw.WriteLine(textBuffer);

                // Add
                tempSprocName = "[" + entity.SchemaName + "].[usp_" + entity.EntityName + "_Add]";
                textBuffer    = "\n";
                textBuffer   += "IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID('" + tempSprocName + "') AND sysstat & 0xf = 4)\n";
                textBuffer   += "\tDROP PROCEDURE " + tempSprocName + ";\n";
                textBuffer   += "GO\n";
                sw.WriteLine(textBuffer);

                textBuffer  = "CREATE PROCEDURE " + tempSprocName + "\n";
                textBuffer += "(\n";

                count = 0;
                foreach (var pk in entity.Attributes.Where(x => !x.IsPrimaryKey).ToList())
                {
                    tempParamName = "@param" + pk.AttributeName + " " + GetDataTypeString(pk.DataType, pk.AttributeSize);
                    textBuffer   += "\t" + tempParamName;
                    if (++count < entity.Attributes.Where(x => !x.IsPrimaryKey).ToList().Count)
                    {
                        textBuffer += ",";
                    }
                    textBuffer += "\n";
                }

                textBuffer += ")\n";
                textBuffer += "AS\n";
                textBuffer += "BEGIN\n";
                textBuffer += "\tSET NOCOUNT ON\n";
                textBuffer += "\tDECLARE @Err INT\n\n";
                textBuffer += "\tINSERT INTO [" + entity.EntityName + "] (";

                count = 0;
                foreach (var attribute in entity.Attributes.Where(x => !x.IsPrimaryKey).ToList())
                {
                    textBuffer += attribute.AttributeName;
                    if (++count < entity.Attributes.Where(x => !x.IsPrimaryKey).ToList().Count)
                    {
                        textBuffer += ",";
                    }
                    else
                    {
                        textBuffer += ")\n";
                    }
                }
                textBuffer += "\tVALUES (";
                count       = 0;
                foreach (var pk in entity.Attributes.Where(x => !x.IsPrimaryKey).ToList())
                {
                    tempParamName = "@param" + pk.AttributeName;
                    textBuffer   += tempParamName;
                    if (++count < entity.Attributes.Where(x => !x.IsPrimaryKey).ToList().Count)
                    {
                        textBuffer += ", ";
                    }
                    else
                    {
                        textBuffer += ")\n";
                    }
                }

                textBuffer += "\n\tSET @Err = @@Error\n";
                textBuffer += "\n\tRETURN @Err\n";
                textBuffer += "END\n";
                textBuffer += "GO\n\n";
                sw.WriteLine(textBuffer);


                // Update
                tempSprocName = "[" + entity.SchemaName + "].[usp_" + entity.EntityName + "_Update]";
                textBuffer    = "\n";
                textBuffer   += "IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID('" + tempSprocName + "') AND sysstat & 0xf = 4)\n";
                textBuffer   += "\tDROP PROCEDURE " + tempSprocName + ";\n";
                textBuffer   += "GO\n";
                sw.WriteLine(textBuffer);

                textBuffer  = "CREATE PROCEDURE " + tempSprocName + "\n";
                textBuffer += "(\n";

                count = 0;
                foreach (var pk in entity.Attributes)
                {
                    tempParamName = "@param" + pk.AttributeName + " " + GetDataTypeString(pk.DataType, pk.AttributeSize);
                    textBuffer   += "\t" + tempParamName;
                    if (++count < entity.Attributes.Count)
                    {
                        textBuffer += ",";
                    }
                    textBuffer += "\n";
                }

                textBuffer += ")\n";
                textBuffer += "AS\n";
                textBuffer += "BEGIN\n";
                textBuffer += "\tSET NOCOUNT ON\n";
                textBuffer += "\tDECLARE @Err INT\n\n";
                textBuffer += "\tUPDATE [" + entity.EntityName + "]\n";
                textBuffer += "\tSET ";
                count       = 0;
                foreach (var pk in entity.Attributes.Where(x => !x.IsPrimaryKey).ToList())
                {
                    tempParamName = "@param" + pk.AttributeName;
                    textBuffer   += pk.AttributeName + " = " + tempParamName;
                    if (++count < entity.Attributes.Where(x => !x.IsPrimaryKey).ToList().Count)
                    {
                        textBuffer += "\n\t\t,";
                    }
                    else
                    {
                        textBuffer += "\n";
                    }
                }

                textBuffer += "\tWHERE\n";
                count       = 0;
                foreach (var pk in entity.Attributes.Where(x => x.IsPrimaryKey).ToList())
                {
                    if (count++ != 0)
                    {
                        textBuffer += "\t\tAND ";
                    }
                    else
                    {
                        textBuffer += "\t\t";
                    }

                    tempParamName = "@param" + pk.AttributeName;
                    textBuffer   += "[" + entity.EntityName + "].[" + pk.AttributeName + "] = " + tempParamName + "\n";
                }

                textBuffer += "\n\tSET @Err = @@Error\n";
                textBuffer += "\n\tRETURN @Err\n";
                textBuffer += "END\n";
                textBuffer += "GO\n\n";
                sw.WriteLine(textBuffer);

                // Delete
                tempSprocName = "[" + entity.SchemaName + "].[usp_" + entity.EntityName + "_Delete]";
                textBuffer    = "\n";
                textBuffer   += "IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID('" + tempSprocName + "') AND sysstat & 0xf = 4)\n";
                textBuffer   += "\tDROP PROCEDURE " + tempSprocName + ";\n";
                textBuffer   += "GO\n";
                sw.WriteLine(textBuffer);

                textBuffer  = "CREATE PROCEDURE " + tempSprocName + "\n";
                textBuffer += "(\n";

                count = 0;
                foreach (var pk in entity.Attributes.Where(x => x.IsPrimaryKey))
                {
                    tempParamName = "@param" + pk.AttributeName + " " + GetDataTypeString(pk.DataType, pk.AttributeSize);
                    textBuffer   += "\t" + tempParamName;
                    if (++count < entity.Attributes.Where(x => x.IsPrimaryKey).ToList().Count)
                    {
                        textBuffer += ",";
                    }
                    textBuffer += "\n";
                }

                textBuffer += ")\n";
                textBuffer += "AS\n";
                textBuffer += "BEGIN\n";
                textBuffer += "\tSET NOCOUNT ON\n";
                textBuffer += "\tDECLARE @Err INT\n\n";
                textBuffer += "\tDELETE FROM [" + entity.EntityName + "]\n";
                textBuffer += "\tWHERE\n";
                count       = 0;
                foreach (var pk in entity.Attributes.Where(x => x.IsPrimaryKey).ToList())
                {
                    if (count++ != 0)
                    {
                        textBuffer += "\t\tAND ";
                    }
                    else
                    {
                        textBuffer += "\t\t";
                    }

                    tempParamName = "@param" + pk.AttributeName;
                    textBuffer   += "[" + entity.EntityName + "].[" + pk.AttributeName + "] = " + tempParamName + "\n";
                }

                textBuffer += "\n\tSET @Err = @@Error\n";
                textBuffer += "\n\tRETURN @Err\n";
                textBuffer += "END\n";
                textBuffer += "GO\n\n";
                sw.WriteLine(textBuffer);


                // Search
                tempSprocName = "[" + entity.SchemaName + "].[usp_" + entity.EntityName + "_Search]";
                textBuffer    = "\n";
                textBuffer   += "IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID('" + tempSprocName + "') AND sysstat & 0xf = 4)\n";
                textBuffer   += "\tDROP PROCEDURE " + tempSprocName + ";\n";
                textBuffer   += "GO\n";
                sw.WriteLine(textBuffer);

                textBuffer  = "CREATE PROCEDURE " + tempSprocName + "\n";
                textBuffer += "(\n";

                count = 0;
                foreach (var pk in entity.Attributes)
                {
                    tempParamName = "@param" + pk.AttributeName + " " + GetDataTypeString(pk.DataType, pk.AttributeSize);
                    textBuffer   += "\t" + tempParamName + " = NULL";
                    if (++count < entity.Attributes.Count)
                    {
                        textBuffer += ",";
                    }
                    textBuffer += "\n";
                }

                textBuffer += ")\n";
                textBuffer += "AS\n";
                textBuffer += "BEGIN\n";
                textBuffer += "\tSET NOCOUNT ON\n";
                textBuffer += "\tDECLARE @Err INT\n\n";
                textBuffer += "\tSELECT\n";

                count = 0;
                foreach (var attribute in entity.Attributes)
                {
                    textBuffer += "\t\t[" + entity.EntityName + "].[" + attribute.AttributeName + "] AS " + "[" + attribute.AttributeName + "]";
                    if (++count < entity.Attributes.Count)
                    {
                        textBuffer += ",";
                    }
                    textBuffer += "\n";
                }

                textBuffer += "\tFROM [" + entity.EntityName + "]\n";
                textBuffer += "\tWHERE\n";
                count       = 0;
                foreach (var pk in entity.Attributes)
                {
                    if (count++ != 0)
                    {
                        textBuffer += "\t\tAND ";
                    }
                    else
                    {
                        textBuffer += "\t\t";
                    }

                    tempParamName = "@param" + pk.AttributeName;

                    if (IsDateDataType(pk.DataType) && pk.DataType != DataType.DATE)
                    {
                        // For general purposes, any Date comparisons will be based on the calendar date and not the time element
                        textBuffer += "COALESCE(CAST(" + entity.EntityName + ".[" + pk.AttributeName + "] AS DATE), CAST(GETDATE() AS DATE)) = COALESCE(CAST(";
                        textBuffer += tempParamName + " AS DATE),CAST(" + entity.EntityName + ".[" + pk.AttributeName + "] AS DATE), CAST(GETDATE() AS DATE))\n";
                    }
                    else
                    {
                        textBuffer += "COALESCE(" + entity.EntityName + ".[" + pk.AttributeName + "]," + GetDataTypeDefaultValue(pk.DataType) + ") = COALESCE(";
                        textBuffer += tempParamName + "," + entity.EntityName + ".[" + pk.AttributeName + "]," + GetDataTypeDefaultValue(pk.DataType) + ")\n";
                    }
                }

                textBuffer += "\n\tSET @Err = @@Error\n";
                textBuffer += "\n\tRETURN @Err\n";
                textBuffer += "END\n";
                textBuffer += "GO\n\n";
                sw.WriteLine(textBuffer);


                sw.Close();
            }
        }
예제 #4
0
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            // First, validate each input
            InputValidationResult validationResult = ValidateInput();

            if (validationResult != InputValidationResult.VALID)
            {
                ShowInputErrorDialog(validationResult);
            }
            else
            {
                // Build DALEntity and DALAttributes
                DALEntity entity = new DALEntity(txtEntityName.Text, txtDatabaseName.Text, txtSchemaName.Text, txtNamespace.Text);
                foreach (SQLAttributePicker attributePicker in attributeList)
                {
                    DALAttributes attribute = new DALAttributes(attributePicker.AttributeName, attributePicker.DataType, attributePicker.AttributeSize, attributePicker.IsPrimaryKey, attributePicker.AutoIncrement, attributePicker.IsForeignKey, attributePicker.ReferenceEntity, attributePicker.ReferenceAttribute);
                    entity.Attributes.Add(attribute);
                }

                String outputPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

                if (radTSQL.Checked)
                {
                    // Generate TSQL Create Table and Stored Procedure scripts.
                    var tsqlGenerator = new TSQLTemplate();
                    tsqlGenerator.GenerateContent(entity, outputPath);
                }
                if (radMySQL.Checked)
                {
                    // Generate MySQL Create Table and Stored Procedure scripts.
                    var mysqlGenerator = new MySQLTemplate();
                    mysqlGenerator.GenerateContent(entity, outputPath);
                }
                if (radOracle.Checked)
                {
                    // Generate Oracle Create Table and Stored Procedure scripts.
                }
                if (chkCPP.Checked)
                {
                    // Generate C++ DAL
                }
                if (chkCSharp.Checked)
                {
                    // Generate C# DAL
                }
                if (chkJava.Checked)
                {
                    // Generate Java DAL
                }
                if (chkPython.Checked)
                {
                    // Generate Python DAL
                }
                if (chkPHP.Checked)
                {
                    // Generate PHP DAL
                    var phpGenerator = new PHPTemplate();
                    phpGenerator.GenerateContent(entity, outputPath);
                }

                MessageBox.Show(this, "Success", "Content Generated Successfully", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
예제 #5
0
파일: MainForm.cs 프로젝트: H0r53/DALGen
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            // First, validate each input
            InputValidationResult validationResult = ValidateInput();

            if (validationResult != InputValidationResult.VALID)
            {
                ShowInputErrorDialog(validationResult);
            }
            else
            {
                // Build DALEntity and DALAttributes
                DALEntity entity = new DALEntity(txtEntityName.Text, txtDatabaseName.Text, txtSchemaName.Text, txtNamespace.Text);
                foreach (SQLAttributePicker attributePicker in attributeList)
                {
                    DALAttributes attribute = new DALAttributes(attributePicker.AttributeName, attributePicker.DataType, attributePicker.AttributeSize, attributePicker.IsPrimaryKey, attributePicker.AutoIncrement, attributePicker.IsForeignKey, attributePicker.ReferenceEntity, attributePicker.ReferenceAttribute);
                    entity.Attributes.Add(attribute);
                }

                String outputPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

                if (entity.Attributes.Where(x => x.IsPrimaryKey).Count() < 1)
                {
                    DialogResult result = MessageBox.Show("No primary key has been selected for this entity. Without a primary key several DAL method such as Load and Update cannot function appropriately. For this reason, only a class library will be generated. Would you like to continue?", "Warning", MessageBoxButtons.OKCancel);
                    if (result == DialogResult.Cancel)
                    {
                        return;
                    }
                }

                if (radTSQL.Checked)
                {
                    // Generate TSQL Create Table and Stored Procedure scripts.
                    var tsqlGenerator = new TSQLTemplate();
                    tsqlGenerator.GenerateContent(entity, outputPath);
                }
                if (radMySQL.Checked)
                {
                    // Generate MySQL Create Table and Stored Procedure scripts.
                    var mysqlGenerator = new MySQLTemplate();
                    mysqlGenerator.GenerateContent(entity, outputPath);
                }
                if (radOracle.Checked)
                {
                    // Generate Oracle Create Table and Stored Procedure scripts.
                }
                if (chkCPP.Checked)
                {
                    // Generate C++ DAL
                }
                if (chkCSharp.Checked)
                {
                    // Generate C# DAL
                }
                if (chkJava.Checked)
                {
                    // Generate Java DAL
                }
                if (chkPython.Checked)
                {
                    // Generate Python DAL
                }
                if (chkPHP.Checked)
                {
                    // Generate PHP DAL
                    var phpGenerator = new PHPTemplate();
                    phpGenerator.GenerateContent(entity, outputPath);
                }

                MessageBox.Show(this, "Success", "Content Generated Successfully", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
예제 #6
0
 /// <summary>
 /// This method should be overridden in derived classes to define the syntax of generated content
 /// </summary>
 /// <param name="entity"></param>
 /// <param name="outputFilePath"></param>
 public abstract void GenerateContent(DALEntity entity, String outputFilePath);