Beispiel #1
0
        private void CreateTable(DataTable dt, System.IO.StreamWriter stream, NDO.Mapping.NDOMapping mappings)
        {
            IProvider provider  = new MySqlProvider();
            string    tableName = QualifiedTableName.Get(dt.TableName, provider);
            Class     cl        = FindClass(dt.TableName, mappings);

            //bool selfGeneratedOid = cl.Oid.FieldName != null;
            if (cl != null)
            {
                string conn  = mappings.FindConnection(cl).Name;
                Regex  regex = new Regex(@"Database\s*=\s*([^;]+)");
                Match  match = regex.Match(conn);
                if (match.Success)
                {
                    stream.WriteLine("USE " + match.Groups[1] + ";");
                }
            }
            else
            {
                stream.WriteLine("/* TODO: Insert correct USE statement */");
            }
            stream.WriteLine("DROP TABLE IF EXISTS " + tableName + ";");
            stream.WriteLine("CREATE TABLE " + 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;
//				if (!selfGeneratedOid)
//					isPrimary = IsPrimary(primaryKeyColumns, dc);
                CreateColumn(dc, stream, cl, provider, IsPrimary(primaryKeyColumns, dc));
                if (i < vorletzterIndex || hasPrimaryKeyColumns)
                {
                    stream.WriteLine(",");
                }
            }
            if (!hasPrimaryKeyColumns)
            {
                stream.WriteLine();
            }
            else
            {
                CreateConstraint(primaryKeyColumns, stream, dt, provider);
            }
            stream.WriteLine(");");
            stream.WriteLine();
        }
        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());
        }
Beispiel #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();
        }