Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fieldid"></param>
        public void RemoveField(string fieldid)
        {
            Entity.Table t  = (Entity.Table)Table;
            var          tb = new Data.TableBuilder(t.TableName, this.Server.Connection());
            var          f  = this.Table.Fields.Where(x => x.FieldID == fieldid).FirstOrDefault();

            if (f == null)
            {
                throw new Data.Exception.TableFieldException(this.Table.TableName, fieldid, fieldid + " field dosenot exist");
            }
            try {
                DBColumn c = DBColumn.Column(f.FieldName, f.FieldType, f.Length);
                tb.AddDropField(c);

                tb.DropColumn();
                t.RemoveField(fieldid);
                // write function here to remove field from table;
            }

            catch (System.Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// table create,alter column ,add column,add primary key
        /// </summary>
        public void AcceptChanges(bool issync = false)
        {
            if (this.Table.TableID == "") // new table
            {
                if (issync == false)
                {
                    Data.TableBuilder tb = new Data.TableBuilder(this.Table.TableName, this.Server.Connection());
                    foreach (Entity.Field f in Table.Fields)
                    {
                        if (f.IsPrimaryKey == true)
                        {
                            DBColumn c = DBColumn.Column(f.FieldName, f.FieldType, f.Length);
                            tb.AddField(c);
                            tb.AddPrimaryKeyField(c);
                        }
                        else if (f.IsNullable == true)
                        {
                            tb.AddField(DBColumn.Column(f.FieldName, f.FieldType, f.Length, Tech.Data.DBColumnFlags.Nullable));
                        }
                        else
                        {
                            tb.AddField(DBColumn.Column(f.FieldName, f.FieldType, f.Length));
                        }
                    }
                    try
                    {
                        if (issync == false)
                        {
                            tb.Create();
                        }

                        ((Entity.Table)Table).Save();
                        //}
                    }

                    catch (Data.Exception.TableException ex)
                    {
                        throw ex;
                    }
                    catch (System.Exception ex)
                    {
                        throw ex;
                    }
                }
                else
                {
                    ((Entity.Table)Table).Save();
                }
            }
            else
            {
                Data.TableBuilder tb = new Data.TableBuilder(this.Table.TableName, this.Server.Connection());
                foreach (Entity.Field f in Table.Fields)
                {
                    if (f.isChanged == true || isKeyUpdated(f))
                    {
                        if (f.IsPrimaryKey == true)
                        {
                            DBColumn c = DBColumn.Column(f.FieldName, f.FieldType, f.Length);
                            if (f.FieldID == "")
                            {
                                tb.AddField(c);
                            }
                            else
                            {
                                tb.AlterField(c, f.NewFieldName == null ? "" : f.NewFieldName);
                            }

                            DBColumn k = DBColumn.Column(f.NewFieldName == ""? f.FieldName:f.NewFieldName,
                                                         f.FieldType,
                                                         f.Length,
                                                         Tech.Data.DBColumnFlags.PrimaryKey);
                            tb.AddPrimaryKeyField(c);
                        }
                        else if (f.IsNullable == true)
                        {
                            DBColumn c = DBColumn.Column(f.FieldName,
                                                         f.FieldType,
                                                         f.Length,
                                                         Tech.Data.DBColumnFlags.Nullable);
                            if (f.FieldID == "")
                            {
                                tb.AddField(c);
                            }
                            else
                            {
                                tb.AlterField(c, f.NewFieldName == null ? "" : f.NewFieldName);
                            }
                        }
                        //else if (isPrimaryUpdated == true) {
                        //    /// check primary key removed

                        //}
                        else
                        {
                            DBColumn c = DBColumn.Column(f.FieldName,
                                                         f.FieldType,
                                                         f.Length);
                            if (f.FieldID == "")
                            {
                                tb.AddField(c);
                            }
                            else
                            {
                                tb.AlterField(c, f.NewFieldName == null ? "" : f.NewFieldName);
                            }
                        }
                    }
                }
                try
                {
                    if (tb.getFieldCount() > 0)
                    {
                        if (issync == false)
                        {
                            tb.AlterTable();
                            if (tb.getPrimaryFieldCount() == 0 && isPrimaryUpdated == true)
                            {
                                tb.DropPrimarykey();
                            }
                        }

                        ((Entity.Table)Table).Save();
                    }
                }
                catch (Data.Exception.TableException ex)
                {
                    throw ex;
                }
                catch (System.Exception ex)
                {
                    //   tb.DropTable();
                    //  ((Entity.Table)Table).Remove();
                    throw new System.Exception("due to the error in this transaction, all process rollbacked. Error is," + ex.Message);
                }
            }
        }