/// <summary>
        /// 解释 CREATE TABLE 命令.
        /// </summary>
        /// <param name="DbParameters">用于缓存在解释过程中可能会产生的参数。</param>
        /// <returns></returns>
        public override string Parsing(ref List <IDbDataParameter> DbParameters)
        {
            TableBuildDescription tableBuild = (TableBuildDescription)this.Description;
            StringBuilder         buffers    = new StringBuilder("CREATE TABLE ");
            StringBuilder         multiPk    = new StringBuilder();
            StringBuilder         fkBuffers  = new StringBuilder();

            buffers.AppendFormat("{0}{1}{2} (", ElemIdentifierL, tableBuild.Name, ElemIdentifierR);
            string columnType = null;
            int    pk_count   = tableBuild.ColumnDefinitions.Count(def => def.PrimaryKey == true);
            DbTableColumnDefinition definition = null;
            DbColumnIdentity        identity   = null;

            for (int i = 0; i < tableBuild.ColumnDefinitions.Count; ++i)
            {
                definition = tableBuild.ColumnDefinitions[i];
                if (string.IsNullOrEmpty(definition.Name))
                {
                    throw new NoNullAllowedException("Undefined column name.");
                }
                columnType = ParseDbType(definition);
                if (string.IsNullOrEmpty(columnType))
                {
                    throw new NoNullAllowedException(string.Format("Type of undefined column: {0}", definition.Name));
                }
                buffers.AppendLine();
                buffers.AppendFormat("\t{0}{1}{2}", ElemIdentifierL, definition.Name, ElemIdentifierR);
                buffers.AppendFormat(" {0} {1}", columnType, definition.NotNull ? "NOT NULL" : "NULL");
                if (definition.Default != null)
                {
                    buffers.AppendFormat(" {0}", ParseDefaultValue(definition, ref DbParameters));
                }
                if (definition.Identity != null)
                {
                    identity = definition.Identity;
                    buffers.Append(" AUTO_INCREMENT");
                }
                if (definition.Unique)
                {
                    buffers.Append(" UNIQUE");
                }
                if (definition.PrimaryKey)
                {
                    if (pk_count > 1) // 联合主键判定.
                    {
                        if (multiPk.Length > 0)
                        {
                            multiPk.AppendFormat(",{0}{1}{2}", ElemIdentifierL, definition.Name, ElemIdentifierR);
                        }
                        else
                        {
                            multiPk.AppendFormat("{0}{1}{2}", ElemIdentifierL, definition.Name, ElemIdentifierR);
                        }
                    }
                    else
                    {
                        buffers.Append(" PRIMARY KEY");
                    }
                }
                if (definition.ForeignKey != null)
                {
                    ParseForeignKey(tableBuild.Name, definition, fkBuffers);
                }
                if (i < (tableBuild.ColumnDefinitions.Count - 1))
                {
                    buffers.Append(",");
                }
            }
            if (pk_count > 1)
            {
                buffers.Append(",").AppendLine();
                buffers.AppendFormat("\tPRIMARY KEY ({0})", multiPk.ToString());
            }
            if (fkBuffers.Length > 0)
            {
                buffers.Append(",").AppendLine().Append(fkBuffers.ToString());
            }
            buffers.AppendLine();
            buffers.AppendFormat(") ENGINE={0} AUTO_INCREMENT={1} DEFAULT CHARSET=utf8;", ((MySqlParserAdapter)Adapter).MysqlEngine, identity.InitValue);
            return(buffers.ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 解释 CREATE TABLE 命令.
        /// </summary>
        /// <param name="DbParameters">用于缓存在解释过程中可能会产生的参数。</param>
        /// <returns></returns>
        public override string Parsing(ref List <IDbDataParameter> DbParameters)
        {
            TableBuildDescription tableBuild = (TableBuildDescription)this.Description;
            StringBuilder         buffers    = new StringBuilder("CREATE TABLE ");
            StringBuilder         multiPk    = new StringBuilder();

            buffers.AppendFormat("{0}{1}{2} (", ElemIdentifierL, tableBuild.Name, ElemIdentifierR);
            string columnType = null;
            int    pk_count   = tableBuild.ColumnDefinitions.Count(def => def.PrimaryKey == true);
            DbTableColumnDefinition definition = null;

            for (int i = 0; i < tableBuild.ColumnDefinitions.Count; ++i)
            {
                definition = tableBuild.ColumnDefinitions[i];
                if (string.IsNullOrEmpty(definition.Name))
                {
                    throw new NoNullAllowedException("Undefined column name.");
                }
                columnType = ParseDbType(definition);
                if (string.IsNullOrEmpty(columnType))
                {
                    throw new NoNullAllowedException(string.Format("Type of undefined column: {0}", definition.Name));
                }
                buffers.AppendLine().AppendFormat("\t{0}{1}{2}", ElemIdentifierL, definition.Name, ElemIdentifierR);
                buffers.AppendFormat(" {0} {1}", columnType, definition.NotNull ? "NOT NULL" : "NULL");
                if (definition.Unique)
                {
                    buffers.Append(" UNIQUE");
                }
                if (definition.Default != null)
                {
                    buffers.AppendFormat(" {0}", ParseDefaultValue(definition, ref DbParameters));
                }
                if (definition.PrimaryKey)
                {
                    if (pk_count > 1) // 联合主键判定.
                    {
                        if (multiPk.Length > 0)
                        {
                            multiPk.AppendFormat(",{0}{1}{2}", ElemIdentifierL, definition.Name, ElemIdentifierR);
                        }
                        else
                        {
                            multiPk.AppendFormat("{0}{1}{2}", ElemIdentifierL, definition.Name, ElemIdentifierR);
                        }
                    }
                    else
                    {
                        buffers.Append(" PRIMARY KEY");
                    }
                }
                if (definition.Identity != null)
                {
                    if (!definition.PrimaryKey)
                    {
                        throw new NotSupportedException(string.Format("AUTOINCREMENT can't be applied to non-primary key column: {0}", definition.Name));
                    }
                    if (pk_count > 1)
                    {
                        throw new NotSupportedException(string.Format("AUTOINCREMENT can't be applied to multi-primary key columns.", definition.Name));
                    }
                    buffers.Append(" AUTOINCREMENT");
                }
                if (definition.ForeignKey != null)
                {
                    ParseForeignKey(definition, buffers);
                }
                if (i < (tableBuild.ColumnDefinitions.Count - 1))
                {
                    buffers.Append(",");
                }
            }
            if (pk_count > 1)
            {
                buffers.Append(",").AppendLine().AppendFormat("\tPRIMARY KEY ({0})", multiPk.ToString());
            }
            buffers.AppendLine().Append(");");
            return(buffers.ToString());
        }
 /// <summary>
 /// 解释 CREATE TABLE 命令.
 /// </summary>
 /// <param name="DbParameters">用于缓存在解释过程中可能会产生的参数。</param>
 /// <returns></returns>
 public override string Parsing(ref List<IDbDataParameter> DbParameters)
 {
     TableBuildDescription tableBuild = (TableBuildDescription)this.Description;
     StringBuilder tableBuffers = new StringBuilder("CREATE TABLE ");
     StringBuilder sequnceBuffers = new StringBuilder();
     StringBuilder pkBuffers = new StringBuilder();
     StringBuilder uniqueBuffers = new StringBuilder();
     StringBuilder fkBuffers = new StringBuilder();
     tableBuffers.AppendFormat("{0}{1}{2} (", ElemIdentifierL, tableBuild.Name, ElemIdentifierR);
     string columnType = null, seqName = null;
     DbTableColumnDefinition definition = null;
     for (int i = 0; i < tableBuild.ColumnDefinitions.Count; ++i)
     {
         definition = tableBuild.ColumnDefinitions[i];
         if (string.IsNullOrEmpty(definition.Name))
             throw new NoNullAllowedException("Undefined column name.");
         columnType = ParseDbType(definition);
         if (string.IsNullOrEmpty(columnType))
             throw new NoNullAllowedException(string.Format("Type of undefined column: {0}", definition.Name));
         tableBuffers.AppendLine().AppendFormat("\t{0}{1}{2}", ElemIdentifierL, definition.Name, ElemIdentifierR);
         tableBuffers.AppendFormat(" {0} {1}", columnType, definition.NotNull ? "NOT NULL" : "NULL");
         if (definition.Default != null && definition.Identity == null)
             tableBuffers.AppendFormat(" {0}", ParseDefaultValue(definition, ref DbParameters));
         if (definition.Identity != null)
         {
             seqName = string.Format("{0}_{1}_seq", tableBuild.Name, definition.Name);
             AddSequnce(definition.Identity, seqName, sequnceBuffers);
             tableBuffers.AppendFormat(" DEFAULT nextval('{0}{1}{2}'::regclass)", ElemIdentifierL, seqName, ElemIdentifierR);
         }
         if (definition.Unique)
         {
             if (uniqueBuffers.Length > 0)
                 uniqueBuffers.AppendFormat(",{0}{1}{2}", ElemIdentifierL, definition.Name, ElemIdentifierR);
             else
                 uniqueBuffers.AppendFormat("{0}{1}{2}", ElemIdentifierL, definition.Name, ElemIdentifierR);
         }
         if (definition.PrimaryKey)
         {
             if (pkBuffers.Length > 0)
                 pkBuffers.AppendFormat(",{0}{1}{2}", ElemIdentifierL, definition.Name, ElemIdentifierR);
             else
                 pkBuffers.AppendFormat("{0}{1}{2}", ElemIdentifierL, definition.Name, ElemIdentifierR);
         }
         if (definition.ForeignKey != null)
             ParseForeignKey(tableBuild.Name, definition, fkBuffers);
         if (i < (tableBuild.ColumnDefinitions.Count - 1))
             tableBuffers.Append(",");
     }
     if (pkBuffers.Length > 0)
     {
         tableBuffers.Append(",").AppendLine();
         tableBuffers.AppendFormat("\tCONSTRAINT {0}PK_{1}{2}", ElemIdentifierL, tableBuild.Name, ElemIdentifierR);
         tableBuffers.AppendFormat(" PRIMARY KEY ({0})", pkBuffers.ToString());
     }
     if (uniqueBuffers.Length > 0)
     {
         tableBuffers.Append(",").AppendLine();
         tableBuffers.AppendFormat("\tCONSTRAINT {0}UK_{1}_UNIQUE{2}", ElemIdentifierL, tableBuild.Name, ElemIdentifierR);
         tableBuffers.AppendFormat(" UNIQUE ({0})", uniqueBuffers.ToString());
     }
     if (fkBuffers.Length > 0)
         tableBuffers.Append(",").AppendLine().Append(fkBuffers.ToString());
     tableBuffers.AppendLine().Append(");");
     return sequnceBuffers.AppendLine().Append(tableBuffers.ToString()).ToString();
 }