Beispiel #1
0
        /// <summary>
        /// 创建表
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="fields"></param>
        /// <param name="tableName"></param>
        public override void CreateTable(DBExtend helper, List <Attribute.FieldAttribute> fields, string tableName)
        {
            var lines = new List <string>();
            //tableName = tableName.ToUpper();
            string        script     = string.Format("create table {0}(\r\n", tableName);
            List <string> list2      = new List <string>();
            string        primaryKey = "id";

            foreach (Attribute.FieldAttribute item in fields)
            {
                if (item.IsPrimaryKey)
                {
                    primaryKey = item.Name;
                }
                string nullStr = item.NotNull ? "NOT NULL" : "";
                string str     = string.Format("{0} {1} {2} ", item.KeyWordName, item.ColumnType, nullStr);
                if (item.IsPrimaryKey)
                {
                    str = " " + item.Name + " INTEGER Not Null Primary Key";
                }
                list2.Add(str);
            }
            script += string.Join(",\r\n", list2.ToArray());
            script += ")";
            string sequenceName   = string.Format("{0}_sequence", tableName);
            string triggerName    = string.Format("{0}_trigge", tableName);
            string sequenceScript = string.Format("Create Sequence {0} MINVALUE 1  MAXVALUE 99999 INCREMENT BY 1 START WITH 1 NOCACHE CYCLE", sequenceName);
            string triggerScript  = string.Format(@"
create or replace trigger {0}
  before insert on {1}   
  for each row
declare
  nextid number;
begin
  IF :new.{3} IS NULL or :new.{3}=0 THEN
    select {2}.nextval 
    into nextid
    from sys.dual;
    :new.{3}:=nextid;
  end if;
end ;", triggerName, tableName, sequenceName, primaryKey);

            lines.Add(sequenceScript);
            //defaultValues.Add(triggerScript); 暂不用触发器,不能编译成功
            //script += script2;
            helper.SetParam("script", script);
            helper.Run("sp_ExecuteScript");
            //helper.SetParam("script", sequenceScript);
            //helper.Run("sp_ExecuteScript");
            //helper.SetParam("script", triggerScript);
            //helper.Run("sp_ExecuteScript");

            foreach (string s in lines)
            {
                try
                {
                    helper.Execute(s);
                }
                catch (Exception ero) { };
            }
        }