public FormTableInfo(TreeNode node) { this.node = node; this.conn = node.Parent.Parent.Tag as UserConn; this.table = node.Tag as IDataTable; InitializeComponent(); }
public static double GetCCT(IDataTable<DataRow> data, string a, string b, WaitObject flag) { int count = data.RowCount; double xyS = 0; double xS = 0; double yS = 0; double x2S = 0; double y2S = 0; double tempx = 0; double tempy = 0; DataRow temprow; flag.Flags = new int[1]; flag.Max = count; for (int i = 0; i < count; i++) { temprow = data[i]; tempx = temprow[a].ConvertToDouble(); tempy = temprow[b].ConvertToDouble(); xyS += tempx * tempy; xS += tempx; yS += tempy; x2S += tempx * tempx; y2S += tempy * tempy; flag.Flags[0]++; } return (xyS * count - xS * yS) / (Math.Pow((x2S * count - xS * xS), 0.5) * Math.Pow((y2S * count - yS * yS), 0.5)); }
/// <summary>克隆</summary> /// <param name="table"></param> /// <returns></returns> public IDataRelation Clone(IDataTable table) { var field = base.MemberwiseClone() as XRelation; field.Table = table; return field; }
public WatcherSetupDialog(string db_path, string table_oid, IDataTable table_to_edit, bool editing) { pathToDb = db_path; tbl_OID = table_oid; tableToEdit = table_to_edit; InitializeComponent(); /* Functionality buttons */ m_saveButton.Click += m_saveButton_Click; m_goToNamespaces.Click += m_goToNamespaces_Click; m_recordBox.Unchecked += m_recordBox_Unchecked; m_recordBox.Checked += m_recordBox_Checked; m_cancelButton.Click += m_cancelButton_Click; m_clearButton.Click += m_clearButton_Click; /* Interface smoothing actions */ m_sourcePathBox.TextChanged += m_sourcePathBox_TextChanged; /* Help dialog buttons */ h_sourceHelp.Click += h_sourceHelp_Click; h_recordPath.Click += h_recordPath_Click; h_advanceMode.Click += h_advanceMode_Click; h_namespaces.Click += h_namespaces_Click; form_Init(editing, pathToDb); }
/// <summary> /// 窗体加载 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FormTable_Load(object sender, EventArgs e) { table = BindingNode.Tag as IDataTable; frmTableInfo = new FormTableInfo(BindingNode); LoadData(); }
public static Tuple<Image, string, double[]> LmGress(IDataTable<DataRow> data, int width, int height, string targetcolumn, string[] sourcecolumns) { AppDomain domain = AppDomain.CreateDomain(Guid.NewGuid().ToString()); string name = Guid.NewGuid().ToString(); var root = new DirectoryInfo(System.Windows.Forms.Application.StartupPath + "\\..\\Temp"); if (!root.Exists) root.Create(); string fullimagepath = root.FullName + "\\" + name + ".png"; string fulltextpath = root.FullName + "\\" + name + ".txt"; byte[] buffer = null; string result = ""; double[] coe = null; try { int rcount = data.RowCount; int ccount = sourcecolumns.Length + 1; int i; List<double[]> sourcedata = new List<double[]>(); Dictionary<string, double[]> doubledata = new Dictionary<string, double[]>(); Dictionary<string, string[]> stringdata = new Dictionary<string, string[]>(); double[] temp = new double[rcount]; for (i = 0; i < rcount; i++) temp[i] = data[i, targetcolumn].ConvertToDouble(); doubledata.Add(targetcolumn, temp); foreach (var sourcecolumn in sourcecolumns) { temp = new double[rcount]; for (i = 0; i < rcount; i++) temp[i] = data[i, sourcecolumn].ConvertToDouble(); doubledata.Add(sourcecolumn, temp); } coe = (domain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(Lmregress).FullName) as Lmregress).BaseStart(root.FullName, name, width, height, targetcolumn, sourcecolumns, doubledata); using (FileStream fs = new FileStream(fullimagepath, FileMode.Open)) { buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); } using (StreamReader sr = new StreamReader(fulltextpath, Encoding.Default)) { result = sr.ReadToEnd(); int start = result.IndexOf("Residuals:"); if (start >= 0) result = result.Substring(start); } } catch (Exception ex) { throw ex; } finally { AppDomain.Unload(domain); File.Delete(fullimagepath); File.Delete(fulltextpath); } return new Tuple<Image, string, double[]>(Image.FromStream(new MemoryStream(buffer)), result, coe); }
public SurveyStore( IDataTable<SurveyRow> surveyTable, IDataTable<QuestionRow> questionTable, ISurveyAnswerContainerFactory surveyAnswerContainerFactory) { Trace.WriteLine(string.Format("Called constructor in SurveyStore"), "UNITY"); this.surveyTable = surveyTable; this.questionTable = questionTable; this.surveyAnswerContainerFactory = surveyAnswerContainerFactory; }
private void DoSelectTableCommand(IDataTable inParameter) { if (SelectedTable != null) SelectedTable.IsSelected = false; inParameter.IsSelected = true; SelectedTable = inParameter; RefreshDataPreview(); }
public static Tuple<List<string>, List<double>, DataTable> Schmidt(IDataTable<DataRow> data, List<string> columns) { int columncount = columns.Count; int rowcount = data.RowCount; double[][] vectors = new double[columncount][]; int i, j; for (i = 0; i < columncount; i++) { vectors[i] = new double[rowcount]; for (j = 0; j < rowcount; j++) { vectors[i][j] = data[j,columns[i]].ConvertToDouble(); } } int[] maxid; double[,] report; MPPO.DataProcess.Schmidt.Start(vectors, columncount, rowcount, out maxid, out report); var result = new List<string>(); var percents = new List<double>(); var resultdetail = new DataTable(); foreach (var column in columns) { resultdetail.Columns.Add(column, typeof(double)); } for (i = 0; i < columncount; i++) { var temprow = resultdetail.NewRow(); for (j = 0; j < columncount; j++) { temprow[j] = report[i, j]; } resultdetail.Rows.Add(temprow); } for (i = 0; i < columncount; i++) { var columnname = columns[maxid[i]]; result.Add(columnname); resultdetail.Columns[columnname].SetOrdinal(i); } double total = 0; double percent = 0; for (j = 0; j < columncount; j++) { total += resultdetail.Rows[j][j].ConvertToDouble(); } for (i = 0; i < columncount; i++) { percent += (resultdetail.Rows[i][i].ConvertToDouble() * 100 / total); percents.Add(percent); } percents[columncount - 1] = 100; return new Tuple<List<string>, List<double>, DataTable>(result, percents, resultdetail); }
public static void ExportToExcel(IDataTable<DataRow> data, string filename,Protocol.Structure.WaitObject wt) { if (data == null) return; var excel = new ApplicationClass(); if (excel == null) throw new Exception("Excel无法启动"); int rowNum = data.RowCount; string[] columns = data.GetColumnsList(); int columnNum = columns.Length; wt.Flags = new int[1]; wt.Max = rowNum * columnNum; int rowIndex = 1; int columnIndex = 0; var book = excel.Application.Workbooks.Add(true); try { foreach (var column in columns) { columnIndex++; excel.Cells[rowIndex, columnIndex] = column; if (data.GetColumnType(column) == typeof(string)) excel.get_Range(excel.Cells[rowIndex + 1, columnIndex], excel.Cells[rowNum + 1, columnIndex]).NumberFormatLocal = "@"; } for (int i = 0; i < rowNum; i++) { rowIndex++; columnIndex = 0; for (int j = 0; j < columnNum; j++) { columnIndex++; excel.Cells[rowIndex, columnIndex] = data[i,columns[j]]; wt.Flags[0]++; } } excel.DisplayAlerts = false; excel.AlertBeforeOverwriting = false; book.SaveCopyAs(filename); } catch (Exception ex) { throw ex; } finally { book.Close(false); book = null; excel.Quit(); excel = null; } }
public static List<Tuple<string, double>> Entropy(IDataTable<DataRow> data, List<string> columns, Protocol.Structure.WaitObject wt) { var result = new List<Tuple<string, double>>(); var columnsarray = columns.ToArray(); var re = MPPO.DataProcess.Entropy.GetEntropy(data, columnsarray,wt); int count = re.Length; for (int i = 0; i < count; i++) { result.Add(new Tuple<string, double>(columns[i], re[i])); } result.Sort(new Comparison<Tuple<string, double>>((a1, a2) => { return a1.Item2.CompareTo(a2.Item2); })); return result; }
public static double[] GetCCTs(IDataTable<DataRow> data, string target, string[] f,WaitObject flag) { int count = data.RowCount; int length = f.Length; double[] fs = new double[length]; double[] xfs = new double[length]; double[] f2s = new double[length]; double[] result = new double[length]; double xS = 0; double x2S = 0; int j = 0; double tempx = 0; double tempy = 0; DataRow temprow; flag.Flags = new int[1]; flag.Max = count * length; for (int i = 0; i < count; i++) { temprow = data[i]; tempx = temprow[target].ConvertToDouble(); xS += tempx; x2S += tempx * tempx; for(j=0;j<length;j++) { tempy = temprow[f[j]].ConvertToDouble(); xfs[j] += tempx * tempy; fs[j] += tempy; f2s[j] += tempy * tempy; flag.Flags[0]++; } } double down; for (j = 0; j < length; j++) { down = (Math.Pow((x2S * count - xS * xS), 0.5) * Math.Pow((f2s[j] * count - fs[j] * fs[j]), 0.5)); if (down != 0) result[j] = (xfs[j] * count - xS * fs[j]) / down; else if (x2S == 0) result[j] = 1; else result[j] = 0; } return result; }
public AddTable(IDataTable table) { InitializeComponent(); combDbType.DataSource = BindComboxEnumType<DatabaseType>.BindTyps; //combDbType.DisplayMember = "Name"; //combDbType.ValueMember = "Type"; CurrentTable = table; //绑定Table信息到文本框 txtTableName.Text = CurrentTable.TableName; txtTableRemark.Text = CurrentTable.Description; combDbType.SelectedValue = CurrentTable.DbType; list = new BindingList<IDataColumn>(); //绑定字段到表格 if(CurrentTable.Columns.Count >0 ) dgvColumns.DataSource = CurrentTable.Columns; BandingDGV(); }
public static Image DrawContourPlot(IDataTable<DataRow> data, string xs, string ys, string zs,int width,int height,double[] levels,bool drawline) { int rowcount = data.RowCount; double[] x = new double[rowcount]; double[] y = new double[rowcount]; double[] z = new double[rowcount]; DataRow row; for (int i = 0; i < rowcount; i++) { row = data[i]; x[i] = row[xs].ConvertToDouble(); y[i] = row[ys].ConvertToDouble(); z[i] = row[zs].ConvertToDouble(); } AppDomain domain = AppDomain.CreateDomain(Guid.NewGuid().ToString()); string name = Guid.NewGuid().ToString(); var root = new DirectoryInfo(System.Windows.Forms.Application.StartupPath+"\\..\\Temp"); if (!root.Exists) root.Create(); string fullpath = root.FullName + "\\" + name+".png"; byte[] buffer; try { (domain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(Graphics).FullName) as Graphics).DrawContourPlot(root.FullName, name, x, y, z,width,height,levels,drawline); using (FileStream fs = new FileStream(fullpath, FileMode.Open)) { buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); } } catch(Exception ex) { throw ex; } finally { AppDomain.Unload(domain); File.Delete(fullpath); } return Image.FromStream(new MemoryStream(buffer)); }
/// <summary> /// 根据 optionNode 中的描述信息,读取 table 中的数据组装为对象列表并返回。 /// 如果 optionNode 中指定要加载更多的子/引用对象,则会递归调用自己实现聚合加载。 /// </summary> /// <param name="list">The list.</param> /// <param name="table">The table.</param> /// <param name="optionNode">The option node.</param> private void ReadFromTable(EntityList list, IDataTable table, LinkedListNode<LoadOptionItem> optionNode) { var option = optionNode.Value; AggregateEntityLoaderHelper.ReadFromTable(list, table, (entity, subTable) => { EntityList listResult = null; //是否还有后继需要加载的对象?如果是,则递归调用自己进行子对象的加载。 var nextNode = optionNode.Next; if (nextNode != null) { listResult = nextNode.Value.OwnerRepository.NewList(); this.ReadFromTable(listResult, subTable, nextNode); } else { listResult = option.PropertyEntityRepository.NewList(); AggregateEntityLoaderHelper.ReadFromTable(listResult, subTable, null); } //是否需要排序? if (listResult.Count > 1 && option.OrderBy != null) { listResult = option.PropertyEntityRepository.NewListOrderBy(listResult, option.OrderBy); } //当前对象是加载类型的子对象还是引用的外键 if (option.LoadType == AggregateLoadType.Children) { listResult.SetParentEntity(entity); entity.LoadProperty(option.PropertyMeta.ManagedProperty, listResult); } else { if (listResult.Count > 0) { option.SetReferenceEntity(entity, listResult[0]); } } }); }
private void VerifyDataTable(IDataTable table) { Assert.AreEqual(12, table.Columns.Count, "Columns Count"); Assert.AreEqual(10, table.Rows.Count, "Rows Count"); var cols = table.Columns; Assert.AreEqual("Id", cols[0].Name); Assert.AreEqual("Sku", cols[1].Name); Assert.AreEqual("Name", cols[2].Name); Assert.AreEqual("Description", cols[3].Name); Assert.AreEqual("Bool", cols[4].Name); Assert.AreEqual("Date", cols[5].Name); Assert.AreEqual("OADate", cols[6].Name); Assert.AreEqual("UnixDate", cols[7].Name); Assert.AreEqual("Int", cols[8].Name); Assert.AreEqual("Double", cols[9].Name); Assert.AreEqual("Guid", cols[10].Name); Assert.AreEqual("IntList", cols[11].Name); var rows = table.Rows; rows[3]["Sku"].ShouldEqual("SKU 4"); rows[1]["Name"].ShouldEqual("äöü"); rows[7]["Description"].ShouldEqual("Description 8"); rows[0]["Bool"].Convert<bool>().ShouldBeTrue(); rows[5]["Bool"].Convert<bool>().ShouldBeTrue(); rows[6]["Bool"].Convert<bool>().ShouldBeFalse(); rows[3]["Double"].Convert<double>().ShouldEqual(9999.765); rows[0]["OADate"].Convert<DateTime>().ShouldEqual(DateTime.FromOADate(rows[0]["OADate"].Convert<double>())); rows[3]["Guid"].Convert<Guid>().ShouldEqual(Guid.Parse("77866957-eec3-4b35-950f-10d1699ac46d")); rows[0]["IntList"].Convert<List<int>>().ShouldSequenceEqual(new List<int> { 1, 2, 3, 4 }); rows[1]["IntList"].Convert<List<short>>().ShouldSequenceEqual(new List<short> { 1, 2, 3, 4 }); rows[5]["IntList"].Convert<List<double>>().ShouldSequenceEqual(new List<double> { 1, 2, 3, 4 }); }
public static Tuple<Image, string, double[, ]> Rpart(IDataTable<DataRow> data,int width,int height, string targetcolumn,string[] sourcecolumns,string method,double cp) { AppDomain domain = AppDomain.CreateDomain(Guid.NewGuid().ToString()); string name = Guid.NewGuid().ToString(); var root = new DirectoryInfo(System.Windows.Forms.Application.StartupPath + "\\..\\Temp"); if (!root.Exists) root.Create(); string fullimagepath = root.FullName + "\\" + name + ".png"; string fulltextpath = root.FullName + "\\" + name + ".txt"; byte[] buffer =null; string result = ""; double[] tempd; string[] temps; double[,] cptable = null; try { int rcount = data.RowCount; int ccount = sourcecolumns.Length+1; int i; List<double[]> sourcedata = new List<double[]>(); Dictionary<string, double[]> doubledata = new Dictionary<string, double[]>(); Dictionary<string, string[]> stringdata = new Dictionary<string, string[]>(); var type = data.GetColumnType(targetcolumn); if (type == typeof(double) || type == typeof(int) || type == typeof(decimal) || type == typeof(float)) { tempd = new double[rcount]; for (i = 0; i < rcount; i++) tempd[i] = data[i, targetcolumn].ConvertToDouble(); doubledata.Add(targetcolumn, tempd); } else { temps = new string[rcount]; for (i = 0; i < rcount; i++) temps[i] = data[i, targetcolumn].ToString(); stringdata.Add(targetcolumn, temps); } foreach(var sourcecolumn in sourcecolumns) { type = data.GetColumnType(sourcecolumn); if (type == typeof(double) || type == typeof(int) || type == typeof(decimal) || type == typeof(float)) { tempd = new double[rcount]; for (i = 0; i < rcount; i++) tempd[i] = data[i, sourcecolumn].ConvertToDouble(); doubledata.Add(sourcecolumn, tempd); } else { temps = new string[rcount]; for (i = 0; i < rcount; i++) temps[i] = data[i, sourcecolumn].ToString(); stringdata.Add(sourcecolumn, temps); } } if (!RpartMethod.Contains(method.ToLower())) throw new Exception("不支持的方法参数"); cptable = (domain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(Rpart).FullName) as Rpart).BaseStart(root.FullName, name, width, height, targetcolumn, sourcecolumns, doubledata, stringdata, method.ToString().ToLower(),cp); using (FileStream fs = new FileStream(fullimagepath, FileMode.Open)) { buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); } using (StreamReader sr = new StreamReader(fulltextpath,Encoding.Default)) { result = sr.ReadToEnd(); } } catch (Exception ex) { throw ex; } finally { AppDomain.Unload(domain); File.Delete(fullimagepath); File.Delete(fulltextpath); } return new Tuple<Image,string,double[,]>(Image.FromStream(new MemoryStream(buffer)),result,cptable); }
/// <summary>读取</summary> /// <param name="table"></param> /// <param name="reader"></param> /// <returns></returns> public static IDataTable ReadXml(this IDataTable table, XmlReader reader) { // 读属性 if (reader.HasAttributes) { reader.MoveToFirstAttribute(); ReadXml(reader, table); } reader.ReadStartElement(); // 读字段 reader.MoveToElement(); // 有些数据表模型没有字段 if (reader.NodeType == XmlNodeType.Element && reader.Name.EqualIgnoreCase("Table")) { return(table); } while (reader.NodeType != XmlNodeType.EndElement) //while (reader.NodeType == XmlNodeType.Element) { switch (reader.Name) { case "Columns": reader.ReadStartElement(); while (reader.IsStartElement()) { var dc = table.CreateColumn(); var v = reader.GetAttribute("DataType"); if (v != null) { dc.DataType = v.GetTypeEx(false); v = reader.GetAttribute("Length"); if (v != null && Int32.TryParse(v, out var len)) { dc.Length = len; } dc = Fix(dc, dc); } (dc as IXmlSerializable).ReadXml(reader); table.Columns.Add(dc); } reader.ReadEndElement(); // 修正可能的主字段 if (!table.Columns.Any(e => e.Master)) { var f = table.Columns.FirstOrDefault(e => e.Name.EqualIgnoreCase("Name", "Title")); if (f != null) { f.Master = true; } } break; case "Indexes": reader.ReadStartElement(); while (reader.IsStartElement()) { var di = table.CreateIndex(); (di as IXmlSerializable).ReadXml(reader); di.Fix(); table.Indexes.Add(di); } reader.ReadEndElement(); break; case "Relations": reader.ReadStartElement(); reader.Skip(); reader.ReadEndElement(); break; default: // 这里必须处理,否则加载特殊Xml文件时将会导致死循环 reader.Read(); break; } } if (reader.NodeType == XmlNodeType.EndElement) { reader.ReadEndElement(); } // 修正 table.Fix(); return(table); }
public override string AddTableDescriptionSQL(IDataTable table) { if (String.IsNullOrEmpty(table.Description)) return null; return String.Format("Alter Table {0} Comment '{1}'", FormatName(table.Name), table.Description); }
public override String DropTableDescriptionSQL(IDataTable table) { return(String.Format("EXEC dbo.sp_dropextendedproperty @name=N'MS_Description', @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'{0}'", table.TableName)); }
public override IDataSource CloneWith(IDataTable dataTable) { return(new DefaultDataTableAdaptor(_lap, dataTable, _vectoriser)); }
/// <summary>根据单字段索引修正对应的关系</summary> /// <param name="table"></param> protected virtual void FixRelationBySingleIndex(IDataTable table) { // 给所有单字段索引建立关系,特别是一对一关系 foreach (var item in table.Indexes) { if (item.Columns == null || item.Columns.Length != 1) continue; var dr = table.GetRelation(item.Columns[0]); if (dr == null) continue; dr.Unique = item.Unique; // 跟关系有关联的索引 dr.Computed = item.Computed; } }
/// <summary>索引应该具有跟字段一样的唯一和主键约束</summary> /// <param name="table"></param> protected virtual void FixIndex(IDataTable table) { // 主要针对MSSQL2000 foreach (var di in table.Indexes) { if (di.Columns == null) continue; var dcs = table.GetColumns(di.Columns); if (dcs == null || dcs.Length <= 0) continue; if (!di.Unique) di.Unique = dcs.All(dc => dc.Identity); if (!di.PrimaryKey) di.PrimaryKey = dcs.All(dc => dc.PrimaryKey); } }
public Boolean DropTableDescription(IDataTable table) { return(AddTableDescription(table, null)); }
/// <summary>修正数据</summary> /// <param name="table"></param> public virtual IDataTable Fix(IDataTable table) { // 去除表名两端的空格 if (!table.TableName.IsNullOrEmpty()) { table.TableName = table.TableName.Trim(); } if (table.Name.IsNullOrEmpty()) { table.Name = GetName(table.TableName); } if (!table.Name.IsNullOrEmpty()) { table.Name = table.Name.Trim(); } // 去除字段名两端的空格 foreach (var item in table.Columns) { if (!item.Name.IsNullOrEmpty()) { item.Name = item.Name.Trim(); } if (!item.ColumnName.IsNullOrEmpty()) { item.ColumnName = item.ColumnName.Trim(); } } // 最后修复主键 if (table.PrimaryKeys.Length < 1) { // 自增作为主键,没办法,如果没有主键,整个实体层都会面临大问题! var dc = table.Columns.FirstOrDefault(c => c.Identity); if (dc != null) { dc.PrimaryKey = true; } } // 从索引中修正主键 FixPrimaryByIndex(table); // 给非主键的自增字段建立唯一索引 CreateUniqueIndexForIdentity(table); // 索引应该具有跟字段一样的唯一和主键约束 FixIndex(table); foreach (var di in table.Indexes) { di.Fix(); } // 修正可能的主字段 if (!table.Columns.Any(e => e.Master)) { var f = table.Columns.FirstOrDefault(e => e.Name.EqualIgnoreCase("Name", "Title")); if (f != null) { f.Master = true; } } return(table); }
private String GetUpdateSql(IDataTable table, IDataColumn[] columns, ICollection <String> updateColumns, ICollection <String> addColumns, ICollection <String> ps) { if ((updateColumns == null || updateColumns.Count == 0) && (addColumns == null || addColumns.Count == 0)) { return(null); } var sb = Pool.StringBuilder.Get(); var db = Database as DbBase; // 字段列表 sb.AppendFormat("Update {0} Set ", db.FormatName(table)); foreach (var dc in columns) { if (dc.Identity || dc.PrimaryKey) { continue; } if (addColumns != null && addColumns.Contains(dc.Name)) { sb.AppendFormat("{0}={0}+{1},", db.FormatName(dc), db.FormatParameterName(dc.Name)); if (!ps.Contains(dc.Name)) { ps.Add(dc.Name); } } else if (updateColumns != null && updateColumns.Contains(dc.Name)) { sb.AppendFormat("{0}={1},", db.FormatName(dc), db.FormatParameterName(dc.Name)); if (!ps.Contains(dc.Name)) { ps.Add(dc.Name); } } } sb.Length--; // 条件 sb.Append(" Where "); foreach (var dc in columns) { if (!dc.PrimaryKey) { continue; } sb.AppendFormat("{0}={1}", db.FormatName(dc), db.FormatParameterName(dc.Name)); sb.Append(" And "); if (!ps.Contains(dc.Name)) { ps.Add(dc.Name); } } sb.Length -= " And ".Length; return(sb.Put(true)); }
public static IDataTable Project(IDataTable table, params int[] columns) { return(Project(table, columns)); }
/// <summary>获取索引</summary> /// <param name="table"></param> /// <param name="indexes">索引</param> /// <param name="indexColumns">索引列</param> /// <returns></returns> protected virtual List <IDataIndex> GetIndexes(IDataTable table, DataTable indexes, DataTable indexColumns) { if (indexes == null) { return(null); } var drs = indexes.Select(String.Format("{0}='{1}'", _.TalbeName, table.TableName)); if (drs == null || drs.Length < 1) { return(null); } var list = new List <IDataIndex>(); foreach (var dr in drs) { if (!TryGetDataRowValue(dr, _.IndexName, out String name)) { continue; } var di = table.CreateIndex(); di.Name = name; if (TryGetDataRowValue(dr, _.ColumnName, out name) && !String.IsNullOrEmpty(name)) { di.Columns = name.Split(","); } else if (indexColumns != null) { String orderby = null; // Oracle数据库用ColumnPosition,其它数据库用OrdinalPosition if (indexColumns.Columns.Contains(_.OrdinalPosition)) { orderby = _.OrdinalPosition; } else if (indexColumns.Columns.Contains(_.ColumnPosition)) { orderby = _.ColumnPosition; } var dics = indexColumns.Select(String.Format("{0}='{1}' And {2}='{3}'", _.TalbeName, table.TableName, _.IndexName, di.Name), orderby); if (dics != null && dics.Length > 0) { var ns = new List <String>(); foreach (var item in dics) { if (TryGetDataRowValue(item, _.ColumnName, out String dcname) && !dcname.IsNullOrEmpty() && !ns.Contains(dcname)) { ns.Add(dcname); } } if (ns.Count < 1) { DAL.WriteLog("表{0}的索引{1}无法取得字段列表!", table, di.Name); } di.Columns = ns.ToArray(); } } if (TryGetDataRowValue(dr, "UNIQUE", out Boolean b)) { di.Unique = b; } if (TryGetDataRowValue(dr, "PRIMARY", out b)) { di.PrimaryKey = b; } else if (TryGetDataRowValue(dr, "PRIMARY_KEY", out b)) { di.PrimaryKey = b; } FixIndex(di, dr); list.Add(di); } return(list != null && list.Count > 0 ? list : null); }
/// <summary>获取指定表的字段</summary> /// <param name="table"></param> /// <param name="rows"></param> /// <returns></returns> protected virtual List <IDataColumn> GetFields(IDataTable table, DataRow[] rows) { var list = new List <IDataColumn>(); foreach (var dr in rows) { var field = table.CreateColumn(); // 名称 field.ColumnName = GetDataRowValue <String>(dr, _.ColumnName); // 标识、主键 if (TryGetDataRowValue(dr, "AUTOINCREMENT", out Boolean b)) { field.Identity = b; } if (TryGetDataRowValue(dr, "PRIMARY_KEY", out b)) { field.PrimaryKey = b; } // 原始数据类型 field.RawType = GetDataRowValue <String>(dr, "DATA_TYPE", "DATATYPE", "COLUMN_DATA_TYPE"); // 长度 field.Length = GetDataRowValue <Int32>(dr, "CHARACTER_MAXIMUM_LENGTH", "LENGTH", "COLUMN_SIZE"); if (field is XField fi) { // 精度 与 位数 fi.Precision = GetDataRowValue <Int32>(dr, "NUMERIC_PRECISION", "DATETIME_PRECISION", "PRECISION"); fi.Scale = GetDataRowValue <Int32>(dr, "NUMERIC_SCALE", "SCALE"); if (field.Length == 0) { field.Length = fi.Precision; } } // 允许空 if (TryGetDataRowValue(dr, "IS_NULLABLE", out b)) { field.Nullable = b; } else if (TryGetDataRowValue(dr, "IS_NULLABLE", out String str)) { if (!String.IsNullOrEmpty(str)) { field.Nullable = "YES".EqualIgnoreCase(str); } } else if (TryGetDataRowValue(dr, "NULLABLE", out str)) { if (!String.IsNullOrEmpty(str)) { field.Nullable = "Y".EqualIgnoreCase(str); } } // 描述 field.Description = GetDataRowValue <String>(dr, "DESCRIPTION"); FixField(field, dr); // 检查是否已正确识别类型 if (field.DataType == null) { WriteLog("无法识别{0}.{1}的类型{2}!", table.TableName, field.ColumnName, field.RawType); } // 非字符串字段,长度没有意义 //else if (field.DataType != typeof(String)) // field.Length = 0; field.Fix(); list.Add(field); } return(list); }
/// <summary>修正表</summary> /// <param name="table"></param> /// <param name="dr"></param> /// <param name="data"></param> protected virtual void FixTable(IDataTable table, DataRow dr, IDictionary <String, DataTable> data) { }
public DataTableCultureDisplayNames(UCultureInfo culture, DisplayContextOptions options) #pragma warning disable 612, 618 : base() #pragma warning restore 612, 618 { this.displayContextOptions = options.Freeze(); this.langData = languageDataTableProvider.GetDataTable(culture, options.SubstituteHandling == SubstituteHandling.NoSubstitute); this.regionData = regionDataTableProvider.GetDataTable(culture, options.SubstituteHandling == SubstituteHandling.NoSubstitute); this.locale = langData.CultureInfo != null && langData.CultureInfo.Equals(CultureInfo.InvariantCulture) ? regionData.CultureInfo.ToUCultureInfo() : langData.CultureInfo.ToUCultureInfo(); // Note, by going through DataTable, this uses table lookup rather than straight lookup. // That should get us the same data, I think. This way we don't have to explicitly // load the bundle again. Using direct lookup didn't seem to make an appreciable // difference in performance. string sep = langData.Get("localeDisplayPattern", "separator"); if (sep == null || "separator".Equals(sep)) { sep = "{0}, {1}"; } StringBuilder sb = new StringBuilder(); this.separatorFormat = SimpleFormatterImpl.CompileToStringMinMaxArguments(sep, sb, 2, 2); string pattern = langData.Get("localeDisplayPattern", "pattern"); if (pattern == null || "pattern".Equals(pattern)) { pattern = "{0} ({1})"; } this.format = SimpleFormatterImpl.CompileToStringMinMaxArguments(pattern, sb, 2, 2); if (pattern.Contains("(")) { formatOpenParen = '('; formatCloseParen = ')'; formatReplaceOpenParen = '['; formatReplaceCloseParen = ']'; } else { formatOpenParen = '('; formatCloseParen = ')'; formatReplaceOpenParen = '['; formatReplaceCloseParen = ']'; } string keyTypePattern = langData.Get("localeDisplayPattern", "keyTypePattern"); if (keyTypePattern == null || "keyTypePattern".Equals(keyTypePattern)) { keyTypePattern = "{0}={1}"; } this.keyTypeFormat = SimpleFormatterImpl.CompileToStringMinMaxArguments( keyTypePattern, sb, 2, 2); // Get values from the contextTransforms data if we need them // Also check whether we will need a break iterator (depends on the data) bool needBrkIter = false; if (options.Capitalization == Capitalization.UIListOrMenu || options.Capitalization == Capitalization.Standalone) { capitalizationUsage = new bool[Enum.GetValues(typeof(CapitalizationContextUsage)).Length]; // initialized to all false ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.GetBundleInstance(ICUData.IcuBaseName, locale); CapitalizationContextSink sink = new CapitalizationContextSink(this); try { rb.GetAllItemsWithFallback("contextTransforms", sink); } catch (MissingManifestResourceException) { // Silently ignore. Not every locale has contextTransforms. } needBrkIter = sink.hasCapitalizationUsage; } // Get a sentence break iterator if we will need it if (needBrkIter || options.Capitalization == Capitalization.BeginningOfSentence) { capitalizationBrkIter = BreakIterator.GetSentenceInstance(locale); } this.currencyDisplayInfo = CurrencyData.Provider.GetInstance(locale, false); }
/// <summary>根据数据行取得数据表</summary> /// <param name="rows">数据行</param> /// <returns></returns> protected List <IDataTable> GetTables(DataRow[] rows) { //if (_columns == null) _columns = GetSchema(_.Columns, null); //if (_indexes == null) _indexes = GetSchema(_.Indexes, null); //if (_indexColumns == null) _indexColumns = GetSchema(_.IndexColumns, null); if (_columns == null) { try { _columns = GetSchema(_.Columns, null); } catch (Exception ex) { DAL.WriteDebugLog(ex.ToString()); } } if (_indexes == null) { try { _indexes = GetSchema(_.Indexes, null); } catch (Exception ex) { DAL.WriteDebugLog(ex.ToString()); } } if (_indexColumns == null) { try { _indexColumns = GetSchema(_.IndexColumns, null); } catch (Exception ex) { DAL.WriteDebugLog(ex.ToString()); } } try { DAL.WriteDebugLog("GetTables(Rows)- Foreach Start"); List <IDataTable> list = new List <IDataTable>(); foreach (DataRow dr in rows) { #region 基本属性 IDataTable table = DAL.CreateTable(); table.Name = GetDataRowValue <String>(dr, _.TalbeName); // 顺序、编号 Int32 id = 0; if (TryGetDataRowValue <Int32>(dr, "TABLE_ID", out id)) { table.ID = id; } else { table.ID = list.Count + 1; } // 描述 table.Description = GetDataRowValue <String>(dr, "DESCRIPTION"); // 拥有者 table.Owner = GetDataRowValue <String>(dr, "OWNER"); // 是否视图 table.IsView = String.Equals("View", GetDataRowValue <String>(dr, "TABLE_TYPE"), StringComparison.OrdinalIgnoreCase); table.DbType = Database.DbType; #endregion #region 字段及修正 // 字段的获取可能有异常,但不应该影响整体架构的获取 try { var columns = GetFields(table); if (columns != null && columns.Count > 0) { table.Columns.AddRange(columns); } var indexes = GetIndexes(table); if (indexes != null && indexes.Count > 0) { table.Indexes.AddRange(indexes); } // 先修正一次关系数据 table.Fix(); } catch (Exception ex) { if (DAL.Debug) { DAL.WriteLog(ex.ToString()); } } FixTable(table, dr); list.Add(table); #endregion } DAL.WriteDebugLog("GetTables(Rows)- Foreach End"); #region 表间关系处理 //// 某字段名,为另一个表的(表名+单主键名)形式时,作为关联字段处理 //foreach (var table in list) //{ // foreach (var rtable in list) // { // if (table != rtable) table.Connect(rtable); // } //} ModelHelper.Connect(list); //// 因为可能修改了表间关系,再修正一次 //foreach (var table in list) //{ // table.Fix(); //} #endregion return(list); // 不要把这些清空。因为,多线程同时操作的时候,前面的线程有可能把后面线程的数据给清空了 } finally { _columns = null; _indexes = null; _indexColumns = null; } }
/// <summary>猜测表间关系</summary> /// <param name="table"></param> /// <param name="rtable"></param> /// <param name="rname"></param> /// <param name="column"></param> /// <param name="name"></param> /// <returns></returns> public virtual Boolean GuessRelation(IDataTable table, IDataTable rtable, String rname, IDataColumn column, String name) { if (name.Length <= rtable.Name.Length || !name.StartsWith(rtable.Name, StringComparison.OrdinalIgnoreCase)) return false; var key = name.Substring(rtable.Name.Length); var dc = rtable.GetColumn(key); // 猜测两表关联关系时,两个字段的类型也必须一致 if (dc == null || dc.DataType != column.DataType) return false; // 建立关系 var dr = table.CreateRelation(); dr.Column = column.Name; dr.RelationTable = rtable.Name; dr.RelationColumn = dc.Name; // 表关系这里一般是多对一,比如管理员的RoleID=>Role+Role.ID,对于索引来说,不是唯一的 dr.Unique = false; // 当然,如果这个字段column有唯一索引,那么,这里也是唯一的。这就是典型的一对一 if (column.PrimaryKey || column.Identity) dr.Unique = true; else { var di = table.GetIndex(column.Name); if (di != null && di.Unique) dr.Unique = true; } dr.Computed = true; if (table.GetRelation(dr) == null) table.Relations.Add(dr); // 给另一方建立关系 //foreach (IDataRelation item in rtable.Relations) //{ // if (item.Column == dc.Name && item.RelationTable == table.Name && item.RelationColumn == column.Name) return dr; //} if (rtable.GetRelation(dc.Name, table.Name, column.Name) != null) return true; dr = rtable.CreateRelation(); dr.Column = dc.Name; dr.RelationTable = table.Name; dr.RelationColumn = column.Name; // 那么这里就是唯一的啦 dr.Unique = true; // 当然,如果字段dc不是主键,也没有唯一索引,那么关系就不是唯一的。这就是典型的多对多 if (!dc.PrimaryKey && !dc.Identity) { var di = rtable.GetIndex(dc.Name); // 没有索引,或者索引不是唯一的 if (di == null || !di.Unique) dr.Unique = false; } dr.Computed = true; if (rtable.GetRelation(dr) == null) rtable.Relations.Add(dr); return true; }
/// <summary>修正表</summary> /// <param name="table"></param> /// <param name="dr"></param> protected virtual void FixTable(IDataTable table, DataRow dr) { }
/// <summary>从索引中修正主键</summary> /// <param name="table"></param> protected virtual void FixPrimaryByIndex(IDataTable table) { var pks = table.PrimaryKeys; if (pks == null || pks.Length < 1) { // 在索引中找唯一索引作为主键 foreach (var item in table.Indexes) { if (!item.PrimaryKey || item.Columns == null || item.Columns.Length < 1) continue; pks = table.GetColumns(item.Columns); if (pks != null && pks.Length > 0) Array.ForEach<IDataColumn>(pks, dc => dc.PrimaryKey = true); } } pks = table.PrimaryKeys; if (pks == null || pks.Length < 1) { // 在索引中找唯一索引作为主键 foreach (var item in table.Indexes) { if (!item.Unique || item.Columns == null || item.Columns.Length < 1) continue; pks = table.GetColumns(item.Columns); if (pks != null && pks.Length > 0) Array.ForEach<IDataColumn>(pks, dc => dc.PrimaryKey = true); } } pks = table.PrimaryKeys; if (pks == null || pks.Length < 1) { // 如果还没有主键,把第一个索引作为主键 foreach (var item in table.Indexes) { if (item.Columns == null || item.Columns.Length < 1) continue; pks = table.GetColumns(item.Columns); if (pks != null && pks.Length > 0) Array.ForEach<IDataColumn>(pks, dc => dc.PrimaryKey = true); } } }
/// <summary>获取指定表的字段</summary> /// <param name="table"></param> /// <param name="rows"></param> /// <returns></returns> protected virtual List <IDataColumn> GetFields(IDataTable table, DataRow[] rows) { var list = new List <IDataColumn>(); // 开始序号 Int32 startIndex = 0; foreach (var dr in rows) { var field = table.CreateColumn(); // 序号 Int32 n = 0; if (TryGetDataRowValue <Int32>(dr, _.OrdinalPosition, out n)) { field.ID = n; } else if (TryGetDataRowValue <Int32>(dr, _.ID, out n)) { field.ID = n; } // 如果从0开始,则所有需要同步增加;如果所有字段序号都是0,则按照先后顺序 if (field.ID == 0) { startIndex++; //field.ID = startIndex; } if (startIndex > 0) { field.ID += startIndex; } // 名称 field.Name = GetDataRowValue <String>(dr, _.ColumnName); // 标识、主键 Boolean b; if (TryGetDataRowValue <Boolean>(dr, "AUTOINCREMENT", out b)) { field.Identity = b; } if (TryGetDataRowValue <Boolean>(dr, "PRIMARY_KEY", out b)) { field.PrimaryKey = b; } // 原始数据类型 String str; if (TryGetDataRowValue <String>(dr, "DATA_TYPE", out str)) { field.RawType = str; } else if (TryGetDataRowValue <String>(dr, "DATATYPE", out str)) { field.RawType = str; } else if (TryGetDataRowValue <String>(dr, "COLUMN_DATA_TYPE", out str)) { field.RawType = str; } // 是否Unicode if (Database is DbBase) { field.IsUnicode = (Database as DbBase).IsUnicode(field.RawType); } // 精度 if (TryGetDataRowValue <Int32>(dr, "NUMERIC_PRECISION", out n)) { field.Precision = n; } else if (TryGetDataRowValue <Int32>(dr, "DATETIME_PRECISION", out n)) { field.Precision = n; } else if (TryGetDataRowValue <Int32>(dr, "PRECISION", out n)) { field.Precision = n; } // 位数 if (TryGetDataRowValue <Int32>(dr, "NUMERIC_SCALE", out n)) { field.Scale = n; } else if (TryGetDataRowValue <Int32>(dr, "SCALE", out n)) { field.Scale = n; } // 长度 if (TryGetDataRowValue <Int32>(dr, "CHARACTER_MAXIMUM_LENGTH", out n)) { field.Length = n; } else if (TryGetDataRowValue <Int32>(dr, "LENGTH", out n)) { field.Length = n; } else if (TryGetDataRowValue <Int32>(dr, "COLUMN_SIZE", out n)) { field.Length = n; } else { field.Length = field.Precision; } // 字节数 if (TryGetDataRowValue <Int32>(dr, "CHARACTER_OCTET_LENGTH", out n)) { field.NumOfByte = n; } else { field.NumOfByte = field.Length; } // 允许空 if (TryGetDataRowValue <Boolean>(dr, "IS_NULLABLE", out b)) { field.Nullable = b; } else if (TryGetDataRowValue <String>(dr, "IS_NULLABLE", out str)) { if (!String.IsNullOrEmpty(str)) { field.Nullable = String.Equals("YES", str, StringComparison.OrdinalIgnoreCase); } } else if (TryGetDataRowValue <String>(dr, "NULLABLE", out str)) { if (!String.IsNullOrEmpty(str)) { field.Nullable = String.Equals("Y", str, StringComparison.OrdinalIgnoreCase); } } // 默认值 field.Default = GetDataRowValue <String>(dr, "COLUMN_DEFAULT"); // 描述 field.Description = GetDataRowValue <String>(dr, "DESCRIPTION"); FixField(field, dr); // 检查是否已正确识别类型 if (field.DataType == null) { WriteLog("无法识别{0}.{1}的类型{2}!", table.Name, field.Name, field.RawType); } field.Fix(); list.Add(field); } return(list); }
/// <summary>备份单表数据</summary> /// <remarks> /// 最大支持21亿行 /// </remarks> /// <param name="table">数据表</param> /// <param name="stream">目标数据流</param> /// <param name="progress">进度回调,参数为已处理行数和当前页表</param> /// <returns></returns> public Int32 Backup(IDataTable table, Stream stream, Action <Int64, DbTable> progress = null) { var writeFile = new WriteFileActor { Stream = stream, // 最多同时堆积数 BoundedCapacity = 4, }; // 自增 var id = table.Columns.FirstOrDefault(e => e.Identity); if (id == null) { var pks = table.PrimaryKeys; if (pks != null && pks.Length == 1 && pks[0].DataType.IsInt()) { id = pks[0]; } } var tableName = Db.FormatName(table); var sb = new SelectBuilder { Table = tableName }; // 总行数 writeFile.Total = SelectCount(sb); WriteLog("备份[{0}/{1}]开始,共[{2:n0}]行", table, ConnName, writeFile.Total); IExtracter <DbTable> extracer = new PagingExtracter(this, tableName); if (id != null) { extracer = new IdExtracter(this, tableName, id.ColumnName); } var sw = Stopwatch.StartNew(); var total = 0; foreach (var dt in extracer.Fetch()) { var count = dt.Rows.Count; WriteLog("备份[{0}/{1}]数据 {2:n0} + {3:n0}", table, ConnName, extracer.Row, count); if (count == 0) { break; } // 进度报告 progress?.Invoke(extracer.Row, dt); // 消费数据 writeFile.Tell(dt); total += count; } // 通知写入完成 writeFile.Stop(-1); sw.Stop(); var ms = sw.Elapsed.TotalMilliseconds; WriteLog("备份[{0}/{1}]完成,共[{2:n0}]行,耗时{3:n0}ms,速度{4:n0}tps", table, ConnName, total, ms, total * 1000L / ms); // 返回总行数 return(total); }
/// <summary>获取索引</summary> /// <param name="table"></param> /// <returns></returns> protected virtual List <IDataIndex> GetIndexes(IDataTable table) { if (_indexes == null) { return(null); } DataRow[] drs = _indexes.Select(String.Format("{0}='{1}'", _.TalbeName, table.Name)); if (drs == null || drs.Length < 1) { return(null); } List <IDataIndex> list = new List <IDataIndex>(); foreach (DataRow dr in drs) { String name = null; if (!TryGetDataRowValue <String>(dr, _.IndexName, out name)) { continue; } IDataIndex di = table.CreateIndex(); di.Name = name; if (TryGetDataRowValue <string>(dr, _.ColumnName, out name) && !String.IsNullOrEmpty(name)) { di.Columns = name.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries); } else if (_indexColumns != null) { String orderby = null; // Oracle数据库用ColumnPosition,其它数据库用OrdinalPosition if (_indexColumns.Columns.Contains(_.OrdinalPosition)) { orderby = _.OrdinalPosition; } else if (_indexColumns.Columns.Contains(_.ColumnPosition)) { orderby = _.ColumnPosition; } DataRow[] dics = _indexColumns.Select(String.Format("{0}='{1}' And {2}='{3}'", _.TalbeName, table.Name, _.IndexName, di.Name), orderby); if (dics != null && dics.Length > 0) { List <String> ns = new List <string>(); foreach (DataRow item in dics) { String dcname = null; if (TryGetDataRowValue <String>(item, _.ColumnName, out dcname) && !String.IsNullOrEmpty(dcname) && !ns.Contains(dcname)) { ns.Add(dcname); } } if (ns.Count < 1) { DAL.WriteDebugLog("表{0}的索引{1}无法取得字段列表!", table, di.Name); } di.Columns = ns.ToArray(); } } Boolean b = false; if (TryGetDataRowValue <Boolean>(dr, "UNIQUE", out b)) { di.Unique = b; } if (TryGetDataRowValue <Boolean>(dr, "PRIMARY", out b)) { di.PrimaryKey = b; } else if (TryGetDataRowValue <Boolean>(dr, "PRIMARY_KEY", out b)) { di.PrimaryKey = b; } FixIndex(di, dr); list.Add(di); } return(list != null && list.Count > 0 ? list : null); }
/// <summary>使用数据架构确定数据表是否存在,因为使用系统视图可能没有权限</summary> /// <param name="table"></param> /// <returns></returns> public Boolean TableExist(IDataTable table) { var dt = GetSchema(_.Tables, new String[] { null, null, table.TableName, null }); return(dt != null && dt.Rows != null && dt.Rows.Count > 0); }
/// <summary> /// 修正表名,去前缀,自动大小写 /// </summary> /// <param name="table"></param> /// <returns></returns> public virtual String GetClassName(IDataTable table) { String name = table.Name; if (Config.AutoCutPrefix) name = CutPrefix(name); if (Config.AutoFixWord) name = FixWord(name); return name; }
public DefaultDataTableAdaptor(ILinearAlgebraProvider lap, IDataTable dataTable, IDataTableVectoriser vectoriser = null) : base(lap, dataTable) { _vectoriser = vectoriser ?? dataTable.GetVectoriser(true); }
/// <summary>从数据流恢复数据</summary> /// <param name="stream">数据流</param> /// <param name="table">数据表</param> /// <param name="progress">进度回调,参数为已处理行数和当前页表</param> /// <returns></returns> public Int32 Restore(Stream stream, IDataTable table, Action <Int32, DbTable> progress = null) { var writeDb = new WriteDbActor { Table = table, Dal = this, // 最多同时堆积数页 BoundedCapacity = 4, }; //writeDb.Start(); var sw = Stopwatch.StartNew(); // 二进制读写器 var bn = new Binary { EncodeInt = true, Stream = stream, }; var dt = new DbTable(); dt.ReadHeader(bn); WriteLog("恢复[{0}/{1}]开始,共[{2:n0}]行", table.Name, ConnName, dt.Total); // 输出日志 var cs = dt.Columns; var ts = dt.Types; WriteLog("字段[{0}]:{1}", cs.Length, cs.Join()); WriteLog("类型[{0}]:{1}", ts.Length, ts.Join(",", e => e.Name)); var row = 0; var pageSize = (Db as DbBase).BatchSize; var total = 0; while (true) { // 读取数据 dt.ReadData(bn, Math.Min(dt.Total - row, pageSize)); var rs = dt.Rows; if (rs == null || rs.Count == 0) { break; } WriteLog("恢复[{0}/{1}]数据 {2:n0} + {3:n0}", table.Name, ConnName, row, rs.Count); // 进度报告 progress?.Invoke(row, dt); // 批量写入数据库。克隆对象,避免被修改 writeDb.Tell(dt.Clone()); // 下一页 total += rs.Count; if (rs.Count < pageSize) { break; } row += pageSize; } // 通知写入完成 writeDb.Stop(-1); sw.Stop(); var ms = sw.Elapsed.TotalMilliseconds; WriteLog("恢复[{0}/{1}]完成,共[{2:n0}]行,耗时{3:n0}ms,速度{4:n0}tps", table.Name, ConnName, total, ms, total * 1000L / ms); // 返回总行数 return(total); }
public override String CreateTableSQL(IDataTable table) { List<IDataColumn> Fields = new List<IDataColumn>(table.Columns); //Fields.Sort(delegate(IDataColumn item1, IDataColumn item2) { return item1.ID.CompareTo(item2.ID); }); Fields.OrderBy(dc => dc.ID); StringBuilder sb = new StringBuilder(); List<String> pks = new List<String>(); sb.AppendFormat("Create Table If Not Exists {0}(", FormatName(table.Name)); for (Int32 i = 0; i < Fields.Count; i++) { sb.AppendLine(); sb.Append("\t"); sb.Append(FieldClause(Fields[i], true)); if (i < Fields.Count - 1) sb.Append(","); if (Fields[i].PrimaryKey) pks.Add(FormatName(Fields[i].Name)); } // 如果有自增,则自增必须作为主键 foreach (IDataColumn item in table.Columns) { if (item.Identity && !item.PrimaryKey) { pks.Clear(); pks.Add(FormatName(item.Name)); break; } } if (pks.Count > 0) { sb.AppendLine(","); sb.AppendFormat("\tPrimary Key ({0})", String.Join(",", pks.ToArray())); } sb.AppendLine(); sb.Append(")"); return sb.ToString(); }
String TableExistSQL(IDataTable table) { return(TableExistSQL(table.Name)); }
/// <summary>克隆</summary> /// <param name="table"></param> /// <returns></returns> public IDataColumn Clone(IDataTable table) { var field = base.MemberwiseClone() as XField; field.Table = table; field.Fix(); return field; }
public virtual String DropTableDescriptionSQL(IDataTable table) { return(null); }
public override void Init() { /* * IEventHandler h = App.On(ApplicationEvents.ON_INITED,(sender,e)=>{ * * Debug.Log("9123891237012897312"); * }); * * IEventHandler aa = App.On(ApplicationEvents.ON_INITED,(sender,e)=>{ * * Debug.Log("aksldjalkds9123891237012897312"); * }); * * IEventHandler bb = App.On(ApplicationEvents.ON_INITED,(sender,e)=>{ * * Debug.Log("ooooooooo9123891237012897312"); * });*/ App.On(ApplicationEvents.ON_APPLICATION_START_COMPLETE, (sender, e) => { IRouter router = App.Make <IRouter>(); router.Dispatch("catlib://*****:*****@test1.com/thisishello/sometime"); router.Dispatch("test://test2?hello=123"); router.Dispatch("catlib://test3"); router.Dispatch("home://test3"); router.Dispatch("catlib://home/test3"); return; //return; /* * Uri uriss = new Uri("catlib:///home/like/?myname=123&ss=222"); * Debug.Log(uriss.DnsSafeHost); * Debug.Log("Query:" + uriss.Query); * Debug.Log("AbsolutePath:" + uriss.AbsolutePath); * return;*/ /* * Regex reg = new Regex("^catlib\\://main/hash/(?<name>[^/]+)(?:/(?<age>[^/]+))?$"); * * Debug.Log(reg.IsMatch("catlib://main/hash/yubin/18") ? "yes" : "no"); */ //return; router.SetDefaultScheme("catlib"); /* * router.Reg("name", (request, response) => * { * * Debug.Log("has routing / !"); * * }); * * router.Reg("myscheme://helloworld", (request, response) => * { * * Debug.Log("hello world"); * * });*/ router.Group(() => //路由组允许统一定义规则 { router.Reg("hello/world", (request, response) => { Debug.Log("hello/world:" + request.Get("say")); }); router.Reg("hello/gold/{say?}", (request, response) => { Debug.Log("hello/gold:" + request.Get("say")); }); router.Reg("hello/cat/{say?}", (request, response) => { Debug.Log("hello/cat:" + request.Get("say")); }); }, "testgroup").Middleware((req, resp, filter) => { Debug.Log("this is group middleware 1"); filter.Do(req, resp); //所有中间件必须do不do意味着请求被拦截 Debug.Log("this is group middleware 1 back"); }).Defaults("say", "this is say"); //只有是请求参数时default才会生效 router.Reg("hello/dog", (request, response) => { Debug.Log("hello/dog"); }).Group("testgroup"); //通过路由组的名字也可以直接设定已经定义好规则的路由组 router.Reg("test/controller", typeof(TestController), "TestAction"); router.Reg("test/controller222", typeof(TestController), "TestAction222"); router.Reg("test/controller333", typeof(TestController), "TestAction333"); router.OnError((req, err, next) => { Debug.Log("has some error:" + err.Message); }); router.Dispatch("catlib://hello/world"); router.Dispatch("catlib://hello/gold"); router.Dispatch("catlib://hello/cat/18"); router.Dispatch("catlib://hello/dog"); router.Dispatch("catlib://test/controller"); router.Dispatch("catlib://test/controller222"); router.Dispatch("catlib://test/controller333"); return; router.Reg("main/hash/{name}/time/{age?}/{apple?}", (request, response) => { Debug.Log("has routing ,my name is:" + request.Get("name") + ", my age is:" + request.Get("age")); response.SetContext("this is response context"); }).Where("age", "[0-9]+"); router.OnNotFound((req, filter) => { Debug.Log("can not find route"); }); //Debug.Log("dispatch : myscheme://helloworld"); //router.Dispatch("myscheme://helloworld"); //Debug.Log("dispatch : catlib://name"); //router.Dispatch("catlib://name"); Debug.Log("dispatch : catlib://main/hash/yubin/time/18?name=cat"); var rData = router.Dispatch("catlib://main/hash/yubin/time/18?name=cat"); Debug.Log(rData.GetContext().ToString()); //Deresponsebug.Log("dispatch : catlib://main/hash/yubin/time/1d8"); //router.Dispatch("catlib://main/hash/yubin/time/1d8"); //Debug.Log("dispatch : catlib://main/hash/yubin/time"); //router.Dispatch("catlib://main/hash/yubin/time"); return; Debug.Log(Regex.Replace("/home/{lik{bbb}e?}/", @"\{(\w+?)\?\}", "{$1}")); return; System.Uri uri = new System.Uri("catlib://*****:*****@hello/home/like/?myname=123&ss=222"); Debug.Log("UserInfo:" + uri.UserInfo); Debug.Log("DnsSafeHost:" + uri.DnsSafeHost); Debug.Log("Fragment:" + uri.Fragment); Debug.Log("Authority:" + uri.Authority); Debug.Log("Query:" + uri.Query); Debug.Log("AbsolutePath:" + uri.AbsolutePath); foreach (var s in uri.Segments) { Debug.Log("Segments:" + s); } Debug.Log("Scheme:" + uri.Scheme); return; IFilterChain fc = App.Make <IFilterChain>(); IFilterChain <string> bb = fc.Create <string>(); bb.Add((inData, chain) => { Debug.Log("first:" + inData); inData = "zzzz"; chain.Do(inData); Debug.Log("first:" + inData); }); bb.Add((inData, chain) => { Debug.Log("second:" + inData); chain.Do(inData); Debug.Log("second:" + inData); //string的不变性 }); bb.Then((inData) => { inData = "end"; Debug.Log("end"); }); bb.Do("hello"); return; var abs = new Foosub() { Hello = 100, IsTrue = true }; ILocalSetting setting = App.Make <ILocalSetting>(); setting.SetObject("testobj", abs); setting.SetInt("aa", 19); var abc = setting.GetObject <Foosub>("testobj"); Debug.Log(abc.Hello); Debug.Log(abc.IsTrue); Debug.Log(setting.GetInt("aa")); Debug.Log(setting.GetInt("ab", 100)); // return; ITimeQueue queue = App.Make <ITimeQueue>(); Debug.Log(Time.frameCount); queue.Task(() => { Debug.Log("time queue complete:" + Time.frameCount); }).LoopFrame(10).Play(); /* * queue.Task(() => * { * Debug.Log("time queue complete"); * }).Delay(3).Loop(5).Delay(5).Loop(()=>{ * * return Random.Range(0,100) > 10; * * }).OnComplete(()=>{ * Debug.Log("query complete"); * }).Play(); * * FThread.Instance.Task(() => * { * if(queue.Replay()){ * * Debug.Log("reset complete!"); * * }else{ * * throw new System.Exception("faild"); * * } * }).Delay(5).Start();*/ //return; ICsvStore csvStore = App.Make <ICsvStore>(); foreach (var v in csvStore["csv2"].Where((selector) => { selector.Where("name", "=", "小兔子").OrWhere("tag", "<", "3"); }).Where("tag", "<", "1000").Get()) { Debug.Log(v["name"]); } //return; IEnv env = App.Make <IEnv>(); IIOFactory fac = App.Make <IIOFactory>(); IDisk disk = fac.Disk(); IFile fff = disk.File(env.ResourcesNoBuildPath + System.IO.Path.AltDirectorySeparatorChar + "csv" + System.IO.Path.AltDirectorySeparatorChar + "csv2.csv"); string ssss = System.Text.Encoding.UTF8.GetString(fff.Read()); /* * string[] ss2 = ssss.Split(new string[]{ System.Environment.NewLine}, System.StringSplitOptions.RemoveEmptyEntries ); * * foreach(var ss in ss2){ * Debug.Log(ss); * } * * return;*/ ICsvParser csvParser = App.Make <ICsvParser>(); string[][] parser = csvParser.Parser(ssss); /* * foreach(var v in parser){ * * Debug.Log(v[0] + "|" + v[1] + "|" + v[2]); * * }*/ IDataTableFactory dataTable = App.Make <IDataTableFactory>(); IDataTable table = dataTable.Make(parser); foreach (var v in table.Where((selector) => { selector.Where("name", "=", "小兔子").OrWhere("tag", "<", "3"); }).Where("tag", "<", "1000").Get()) { Debug.Log(v["name"]); } // return; foreach (string[] s in parser) { Debug.Log(s[0] + "|" + s[1] + "|" + s[2]); } //return; IProtobuf protobuf = App.Make <IProtobuf>(); var person = new TestProto.Person { Id = 12345, Name = "Fred", Address = new TestProto.Address { Line1 = "Flat 1", Line2 = "The Meadows" } }; byte[] data = protobuf.Serializers(person); Debug.Log(data.Length); var p = protobuf.UnSerializers <TestProto.Person>(data); Debug.Log(p.Name); IFile file = disk.File("hello.gz"); ICompress comp = App.Make <ICompress>(); byte[] byt = comp.Compress("helloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworld".ToByte()); Debug.Log("UnCompress: " + "helloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworld".ToByte().Length); Debug.Log("Compress: " + byt.Length); //file.Create(byt); byt = comp.UnCompress(byt); Debug.Log(System.Text.Encoding.UTF8.GetString(byt)); //byte[] debyt = comp.Expand(byt); //Debug.Log(System.Text.Encoding.UTF8.GetString(debyt)); IJson json = App.Make <IJson>(); Foos ff = new Foos(); ff.Value = 100; ff.Value2 = "123"; ff.SubList = new List <Foosub>(); ff.SubList.Add(new Foosub() { Hello = 10, IsTrue = true }); ff.SubList.Add(new Foosub() { Hello = 20, IsTrue = true }); ff.SubList.Add(new Foosub() { Hello = 30, IsTrue = false }); Debug.Log(json.Encode(ff)); Foos f = json.Decode <Foos>(json.Encode(ff)); Debug.Log(f.Value2); foreach (Foosub sb in f.SubList) { Debug.Log(sb.Hello); } //Debug.Log(f.Value); ITranslator tran = App.Make <ITranslator>(); Debug.Log(tran.Trans("test.messages3")); Debug.Log(tran.Trans("test.messages3", "age:18", "name", "anny")); Debug.Log(tran.Trans("test.message", "name:anny")); Debug.Log(tran.TransChoice("test.messages", 0, "name", "喵喵")); Debug.Log(tran.TransChoice("test.messages", 12, "name", "miaomiao")); Debug.Log(tran.TransChoice("test.messages", 20, "name", "miaomiao")); tran.SetLocale("en"); Debug.Log(tran.Trans("test.message", "name", "喵喵")); //IEnv env = App.Make<IEnv>(); //IDisk disk = App.Make<IIOFactory>().Disk(); //IINIResult result = App.Make<IINILoader>().Load(disk.File(env.ResourcesNoBuildPath + System.IO.Path.AltDirectorySeparatorChar + "/lang/cn/test.ini")); //result.Set("helloworld", "mynameisyb", "yb"); //result.Remove("myname"); //result.Save(); IResources res = App.Make <IResources>(); /*res.LoadAsync("prefab/asset6/test-prefab",(a)=> * { * a.Instantiate(); * }); * res.LoadAsync("prefab/asset6/test-prefab2", (a) => * { * a.Instantiate(); * });*/ //var b = res.Load<Object>("prefab/asset6/test-prefab"); res.LoadAsync("prefab/asset6/test-prefab", (aa) => { var dd = aa.Instantiate(); App.Make <ITimeQueue>().Task(() => { Debug.Log("now destroy 1 prefab"); GameObject.Destroy(dd); }).Delay(20).Play(); }); //var a = res.Load("prefab/asset6/test-prefab2"); //GameObject obj = a.Instantiate(); //GameObject.Instantiate(obj); //绕过控制克隆 /*App.Make<ITimeQueue>().Task(() => * { * * Debug.Log("now destroy 1 prefab"); * GameObject.Destroy(obj); * * }).Delay(10).Play();*/ /* * IResources res = App.Make<IResources>(); * res.LoadAsync<GameObject>("prefab/asset6/test-prefab", (obj) => * { * Object.Instantiate(obj); * }); * * //h.Cancel(); * * //App.Event.Trigger(ApplicationEvents.ON_INITED); * * //Debug.Log(App.Make(typeof(Test).ToString(),"123")); * * //IHash hash = App.Make<IHash>(); * //Debug.Log(hash.Bcrypt("helloworld")); * * //ICrypt secret = App.Make<ICrypt>(); * //string code = secret.Encrypt("helloworld"); * //Debug.Log(code); * * //Debug.Log(secret.Decrypt(code)); * * /*FThread.Instance.Task(() => * { * Debug.Log("pppppppppppppppppppp"); * int i = 0; * i++; * return i; * }).Delay(5).Start().Cancel(); */ //Debug.Log(hash.BcryptVerify("helloworld", "$2a$10$Y8BxbHFgGArGVHIucx8i7u7t5ByLlSdWgWcQc187hqFfSiKFJfz3C")); //Debug.Log(hash.BcryptVerify("helloworld", "$2a$15$td2ASPNq.8BXbpa6yUU0c.pQpfYLxtcbXviM8fZXw4v8FDeO3hCoC")); //IAssetBundle bundle = App.Make<IAssetBundle>(); //Object.Instantiate(res.Load<GameObject>("prefab/asset6/test-prefab.prefab")); //Object[] p = res.LoadAll("prefab/asset6"); //IResources res = App.Make<IResources>(); /*res.LoadAsync<GameObject>("prefab/asset6/test-prefab", (obj) => * { * Object.Instantiate(obj); * });*/ //res.UnloadAll(); //Object.Instantiate(res.Load<GameObject>("prefab/asset6/test-prefab.prefab")); /* * Thread subThread = new Thread(new ThreadStart(() => { * * App.MainThread(() => { new GameObject(); }); * * })); * * FThread.Instance.Task(() => * { * int i = 0; * i++; * return i; * }).Delay(5).OnComplete((obj) => Debug.Log("sub thread complete:" + obj)).Start(); * * subThread.Start(); * */ /* * ITimeQueue timeQueue = App.Make<ITimeQueue>(); * * ITimeTaskHandler h = timeQueue.Task(() => * { * Debug.Log("this is in task"); * }).Delay(3).Loop(3).Push(); * * * timeQueue.Task(() => * { * Debug.Log("2222222"); * }).Delay(1).Loop(3).OnComplete(()=> { h.Cancel(); Debug.Log("2 complete"); }).Push(); * * timeQueue.Task(() => * { * Debug.Log("rand!"); * }).Loop(() => { return Random.Range(0,100) > 10; }).Push(); * * timeQueue.OnComplete(() => * { * Debug.Log("queueComplete"); * }); * * timeQueue.Play(); */ /* * * FThread.Instance.Task(() => * { * Debug.Log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); * //Debug.Log(App.Time.Time); * timeQueue.Replay(); * * }).Delay(9).Start();*/ App.On(HttpRequestEvents.ON_MESSAGE + typeof(IConnectorHttp).ToString(), (obj1, obj2) => { Debug.Log((obj2 as IHttpResponse).Text); Debug.Log((obj2 as IHttpResponse).IsError); Debug.Log((obj2 as IHttpResponse).Error); }); IConnectorHttp http = FNetwork.Instance.Create <IConnectorHttp>("http"); //http.SetConfig(new System.Collections.Hashtable() { { "host", "http://www.qidian.com/" } }); http.Get(string.Empty); App.On(SocketRequestEvents.ON_MESSAGE + typeof(IConnectorSocket).ToString(), (obj1, obj2) => { if ((obj2 as PackageResponseEventArgs).Response.Package is string) { Debug.Log((obj2 as PackageResponseEventArgs).Response.Package as string); } else { Debug.Log(Encoding.UTF8.GetString(((obj2 as PackageResponseEventArgs).Response.Package as byte[]))); } }); App.On(SocketRequestEvents.ON_CONNECT, (obj1, obj2) => { Debug.Log("on connect"); }); App.On(SocketRequestEvents.ON_ERROR, (obj1, obj2) => { Debug.Log("on tcp error:" + (obj2 as ErrorEventArgs).Error.Message); }); //链接配置见 NetworkConfig 配置文件 IConnectorTcp tcpConnect = FNetwork.Instance.Create <IConnectorTcp>("tcp.text"); (tcpConnect as IEvent).Event.One(SocketRequestEvents.ON_MESSAGE, (s1, e1) => { Debug.Log((e1 as PackageResponseEventArgs)); if ((e1 as PackageResponseEventArgs).Response.Package is string) { Debug.Log((e1 as PackageResponseEventArgs).Response.Package as string); } else { Debug.Log(Encoding.UTF8.GetString(((e1 as PackageResponseEventArgs).Response.Package as byte[]))); } }); tcpConnect.Connect(); tcpConnect.Send("hello this is tcp msg with [text]".ToByte()); IConnectorTcp tcpConnect2 = FNetwork.Instance.Create <IConnectorTcp>("tcp.frame"); tcpConnect2.Connect(); tcpConnect2.Send("hello this is tcp msg with [frame]".ToByte()); /* * IConnectorUdp udpConnect = FNetwork.Instance.Create<IConnectorUdp>("udp.bind.host.text"); * udpConnect.Connect(); * udpConnect.Send("hello this is udp msg with [text]".ToByte()); * * * IConnectorUdp udpConnectFrame = FNetwork.Instance.Create<IConnectorUdp>("udp.bind.host.frame"); * udpConnectFrame.Connect(); * udpConnectFrame.Send("hello this is udp msg with [frame]".ToByte());*/ IConnectorUdp udpConnect2 = FNetwork.Instance.Create <IConnectorUdp>("udp.unbind.host.frame"); udpConnect2.Connect(); udpConnect2.Send("hello world(client udp)".ToByte(), "pvp.gift", 3301); }); }
/// <summary>检查字段改变。某些数据库(如SQLite)没有添删改字段的DDL语法,可重载该方法,使用重建表方法ReBuildTable</summary> /// <param name="entitytable"></param> /// <param name="dbtable"></param> /// <param name="setting"></param> /// <returns></returns> protected virtual String CheckColumnsChange(IDataTable entitytable, IDataTable dbtable, NegativeSetting setting) { #region 准备工作 var onlySql = setting.CheckOnly; var sql = String.Empty; var sb = new StringBuilder(); var entitydic = new Dictionary <String, IDataColumn>(StringComparer.OrdinalIgnoreCase); if (entitytable.Columns != null) { foreach (var item in entitytable.Columns) { entitydic.Add(item.Name.ToLower(), item); } } var dbdic = new Dictionary <String, IDataColumn>(StringComparer.OrdinalIgnoreCase); if (dbtable.Columns != null) { foreach (var item in dbtable.Columns) { dbdic.Add(item.Name.ToLower(), item); } } #endregion #region 新增列 foreach (IDataColumn item in entitytable.Columns) { if (!dbdic.ContainsKey(item.Name.ToLower())) { //AddColumn(sb, item, onlySql); PerformSchema(sb, onlySql, DDLSchema.AddColumn, item); if (!String.IsNullOrEmpty(item.Description)) { PerformSchema(sb, onlySql, DDLSchema.AddColumnDescription, item); } //! 以下已经不需要了,目前只有SQLite会采用重建表的方式添加删除字段。如果这里提前添加了字段,重建表的时候,会导致失败。 //// 这里必须给dbtable加加上当前列,否则下面如果刚好有删除列的话,会导致增加列成功,然后删除列重建表的时候没有新加的列 //dbtable.Columns.Add(item.Clone(dbtable)); } } #endregion #region 除列 var sbDelete = new StringBuilder(); for (int i = dbtable.Columns.Count - 1; i >= 0; i--) { var item = dbtable.Columns[i]; if (!entitydic.ContainsKey(item.Name.ToLower())) { if (!String.IsNullOrEmpty(item.Description)) { PerformSchema(sb, onlySql, DDLSchema.DropColumnDescription, item); } PerformSchema(sbDelete, setting.NoDelete, DDLSchema.DropColumn, item); } } if (sbDelete.Length > 0) { if (setting.NoDelete) { //不许删除列,显示日志 WriteLog("数据表中发现有多余字段,请手工执行以下语句删除:" + Environment.NewLine + sbDelete.ToString()); } else { if (sb.Length > 0) { sb.AppendLine(";"); } sb.Append(sbDelete.ToString()); } } #endregion #region 修改列 // 开发时的实体数据库 var entityDb = DbFactory.Create(entitytable.DbType); foreach (var item in entitytable.Columns) { IDataColumn dbf = null; if (!dbdic.TryGetValue(item.Name, out dbf)) { continue; } if (IsColumnChanged(item, dbf, entityDb)) { PerformSchema(sb, onlySql, DDLSchema.AlterColumn, item, dbf); } if (IsColumnDefaultChanged(item, dbf, entityDb)) { ChangeColmnDefault(sb, onlySql, item, dbf, entityDb); } if (item.Description + "" != dbf.Description + "") { // 先删除旧注释 //if (!String.IsNullOrEmpty(dbf.Description)) DropColumnDescription(sb, dbf, onlySql); //if (!String.IsNullOrEmpty(dbf.Description)) PerformSchema(sb, onlySql, DDLSchema.DropColumnDescription, dbf); if (dbf.Description != null) { PerformSchema(sb, onlySql, DDLSchema.DropColumnDescription, dbf); } // 加上新注释 if (!String.IsNullOrEmpty(item.Description)) { PerformSchema(sb, onlySql, DDLSchema.AddColumnDescription, item); } } } #endregion return(sb.ToString()); }
/// <summary>连接两个表。 /// 实际上是猜测它们之间的关系,根据一个字段名是否等于另一个表的表名加某个字段名来判断是否存在关系。</summary> /// <param name="table"></param> /// <param name="rtable"></param> public virtual IDataTable Connect(IDataTable table, IDataTable rtable) { foreach (var dc in table.Columns) { if (dc.PrimaryKey || dc.Identity) continue; if (GuessRelation(table, rtable, rtable.Name, dc, dc.Name)) continue; if (!dc.Name.EqualIgnoreCase(dc.Alias)) { if (GuessRelation(table, rtable, rtable.Name, dc, dc.Alias)) continue; } //if (String.Equals(rtable.Alias, rtable.Name, StringComparison.OrdinalIgnoreCase)) continue; if (rtable.Name.EqualIgnoreCase(rtable.Alias)) continue; // 如果表2的别名和名称不同,还要继续 if (GuessRelation(table, rtable, rtable.Alias, dc, dc.Name)) continue; if (!dc.Name.EqualIgnoreCase(dc.Alias)) { if (GuessRelation(table, rtable, rtable.Alias, dc, dc.Alias)) continue; } } return table; }
/// <summary>检查表说明和索引</summary> /// <param name="entitytable"></param> /// <param name="dbtable"></param> /// <param name="onlySql"></param> /// <returns></returns> protected virtual String CheckTableDescriptionAndIndex(IDataTable entitytable, IDataTable dbtable, Boolean onlySql) { var sb = new StringBuilder(); #region 表说明 if (entitytable.Description + "" != dbtable.Description + "") { // 先删除旧注释 //if (!String.IsNullOrEmpty(dbtable.Description)) DropTableDescription(sb, dbtable, onlySql); if (!String.IsNullOrEmpty(dbtable.Description)) { PerformSchema(sb, onlySql, DDLSchema.DropTableDescription, dbtable); } // 加上新注释 //if (!String.IsNullOrEmpty(entitytable.Description)) AddTableDescription(sb, entitytable, onlySql); if (!String.IsNullOrEmpty(entitytable.Description)) { PerformSchema(sb, onlySql, DDLSchema.AddTableDescription, entitytable); } } #endregion #region 除索引 if (dbtable.Indexes != null) { for (int i = dbtable.Indexes.Count - 1; i >= 0; i--) { var item = dbtable.Indexes[i]; // 计算的索引不需要删除 if (item.Computed) { continue; } // 主键的索引不能删 if (item.PrimaryKey) { continue; } var di = ModelHelper.GetIndex(entitytable, item.Columns); if (di != null) { continue; } PerformSchema(sb, onlySql, DDLSchema.DropIndex, item); dbtable.Indexes.RemoveAt(i); } } #endregion #region 新增索引 if (entitytable.Indexes != null) { foreach (var item in entitytable.Indexes) { if (item.PrimaryKey) { continue; } var di = ModelHelper.GetIndex(dbtable, item.Columns); // 计算出来的索引,也表示没有,需要创建 if (di != null && !di.Computed) { continue; } //// 如果这个索引的唯一字段是主键,则无需建立索引 //if (item.Columns.Length == 1 && entitytable.GetColumn(item.Columns[0]).PrimaryKey) continue; // 如果索引全部就是主键,无需创建索引 if (entitytable.GetColumns(item.Columns).All(e => e.PrimaryKey)) { continue; } PerformSchema(sb, onlySql, DDLSchema.CreateIndex, item); if (di == null) { dbtable.Indexes.Add(item.Clone(dbtable)); } else { di.Computed = false; } } } #endregion return(sb.ToString()); }
/// <summary>修正数据</summary> /// <param name="table"></param> public virtual IDataTable Fix(IDataTable table) { // 根据单字段索引修正对应的关系 FixRelationBySingleIndex(table); // 给所有关系字段建立索引 CreateIndexForRelation(table); // 从索引中修正主键 FixPrimaryByIndex(table); #region 最后修复主键 if (table.PrimaryKeys.Length < 1) { // 自增作为主键,然后是ID/Guid/UID,最后默认使用第一个 // 没办法,如果没有主键,整个实体层都会面临大问题! IDataColumn dc = null; if ((dc = table.Columns.FirstOrDefault(c => c.Identity)) != null) dc.PrimaryKey = true; //else if ((dc = table.Columns.FirstOrDefault(c => c.Is("ID"))) != null) // dc.PrimaryKey = true; //else if ((dc = table.Columns.FirstOrDefault(c => c.Is("Guid"))) != null) // dc.PrimaryKey = true; //else if ((dc = table.Columns.FirstOrDefault(c => c.Is("UID"))) != null) // dc.PrimaryKey = true; //else if ((dc = table.Columns.FirstOrDefault()) != null) // dc.PrimaryKey = true; } #endregion // 给非主键的自增字段建立唯一索引 CreateUniqueIndexForIdentity(table); // 索引应该具有跟字段一样的唯一和主键约束 FixIndex(table); #region 修正可能错误的别名 //var ns = new List<String>(); //ns.Add(table.Alias); //foreach (var item in table.Columns) //{ // if (ns.Contains(item.Alias) || IsKeyWord(item.Alias)) // { // // 通过加数字的方式,解决关键字问题 // for (int i = 2; i < table.Columns.Count; i++) // { // var name = item.Alias + i; // // 加了数字后,不可能是关键字 // if (!ns.Contains(name)) // { // item.Alias = name; // break; // } // } // } // ns.Add(item.Alias); //} foreach (var dc in table.Columns) { dc.Fix(); } #endregion return table; }
protected virtual String ReBuildTable(IDataTable entitytable, IDataTable dbtable) { // 通过重建表的方式修改字段 String tableName = dbtable.Name; String tempTableName = "Temp_" + tableName + "_" + new Random((Int32)DateTime.Now.Ticks).Next(1000, 10000); tableName = FormatName(tableName); tempTableName = FormatName(tempTableName); // 每个分号后面故意加上空格,是为了让DbMetaData执行SQL时,不要按照分号加换行来拆分这个SQL语句 var sb = new StringBuilder(); sb.AppendLine("BEGIN TRANSACTION; "); sb.AppendFormat("Alter Table {0} Rename To {1}", tableName, tempTableName); sb.AppendLine("; "); sb.Append(CreateTableSQL(entitytable)); sb.AppendLine("; "); // 如果指定了新列和旧列,则构建两个集合 if (entitytable.Columns != null && entitytable.Columns.Count > 0 && dbtable.Columns != null && dbtable.Columns.Count > 0) { var sbName = new StringBuilder(); var sbValue = new StringBuilder(); foreach (var item in entitytable.Columns) { String name = item.Name; var field = dbtable.GetColumn(item.Name); if (field == null) { // 如果新增了不允许空的列,则处理一下默认值 if (!item.Nullable) { if (item.DataType == typeof(String)) { if (sbName.Length > 0) { sbName.Append(", "); } if (sbValue.Length > 0) { sbValue.Append(", "); } sbName.Append(FormatName(name)); sbValue.Append("''"); } else if (item.DataType == typeof(Int16) || item.DataType == typeof(Int32) || item.DataType == typeof(Int64) || item.DataType == typeof(Single) || item.DataType == typeof(Double) || item.DataType == typeof(Decimal)) { if (sbName.Length > 0) { sbName.Append(", "); } if (sbValue.Length > 0) { sbValue.Append(", "); } sbName.Append(FormatName(name)); sbValue.Append("0"); } else if (item.DataType == typeof(DateTime)) { if (sbName.Length > 0) { sbName.Append(", "); } if (sbValue.Length > 0) { sbValue.Append(", "); } sbName.Append(FormatName(name)); sbValue.Append(Database.FormatDateTime(Database.DateTimeMin)); } } } else { if (sbName.Length > 0) { sbName.Append(", "); } if (sbValue.Length > 0) { sbValue.Append(", "); } sbName.Append(FormatName(name)); //sbValue.Append(FormatName(name)); // 处理字符串不允许空 if (item.DataType == typeof(String) && !item.Nullable) { sbValue.Append(Database.StringConcat(FormatName(name), "\'\'")); } else { sbValue.Append(FormatName(name)); } } } sb.AppendFormat("Insert Into {0}({2}) Select {3} From {1}", tableName, tempTableName, sbName.ToString(), sbValue.ToString()); } else { sb.AppendFormat("Insert Into {0} Select * From {1}", tableName, tempTableName); } sb.AppendLine("; "); sb.AppendFormat("Drop Table {0}", tempTableName); sb.AppendLine("; "); sb.Append("COMMIT;"); return(sb.ToString()); }
/// <summary>给所有关系字段建立索引</summary> /// <param name="table"></param> protected virtual void CreateIndexForRelation(IDataTable table) { foreach (var dr in table.Relations) { // 跳过主键 var dc = table.GetColumn(dr.Column); if (dc == null || dc.PrimaryKey) continue; if (table.GetIndex(dr.Column) == null) { var di = table.CreateIndex(); di.Columns = new String[] { dr.Column }; // 这两个的关系,唯一性 di.Unique = dr.Unique; di.Computed = true; table.Indexes.Add(di); } } }
/// <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="table"></param> protected virtual void CreateUniqueIndexForIdentity(IDataTable table) { foreach (var dc in table.Columns) { if (dc.Identity && !dc.PrimaryKey) { var di = table.GetIndex(dc.Name); if (di == null) { di = table.CreateIndex(); di.Columns = new String[] { dc.Name }; di.Computed = true; } // 不管是不是原来有的索引,都要唯一 di.Unique = true; } } }
public DataTableContainer(string name, IDataTable table) { this.name = name; this.table = table; }
public RowBasedDataTableAdaptorBase(ILinearAlgebraProvider lap, IDataTable dataTable) : base(lap, dataTable) { // read the entire data table into memory dataTable.ForEach(row => _data.Add(row)); }
/// <summary>同步单表数据</summary> /// <remarks> /// 把数据同一张表同步到另一个库 /// </remarks> /// <param name="table">数据表</param> /// <param name="connName">目标连接名</param> /// <param name="syncSchema">同步架构</param> /// <param name="progress">进度回调,参数为已处理行数和当前页表</param> /// <returns></returns> public Int32 Sync(IDataTable table, String connName, Boolean syncSchema = true, Action <Int32, DbTable> progress = null) { var dal = connName.IsNullOrEmpty() ? null : Create(connName); var writeDb = new WriteDbActor { Table = table, Dal = dal, // 最多同时堆积数页 BoundedCapacity = 4, }; // 自增 var id = table.Columns.FirstOrDefault(e => e.Identity); // 主键 if (id == null) { var pks = table.PrimaryKeys; if (pks != null && pks.Length == 1 && pks[0].DataType.IsInt()) { id = pks[0]; } } var sw = Stopwatch.StartNew(); // 表结构 if (syncSchema) { dal.SetTables(table); } var sb = new SelectBuilder { Table = Db.FormatName(table) }; var row = 0L; var pageSize = (Db as DbBase).BatchSize; var total = 0; while (true) { var sql = ""; // 分割数据页,自增或分页 if (id != null) { sb.Where = $"{id.ColumnName}>={row}"; sql = PageSplit(sb, 0, pageSize); } else { sql = PageSplit(sb, row, pageSize); } // 查询数据 var dt = Session.Query(sql, null); if (dt == null || dt.Rows.Count == 0) { break; } var count = dt.Rows.Count; WriteLog("同步[{0}/{1}]数据 {2:n0} + {3:n0}", table.Name, ConnName, row, count); // 进度报告 progress?.Invoke((Int32)row, dt); // 消费数据 writeDb.Tell(dt); // 下一页 total += count; //if (count < pageSize) break; // 自增分割时,取最后一行 if (id != null) { row = dt.Get <Int64>(count - 1, id.ColumnName) + 1; } else { row += pageSize; } } // 通知写入完成 writeDb.Stop(-1); sw.Stop(); var ms = sw.Elapsed.TotalMilliseconds; WriteLog("同步[{0}/{1}]完成,共[{2:n0}]行,耗时{3:n0}ms,速度{4:n0}tps", table.Name, ConnName, total, ms, total * 1000L / ms); // 返回总行数 return(total); }
/// <summary> /// 表注释 /// </summary> /// <param name="table"></param> /// <returns></returns> public virtual String GetClassDescription(IDataTable table) { if (!Config.UseCNFileName) return table.Description; String remark = table.Description; if (Config.UseCNFileName && String.IsNullOrEmpty(remark)) remark = Engine.ENameToCName(GetClassName(table)); return remark; }
String DropTableSQL(IDataTable table) { return(DropTableSQL(table.Name)); }