internal DataTable GetSchemaTable(bool wantUniqueInfo, bool wantDefaultValue) { this.CheckClosed(); DataTable tbl = new DataTable("SchemaTable"); DataTable schema = null; string str2 = ""; string str3 = ""; string str4 = ""; tbl.Locale = CultureInfo.InvariantCulture; tbl.Columns.Add(SchemaTableColumn.ColumnName, typeof(string)); tbl.Columns.Add(SchemaTableColumn.ColumnOrdinal, typeof(int)); tbl.Columns.Add(SchemaTableColumn.ColumnSize, typeof(int)); tbl.Columns.Add(SchemaTableColumn.NumericPrecision, typeof(short)); tbl.Columns.Add(SchemaTableColumn.NumericScale, typeof(short)); tbl.Columns.Add(SchemaTableColumn.IsUnique, typeof(bool)); tbl.Columns.Add(SchemaTableColumn.IsKey, typeof(bool)); tbl.Columns.Add(SchemaTableOptionalColumn.BaseServerName, typeof(string)); tbl.Columns.Add(SchemaTableOptionalColumn.BaseCatalogName, typeof(string)); tbl.Columns.Add(SchemaTableColumn.BaseColumnName, typeof(string)); tbl.Columns.Add(SchemaTableColumn.BaseSchemaName, typeof(string)); tbl.Columns.Add(SchemaTableColumn.BaseTableName, typeof(string)); tbl.Columns.Add(SchemaTableColumn.DataType, typeof(Type)); tbl.Columns.Add(SchemaTableColumn.AllowDBNull, typeof(bool)); tbl.Columns.Add(SchemaTableColumn.ProviderType, typeof(int)); tbl.Columns.Add(SchemaTableColumn.IsAliased, typeof(bool)); tbl.Columns.Add(SchemaTableColumn.IsExpression, typeof(bool)); tbl.Columns.Add(SchemaTableOptionalColumn.IsAutoIncrement, typeof(bool)); tbl.Columns.Add(SchemaTableOptionalColumn.IsRowVersion, typeof(bool)); tbl.Columns.Add(SchemaTableOptionalColumn.IsHidden, typeof(bool)); tbl.Columns.Add(SchemaTableColumn.IsLong, typeof(bool)); tbl.Columns.Add(SchemaTableOptionalColumn.IsReadOnly, typeof(bool)); tbl.Columns.Add(SchemaTableOptionalColumn.ProviderSpecificDataType, typeof(Type)); tbl.Columns.Add(SchemaTableOptionalColumn.DefaultValue, typeof(object)); tbl.Columns.Add("DataTypeName", typeof(string)); tbl.Columns.Add("CollationType", typeof(string)); tbl.BeginLoadData(); for (int i = 0; i < this._fieldCount; i++) { string str6; bool flag; bool flag2; bool flag3; DataRow row = tbl.NewRow(); DbType typ = this.GetSQLiteType(i).Type; row[SchemaTableColumn.ColumnName] = this.GetName(i); row[SchemaTableColumn.ColumnOrdinal] = i; row[SchemaTableColumn.ColumnSize] = SQLiteConvert.DbTypeToColumnSize(typ); row[SchemaTableColumn.NumericPrecision] = SQLiteConvert.DbTypeToNumericPrecision(typ); row[SchemaTableColumn.NumericScale] = SQLiteConvert.DbTypeToNumericScale(typ); row[SchemaTableColumn.ProviderType] = this.GetSQLiteType(i).Type; row[SchemaTableColumn.IsLong] = false; row[SchemaTableColumn.AllowDBNull] = true; row[SchemaTableOptionalColumn.IsReadOnly] = false; row[SchemaTableOptionalColumn.IsRowVersion] = false; row[SchemaTableColumn.IsUnique] = false; row[SchemaTableColumn.IsKey] = false; row[SchemaTableOptionalColumn.IsAutoIncrement] = false; row[SchemaTableColumn.DataType] = this.GetFieldType(i); row[SchemaTableOptionalColumn.IsHidden] = false; str4 = this._command.Connection._sql.ColumnOriginalName(this._activeStatement, i); if (!string.IsNullOrEmpty(str4)) { row[SchemaTableColumn.BaseColumnName] = str4; } row[SchemaTableColumn.IsExpression] = string.IsNullOrEmpty(str4); row[SchemaTableColumn.IsAliased] = string.Compare(this.GetName(i), str4, true, CultureInfo.InvariantCulture) != 0; string str = this._command.Connection._sql.ColumnTableName(this._activeStatement, i); if (!string.IsNullOrEmpty(str)) { row[SchemaTableColumn.BaseTableName] = str; } str = this._command.Connection._sql.ColumnDatabaseName(this._activeStatement, i); if (!string.IsNullOrEmpty(str)) { row[SchemaTableOptionalColumn.BaseCatalogName] = str; } string dataType = null; if (string.IsNullOrEmpty(str4)) { goto Label_09B7; } this._command.Connection._sql.ColumnMetaData((string)row[SchemaTableOptionalColumn.BaseCatalogName], (string)row[SchemaTableColumn.BaseTableName], str4, out dataType, out str6, out flag, out flag2, out flag3); if (flag || flag2) { row[SchemaTableColumn.AllowDBNull] = false; } row[SchemaTableColumn.IsKey] = flag2; row[SchemaTableOptionalColumn.IsAutoIncrement] = flag3; row["CollationType"] = str6; string[] strArray = dataType.Split(new char[] { '(' }); if (strArray.Length > 1) { dataType = strArray[0]; strArray = strArray[1].Split(new char[] { ')' }); if (strArray.Length > 1) { strArray = strArray[0].Split(new char[] { ',', '.' }); if ((this.GetSQLiteType(i).Type == DbType.String) || (this.GetSQLiteType(i).Type == DbType.Binary)) { row[SchemaTableColumn.ColumnSize] = Convert.ToInt32(strArray[0], CultureInfo.InvariantCulture); } else { row[SchemaTableColumn.NumericPrecision] = Convert.ToInt32(strArray[0], CultureInfo.InvariantCulture); if (strArray.Length > 1) { row[SchemaTableColumn.NumericScale] = Convert.ToInt32(strArray[1], CultureInfo.InvariantCulture); } } } } if (wantDefaultValue) { using (SQLiteCommand command = new SQLiteCommand(string.Format(CultureInfo.InvariantCulture, "PRAGMA [{0}].TABLE_INFO([{1}])", new object[] { row[SchemaTableOptionalColumn.BaseCatalogName], row[SchemaTableColumn.BaseTableName] }), this._command.Connection)) { using (DbDataReader reader = command.ExecuteReader()) { while (reader.Read()) { if (string.Compare((string)row[SchemaTableColumn.BaseColumnName], reader.GetString(1), true, CultureInfo.InvariantCulture) == 0) { if (!reader.IsDBNull(4)) { row[SchemaTableOptionalColumn.DefaultValue] = reader[4]; } goto Label_075E; } } } } } Label_075E: if (wantUniqueInfo) { if ((((string)row[SchemaTableOptionalColumn.BaseCatalogName]) != str2) || (((string)row[SchemaTableColumn.BaseTableName]) != str3)) { str2 = (string)row[SchemaTableOptionalColumn.BaseCatalogName]; str3 = (string)row[SchemaTableColumn.BaseTableName]; string[] restrictionValues = new string[4]; restrictionValues[0] = (string)row[SchemaTableOptionalColumn.BaseCatalogName]; restrictionValues[2] = (string)row[SchemaTableColumn.BaseTableName]; schema = this._command.Connection.GetSchema("Indexes", restrictionValues); } foreach (DataRow row2 in schema.Rows) { string[] strArray3 = new string[5]; strArray3[0] = (string)row[SchemaTableOptionalColumn.BaseCatalogName]; strArray3[2] = (string)row[SchemaTableColumn.BaseTableName]; strArray3[3] = (string)row2["INDEX_NAME"]; DataTable table3 = this._command.Connection.GetSchema("IndexColumns", strArray3); foreach (DataRow row3 in table3.Rows) { if (string.Compare((string)row3["COLUMN_NAME"], str4, true, CultureInfo.InvariantCulture) == 0) { if ((table3.Rows.Count == 1) && !((bool)row[SchemaTableColumn.AllowDBNull])) { row[SchemaTableColumn.IsUnique] = row2["UNIQUE"]; } if (((table3.Rows.Count != 1) || !((bool)row2["PRIMARY_KEY"])) || (string.IsNullOrEmpty(dataType) || (string.Compare(dataType, "integer", true, CultureInfo.InvariantCulture) != 0))) { } break; } } } } if (string.IsNullOrEmpty(dataType)) { TypeAffinity affinity; dataType = this._activeStatement._sql.ColumnType(this._activeStatement, i, out affinity); } if (!string.IsNullOrEmpty(dataType)) { row["DataTypeName"] = dataType; } Label_09B7: tbl.Rows.Add(row); } if (this._keyInfo != null) { this._keyInfo.AppendSchemaTable(tbl); } tbl.AcceptChanges(); tbl.EndLoadData(); return(tbl); }
internal DataTable GetSchemaTable(bool wantUniqueInfo, bool wantDefaultValue) { CheckClosed(); DataTable tbl = new DataTable("SchemaTable"); DataTable tblIndexes = null; DataTable tblIndexColumns; DataRow row; string temp; string strCatalog = ""; string strTable = ""; string strColumn = ""; tbl.Locale = CultureInfo.InvariantCulture; tbl.Columns.Add(SchemaTableColumn.ColumnName, typeof(String)); tbl.Columns.Add(SchemaTableColumn.ColumnOrdinal, typeof(int)); tbl.Columns.Add(SchemaTableColumn.ColumnSize, typeof(int)); tbl.Columns.Add(SchemaTableColumn.NumericPrecision, typeof(short)); tbl.Columns.Add(SchemaTableColumn.NumericScale, typeof(short)); tbl.Columns.Add(SchemaTableColumn.IsUnique, typeof(Boolean)); tbl.Columns.Add(SchemaTableColumn.IsKey, typeof(Boolean)); tbl.Columns.Add(SchemaTableOptionalColumn.BaseServerName, typeof(string)); tbl.Columns.Add(SchemaTableOptionalColumn.BaseCatalogName, typeof(String)); tbl.Columns.Add(SchemaTableColumn.BaseColumnName, typeof(String)); tbl.Columns.Add(SchemaTableColumn.BaseSchemaName, typeof(String)); tbl.Columns.Add(SchemaTableColumn.BaseTableName, typeof(String)); tbl.Columns.Add(SchemaTableColumn.DataType, typeof(Type)); tbl.Columns.Add(SchemaTableColumn.AllowDBNull, typeof(Boolean)); tbl.Columns.Add(SchemaTableColumn.ProviderType, typeof(int)); tbl.Columns.Add(SchemaTableColumn.IsAliased, typeof(Boolean)); tbl.Columns.Add(SchemaTableColumn.IsExpression, typeof(Boolean)); tbl.Columns.Add(SchemaTableOptionalColumn.IsAutoIncrement, typeof(Boolean)); tbl.Columns.Add(SchemaTableOptionalColumn.IsRowVersion, typeof(Boolean)); tbl.Columns.Add(SchemaTableOptionalColumn.IsHidden, typeof(Boolean)); tbl.Columns.Add(SchemaTableColumn.IsLong, typeof(Boolean)); tbl.Columns.Add(SchemaTableOptionalColumn.IsReadOnly, typeof(Boolean)); tbl.Columns.Add(SchemaTableOptionalColumn.ProviderSpecificDataType, typeof(Type)); tbl.Columns.Add(SchemaTableOptionalColumn.DefaultValue, typeof(object)); tbl.Columns.Add("DataTypeName", typeof(string)); tbl.Columns.Add("CollationType", typeof(string)); tbl.BeginLoadData(); for (int n = 0; n < _fieldCount; n++) { row = tbl.NewRow(); DbType typ = GetSQLiteType(n).Type; // Default settings for the column row[SchemaTableColumn.ColumnName] = GetName(n); row[SchemaTableColumn.ColumnOrdinal] = n; row[SchemaTableColumn.ColumnSize] = SQLiteConvert.DbTypeToColumnSize(typ); row[SchemaTableColumn.NumericPrecision] = SQLiteConvert.DbTypeToNumericPrecision(typ); row[SchemaTableColumn.NumericScale] = SQLiteConvert.DbTypeToNumericScale(typ); row[SchemaTableColumn.ProviderType] = GetSQLiteType(n).Type; row[SchemaTableColumn.IsLong] = false; row[SchemaTableColumn.AllowDBNull] = true; row[SchemaTableOptionalColumn.IsReadOnly] = false; row[SchemaTableOptionalColumn.IsRowVersion] = false; row[SchemaTableColumn.IsUnique] = false; row[SchemaTableColumn.IsKey] = false; row[SchemaTableOptionalColumn.IsAutoIncrement] = false; row[SchemaTableColumn.DataType] = GetFieldType(n); row[SchemaTableOptionalColumn.IsHidden] = false; strColumn = _command.Connection._sql.ColumnOriginalName(_activeStatement, n); if (String.IsNullOrEmpty(strColumn) == false) { row[SchemaTableColumn.BaseColumnName] = strColumn; } row[SchemaTableColumn.IsExpression] = String.IsNullOrEmpty(strColumn); row[SchemaTableColumn.IsAliased] = (String.Compare(GetName(n), strColumn, StringComparison.OrdinalIgnoreCase) != 0); temp = _command.Connection._sql.ColumnTableName(_activeStatement, n); if (String.IsNullOrEmpty(temp) == false) { row[SchemaTableColumn.BaseTableName] = temp; } temp = _command.Connection._sql.ColumnDatabaseName(_activeStatement, n); if (String.IsNullOrEmpty(temp) == false) { row[SchemaTableOptionalColumn.BaseCatalogName] = temp; } string dataType = null; // If we have a table-bound column, extract the extra information from it if (String.IsNullOrEmpty(strColumn) == false) { string collSeq; bool bNotNull; bool bPrimaryKey; bool bAutoIncrement; string[] arSize; // Get the column meta data _command.Connection._sql.ColumnMetaData( (string)row[SchemaTableOptionalColumn.BaseCatalogName], (string)row[SchemaTableColumn.BaseTableName], strColumn, out dataType, out collSeq, out bNotNull, out bPrimaryKey, out bAutoIncrement); if (bNotNull || bPrimaryKey) { row[SchemaTableColumn.AllowDBNull] = false; } row[SchemaTableColumn.IsKey] = bPrimaryKey; row[SchemaTableOptionalColumn.IsAutoIncrement] = bAutoIncrement; row["CollationType"] = collSeq; // For types like varchar(50) and such, extract the size arSize = dataType.Split('('); if (arSize.Length > 1) { dataType = arSize[0]; arSize = arSize[1].Split(')'); if (arSize.Length > 1) { arSize = arSize[0].Split(',', '.'); if (GetSQLiteType(n).Type == DbType.String || GetSQLiteType(n).Type == DbType.Binary) { row[SchemaTableColumn.ColumnSize] = Convert.ToInt32(arSize[0], CultureInfo.InvariantCulture); } else { row[SchemaTableColumn.NumericPrecision] = Convert.ToInt32(arSize[0], CultureInfo.InvariantCulture); if (arSize.Length > 1) { row[SchemaTableColumn.NumericScale] = Convert.ToInt32(arSize[1], CultureInfo.InvariantCulture); } } } } if (wantDefaultValue) { // Determine the default value for the column, which sucks because we have to query the schema for each column using (SQLiteCommand cmdTable = new SQLiteCommand(String.Format(CultureInfo.InvariantCulture, "PRAGMA [{0}].TABLE_INFO([{1}])", row[SchemaTableOptionalColumn.BaseCatalogName], row[SchemaTableColumn.BaseTableName] ), _command.Connection)) using (DbDataReader rdTable = cmdTable.ExecuteReader()) { // Find the matching column while (rdTable.Read()) { if (String.Compare((string)row[SchemaTableColumn.BaseColumnName], rdTable.GetString(1), StringComparison.OrdinalIgnoreCase) == 0) { if (rdTable.IsDBNull(4) == false) { row[SchemaTableOptionalColumn.DefaultValue] = rdTable[4]; } break; } } } } // Determine IsUnique properly, which is a pain in the butt! if (wantUniqueInfo) { if ((string)row[SchemaTableOptionalColumn.BaseCatalogName] != strCatalog || (string)row[SchemaTableColumn.BaseTableName] != strTable) { strCatalog = (string)row[SchemaTableOptionalColumn.BaseCatalogName]; strTable = (string)row[SchemaTableColumn.BaseTableName]; tblIndexes = _command.Connection.GetSchema("Indexes", new string[] { (string)row[SchemaTableOptionalColumn.BaseCatalogName], null, (string)row[SchemaTableColumn.BaseTableName], null }); } foreach (DataRow rowIndexes in tblIndexes.Rows) { tblIndexColumns = _command.Connection.GetSchema("IndexColumns", new string[] { (string)row[SchemaTableOptionalColumn.BaseCatalogName], null, (string)row[SchemaTableColumn.BaseTableName], (string)rowIndexes["INDEX_NAME"], null }); foreach (DataRow rowColumnIndex in tblIndexColumns.Rows) { if (String.Compare((string)rowColumnIndex["COLUMN_NAME"], strColumn, StringComparison.OrdinalIgnoreCase) == 0) { if (tblIndexColumns.Rows.Count == 1 && (bool)row[SchemaTableColumn.AllowDBNull] == false) { row[SchemaTableColumn.IsUnique] = rowIndexes["UNIQUE"]; } // If its an integer primary key and the only primary key in the table, then its a rowid alias and is autoincrement // NOTE: Currently commented out because this is not always the desired behavior. For example, a 1:1 relationship with // another table, where the other table is autoincrement, but this one is not, and uses the rowid from the other. // It is safer to only set Autoincrement on tables where we're SURE the user specified AUTOINCREMENT, even if its a rowid column. if (tblIndexColumns.Rows.Count == 1 && (bool)rowIndexes["PRIMARY_KEY"] == true && String.IsNullOrEmpty(dataType) == false && String.Compare(dataType, "integer", StringComparison.OrdinalIgnoreCase) == 0) { // row[SchemaTableOptionalColumn.IsAutoIncrement] = true; } break; } } } } if (String.IsNullOrEmpty(dataType)) { TypeAffinity affin; dataType = _activeStatement._sql.ColumnType(_activeStatement, n, out affin); } if (String.IsNullOrEmpty(dataType) == false) { row["DataTypeName"] = dataType; } } tbl.Rows.Add(row); } if (_keyInfo != null) { _keyInfo.AppendSchemaTable(tbl); } tbl.AcceptChanges(); tbl.EndLoadData(); return(tbl); }