/// <summary>获取数据定义语句</summary> /// <param name="schema">数据定义模式</param> /// <param name="values">其它信息</param> /// <returns></returns> public virtual String GetSchemaSQL(DDLSchema schema, params Object[] values) { switch (schema) { case DDLSchema.CreateDatabase: return(CreateDatabaseSQL((String)values[0], (String)values[1])); //case DDLSchema.DropDatabase: // return DropDatabaseSQL((String)values[0]); case DDLSchema.DatabaseExist: return(DatabaseExistSQL(values == null || values.Length < 1 ? null : (String)values[0])); case DDLSchema.CreateTable: return(CreateTableSQL((IDataTable)values[0])); //case DDLSchema.DropTable: // if (values[0] is IDataTable) // return DropTableSQL((IDataTable)values[0]); // else // return DropTableSQL(values[0].ToString()); //case DDLSchema.TableExist: // if (values[0] is IDataTable) // return TableExistSQL((IDataTable)values[0]); // else // return TableExistSQL(values[0].ToString()); case DDLSchema.AddTableDescription: return(AddTableDescriptionSQL((IDataTable)values[0])); case DDLSchema.DropTableDescription: return(DropTableDescriptionSQL((IDataTable)values[0])); case DDLSchema.AddColumn: return(AddColumnSQL((IDataColumn)values[0])); case DDLSchema.AlterColumn: return(AlterColumnSQL((IDataColumn)values[0], values.Length > 1 ? (IDataColumn)values[1] : null)); case DDLSchema.DropColumn: return(DropColumnSQL((IDataColumn)values[0])); case DDLSchema.AddColumnDescription: return(AddColumnDescriptionSQL((IDataColumn)values[0])); case DDLSchema.DropColumnDescription: return(DropColumnDescriptionSQL((IDataColumn)values[0])); case DDLSchema.CreateIndex: return(CreateIndexSQL((IDataIndex)values[0])); case DDLSchema.DropIndex: return(DropIndexSQL((IDataIndex)values[0])); //case DDLSchema.CompactDatabase: // return CompactDatabaseSQL(); default: break; } throw new NotSupportedException("不支持该操作!"); }
public override object SetSchema(DDLSchema schema, params object[] values) { var session = Database.CreateSession(); var databaseName = session.DatabaseName; if (values != null && values.Length > 0 && values[0] is String && values[0] + "" != "") { databaseName = values[0] + ""; //ahuang 2014.06.12 类型强制转string的bug } switch (schema) { case DDLSchema.TableExist: return(session.QueryCount(GetSchemaSQL(schema, values)) > 0); case DDLSchema.DatabaseExist: return(ProcessWithSystem(s => DatabaseExist(databaseName))); case DDLSchema.CreateDatabase: values = new Object[] { databaseName, values == null || values.Length < 2 ? null : values[1] }; var obj = ProcessWithSystem(s => base.SetSchema(schema, values)); // 创建数据库后,需要等待它初始化 Thread.Sleep(5000); return(obj); case DDLSchema.DropDatabase: return(ProcessWithSystem(s => DropDatabase(databaseName))); default: break; } return(base.SetSchema(schema, values)); }
/// <summary>设置数据定义模式</summary> /// <param name="schema">数据定义模式</param> /// <param name="values">其它信息</param> /// <returns></returns> public virtual Object SetSchema(DDLSchema schema, params Object[] values) { //Object obj = null; //switch (schema) //{ // case DDLSchema.CreateTable: // //if (MetaDataCollections.Contains(_.Databases)) // //{ // //} // break; // case DDLSchema.TableExist: // { // String name; // if (values[0] is IDataTable) // name = (values[0] as IDataTable).TableName; // else // name = values[0].ToString(); // var dt = GetSchema(_.Tables, new String[] { null, null, name, "TABLE" }); // if (dt == null || dt.Rows == null || dt.Rows.Count < 1) return false; // return true; // } // case DDLSchema.BackupDatabase: // return Backup((String)values[0], (String)values[1], (Boolean)values[2]); // default: // break; //} var sql = GetSchemaSQL(schema, values); if (String.IsNullOrEmpty(sql)) { return(null); } var session = Database.CreateSession(); if (/*schema == DDLSchema.TableExist ||*/ schema == DDLSchema.DatabaseExist) { return(session.QueryCount(sql) > 0); } // 分隔符是分号加换行,如果不想被拆开执行(比如有事务),可以在分号和换行之间加一个空格 var ss = sql.Split(";" + Environment.NewLine); if (ss == null || ss.Length < 1) { return(session.Execute(sql)); } foreach (var item in ss) { session.Execute(item); } return(0); }
public override Object SetSchema(DDLSchema schema, params Object[] values) { var session = Database.CreateSession(); var databaseName = Database.DatabaseName; // ahuang 2014.06.12 类型强制转string的bug if (values != null && values.Length > 0 && values[0] is String str && !str.IsNullOrEmpty()) { databaseName = str; } switch (schema) { //case DDLSchema.TableExist: // return session.QueryCount(GetSchemaSQL(schema, values)) > 0; case DDLSchema.DatabaseExist: //return ProcessWithSystem(s => DatabaseExist(databaseName)); return(DatabaseExist(databaseName)); case DDLSchema.CreateDatabase: values = new Object[] { databaseName, values == null || values.Length < 2 ? null : values[1] }; //return ProcessWithSystem(s => base.SetSchema(schema, values)); var sql = base.GetSchemaSQL(schema, values); if (sql.IsNullOrEmpty()) { return(null); } if (session is RemoteDbSession ss) { ss.WriteSQL(sql); return(ss.ProcessWithSystem((s, c) => { using (var cmd = Database.Factory.CreateCommand()) { cmd.Connection = c; cmd.CommandText = sql; return cmd.ExecuteNonQuery(); } })); } return(0); //case DDLSchema.DropDatabase: // //return ProcessWithSystem(s => DropDatabase(databaseName)); // return DropDatabase(databaseName); default: break; } return(base.SetSchema(schema, values)); }
//protected override void FixIndex(IDataIndex index, DataRow dr) //{ // base.FixIndex(index, dr); // Boolean b; // if (TryGetDataRowValue<Boolean>(dr, "UNIQUE", out b)) index.Unique = b; // if (TryGetDataRowValue<Boolean>(dr, "PRIMARY", out b)) index.PrimaryKey = b; //} #region 架构定义 public override object SetSchema(DDLSchema schema, params object[] values) { if (schema == DDLSchema.DatabaseExist) { IDbSession session = Database.CreateSession(); DataTable dt = GetSchema(_.Databases, new String[] { values != null && values.Length > 0 ? (String)values[0] : session.DatabaseName }); if (dt == null || dt.Rows == null || dt.Rows.Count < 1) { return(false); } return(true); } return(base.SetSchema(schema, values)); }
/// <summary>设置数据定义模式</summary> /// <param name="schema"></param> /// <param name="values"></param> /// <returns></returns> public override object SetSchema(DDLSchema schema, object[] values) { Object obj = null; switch (schema) { case DDLSchema.CreateTable: obj = base.SetSchema(DDLSchema.CreateTable, values); IDataTable table = values[0] as IDataTable; // Access建表语句的不能操作默认值,所以在这里操作 // 默认值 foreach (IDataColumn item in table.Columns) { if (!String.IsNullOrEmpty(item.Default)) { AddDefault(item, item.Default); } } return(obj); case DDLSchema.AddTableDescription: return(AddTableDescription((IDataTable)values[0], ((IDataTable)values[0]).Description)); case DDLSchema.DropTableDescription: return(DropTableDescription((IDataTable)values[0])); case DDLSchema.AddColumnDescription: return(AddColumnDescription((IDataColumn)values[0], ((IDataColumn)values[0]).Description)); case DDLSchema.DropColumnDescription: return(DropColumnDescription((IDataColumn)values[0])); case DDLSchema.AddDefault: return(AddDefault((IDataColumn)values[0], ((IDataColumn)values[0]).Default)); case DDLSchema.DropDefault: return(DropDefault((IDataColumn)values[0])); default: break; } return(base.SetSchema(schema, values)); }
public override Object SetSchema(DDLSchema schema, params Object[] values) { var session = Database.CreateSession(); var dbname = String.Empty; var databaseName = String.Empty; switch (schema) { case DDLSchema.DatabaseExist: // Oracle不支持判断数据库是否存在 return(true); default: break; } return(base.SetSchema(schema, values)); }
/// <summary>设置数据定义模式</summary> /// <param name="schema"></param> /// <param name="values"></param> /// <returns></returns> public override Object SetSchema(DDLSchema schema, Object[] values) { //Object obj = null; switch (schema) { case DDLSchema.CreateDatabase: CreateDatabase(); return(null); //case DDLSchema.DropDatabase: // DropDatabase(); // return null; case DDLSchema.DatabaseExist: return(File.Exists(FileName)); default: break; } return(base.SetSchema(schema, values)); }
/// <summary>设置数据定义模式</summary> /// <param name="schema">数据定义模式</param> /// <param name="values">其它信息</param> /// <returns></returns> public virtual Object SetSchema(DDLSchema schema, params Object[] values) { var sql = GetSchemaSQL(schema, values); if (String.IsNullOrEmpty(sql)) { return(null); } var session = Database.CreateSession(); if (/*schema == DDLSchema.TableExist ||*/ schema == DDLSchema.DatabaseExist) { return(session.QueryCount(sql) > 0); } // 分隔符是分号加换行,如果不想被拆开执行(比如有事务),可以在分号和换行之间加一个空格 var sqls = sql.Split(new[] { ";" + Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); if (sqls == null || sqls.Length <= 1) { return(session.Execute(sql)); } session.BeginTransaction(IsolationLevel.Serializable); try { foreach (var item in sqls) { session.Execute(item); } session.Commit(); } catch { session.Rollback(); throw; } return(0); }
public override object SetSchema(DDLSchema schema, params object[] values) { IDbSession session = Database.CreateSession(); Object obj = null; String dbname = String.Empty; String databaseName = String.Empty; switch (schema) { case DDLSchema.DatabaseExist: databaseName = values == null || values.Length < 1 ? null : (String)values[0]; if (String.IsNullOrEmpty(databaseName)) databaseName = session.DatabaseName; dbname = session.DatabaseName; session.DatabaseName = SystemDatabaseName; try { obj = DatabaseExist(databaseName); } finally { session.DatabaseName = dbname; } return obj; case DDLSchema.DropDatabase: databaseName = values == null || values.Length < 1 ? null : (String)values[0]; if (String.IsNullOrEmpty(databaseName)) databaseName = session.DatabaseName; values = new Object[] { databaseName, values == null || values.Length < 2 ? null : values[1] }; dbname = session.DatabaseName; session.DatabaseName = SystemDatabaseName; try { //obj = base.SetSchema(schema, values); //if (Execute(String.Format("Drop Database [{0}]", dbname)) < 1) //{ // Execute(DropDatabaseSQL(databaseName)); //} var sb = new StringBuilder(); sb.AppendLine("use master"); sb.AppendLine(";"); sb.AppendLine("declare @spid varchar(20),@dbname varchar(20)"); sb.AppendLine("declare #spid cursor for"); sb.AppendFormat("select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id('{0}')", dbname); sb.AppendLine(); sb.AppendLine("open #spid"); sb.AppendLine("fetch next from #spid into @spid"); sb.AppendLine("while @@fetch_status=0"); sb.AppendLine("begin"); sb.AppendLine("exec('kill '+@spid)"); sb.AppendLine("fetch next from #spid into @spid"); sb.AppendLine("end"); sb.AppendLine("close #spid"); sb.AppendLine("deallocate #spid"); Int32 count = 0; try { count = session.Execute(sb.ToString()); } catch { } obj = session.Execute(String.Format("Drop Database {0}", FormatName(dbname))) > 0; //sb.AppendFormat("Drop Database [{0}]", dbname); } finally { session.DatabaseName = dbname; } return obj; case DDLSchema.TableExist: return TableExist((IDataTable)values[0]); default: break; } return base.SetSchema(schema, values); }
/// <summary>设置数据定义模式</summary> /// <param name="schema">数据定义模式</param> /// <param name="values">其它信息</param> /// <returns></returns> public virtual Object SetSchema(DDLSchema schema, params Object[] values) { //Object obj = null; switch (schema) { case DDLSchema.CreateTable: if (MetaDataCollections.Contains(_.Databases)) { } break; case DDLSchema.TableExist: { String name; if (values[0] is IDataTable) name = (values[0] as IDataTable).Name; else name = values[0].ToString(); DataTable dt = GetSchema(_.Tables, new String[] { null, null, name, "TABLE" }); if (dt == null || dt.Rows == null || dt.Rows.Count < 1) return false; return true; } default: break; } String sql = GetSchemaSQL(schema, values); if (String.IsNullOrEmpty(sql)) return null; IDbSession session = Database.CreateSession(); if (schema == DDLSchema.TableExist || schema == DDLSchema.DatabaseExist) { return session.QueryCount(sql) > 0; } else { // 分隔符是分号加换行,如果不想被拆开执行(比如有事务),可以在分号和换行之间加一个空格 String[] ss = sql.Split(new String[] { ";" + Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); if (ss == null || ss.Length < 1) return session.Execute(sql); else { foreach (String item in ss) { session.Execute(item); } return 0; } } }
public override object SetSchema(DDLSchema schema, params object[] values) { IDbSession session = Database.CreateSession(); Object obj = null; String dbname = String.Empty; String databaseName = String.Empty; String sysdbname = SystemDatabaseName; switch (schema) { case DDLSchema.DatabaseExist: databaseName = values == null || values.Length < 1 ? null : (String)values[0]; if (String.IsNullOrEmpty(databaseName)) { databaseName = session.DatabaseName; } values = new Object[] { databaseName }; dbname = session.DatabaseName; //如果指定了数据库名,并且不是master,则切换到master if (!String.IsNullOrEmpty(dbname) && !String.Equals(dbname, sysdbname, StringComparison.OrdinalIgnoreCase)) { session.DatabaseName = sysdbname; try { obj = session.QueryCount(GetSchemaSQL(schema, values)) > 0; } finally { session.DatabaseName = dbname; } return(obj); } else { return(session.QueryCount(GetSchemaSQL(schema, values)) > 0); } case DDLSchema.TableExist: return(session.QueryCount(GetSchemaSQL(schema, values)) > 0); case DDLSchema.CreateDatabase: databaseName = values == null || values.Length < 1 ? null : (String)values[0]; if (String.IsNullOrEmpty(databaseName)) { databaseName = session.DatabaseName; } values = new Object[] { databaseName, values == null || values.Length < 2 ? null : values[1] }; dbname = session.DatabaseName; session.DatabaseName = sysdbname; try { obj = base.SetSchema(schema, values); } finally { session.DatabaseName = dbname; } // 创建数据库后,需要等待它初始化 Thread.Sleep(5000); return(obj); default: break; } return(base.SetSchema(schema, values)); }
/// <summary> /// 获取架构语句,该执行的已经执行。 /// 如果取不到语句,则输出日志信息; /// 如果不是纯语句,则执行; /// </summary> /// <param name="sb"></param> /// <param name="onlySql"></param> /// <param name="schema"></param> /// <param name="values"></param> protected void PerformSchema(StringBuilder sb, Boolean onlySql, DDLSchema schema, params Object[] values) { String sql = GetSchemaSQL(schema, values); if (!String.IsNullOrEmpty(sql)) { if (sb.Length > 0) { sb.AppendLine(";"); } sb.Append(sql); //if (!onlySql) XTrace.WriteLine("修改表:" + sql); } else if (sql == null) { // 只有null才表示通过非SQL的方式处理,而String.Empty表示已经通过别的SQL处理,这里不用输出日志 // 没办法形成SQL,输出日志信息 StringBuilder s = new StringBuilder(); if (values != null && values.Length > 0) { foreach (Object item in values) { if (s.Length > 0) { s.Append(" "); } s.Append(item); } } IDataColumn dc = null; IDataTable dt = null; if (values != null && values.Length > 0) { dc = values[0] as IDataColumn; dt = values[0] as IDataTable; } switch (schema) { //case DDLSchema.CreateDatabase: // break; //case DDLSchema.DropDatabase: // break; //case DDLSchema.DatabaseExist: // break; //case DDLSchema.CreateTable: // break; //case DDLSchema.DropTable: // break; //case DDLSchema.TableExist: // break; case DDLSchema.AddTableDescription: WriteLog("{0}({1},{2})", schema, dt.Name, dt.Description); break; case DDLSchema.DropTableDescription: WriteLog("{0}({1})", schema, dt); break; case DDLSchema.AddColumn: WriteLog("{0}({1})", schema, dc); break; //case DDLSchema.AlterColumn: // break; case DDLSchema.DropColumn: WriteLog("{0}({1})", schema, dc.Name); break; case DDLSchema.AddColumnDescription: WriteLog("{0}({1},{2})", schema, dc.Name, dc.Description); break; case DDLSchema.DropColumnDescription: WriteLog("{0}({1})", schema, dc.Name); break; case DDLSchema.AddDefault: WriteLog("{0}({1},{2})", schema, dc.Name, dc.Default); break; case DDLSchema.DropDefault: WriteLog("{0}({1})", schema, dc.Name); break; //case DDLSchema.CreateIndex: // break; //case DDLSchema.DropIndex: // break; //case DDLSchema.BackupDatabase: // break; //case DDLSchema.RestoreDatabase: // break; default: WriteLog("修改表:{0} {1}", schema.ToString(), s.ToString()); break; } //WriteLog("修改表:{0} {1}", schema.ToString(), s.ToString()); } if (!onlySql) { try { SetSchema(schema, values); } catch (Exception ex) { WriteLog("修改表{0}失败!{1}", schema.ToString(), ex.Message); } } }
//protected override void FixIndex(IDataIndex index, DataRow dr) //{ // base.FixIndex(index, dr); // Boolean b; // if (TryGetDataRowValue<Boolean>(dr, "UNIQUE", out b)) index.Unique = b; // if (TryGetDataRowValue<Boolean>(dr, "PRIMARY", out b)) index.PrimaryKey = b; //} #region 架构定义 public override object SetSchema(DDLSchema schema, params object[] values) { if (schema == DDLSchema.DatabaseExist) { IDbSession session = Database.CreateSession(); DataTable dt = GetSchema(_.Databases, new String[] { values != null && values.Length > 0 ? (String)values[0] : session.DatabaseName }); if (dt == null || dt.Rows == null || dt.Rows.Count < 1) return false; return true; } return base.SetSchema(schema, values); }
public override object SetSchema(DDLSchema schema, params object[] values) { IDbSession session = Database.CreateSession(); Object obj = null; String dbname = String.Empty; String databaseName = String.Empty; switch (schema) { case DDLSchema.DatabaseExist: databaseName = values == null || values.Length < 1 ? null : (String)values[0]; if (String.IsNullOrEmpty(databaseName)) { databaseName = session.DatabaseName; } dbname = session.DatabaseName; session.DatabaseName = SystemDatabaseName; try { obj = DatabaseExist(databaseName); } finally { session.DatabaseName = dbname; } return(obj); case DDLSchema.DropDatabase: databaseName = values == null || values.Length < 1 ? null : (String)values[0]; if (String.IsNullOrEmpty(databaseName)) { databaseName = session.DatabaseName; } values = new Object[] { databaseName, values == null || values.Length < 2 ? null : values[1] }; dbname = session.DatabaseName; session.DatabaseName = SystemDatabaseName; try { //obj = base.SetSchema(schema, values); //if (Execute(String.Format("Drop Database [{0}]", dbname)) < 1) //{ // Execute(DropDatabaseSQL(databaseName)); //} var sb = new StringBuilder(); sb.AppendLine("use master"); sb.AppendLine(";"); sb.AppendLine("declare @spid varchar(20),@dbname varchar(20)"); sb.AppendLine("declare #spid cursor for"); sb.AppendFormat("select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id('{0}')", dbname); sb.AppendLine(); sb.AppendLine("open #spid"); sb.AppendLine("fetch next from #spid into @spid"); sb.AppendLine("while @@fetch_status=0"); sb.AppendLine("begin"); sb.AppendLine("exec('kill '+@spid)"); sb.AppendLine("fetch next from #spid into @spid"); sb.AppendLine("end"); sb.AppendLine("close #spid"); sb.AppendLine("deallocate #spid"); Int32 count = 0; try { count = session.Execute(sb.ToString()); } catch { } obj = session.Execute(String.Format("Drop Database {0}", FormatName(dbname))) > 0; //sb.AppendFormat("Drop Database [{0}]", dbname); } finally { session.DatabaseName = dbname; } return(obj); case DDLSchema.TableExist: return(TableExist((IDataTable)values[0])); default: break; } return(base.SetSchema(schema, values)); }
/// <summary>设置数据定义模式</summary> /// <param name="schema">数据定义模式</param> /// <param name="values">其它信息</param> /// <returns></returns> public virtual Object SetSchema(DDLSchema schema, params Object[] values) { //Object obj = null; switch (schema) { case DDLSchema.CreateTable: if (MetaDataCollections.Contains(_.Databases)) { } break; case DDLSchema.TableExist: { String name; if (values[0] is IDataTable) { name = (values[0] as IDataTable).Name; } else { name = values[0].ToString(); } DataTable dt = GetSchema(_.Tables, new String[] { null, null, name, "TABLE" }); if (dt == null || dt.Rows == null || dt.Rows.Count < 1) { return(false); } return(true); } default: break; } String sql = GetSchemaSQL(schema, values); if (String.IsNullOrEmpty(sql)) { return(null); } IDbSession session = Database.CreateSession(); if (schema == DDLSchema.TableExist || schema == DDLSchema.DatabaseExist) { return(session.QueryCount(sql) > 0); } else { // 分隔符是分号加换行,如果不想被拆开执行(比如有事务),可以在分号和换行之间加一个空格 String[] ss = sql.Split(new String[] { ";" + Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); if (ss == null || ss.Length < 1) { return(session.Execute(sql)); } else { foreach (String item in ss) { session.Execute(item); } return(0); } } }
/// <summary>设置数据定义模式</summary> /// <param name="schema"></param> /// <param name="values"></param> /// <returns></returns> public override object SetSchema(DDLSchema schema, object[] values) { //Object obj = null; switch (schema) { case DDLSchema.CreateDatabase: CreateDatabase(); return null; case DDLSchema.DropDatabase: DropDatabase(); return null; case DDLSchema.DatabaseExist: return File.Exists(FileName); default: break; } return base.SetSchema(schema, values); }
/// <summary>设置数据定义模式</summary> /// <param name="schema"></param> /// <param name="values"></param> /// <returns></returns> public override object SetSchema(DDLSchema schema, object[] values) { Object obj = null; switch (schema) { case DDLSchema.CreateTable: obj = base.SetSchema(DDLSchema.CreateTable, values); IDataTable table = values[0] as IDataTable; // Access建表语句的不能操作默认值,所以在这里操作 // 默认值 foreach (IDataColumn item in table.Columns) { if (!String.IsNullOrEmpty(item.Default)) AddDefault(item, item.Default); } return obj; case DDLSchema.AddTableDescription: return AddTableDescription((IDataTable)values[0], ((IDataTable)values[0]).Description); case DDLSchema.DropTableDescription: return DropTableDescription((IDataTable)values[0]); case DDLSchema.AddColumnDescription: return AddColumnDescription((IDataColumn)values[0], ((IDataColumn)values[0]).Description); case DDLSchema.DropColumnDescription: return DropColumnDescription((IDataColumn)values[0]); case DDLSchema.AddDefault: return AddDefault((IDataColumn)values[0], ((IDataColumn)values[0]).Default); case DDLSchema.DropDefault: return DropDefault((IDataColumn)values[0]); default: break; } return base.SetSchema(schema, values); }
public override object SetSchema(DDLSchema schema, params object[] values) { IDbSession session = Database.CreateSession(); String dbname = String.Empty; String databaseName = String.Empty; switch (schema) { case DDLSchema.DatabaseExist: // Oracle��֧���ж����ݿ��Ƿ���� return true; default: break; } return base.SetSchema(schema, values); }
/// <summary> /// 获取架构语句,该执行的已经执行。 /// 如果取不到语句,则输出日志信息; /// 如果不是纯语句,则执行; /// </summary> /// <param name="sb"></param> /// <param name="onlySql"></param> /// <param name="schema"></param> /// <param name="values"></param> protected Boolean PerformSchema(StringBuilder sb, Boolean onlySql, DDLSchema schema, params Object[] values) { var sql = GetSchemaSQL(schema, values); if (!String.IsNullOrEmpty(sql)) { if (sb.Length > 0) { sb.AppendLine(";"); } sb.Append(sql); } else if (sql == null) { // 只有null才表示通过非SQL的方式处理,而String.Empty表示已经通过别的SQL处理,这里不用输出日志 // 没办法形成SQL,输出日志信息 var s = new StringBuilder(); if (values != null && values.Length > 0) { foreach (var item in values) { if (s.Length > 0) { s.Append(" "); } s.Append(item); } } IDataColumn dc = null; IDataTable dt = null; if (values != null && values.Length > 0) { dc = values[0] as IDataColumn; dt = values[0] as IDataTable; } switch (schema) { case DDLSchema.AddTableDescription: WriteLog("{0}({1},{2})", schema, dt.TableName, dt.Description); break; case DDLSchema.DropTableDescription: WriteLog("{0}({1})", schema, dt); break; case DDLSchema.AddColumn: WriteLog("{0}({1})", schema, dc); break; //case DDLSchema.AlterColumn: // break; case DDLSchema.DropColumn: WriteLog("{0}({1})", schema, dc.ColumnName); break; case DDLSchema.AddColumnDescription: WriteLog("{0}({1},{2})", schema, dc.ColumnName, dc.Description); break; case DDLSchema.DropColumnDescription: WriteLog("{0}({1})", schema, dc.ColumnName); break; default: WriteLog("修改表:{0} {1}", schema.ToString(), s.ToString()); break; } //WriteLog("修改表:{0} {1}", schema.ToString(), s.ToString()); } if (!onlySql) { try { SetSchema(schema, values); } catch (Exception ex) { WriteLog("修改表{0}失败!{1}", schema.ToString(), ex.Message); return(false); } } return(true); }
/// <summary> /// 获取架构语句,该执行的已经执行。 /// 如果取不到语句,则输出日志信息; /// 如果不是纯语句,则执行; /// </summary> /// <param name="sb"></param> /// <param name="onlySql"></param> /// <param name="schema"></param> /// <param name="values"></param> protected void PerformSchema(StringBuilder sb, Boolean onlySql, DDLSchema schema, params Object[] values) { String sql = GetSchemaSQL(schema, values); if (!String.IsNullOrEmpty(sql)) { if (sb.Length > 0) sb.AppendLine(";"); sb.Append(sql); //if (!onlySql) XTrace.WriteLine("修改表:" + sql); } else if (sql == null) { // 只有null才表示通过非SQL的方式处理,而String.Empty表示已经通过别的SQL处理,这里不用输出日志 // 没办法形成SQL,输出日志信息 StringBuilder s = new StringBuilder(); if (values != null && values.Length > 0) { foreach (Object item in values) { if (s.Length > 0) s.Append(" "); s.Append(item); } } IDataColumn dc = null; IDataTable dt = null; if (values != null && values.Length > 0) { dc = values[0] as IDataColumn; dt = values[0] as IDataTable; } switch (schema) { //case DDLSchema.CreateDatabase: // break; //case DDLSchema.DropDatabase: // break; //case DDLSchema.DatabaseExist: // break; //case DDLSchema.CreateTable: // break; //case DDLSchema.DropTable: // break; //case DDLSchema.TableExist: // break; case DDLSchema.AddTableDescription: WriteLog("{0}({1},{2})", schema, dt.Name, dt.Description); break; case DDLSchema.DropTableDescription: WriteLog("{0}({1})", schema, dt); break; case DDLSchema.AddColumn: WriteLog("{0}({1})", schema, dc); break; //case DDLSchema.AlterColumn: // break; case DDLSchema.DropColumn: WriteLog("{0}({1})", schema, dc.Name); break; case DDLSchema.AddColumnDescription: WriteLog("{0}({1},{2})", schema, dc.Name, dc.Description); break; case DDLSchema.DropColumnDescription: WriteLog("{0}({1})", schema, dc.Name); break; case DDLSchema.AddDefault: WriteLog("{0}({1},{2})", schema, dc.Name, dc.Default); break; case DDLSchema.DropDefault: WriteLog("{0}({1})", schema, dc.Name); break; //case DDLSchema.CreateIndex: // break; //case DDLSchema.DropIndex: // break; //case DDLSchema.BackupDatabase: // break; //case DDLSchema.RestoreDatabase: // break; default: WriteLog("修改表:{0} {1}", schema.ToString(), s.ToString()); break; } //WriteLog("修改表:{0} {1}", schema.ToString(), s.ToString()); } if (!onlySql) { try { SetSchema(schema, values); } catch (Exception ex) { WriteLog("修改表{0}失败!{1}", schema.ToString(), ex.Message); } } }
/// <summary>获取数据定义语句</summary> /// <param name="schema">数据定义模式</param> /// <param name="values">其它信息</param> /// <returns></returns> public virtual String GetSchemaSQL(DDLSchema schema, params Object[] values) { switch (schema) { case DDLSchema.CreateDatabase: return CreateDatabaseSQL((String)values[0], (String)values[1]); case DDLSchema.DropDatabase: return DropDatabaseSQL((String)values[0]); case DDLSchema.DatabaseExist: return DatabaseExistSQL(values == null || values.Length < 1 ? null : (String)values[0]); case DDLSchema.CreateTable: return CreateTableSQL((IDataTable)values[0]); case DDLSchema.DropTable: if (values[0] is IDataTable) return DropTableSQL((IDataTable)values[0]); else return DropTableSQL(values[0].ToString()); case DDLSchema.TableExist: if (values[0] is IDataTable) return TableExistSQL((IDataTable)values[0]); else return TableExistSQL(values[0].ToString()); case DDLSchema.AddTableDescription: return AddTableDescriptionSQL((IDataTable)values[0]); case DDLSchema.DropTableDescription: return DropTableDescriptionSQL((IDataTable)values[0]); case DDLSchema.AddColumn: return AddColumnSQL((IDataColumn)values[0]); case DDLSchema.AlterColumn: return AlterColumnSQL((IDataColumn)values[0], values.Length > 1 ? (IDataColumn)values[1] : null); case DDLSchema.DropColumn: return DropColumnSQL((IDataColumn)values[0]); case DDLSchema.AddColumnDescription: return AddColumnDescriptionSQL((IDataColumn)values[0]); case DDLSchema.DropColumnDescription: return DropColumnDescriptionSQL((IDataColumn)values[0]); case DDLSchema.AddDefault: return AddDefaultSQL((IDataColumn)values[0]); case DDLSchema.DropDefault: return DropDefaultSQL((IDataColumn)values[0]); case DDLSchema.CreateIndex: return CreateIndexSQL((IDataIndex)values[0]); case DDLSchema.DropIndex: return DropIndexSQL((IDataIndex)values[0]); default: break; } throw new NotSupportedException("不支持该操作!"); }
public override object SetSchema(DDLSchema schema, params object[] values) { IDbSession session = Database.CreateSession(); Object obj = null; String dbname = String.Empty; String databaseName = String.Empty; String sysdbname = SystemDatabaseName; switch (schema) { case DDLSchema.DatabaseExist: databaseName = values == null || values.Length < 1 ? null : (String)values[0]; if (String.IsNullOrEmpty(databaseName)) databaseName = session.DatabaseName; values = new Object[] { databaseName }; dbname = session.DatabaseName; //如果指定了数据库名,并且不是master,则切换到master if (!String.IsNullOrEmpty(dbname) && !String.Equals(dbname, sysdbname, StringComparison.OrdinalIgnoreCase)) { session.DatabaseName = sysdbname; try { obj = session.QueryCount(GetSchemaSQL(schema, values)) > 0; } finally { session.DatabaseName = dbname; } return obj; } else { return session.QueryCount(GetSchemaSQL(schema, values)) > 0; } case DDLSchema.TableExist: return session.QueryCount(GetSchemaSQL(schema, values)) > 0; case DDLSchema.CreateDatabase: databaseName = values == null || values.Length < 1 ? null : (String)values[0]; if (String.IsNullOrEmpty(databaseName)) databaseName = session.DatabaseName; values = new Object[] { databaseName, values == null || values.Length < 2 ? null : values[1] }; dbname = session.DatabaseName; session.DatabaseName = sysdbname; try { obj = base.SetSchema(schema, values); } finally { session.DatabaseName = dbname; } // 创建数据库后,需要等待它初始化 Thread.Sleep(5000); return obj; default: break; } return base.SetSchema(schema, values); }