/// <summary> /// 获取表格信息 /// </summary> /// <param name="client">SQL 客户端操作</param> /// <param name="tableName">表格名称</param> /// <returns>表格信息</returns> internal static TableColumnCollection GetTable(Client client, string tableName) { TableColumnCollection table = new TableColumnCollection { Columns = new ColumnCollection { Name = tableName } }; Column[] columns = new Column[Fields.Length]; Column[] primaryKeyColumns = new Column[PrimaryKeys.Length]; int index = 0, primaryKeyIndex = 0; foreach (Field member in Fields) { Column column = client.GetColumn(member.FieldInfo.Name, member.FieldInfo.FieldType, member.DataMember); columns[index++] = column; if (Identity == member) { table.Identity = column; } if (member.DataMember.PrimaryKeyIndex != 0) { primaryKeyColumns[primaryKeyIndex++] = column; } } table.Columns.Columns = columns; if (primaryKeyColumns.Length != 0) { table.PrimaryKey = new ColumnCollection { Columns = PrimaryKeys.getArray(value => primaryKeyColumns.firstOrDefault(column => column.Name == value.FieldInfo.Name)) }; } return(table); }
/// <summary> /// Constructor. /// </summary> /// <param name="table">The <see cref="Table{TItem}"/> to filter.</param> /// <param name="filterParams">The filter parameters.</param> public FilteredItemCollection(ITable <TItem> table, TableFilterParams filterParams) : base(table.Items) { _baseCollection = table.Items; _columns = table.Columns; _filterParams = filterParams; _filteredList = _filterParams.Column == null ? GetFilteredItemCollection(AnyColumnMatch) : GetFilteredItemCollection(SingleColumnMatch); _baseCollection.ItemsChanged += BaseItemsChanged; }
/// <summary> /// Initializes a new instance of the <see cref="Table" /> class. /// </summary> public Table() : base() { // Set default values this.headerStyleKey = SharedMapStyleKeys.HeaderExport; this.subHeaderStyleKey = SharedMapStyleKeys.SubHeaderExport; this.footerStyleKey = SharedMapStyleKeys.FooterExport; this.subFooterStyleKey = SharedMapStyleKeys.SubFooterExport; this.properties = new PropertyCollection(); this.columnHeaders = new TableColumnHeaderCollection(); this.columns = new TableColumnCollection(); this.excelChartOptionsList = new List <ExcelChartOptions>(); this.items = new MapCollection(); }
/// <summary> /// 创建表格 /// </summary> /// <param name="connection">SQL连接</param> /// <param name="table">表格信息</param> internal override void CreateTable(DbConnection connection, TableColumnCollection table) { string tableName = table.Columns.Name, sql; CharStream sqlStream = Interlocked.Exchange(ref this.sqlStream, null); if (sqlStream == null) { sqlStream = new CharStream(null, 0); } byte *buffer = null; try { sqlStream.Reset(buffer = AutoCSer.UnmanagedPool.Default.Get(), AutoCSer.UnmanagedPool.DefaultSize); sqlStream.SimpleWriteNotNull("create table "); sqlStream.SimpleWriteNotNull(tableName); sqlStream.SimpleWriteNotNull(" ("); bool isNext = false; foreach (Column column in table.Columns.Columns) { if (isNext) { sqlStream.Write(','); } sqlStream.SimpleWriteNotNull(column.SqlName); sqlStream.Write(' '); sqlStream.Write(column.DbType.getSqlTypeName()); isNext = true; } sqlStream.Write(')'); sql = sqlStream.ToString(); } finally { if (buffer != null) { AutoCSer.UnmanagedPool.Default.Push(buffer); } sqlStream.Dispose(); Interlocked.Exchange(ref this.sqlStream, sqlStream); } executeNonQuery(connection, sql); }
public DicomPrinterTable(bool updateSelectStatus) { TableColumnCollection <Checkable <DicomPrinter> > columns = base.Columns; string columnName = SR.ColumnAETitle; if (DicomPrinterTable._aetile == null) { DicomPrinterTable._aetile = new TableColumn <Checkable <DicomPrinter>, string> .GetColumnValueDelegate <Checkable <DicomPrinter>, string>(DicomPrinterTable.AETitle); } columns.Add(new TableColumn <Checkable <DicomPrinter>, string>(columnName, DicomPrinterTable._aetile, 0.3f)); if (DicomPrinterTable._name == null) { DicomPrinterTable._name = new TableColumn <Checkable <DicomPrinter>, string> .GetColumnValueDelegate <Checkable <DicomPrinter>, string>(DicomPrinterTable.Name); } columns.Add(new TableColumn <Checkable <DicomPrinter>, string>(SR.ColumnName, DicomPrinterTable._name, 0.3f)); if (DicomPrinterTable._host == null) { DicomPrinterTable._host = new TableColumn <Checkable <DicomPrinter>, string> .GetColumnValueDelegate <Checkable <DicomPrinter>, string>(DicomPrinterTable.Host); } columns.Add(new TableColumn <Checkable <DicomPrinter>, string>(SR.ColumnHost, DicomPrinterTable._host, 0.2f)); if (DicomPrinterTable._port == null) { DicomPrinterTable._port = new TableColumn <Checkable <DicomPrinter>, int> .GetColumnValueDelegate <Checkable <DicomPrinter>, int>(DicomPrinterTable.Port); } columns.Add(new TableColumn <Checkable <DicomPrinter>, int>(SR.ColumnPort, DicomPrinterTable._port, 0.1f)); if (updateSelectStatus) { if (DicomPrinterTable._default == null) { DicomPrinterTable._default = new TableColumn <Checkable <DicomPrinter>, bool> .GetColumnValueDelegate <Checkable <DicomPrinter>, bool>(DicomPrinterTable.IsChecked); } columns.Add(new TableColumn <Checkable <DicomPrinter>, bool>(SR.ColumnDefault, DicomPrinterTable._default, new TableColumn <Checkable <DicomPrinter>, bool> .SetColumnValueDelegate <Checkable <DicomPrinter>, bool>(this.UpdateCheckableItemSelectStatus), 0.1f)); } else { if (DicomPrinterTable._noUpdateSelectStatusDefault == null) { DicomPrinterTable._noUpdateSelectStatusDefault = new TableColumn <Checkable <DicomPrinter>, bool> .GetColumnValueDelegate <Checkable <DicomPrinter>, bool>(DicomPrinterTable.IsChecked); } columns.Add(new TableColumn <Checkable <DicomPrinter>, bool>(SR.ColumnColor, DicomPrinterTable._noUpdateSelectStatusDefault, 0.1f)); } }
void autoresizeColumns(Table table) { TableColumnCollection columns = table.Columns; TableRowCollection rows = table.RowGroups[0].Rows; TableCellCollection cells; TableRow row; TableCell cell; int columnCount = columns.Count; int rowCount = rows.Count; int cellCount = 0; double[] columnWidths = new double[columnCount]; double columnWidth; // loop through all rows for (int r = 0; r < rowCount; r++) { row = rows[r]; cells = row.Cells; cellCount = cells.Count; // loop through all cells in the row for (int c = 0; c < columnCount && c < cellCount; c++) { cell = cells[c]; columnWidth = getDesiredWidth(new TextRange(cell.ContentStart, cell.ContentEnd)) + 19; if (columnWidth > columnWidths[c]) { columnWidths[c] = columnWidth; } } } // set the columns width to the widest cell for (int c = 0; c < columnCount; c++) { columns[c].Width = new GridLength(columnWidths[c]); } }
/// <summary> /// 创建表格 /// </summary> /// <param name="connection">SQL连接</param> /// <param name="table">表格信息</param> internal override void CreateTable(DbConnection connection, TableColumnCollection table) { string tableName = table.Columns.Name, sql; CharStream sqlStream = Interlocked.Exchange(ref this.sqlStream, null); if (sqlStream == null) { sqlStream = new CharStream(default(AutoCSer.Memory.Pointer)); } AutoCSer.Memory.Pointer buffer = default(AutoCSer.Memory.Pointer); try { buffer = UnmanagedPool.Default.GetPointer(); sqlStream.Reset(ref buffer); sqlStream.SimpleWrite("create table "); sqlStream.SimpleWrite(tableName); sqlStream.SimpleWrite(" ("); bool isNext = false; foreach (Column column in table.Columns.Columns) { if (isNext) { sqlStream.Write(','); } constantConverter.ConvertNameToSqlStream(sqlStream, column.Name); sqlStream.Write(' '); sqlStream.Write(column.DbType.getSqlTypeName()); isNext = true; } sqlStream.Write(')'); sql = sqlStream.ToString(); } finally { UnmanagedPool.Default.Push(ref buffer); sqlStream.Dispose(); Interlocked.Exchange(ref this.sqlStream, sqlStream); } executeNonQuery(connection, sql); }
///<summary> /// Constructs a table with the specified number of cells in each row. ///</summary> public Table(int cellRowCount) { Platform.CheckArgumentRange((int)cellRowCount, 1, int.MaxValue, "cellRowCount"); _cellRowCount = cellRowCount; _baseColumnWidth = 10; _columns = new TableColumnCollection <TItem>(); _columns.ItemsChanged += delegate(object sender, ItemChangedEventArgs args) { switch (args.ChangeType) { case ItemChangeType.ItemAdded: _columns[args.ItemIndex].Table = this; break; case ItemChangeType.ItemRemoved: _columns[args.ItemIndex].Table = null; break; } }; _data = new ItemCollection <TItem>(); _filteredData = null; }
/// <summary> /// Public ctor. Creates instance of <see cref="TableData"/> /// </summary> public TableData() : base() { this.columns = new TableColumnCollection(); this.rowData = new List <TableDataRowInfo>(); }
//------------------------------------------------------ // // Private Methods // //------------------------------------------------------ #region Private Methods /// <summary> /// Private ctor time initialization. /// </summary> private void PrivateInitialize() { // Acquire new PTS Context. _columns = new TableColumnCollection(this); _rowGroups = new TableRowGroupCollection(this); _rowGroupInsertionIndex = -1; }
/// <summary> /// 创建表格 /// </summary> /// <param name="connection">SQL连接</param> /// <param name="table">表格信息</param> internal override void CreateTable(DbConnection connection, TableColumnCollection table) { throw new InvalidOperationException(); }
public CreateTableIfNotExistsStatement(string tableName, TableColumnCollection columns) { _tableName = tableName; _columns = columns; }
internal ShimTableMember(IDefinitionPath parentDefinitionPath, Tablix owner, int columnIndex, TableColumnCollection columns) : base(parentDefinitionPath, owner, null, columnIndex, isColumn: true) { m_column = columns[columnIndex]; m_isFixedHeader = m_column.FixedHeader; m_rowDefinitionStartIndex = (m_rowDefinitionEndIndex = columnIndex); }
/// <summary> /// Determine Row and Column Count, and ColumnWidths /// </summary> /// <returns></returns> float S3_CalculateCountAndWidth(List<CssBox> userDefinedColumnBoxes) { //----------------------------------------------------------- //1. count columns int columnCount = 0; if (userDefinedColumnBoxes.Count > 0) { //mode 1: user defined columns columnCount = userDefinedColumnBoxes.Count; } else { //mode 2: anonymous column definitions, int cellcount = 0; //find max column count in the table //each row may contain different number of cell for (int i = _allRowBoxes.Count - 1; i >= 0; --i) { if ((cellcount = _allRowBoxes[i].ChildCount) > columnCount) { columnCount = cellcount; } } } //------------------------------------------------------------- //2. create column collection this.columnCollection = new TableColumnCollection(columnCount); float availbleWidthForAllCells = GetAvailableTableWidth() - (GetHorizontalSpacing(_tableBox) * (columnCount + 1)) - _tableBox.ActualBorderLeftWidth - _tableBox.ActualBorderRightWidth; //------------------------------------------------------------- //3. if (userDefinedColumnBoxes.Count > 0) { //mode 1: user defined columns // Fill ColumnWidths array by scanning column widths for (int i = userDefinedColumnBoxes.Count - 1; i >= 0; --i) { CssLength colWidth = userDefinedColumnBoxes[i].Width; //Get specified width if (colWidth.Number > 0) //If some width specified { switch (colWidth.UnitOrNames) { case CssUnitOrNames.Percent: { return (colWidth.Number / 100) * availbleWidthForAllCells; } case CssUnitOrNames.Pixels: case CssUnitOrNames.EmptyValue: { //Get width as an absolute-pixel value columnCollection.SetColumnWidth(i, colWidth.Number); } break; } } } } else { //mode 2: anonymous column definitions, // Fill ColumnWidths array by scanning width in table-cell definitions var tmpRows = this._allRowBoxes; int rowCount = tmpRows.Count; for (int rr = 0; rr < rowCount; ++rr) { CssBox row = tmpRows[rr]; //Check for column width in table-cell definitions int col_limit = columnCount > MAX_COL_AT_THIS_VERSION ? MAX_COL_AT_THIS_VERSION : columnCount; var cnode = row.GetFirstChild(); int i = 0; while (cnode != null) { if (!columnCollection[i].HasSpecificWidth) { if (i < row.ChildCount) { var childBox = cnode; if (childBox.CssDisplay == CssDisplay.TableCell) { CssLength cellWidth = childBox.Width; if (cellWidth.IsAuto) { //auto width - always 0 } else { float cellBoxWidth = CssValueParser.ConvertToPx(childBox.Width, availbleWidthForAllCells, childBox); if (cellBoxWidth > 0) //If some width specified { int colspan = childBox.ColSpan; cellBoxWidth /= colspan; for (int n = i; n < i + colspan; n++) { columnCollection[n].S3_UpdateIfWider(cellBoxWidth, ColumnSpecificWidthLevel.FromCellConstraint); } } } } } } //-------------------------- cnode = cnode.GetNextNode(); i++; if (i >= col_limit) { break; } } } } return availbleWidthForAllCells; }
public ShimTableMember(IDefinitionPath parentDefinitionPath, Tablix owner, int columnIndex, TableColumnCollection columns) : base(parentDefinitionPath, owner, null, columnIndex, true) { this.m_column = columns[columnIndex]; this.m_isFixedHeader = this.m_column.FixedHeader; this.m_rowDefinitionStartIndex = (this.m_rowDefinitionEndIndex = columnIndex); }
public void FillTableSchema(Table tbl, TableType tableType) { TableColumnCollection columns = new TableColumnCollection(); DataSet dsColumns; tbl.ForeignKeys = new ForeignKeyCollection(); if (tableType == TableType.Function) { if (dsColumns2.Tables[Name] == null) { var cmdColumns = new SqlCommand(ROUTINE_COLUMN_SQL_ALL); var dt = new DataTable(Name); dt.Load(cmdColumns.GetReader()); dsColumns2.Tables.Add(dt); } dsColumns = dsColumns2; } else { if (dsColumns1.Tables[Name] == null) { var cmdColumns = new SqlCommand(TABLE_COLUMN_SQL_ALL); var dt = new DataTable(Name); dt.Load(cmdColumns.GetReader()); dsColumns1.Tables.Add(dt); } dsColumns = dsColumns1; } DataRow[] drColumns = dsColumns.Tables[Name].Select("TableName ='" + tbl.Name + "'", "OrdinalPosition ASC"); for (int i = 0; i < drColumns.Length; i++) { TableColumn column = new TableColumn(tbl); column.ColumnName = drColumns[i][SqlSchemaVariable.COLUMN_NAME].ToString(); column.NativeDataType = drColumns[i][SqlSchemaVariable.DATA_TYPE].ToString(); if (column.NativeDataType == "numeric") column.NativeDataType = string.Format("Decimal({0},{1})", drColumns[i]["precision"], drColumns[i]["scale"]); column.DataType = GetDbType(column.NativeDataType); if (drColumns[i][SqlSchemaVariable.COLUMN_DEFAULT] != DBNull.Value) { string defaultSetting = drColumns[i][SqlSchemaVariable.COLUMN_DEFAULT].ToString().Trim(); if (defaultSetting.ToLower().IndexOf("newsequentialid()") > -1) column.DefaultSetting = SqlSchemaVariable.DEFAULT; else column.DefaultSetting = defaultSetting; } column.AutoIncrement = Convert.ToBoolean(drColumns[i][SqlSchemaVariable.IS_IDENTITY]); int maxLength; int.TryParse(drColumns[i][SqlSchemaVariable.MAX_LENGTH].ToString(), out maxLength); column.MaxLength = maxLength; if (maxLength > 0) column.NativeDataType += "(" + maxLength + ")"; column.IsNullable = drColumns[i][SqlSchemaVariable.IS_NULLABLE].ToString() == "YES"; bool isComputed = (drColumns[i][SqlSchemaVariable.IS_COMPUTED].ToString() == "1"); column.IsReadOnly = (column.NativeDataType == "timestamp" || isComputed); columns.Add(column); tbl.SchemaName = drColumns[i]["Owner"].ToString(); } tbl.Columns = columns; if (dsIndex.Tables[Name] == null) { var cmdIndex = new SqlCommand(INDEX_SQL_ALL); DataTable dt = new DataTable(Name); dt.Load(cmdIndex.GetReader()); dsIndex.Tables.Add(dt); } DataRow[] drIndexes = dsIndex.Tables[Name].Select("TableName ='" + tbl.Name + "'"); for (int i = 0; i < drIndexes.Length; i++) { string colName = drIndexes[i][SqlSchemaVariable.COLUMN_NAME].ToString(); string constraintType = drIndexes[i][SqlSchemaVariable.CONSTRAINT_TYPE].ToString(); TableColumn column = columns.GetColumn(colName); if (Utility.IsMatch(constraintType, SqlSchemaVariable.PRIMARY_KEY)) column.IsPrimaryKey = true; else if (Utility.IsMatch(constraintType, SqlSchemaVariable.FOREIGN_KEY)) column.IsForeignKey = true; //HACK: Allow second pass naming adjust based on whether a column is keyed column.ColumnName = column.ColumnName; } if (dtFKR == null) { var cmdFKR = new SqlCommand(FOREIGN_KEY_RELATIONSHIPS); dtFKR = new DataTable(); dtFKR.Load(cmdFKR.GetReader()); var cmdFK = new SqlCommand(FOREIGN_KEYS); dtFK = new DataTable(); dtFK.Load(cmdFK.GetReader()); } DataRow[] drfkr; drfkr = dtFKR.Select("PkTable ='" + tbl.Name + "'"); for (int i = 0; i < drfkr.Length; i++) { Relationship rel = new Relationship(); rel.Name = drfkr[i]["RelName"].ToString(); Table fktable = Db.Service.GetSchema(drfkr[i]["FkTable"].ToString()); rel.ClassNameOne = tbl.ClassName; rel.ClassNameMany = fktable.ClassName; string[] a = Regex.Split(rel.Name, "__"); if (a.Length == 2) { rel.PropertyNameMany = a[0]; // name used in primary table to fetch many of this table rel.PropertyNameOne = a[1]; // named used for foreign key in this table } else { rel.PropertyNameOne = tbl.ClassName; rel.PropertyNameMany = fktable.ClassNamePlural; } DataRow[] drfk = dtFK.Select("PkTable = '" + tbl.Name + "' and RelName = '" + rel.Name + "'"); for (int ii = 0; ii < drfk.Length; ii++) { Relationship.KeyPair kp = new Relationship.KeyPair(); TableColumn fkcol = fktable.Columns.GetColumn(drfk[ii]["FkColumn"].ToString()); TableColumn pkcol = columns.GetColumn(drfk[ii]["PkColumn"].ToString()); kp.vartype = fkcol.VarType; kp.ForeignKey = fkcol.Name; kp.PrimaryKey = pkcol.Name; rel.KeyPairs.Add(kp); if (ii > 0) { rel.ForeignKey += ","; rel.PrimaryKey += ","; } rel.PrimaryKey += kp.PrimaryKey; rel.ForeignKey += kp.ForeignKey; } tbl.ForeignKeyTables.Add(rel); } drfkr = dtFKR.Select("FkTable ='" + tbl.Name + "'"); for (int i = 0; i < drfkr.Length; i++) { Relationship rel = new Relationship(); rel.Name = drfkr[i]["RelName"].ToString(); Table pktable = Db.Service.GetSchema(drfkr[i]["PkTable"].ToString()); rel.ClassNameOne = pktable.ClassName; rel.ClassNameMany = tbl.ClassName; string[] a = Regex.Split(rel.Name, "__"); if (a.Length == 2) { rel.PropertyNameMany = a[0]; // name used in primary table to fetch many of this table rel.PropertyNameOne = a[1]; // named used for foreign key in this table } else { rel.PropertyNameOne = pktable.ClassName; rel.PropertyNameMany = tbl.ClassNamePlural; } DataRow[] drfk = dtFK.Select("FkTable = '" + tbl.Name + "' and RelName = '" + rel.Name + "'"); for (int ii = 0; ii < drfk.Length; ii++) { Relationship.KeyPair kp = new Relationship.KeyPair(); TableColumn fkcol = columns.GetColumn(drfk[ii]["FkColumn"].ToString()); TableColumn pkcol = pktable.Columns.GetColumn(drfk[ii]["PkColumn"].ToString()); kp.vartype = fkcol.VarType; kp.ForeignKey = fkcol.Name; kp.PrimaryKey = pkcol.Name; rel.KeyPairs.Add(kp); if (ii > 0) { rel.ForeignKey += ","; rel.PrimaryKey += ","; } rel.PrimaryKey += kp.PrimaryKey; rel.ForeignKey += kp.ForeignKey; fkcol.PropertyNameOne = rel.PropertyNameOne; } tbl.ForeignKeys.Add(rel); } if (dsManyToManyCheck.Tables[Name] == null) { var cmdM2M = new SqlCommand(MANY_TO_MANY_CHECK_ALL); var dt = new DataTable(Name); dt.Load(cmdM2M.GetReader()); dsManyToManyCheck.Tables.Add(dt); } DataRow[] drs = dsManyToManyCheck.Tables[Name].Select("PK_Table = '" + tbl.Name + "'"); if (drs.Length > 0) { for (int count = 0; count < drs.Length; count++) { string mapTable = drs[count]["FK_Table"].ToString(); string localKey = drs[count]["FK_Column"].ToString(); if (dsManyToManyMap.Tables[Name] == null) { var cmdM2MMap = new SqlCommand(MANY_TO_MANY_FOREIGN_MAP_ALL); DataTable dt = new DataTable(Name); dt.Load(cmdM2MMap.GetReader()); dsManyToManyMap.Tables.Add(dt); } DataRow[] drMap = dsManyToManyMap.Tables[Name].Select("FK_Table = '" + mapTable + "' AND PK_Table <> '" + tbl.Name + "'"); for (int i = 0; i < drMap.Length; i++) { ManyToManyRelationship m = new ManyToManyRelationship(mapTable); m.ForeignTableName = drMap[i]["PK_Table"].ToString(); m.ForeignPrimaryKey = drMap[i]["PK_Column"].ToString(); m.MapTableLocalTableKeyColumn = localKey; m.MapTableForeignTableKeyColumn = drMap[i]["FK_Column"].ToString(); tbl.ManyToManys.Add(m); } } } }
public Table() { Columns = new TableColumnCollection(); rows = new List<TableRow>(); }
/// <summary> /// 创建表格 /// </summary> /// <param name="connection">SQL连接</param> /// <param name="table">表格信息</param> internal override void CreateTable(DbConnection connection, TableColumnCollection table) { string tableName = table.Columns.Name, sql; CharStream sqlStream = Interlocked.Exchange(ref this.sqlStream, null); if (sqlStream == null) { sqlStream = new CharStream(null, 0); } byte *buffer = null; try { sqlStream.Reset(buffer = AutoCSer.UnmanagedPool.Default.Get(), AutoCSer.UnmanagedPool.DefaultSize); sqlStream.WriteNotNull("create table`"); sqlStream.WriteNotNull(tableName); sqlStream.WriteNotNull("`("); bool isNext = false; foreach (Column column in table.Columns.Columns) { if (isNext) { sqlStream.Write(','); } appendColumn(sqlStream, column); isNext = true; } ColumnCollection primaryKey = table.PrimaryKey; if (primaryKey != null && primaryKey.Columns.length() != 0) { isNext = false; sqlStream.WriteNotNull(",primary key("); foreach (Column column in primaryKey.Columns) { if (isNext) { sqlStream.Write(','); } sqlStream.WriteNotNull(column.Name); isNext = true; } sqlStream.Write(')'); } if (table.Indexs != null) { foreach (ColumnCollection columns in table.Indexs) { if (columns != null && columns.Columns.length() != 0) { if (columns.Type == ColumnCollectionType.UniqueIndex) { sqlStream.WriteNotNull(@"unique index "); } else { sqlStream.WriteNotNull(@" index "); } AppendIndexName(sqlStream, tableName, columns); sqlStream.Write('('); isNext = false; foreach (Column column in columns.Columns) { if (isNext) { sqlStream.Write(','); } sqlStream.Write('`'); sqlStream.WriteNotNull(column.Name); sqlStream.Write('`'); isNext = true; } sqlStream.Write(')'); } } } sqlStream.WriteNotNull(");"); sql = sqlStream.ToString(); } finally { if (buffer != null) { AutoCSer.UnmanagedPool.Default.Push(buffer); } sqlStream.Dispose(); Interlocked.Exchange(ref this.sqlStream, sqlStream); } executeNonQuery(connection, sql); }
public void FillTableSchema(Table tbl, TableType tableType) { TableColumnCollection columns = new TableColumnCollection(); DataSet dsColumns; tbl.ForeignKeys = new ForeignKeyCollection(); if (tableType == TableType.Function) { if (dsColumns2.Tables[Name] == null) { var cmdColumns = new SqlCommand(ROUTINE_COLUMN_SQL_ALL); var dt = new DataTable(Name); dt.Load(cmdColumns.GetReader()); dsColumns2.Tables.Add(dt); } dsColumns = dsColumns2; } else { if (dsColumns1.Tables[Name] == null) { var cmdColumns = new SqlCommand(TABLE_COLUMN_SQL_ALL); var dt = new DataTable(Name); dt.Load(cmdColumns.GetReader()); dsColumns1.Tables.Add(dt); } dsColumns = dsColumns1; } DataRow[] drColumns = dsColumns.Tables[Name].Select("TableName ='" + tbl.Name + "'", "OrdinalPosition ASC"); for (int i = 0; i < drColumns.Length; i++) { TableColumn column = new TableColumn(tbl); column.ColumnName = drColumns[i][SqlSchemaVariable.COLUMN_NAME].ToString(); column.NativeDataType = drColumns[i][SqlSchemaVariable.DATA_TYPE].ToString(); if (column.NativeDataType == "numeric") { column.NativeDataType = string.Format("Decimal({0},{1})", drColumns[i]["precision"], drColumns[i]["scale"]); } column.DataType = GetDbType(column.NativeDataType); if (drColumns[i][SqlSchemaVariable.COLUMN_DEFAULT] != DBNull.Value) { string defaultSetting = drColumns[i][SqlSchemaVariable.COLUMN_DEFAULT].ToString().Trim(); if (defaultSetting.ToLower().IndexOf("newsequentialid()") > -1) { column.DefaultSetting = SqlSchemaVariable.DEFAULT; } else { column.DefaultSetting = defaultSetting; } } column.AutoIncrement = Convert.ToBoolean(drColumns[i][SqlSchemaVariable.IS_IDENTITY]); int maxLength; int.TryParse(drColumns[i][SqlSchemaVariable.MAX_LENGTH].ToString(), out maxLength); column.MaxLength = maxLength; if (maxLength > 0) { column.NativeDataType += "(" + maxLength + ")"; } column.IsNullable = drColumns[i][SqlSchemaVariable.IS_NULLABLE].ToString() == "YES"; bool isComputed = (drColumns[i][SqlSchemaVariable.IS_COMPUTED].ToString() == "1"); column.IsReadOnly = (column.NativeDataType == "timestamp" || isComputed); columns.Add(column); tbl.SchemaName = drColumns[i]["Owner"].ToString(); } tbl.Columns = columns; if (dsIndex.Tables[Name] == null) { var cmdIndex = new SqlCommand(INDEX_SQL_ALL); DataTable dt = new DataTable(Name); dt.Load(cmdIndex.GetReader()); dsIndex.Tables.Add(dt); } DataRow[] drIndexes = dsIndex.Tables[Name].Select("TableName ='" + tbl.Name + "'"); for (int i = 0; i < drIndexes.Length; i++) { string colName = drIndexes[i][SqlSchemaVariable.COLUMN_NAME].ToString(); string constraintType = drIndexes[i][SqlSchemaVariable.CONSTRAINT_TYPE].ToString(); TableColumn column = columns.GetColumn(colName); if (Utility.IsMatch(constraintType, SqlSchemaVariable.PRIMARY_KEY)) { column.IsPrimaryKey = true; } else if (Utility.IsMatch(constraintType, SqlSchemaVariable.FOREIGN_KEY)) { column.IsForeignKey = true; } //HACK: Allow second pass naming adjust based on whether a column is keyed column.ColumnName = column.ColumnName; } if (dtFKR == null) { var cmdFKR = new SqlCommand(FOREIGN_KEY_RELATIONSHIPS); dtFKR = new DataTable(); dtFKR.Load(cmdFKR.GetReader()); var cmdFK = new SqlCommand(FOREIGN_KEYS); dtFK = new DataTable(); dtFK.Load(cmdFK.GetReader()); } DataRow[] drfkr; drfkr = dtFKR.Select("PkTable ='" + tbl.Name + "'"); for (int i = 0; i < drfkr.Length; i++) { Relationship rel = new Relationship(); rel.Name = drfkr[i]["RelName"].ToString(); Table fktable = Db.Service.GetSchema(drfkr[i]["FkTable"].ToString()); rel.ClassNameOne = tbl.ClassName; rel.ClassNameMany = fktable.ClassName; string[] a = Regex.Split(rel.Name, "__"); if (a.Length == 2) { rel.PropertyNameMany = a[0]; // name used in primary table to fetch many of this table rel.PropertyNameOne = a[1]; // named used for foreign key in this table } else { rel.PropertyNameOne = tbl.ClassName; rel.PropertyNameMany = fktable.ClassNamePlural; } DataRow[] drfk = dtFK.Select("PkTable = '" + tbl.Name + "' and RelName = '" + rel.Name + "'"); for (int ii = 0; ii < drfk.Length; ii++) { Relationship.KeyPair kp = new Relationship.KeyPair(); TableColumn fkcol = fktable.Columns.GetColumn(drfk[ii]["FkColumn"].ToString()); TableColumn pkcol = columns.GetColumn(drfk[ii]["PkColumn"].ToString()); kp.vartype = fkcol.VarType; kp.ForeignKey = fkcol.Name; kp.PrimaryKey = pkcol.Name; rel.KeyPairs.Add(kp); if (ii > 0) { rel.ForeignKey += ","; rel.PrimaryKey += ","; } rel.PrimaryKey += kp.PrimaryKey; rel.ForeignKey += kp.ForeignKey; } tbl.ForeignKeyTables.Add(rel); } drfkr = dtFKR.Select("FkTable ='" + tbl.Name + "'"); for (int i = 0; i < drfkr.Length; i++) { Relationship rel = new Relationship(); rel.Name = drfkr[i]["RelName"].ToString(); Table pktable = Db.Service.GetSchema(drfkr[i]["PkTable"].ToString()); rel.ClassNameOne = pktable.ClassName; rel.ClassNameMany = tbl.ClassName; string[] a = Regex.Split(rel.Name, "__"); if (a.Length == 2) { rel.PropertyNameMany = a[0]; // name used in primary table to fetch many of this table rel.PropertyNameOne = a[1]; // named used for foreign key in this table } else { rel.PropertyNameOne = pktable.ClassName; rel.PropertyNameMany = tbl.ClassNamePlural; } DataRow[] drfk = dtFK.Select("FkTable = '" + tbl.Name + "' and RelName = '" + rel.Name + "'"); for (int ii = 0; ii < drfk.Length; ii++) { Relationship.KeyPair kp = new Relationship.KeyPair(); TableColumn fkcol = columns.GetColumn(drfk[ii]["FkColumn"].ToString()); TableColumn pkcol = pktable.Columns.GetColumn(drfk[ii]["PkColumn"].ToString()); kp.vartype = fkcol.VarType; kp.ForeignKey = fkcol.Name; kp.PrimaryKey = pkcol.Name; rel.KeyPairs.Add(kp); if (ii > 0) { rel.ForeignKey += ","; rel.PrimaryKey += ","; } rel.PrimaryKey += kp.PrimaryKey; rel.ForeignKey += kp.ForeignKey; fkcol.PropertyNameOne = rel.PropertyNameOne; } tbl.ForeignKeys.Add(rel); } if (dsManyToManyCheck.Tables[Name] == null) { var cmdM2M = new SqlCommand(MANY_TO_MANY_CHECK_ALL); var dt = new DataTable(Name); dt.Load(cmdM2M.GetReader()); dsManyToManyCheck.Tables.Add(dt); } DataRow[] drs = dsManyToManyCheck.Tables[Name].Select("PK_Table = '" + tbl.Name + "'"); if (drs.Length > 0) { for (int count = 0; count < drs.Length; count++) { string mapTable = drs[count]["FK_Table"].ToString(); string localKey = drs[count]["FK_Column"].ToString(); if (dsManyToManyMap.Tables[Name] == null) { var cmdM2MMap = new SqlCommand(MANY_TO_MANY_FOREIGN_MAP_ALL); DataTable dt = new DataTable(Name); dt.Load(cmdM2MMap.GetReader()); dsManyToManyMap.Tables.Add(dt); } DataRow[] drMap = dsManyToManyMap.Tables[Name].Select("FK_Table = '" + mapTable + "' AND PK_Table <> '" + tbl.Name + "'"); for (int i = 0; i < drMap.Length; i++) { ManyToManyRelationship m = new ManyToManyRelationship(mapTable); m.ForeignTableName = drMap[i]["PK_Table"].ToString(); m.ForeignPrimaryKey = drMap[i]["PK_Column"].ToString(); m.MapTableLocalTableKeyColumn = localKey; m.MapTableForeignTableKeyColumn = drMap[i]["FK_Column"].ToString(); tbl.ManyToManys.Add(m); } } } }