Example #1
0
        public string GetCreateDatabaseCommand(string catalog, string dataFilename, string logFilename)
        {
            var builder = new StringBuilder();

            builder.AppendFormat("CREATE DATABASE {0}", SqlIdentifier.QuoteIdentifier(catalog));
            if (dataFilename != null)
            {
                builder.AppendFormat(" ON PRIMARY (NAME='{0}', FILENAME='{1}')", Path.GetFileName(dataFilename), dataFilename);
                builder.AppendFormat(" LOG ON (NAME='{0}', FILENAME='{1}')", Path.GetFileName(logFilename), logFilename);
            }
            return(builder.ToString());
        }
Example #2
0
        internal static string GetDropDatabaseCommand(string catalog)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("DROP DATABASE {0}", SqlIdentifier.QuoteIdentifier(catalog));
            return(sb.ToString());
        }
Example #3
0
 private string QuoteIdentifier(string name)
 {
     if (sqlIdentifier.NeedToQuote(name))
     {
         return(sqlIdentifier.QuoteIdentifier(name));
     }
     return(name);
 }
Example #4
0
            internal override SqlRow VisitRow(SqlRow row)
            {
                int num   = 0;
                int count = row.Columns.Count;

                while (num < count)
                {
                    SqlColumn key = row.Columns[num];
                    if (num > 0)
                    {
                        sb.Append(", ");
                    }
                    Visit(key.Expression);
                    string name = key.Name;
                    string str2 = InferName(key.Expression, null);
                    if (name == null)
                    {
                        name = str2;
                    }
                    if ((name == null) && !names.TryGetValue(key, out name))
                    {
                        name       = "C" + names.Count;
                        names[key] = name;
                    }

                    if (key.Expression != null && key.Expression.NodeType == SqlNodeType.FunctionCall &&
                        ((SqlFunctionCall)key.Expression).Name.StartsWith("GEN_ID"))
                    {
                        name = null;
                    }

                    if (!string.IsNullOrEmpty(str2))
                    {
                        str2 = SqlIdentifier.UnquoteIdentifier(str2);
                    }

                    if (name != str2 && !string.IsNullOrEmpty(name))
                    {
                        if (!string.IsNullOrEmpty(str2))
                        {
                            sb.Append(" AS ");
                        }

                        if (SqlIdentifier.NeedToQuote(name))
                        {
                            WriteName(SqlIdentifier.QuoteIdentifier(name));
                        }
                        else
                        {
                            WriteName(name);
                        }
                    }
                    num++;
                }
                return(row);
            }
Example #5
0
 private IEnumerable <string> GetCreateForeignKeyCommands(MetaType metaType)
 {
     foreach (var member in metaType.DataMembers)
     {
         if (member.IsDeclaredBy(metaType) && member.IsAssociation)
         {
             MetaAssociation association = member.Association;
             if (association.IsForeignKey)
             {
                 var stringBuilder = new StringBuilder();
                 var thisKey       = BuildKey(association.ThisKey);
                 var otherKey      = BuildKey(association.OtherKey);
                 var otherTable    = association.OtherType.Table.TableName;
                 var mappedName    = member.MappedName;
                 if (mappedName == member.Name)
                 {
                     mappedName = string.Format(CultureInfo.InvariantCulture, "FK_{0}_{1}", SqlIdentifier.UnquoteIdentifier(metaType.Table.TableName),
                                                SqlIdentifier.UnquoteIdentifier(member.Name));
                 }
                 var command     = "ALTER TABLE {0}" + Environment.NewLine + "  ADD CONSTRAINT {1} FOREIGN KEY ({2}) REFERENCES {3}({4})";
                 var otherMember = association.OtherMember;
                 if (otherMember != null)
                 {
                     string deleteRule = association.DeleteRule;
                     if (deleteRule != null)
                     {
                         command += Environment.NewLine + "  ON DELETE " + deleteRule;
                     }
                 }
                 yield return(stringBuilder.AppendFormat(command, new object[]
                 {
                     SqlIdentifier.QuoteCompoundIdentifier(metaType.Table.TableName),
                     SqlIdentifier.QuoteIdentifier(mappedName),
                     SqlIdentifier.QuoteCompoundIdentifier(thisKey),
                     SqlIdentifier.QuoteCompoundIdentifier(otherTable),
                     SqlIdentifier.QuoteCompoundIdentifier(otherKey)
                 }).ToString());
             }
         }
     }
 }
Example #6
0
            //internal override SqlRow VisitRow(SqlRow row)
            //{
            //    int num = 0;
            //    int count = row.Columns.Count;
            //    while (num < count)
            //    {
            //        SqlColumn key = row.Columns[num];
            //        if (num > 0)
            //        {
            //            sb.Append(", ");
            //        }

            //        num++;
            //    }
            //    return row;
            //}

            internal override void VisitRowColumn(SqlColumn key)
            {
                Visit(key.Expression);
                string name = key.Name;
                string str2 = InferName(key.Expression, null);

                if (name == null)
                {
                    name = str2;
                }
                if ((name == null) && !names.TryGetValue(key, out name))
                {
                    name       = "C" + names.Count;
                    names[key] = name;
                }

                if (key.Expression != null && key.Expression.NodeType == SqlNodeType.FunctionCall &&
                    ((SqlFunctionCall)key.Expression).Name.StartsWith("GEN_ID"))
                {
                    name = null;
                }

                if (!string.IsNullOrEmpty(str2))
                {
                    str2 = SqlIdentifier.UnquoteIdentifier(str2);
                }

                if (name != str2 && !string.IsNullOrEmpty(name))
                {
                    if (string.IsNullOrEmpty(str2) && key.Expression == null)
                    {
                        this.WriteName(name);
                    }
                    else
                    {
                        sb.Append(" AS ");

                        if (SqlIdentifier.NeedToQuote(name))
                        {
                            WriteName(SqlIdentifier.QuoteIdentifier(name));
                        }
                        else
                        {
                            WriteName(name);
                        }
                    }
                }
            }
Example #7
0
        private static IEnumerable <String> GetCreateForeignKeyCommands(MetaType type)
        {
            string tableName = type.Table.TableName;

            foreach (MetaDataMember mm in type.DataMembers)
            {
                if (mm.IsDeclaredBy(type) && mm.IsAssociation)
                {
                    MetaAssociation assoc = mm.Association;
                    if (assoc.IsForeignKey)
                    {
                        StringBuilder sb         = new StringBuilder();
                        string        thisKey    = BuildKey(assoc.ThisKey);
                        string        otherKey   = BuildKey(assoc.OtherKey);
                        string        otherTable = assoc.OtherType.Table.TableName;
                        string        name;
                        name = mm.MappedName;
                        if (name == mm.Name)
                        {
                            name = String.Format(Globalization.CultureInfo.InvariantCulture, "FK_{0}_{1}", tableName, mm.Name);
                        }
                        string cmd = "ALTER TABLE {0}" + Environment.NewLine + "  ADD CONSTRAINT {1} FOREIGN KEY ({2}) REFERENCES {3}({4})";
                        //In DLinq we put the constraint on the child object (which triggers the behavior when deleted),
                        //but in SQL it is part of the parent constraint (the parent row gets changed / deleted to satisfy the constraint)
                        MetaDataMember otherMember = mm.Association.OtherMember;
                        if (otherMember != null)
                        {
                            string delConstr = otherMember.Association.DeleteRule;
                            if (delConstr != null)
                            {
                                cmd += Environment.NewLine + "  ON DELETE " + delConstr;
                            }
                        }
                        sb.AppendFormat(cmd,
                                        SqlIdentifier.QuoteCompoundIdentifier(tableName),
                                        SqlIdentifier.QuoteIdentifier(name),
                                        SqlIdentifier.QuoteCompoundIdentifier(thisKey),
                                        SqlIdentifier.QuoteCompoundIdentifier(otherTable),
                                        SqlIdentifier.QuoteCompoundIdentifier(otherKey));
                        yield return(sb.ToString());
                    }
                }
            }
        }
Example #8
0
        internal static string GetCreateTableCommand(MetaTable table)
        {
            StringBuilder sb   = new StringBuilder();
            StringBuilder decl = new StringBuilder();

            BuildFieldDeclarations(table, decl);
            sb.AppendFormat("CREATE TABLE {0}", SqlIdentifier.QuoteCompoundIdentifier(table.TableName));
            sb.Append("(");
            sb.Append(decl.ToString());
            decl = new StringBuilder();
            BuildPrimaryKey(table, decl);
            if (decl.Length > 0)
            {
                string name = String.Format(Globalization.CultureInfo.InvariantCulture, "PK_{0}", table.TableName);
                sb.Append(", ");
                sb.AppendLine();
                sb.AppendFormat("  CONSTRAINT {0} PRIMARY KEY ({1})", SqlIdentifier.QuoteIdentifier(name), decl.ToString());
            }
            sb.AppendLine();
            sb.Append("  )");
            return(sb.ToString());
        }
Example #9
0
        internal static string GetCreateSchemaForTableCommand(MetaTable table)
        {
            StringBuilder sb    = new StringBuilder();
            List <string> parts = new List <string>(SqlIdentifier.GetCompoundIdentifierParts(table.TableName));

            // table names look like this in Yukon (according to MSDN):
            //     [ database_name . [ schema_name ] . | schema_name . ] table_name
            // ... which means that either way, the schema name is the second to last part.

            if ((parts.Count) < 2)
            {
                return(null);
            }

            string schema = parts[parts.Count - 2];

            if (String.Compare(schema, "DBO", StringComparison.OrdinalIgnoreCase) != 0 &&
                String.Compare(schema, "[DBO]", StringComparison.OrdinalIgnoreCase) != 0)
            {
                sb.AppendFormat("CREATE SCHEMA {0}", SqlIdentifier.QuoteIdentifier(schema));
            }
            return(sb.ToString());
        }