Beispiel #1
0
        public virtual string GetCreateTableSql(DiscoveredDatabase database, string tableName, DatabaseColumnRequest[] columns, Dictionary <DatabaseColumnRequest, DiscoveredColumn> foreignKeyPairs, bool cascadeDelete, string schema)
        {
            if (string.IsNullOrWhiteSpace(tableName))
            {
                throw new ArgumentNullException(FAnsiStrings.DiscoveredDatabaseHelper_GetCreateTableSql_Table_name_cannot_be_null, "tableName");
            }

            StringBuilder bodySql = new StringBuilder();

            var server       = database.Server;
            var syntaxHelper = server.GetQuerySyntaxHelper();

            syntaxHelper.ValidateTableName(tableName);

            foreach (DatabaseColumnRequest c in columns)
            {
                syntaxHelper.ValidateColumnName(c.ColumnName);
            }

            //the name sans brackets (hopefully they didn't pass any brackets)
            tableName = syntaxHelper.GetRuntimeName(tableName);

            //the name uflly specified e.g. [db]..[tbl] or `db`.`tbl` - See Test HorribleColumnNames
            var fullyQualifiedName = syntaxHelper.EnsureFullyQualified(database.GetRuntimeName(), schema, tableName);

            bodySql.AppendLine("CREATE TABLE " + fullyQualifiedName + "(");

            foreach (var col in columns)
            {
                var datatype = col.GetSQLDbType(syntaxHelper.TypeTranslater);

                //add the column name and accompanying datatype
                bodySql.AppendLine(GetCreateTableSqlLineForColumn(col, datatype, syntaxHelper) + ",");
            }

            var pks = columns.Where(c => c.IsPrimaryKey).ToArray();

            if (pks.Any())
            {
                bodySql.Append(GetPrimaryKeyDeclarationSql(tableName, pks, syntaxHelper));
            }

            if (foreignKeyPairs != null)
            {
                bodySql.AppendLine();
                bodySql.AppendLine(GetForeignKeyConstraintSql(tableName, syntaxHelper,
                                                              foreignKeyPairs.ToDictionary(k => (IHasRuntimeName)k.Key, v => v.Value), cascadeDelete, null));
            }

            var toReturn = bodySql.ToString().TrimEnd('\r', '\n', ',');

            toReturn += ")" + Environment.NewLine;

            return(toReturn);
        }
Beispiel #2
0
        public virtual string GetCreateTableSql(DiscoveredDatabase database, string tableName, DatabaseColumnRequest[] columns, Dictionary <DatabaseColumnRequest, DiscoveredColumn> foreignKeyPairs, bool cascadeDelete, string schema)
        {
            if (string.IsNullOrWhiteSpace(tableName))
            {
                throw new ArgumentNullException("Table name cannot be null", "tableName");
            }

            string bodySql = "";

            var server       = database.Server;
            var syntaxHelper = server.GetQuerySyntaxHelper();

            //the name sans brackets (hopefully they didn't pass any brackets)
            tableName = syntaxHelper.GetRuntimeName(tableName);

            //the name uflly specified e.g. [db]..[tbl] or `db`.`tbl` - See Test HorribleColumnNames
            var fullyQualifiedName = syntaxHelper.EnsureFullyQualified(database.GetRuntimeName(), schema, tableName);

            bodySql += "CREATE TABLE " + fullyQualifiedName + "(" + Environment.NewLine;

            foreach (var col in columns)
            {
                var datatype = col.GetSQLDbType(syntaxHelper.TypeTranslater);

                //add the column name and accompanying datatype
                bodySql += GetCreateTableSqlLineForColumn(col, datatype, syntaxHelper) + "," + Environment.NewLine;
            }

            var pks = columns.Where(c => c.IsPrimaryKey).ToArray();

            if (pks.Any())
            {
                bodySql += GetPrimaryKeyDeclarationSql(tableName, pks, syntaxHelper);
            }

            if (foreignKeyPairs != null)
            {
                bodySql += Environment.NewLine + GetForeignKeyConstraintSql(tableName, syntaxHelper, foreignKeyPairs, cascadeDelete) + Environment.NewLine;
            }

            bodySql = bodySql.TrimEnd('\r', '\n', ',');

            bodySql += ")" + Environment.NewLine;

            return(bodySql);
        }