protected string CreateTable(XElement actionElement)
        {
            StringBuilder sb       = new StringBuilder();
            IProvider     provider = NDOProviderFactory.Instance[concreteGenerator.ProviderName];

            if (provider == null)
            {
                throw new Exception("Can't find NDO provider '" + concreteGenerator.ProviderName + "'.");
            }

            string dtTableName = actionElement.Attribute("name").Value;

            string tableName = this.provider.GetQualifiedTableName(dtTableName);

            Class cl = FindClass(dtTableName, mappings);

            if (cl != null)
            {
                Connection conn = mappings.FindConnection(cl);
                if (conn != null)
                {
                    concreteGenerator.ConnectToDatabase(conn.Name);
                }
            }

            sb.Append(concreteGenerator.BeginnTable(tableName));
            sb.Append('\n');

            List <XElement> columnElements = actionElement.Elements("CreateColumn").ToList();

            int             vorletzterIndex   = columnElements.Count - 1;
            List <XElement> primaryKeyColumns = columnElements.Where(e =>
            {
                XAttribute attr;
                return((attr = e.Attribute("isPrimary")) != null && String.Compare(attr.Value, "true", true) == 0);
            }).ToList();

            bool hasPrimaryKeyColumns = primaryKeyColumns.Count > 0;

            for (int i = 0; i < columnElements.Count; i++)
            {
                XElement columnElement = columnElements[i];
                string   columnName    = columnElement.Attribute("name").Value;

                bool isPrimary = primaryKeyColumns.Any(e => e.Attribute("name").Value == columnName);

                sb.Append(CreateColumn(columnElement, cl, provider, isPrimary));
                if (i < vorletzterIndex)
                {
                    sb.Append(",");
                    sb.Append('\n');
                }
            }

            if (concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InTable &&
                hasPrimaryKeyColumns)
            {
                sb.Append(",");
                sb.Append('\n');
            }

            if (hasPrimaryKeyColumns && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InTable)
            {
                sb.Append(CreatePrimaryKeyConstraint(primaryKeyColumns, dtTableName, provider));
            }
            else
            {
                sb.Append('\n');
            }
            sb.Append(concreteGenerator.EndTable(tableName));
            sb.Append('\n');

            //			CreateIndex(primaryKeyColumns, sb, dt, provider);

            if (hasPrimaryKeyColumns && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.AfterTable)
            {
                sb.Append(CreatePrimaryKeyConstraint(primaryKeyColumns, dtTableName, provider));
            }

            sb.Append('\n');

            return(sb.ToString());
        }
Example #2
0
        protected string CreateTable(DataTable dt)
        {
            StringBuilder sb       = new StringBuilder();
            IProvider     provider = NDOProviderFactory.Instance[concreteGenerator.ProviderName];

            if (provider == null)
            {
                throw new Exception("Can't find NDO provider '" + concreteGenerator.ProviderName + "'.");
            }

            string tableName = QualifiedTableName.Get(dt.TableName, provider);

            Class cl = FindClass(dt.TableName, mappings);

            if (cl != null)
            {
                Connection conn = mappings.FindConnection(cl);
                if (conn != null)
                {
                    concreteGenerator.ConnectToDatabase(conn.Name);
                }
            }

            sb.Append(concreteGenerator.BeginnTable(tableName));
            sb.Append('\n');

            int vorletzterIndex = dt.Columns.Count - 1;

            DataColumn[] primaryKeyColumns    = dt.PrimaryKey;
            bool         hasPrimaryKeyColumns = primaryKeyColumns.Length > 0;

            for (int i = 0; i < dt.Columns.Count; i++)
            {
                System.Data.DataColumn dc = dt.Columns[i];

                bool isPrimary = false;
                foreach (DataColumn pkc in primaryKeyColumns)
                {
                    if (pkc.ColumnName == dc.ColumnName)
                    {
                        isPrimary = true;
                        break;
                    }
                }

                sb.Append(CreateColumn(dc, cl, provider, isPrimary));
                if (i < vorletzterIndex)
                {
                    sb.Append(",");
                    sb.Append('\n');
                }
            }

            //			vorletzterIndex = dt.ParentRelations.Count - 1;
            //			if (vorletzterIndex > -1)
            //				sb.Append(",");
            //
            //			for (int i = 0; i < dt.ParentRelations.Count; i++)
            //			{
            //				DataRelation dr = dt.ParentRelations[i];
            //				sb.Append(concreteGenerator.CreateForeignKeyConstraint(dr.ChildColumns, dr.ParentColumns, provider.GetQuotedName(dr.RelationName), dr.ParentTable.TableName));
            //				if (i < vorletzterIndex)
            //					sb.Append(",");
            //			}

            if (concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InTable &&
                hasPrimaryKeyColumns)
            {
                sb.Append(",");
                sb.Append('\n');
            }

            if (hasPrimaryKeyColumns && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InTable)
            {
                sb.Append(CreatePrimaryKeyConstraint(primaryKeyColumns, dt, provider));
            }
            else
            {
                sb.Append('\n');
            }
            sb.Append(concreteGenerator.EndTable(tableName));
            sb.Append('\n');

            //			CreateIndex(primaryKeyColumns, sb, dt, provider);

            if (hasPrimaryKeyColumns && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.AfterTable)
            {
                sb.Append(CreatePrimaryKeyConstraint(primaryKeyColumns, dt, provider));
            }

            sb.Append('\n');

            return(sb.ToString());
        }
Example #3
0
        private void CreateTable(DataTable dt, System.IO.StreamWriter stream, NDO.Mapping.NDOMapping mappings)
        {
            IProvider provider = NDOProviderFactory.Instance[concreteGenerator.ProviderName];

            if (provider == null)
            {
                throw new NDOException("Can't find NDO provider '" + concreteGenerator.ProviderName + "'.");
            }

            string tableName = QualifiedTableName.Get(dt.TableName, provider);

            Class cl = FindClass(dt.TableName, mappings);

            if (cl != null)
            {
                Connection conn = mappings.FindConnection(cl);
                if (conn != null)
                {
                    concreteGenerator.ConnectToDatabase(conn.Name);
                }
            }

            stream.WriteLine(concreteGenerator.DropTable(tableName));

            stream.WriteLine(concreteGenerator.BeginnTable(tableName));

            int vorletzterIndex = dt.Columns.Count - 1;

            DataColumn[] primaryKeyColumns    = dt.PrimaryKey;
            bool         hasPrimaryKeyColumns = primaryKeyColumns.Length > 0;

            for (int i = 0; i < dt.Columns.Count; i++)
            {
                System.Data.DataColumn dc = dt.Columns[i];

                bool isPrimary = false;
                foreach (DataColumn pkc in primaryKeyColumns)
                {
                    if (pkc.ColumnName == dc.ColumnName)
                    {
                        isPrimary = true;
                        break;
                    }
                }

                CreateColumn(dc, stream, cl, provider, isPrimary);
                if (i < vorletzterIndex ||
                    (concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InTable && hasPrimaryKeyColumns))
                {
                    stream.WriteLine(",");
                }
            }

            if (hasPrimaryKeyColumns && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InTable)
            {
                CreateConstraint(primaryKeyColumns, stream, dt, provider);
            }
            else
            {
                stream.WriteLine();
            }
            stream.WriteLine(concreteGenerator.EndTable(tableName));

            //			CreateIndex(primaryKeyColumns, stream, dt, provider);

            if (hasPrimaryKeyColumns && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.AfterTable)
            {
                CreateConstraint(primaryKeyColumns, stream, dt, provider);
            }

            stream.WriteLine();
        }