Example #1
0
		public static GetSchemaOptions? GetDefaultSchemaOptions(string context, GetSchemaOptions? baseOptions = null)
		{
			if (context.Contains("SapHana"))
			{
				// SAP HANA provider throws C++ assertions when we try to load schema for some functions
				var options = baseOptions ?? new GetSchemaOptions();

				var oldLoad = options.LoadProcedure;
				if (oldLoad != null)
					options.LoadProcedure = p => oldLoad(p) && loadCheck(p);
				else
					options.LoadProcedure = loadCheck;

				bool loadCheck(ProcedureSchema p)
				{
					// TODO: actualize list for SPS04
					return false;
					//return p.ProcedureName != "SERIES_GENERATE_TIME"
					//	&& p.ProcedureName != "SERIES_DISAGGREGATE_TIME"
					//	// just too slow
					//	&& p.ProcedureName != "GET_FULL_SYSTEM_INFO_DUMP"
					//	&& p.ProcedureName != "GET_FULL_SYSTEM_INFO_DUMP_WITH_PARAMETERS"
					//	&& p.ProcedureName != "FULL_SYSTEM_INFO_DUMP_CREATE";
				}

				return options;
			}

			return baseOptions;
		}
Example #2
0
 public override DatabaseSchema GetSchema(DataConnection dataConnection, GetSchemaOptions?options = null)
 {
     HanaSchemaOptions             = options as GetHanaSchemaOptions;
     DefaultSchema                 = dataConnection.Execute <string>("SELECT CURRENT_SCHEMA FROM DUMMY");
     HaveAccessForCalculationViews = CheckAccessForCalculationViews(dataConnection);
     return(base.GetSchema(dataConnection, options));
 }
Example #3
0
        public override DatabaseSchema GetSchema(DataConnection dataConnection, GetSchemaOptions?options = null)
        {
            var defaultSchema = dataConnection.Execute <string>("SELECT USER FROM DUAL");

            SchemasFilter = BuildSchemaFilter(options, defaultSchema, OracleMappingSchema.ConvertStringToSql);

            return(base.GetSchema(dataConnection, options));
        }
Example #4
0
        public override DatabaseSchema GetSchema(DataConnection dataConnection, GetSchemaOptions?options = null)
        {
            HanaSchemaOptions            = options as GetHanaSchemaOptions;
            DefaultSchema                = dataConnection.Execute <string>("SELECT CURRENT_SCHEMA FROM DUMMY");
            HasAccessForCalculationViews = CheckAccessForCalculationViews(dataConnection);
            SchemasFilter                = BuildSchemaFilter(options, DefaultSchema, SapHanaMappingSchema.ConvertStringToSql);

            return(base.GetSchema(dataConnection, options));
        }
Example #5
0
        public override DatabaseSchema GetSchema(DataConnection dataConnection, GetSchemaOptions?options = null)
        {
            // TODO: Connection.GetSchema is not supported by MS provider, so we need to implement direct read of metadata
            if (dataConnection.DataProvider.Name == ProviderName.SQLiteMS)
            {
                return new DatabaseSchema()
                       {
                           DataSource      = string.Empty,
                           Database        = string.Empty,
                           ServerVersion   = string.Empty,
                           Tables          = new List <TableSchema>(),
                           Procedures      = new List <ProcedureSchema>(),
                           DataTypesSchema = new DataTable()
                       }
            }
            ;

            return(base.GetSchema(dataConnection, options));
        }
Example #6
0
        protected string?BuildSchemaFilter(GetSchemaOptions?options, string defaultSchema, Action <StringBuilder, string> stringLiteralBuilder)
        {
            var schemas = new HashSet <string>();

            schemas.Add(defaultSchema);

            if (options != null)
            {
                if (options.IncludedSchemas != null)
                {
                    schemas.Clear();
                    foreach (var schema in options.IncludedSchemas)
                    {
                        if (!string.IsNullOrEmpty(schema))
                        {
                            schemas.Add(schema !);
                        }
                    }
                }

                if (options.ExcludedSchemas != null)
                {
                    foreach (var schema in options.ExcludedSchemas)
                    {
                        if (!string.IsNullOrEmpty(schema))
                        {
                            schemas.Remove(schema !);
                        }
                    }
                }
            }

            if (schemas.Count == 0)
            {
                return(null);
            }

            var first = true;

            var sb = new StringBuilder();

            sb.Append("IN (");

            foreach (var schema in schemas)
            {
                if (!first)
                {
                    sb.Append(", ");
                }
                else
                {
                    first = false;
                }

                stringLiteralBuilder(sb, schema);
            }

            sb.Append(")");

            return(sb.ToString());
        }
Example #7
0
        public virtual DatabaseSchema GetSchema(DataConnection dataConnection, GetSchemaOptions?options = null)
        {
            if (options == null)
            {
                options = new GetSchemaOptions();
            }

            IncludedSchemas       = GetHashSet(options.IncludedSchemas, options.StringComparer);
            ExcludedSchemas       = GetHashSet(options.ExcludedSchemas, options.StringComparer);
            IncludedCatalogs      = GetHashSet(options.IncludedCatalogs, options.StringComparer);
            ExcludedCatalogs      = GetHashSet(options.ExcludedCatalogs, options.StringComparer);
            GenerateChar1AsString = options.GenerateChar1AsString;

            var dbConnection = (DbConnection)dataConnection.Connection;

            InitProvider(dataConnection);

            DataTypesDic = new Dictionary <string, DataTypeInfo>(StringComparer.OrdinalIgnoreCase);
            ProviderSpecificDataTypesDic = new Dictionary <string, DataTypeInfo>(StringComparer.OrdinalIgnoreCase);
            DataTypesByProviderDbTypeDic = new Dictionary <int, DataTypeInfo>();
            ProviderSpecificDataTypesByProviderDbTypeDic = new Dictionary <int, DataTypeInfo>();

            foreach (var dt in GetDataTypes(dataConnection))
            {
                if (dt.ProviderSpecific)
                {
                    if (!ProviderSpecificDataTypesDic.ContainsKey(dt.TypeName))
                    {
                        ProviderSpecificDataTypesDic.Add(dt.TypeName, dt);
                    }
                    if (!ProviderSpecificDataTypesByProviderDbTypeDic.ContainsKey(dt.ProviderDbType))
                    {
                        ProviderSpecificDataTypesByProviderDbTypeDic.Add(dt.ProviderDbType, dt);
                    }
                }
                else
                {
                    if (!DataTypesDic.ContainsKey(dt.TypeName))
                    {
                        DataTypesDic.Add(dt.TypeName, dt);
                    }
                    if (!DataTypesByProviderDbTypeDic.ContainsKey(dt.ProviderDbType))
                    {
                        DataTypesByProviderDbTypeDic.Add(dt.ProviderDbType, dt);
                    }
                }
            }

            List <TableSchema>     tables;
            List <ProcedureSchema> procedures;

            if (options.GetTables)
            {
                tables =
                    (
                        from t in GetTables(dataConnection, options)
                        where
                        (IncludedSchemas.Count == 0 || IncludedSchemas.Contains(t.SchemaName)) &&
                        (ExcludedSchemas.Count == 0 || !ExcludedSchemas.Contains(t.SchemaName)) &&
                        (IncludedCatalogs.Count == 0 || IncludedCatalogs.Contains(t.CatalogName)) &&
                        (ExcludedCatalogs.Count == 0 || !ExcludedCatalogs.Contains(t.CatalogName)) &&
                        (options.LoadTable == null || options.LoadTable(new LoadTableData(t)))
                        select new TableSchema
                {
                    ID = t.TableID,
                    CatalogName = t.CatalogName,
                    SchemaName = t.SchemaName,
                    TableName = t.TableName,
                    Description = t.Description,
                    IsDefaultSchema = t.IsDefaultSchema,
                    IsView = t.IsView,
                    TypeName = ToValidName(t.TableName),
                    Columns = new List <ColumnSchema>(),
                    ForeignKeys = new List <ForeignKeySchema>(),
                    IsProviderSpecific = t.IsProviderSpecific
                }
                    ).ToList();

                var pks = GetPrimaryKeys(dataConnection, tables, options);

                #region Columns

                var columns =
                    from c  in GetColumns(dataConnection, options)

                    join pk in pks
                    on c.TableID + "." + c.Name equals pk.TableID + "." + pk.ColumnName into g2
                    from pk in g2.DefaultIfEmpty()

                    join t  in tables on c.TableID equals t.ID

                    orderby c.Ordinal
                    select new { t, c, dt = GetDataType(c.DataType, options), pk };

                foreach (var column in columns)
                {
                    var dataType   = column.c.DataType;
                    var systemType = GetSystemType(dataType, column.c.ColumnType, column.dt, column.c.Length, column.c.Precision, column.c.Scale, options);
                    var isNullable = column.c.IsNullable;
                    var columnType = column.c.ColumnType ?? GetDbType(options, dataType, column.dt, column.c.Length, column.c.Precision, column.c.Scale, null, null, null);

                    column.t.Columns.Add(new ColumnSchema
                    {
                        Table                = column.t,
                        ColumnName           = column.c.Name,
                        ColumnType           = columnType,
                        IsNullable           = isNullable,
                        MemberName           = ToValidName(column.c.Name),
                        MemberType           = ToTypeName(systemType, isNullable),
                        SystemType           = systemType ?? typeof(object),
                        DataType             = GetDataType(dataType, column.c.ColumnType, column.c.Length, column.c.Precision, column.c.Scale),
                        ProviderSpecificType = GetProviderSpecificType(dataType),
                        SkipOnInsert         = column.c.SkipOnInsert || column.c.IsIdentity,
                        SkipOnUpdate         = column.c.SkipOnUpdate || column.c.IsIdentity,
                        IsPrimaryKey         = column.pk != null,
                        PrimaryKeyOrder      = column.pk?.Ordinal ?? -1,
                        IsIdentity           = column.c.IsIdentity,
                        Description          = column.c.Description,
                        Length               = column.c.Length,
                        Precision            = column.c.Precision,
                        Scale                = column.c.Scale,
                    });
                }

                #endregion

                #region FK

                var fks = options.GetForeignKeys ? GetForeignKeys(dataConnection, tables, options) : Array <ForeignKeyInfo> .Empty;

                foreach (var fk in fks.OrderBy(f => f.Ordinal))
                {
                    var thisTable  = (from t in tables where t.ID == fk.ThisTableID select t).FirstOrDefault();
                    var otherTable = (from t in tables where t.ID == fk.OtherTableID select t).FirstOrDefault();

                    if (thisTable == null || otherTable == null)
                    {
                        continue;
                    }

                    var stringComparison = ForeignKeyColumnComparison(fk.OtherColumn);

                    var thisColumn  = (from c in thisTable.Columns where c.ColumnName == fk.ThisColumn select c).SingleOrDefault();
                    var otherColumn =
                        (
                            from c in otherTable.Columns
                            where string.Compare(c.ColumnName, fk.OtherColumn, stringComparison) == 0
                            select c
                        ).SingleOrDefault();

                    if (thisColumn == null || otherColumn == null)
                    {
                        continue;
                    }

                    var key = thisTable.ForeignKeys.FirstOrDefault(f => f.KeyName == fk.Name);

                    if (key == null)
                    {
                        key = new ForeignKeySchema
                        {
                            KeyName      = fk.Name,
                            MemberName   = ToValidName(fk.Name),
                            ThisTable    = thisTable,
                            OtherTable   = otherTable,
                            ThisColumns  = new List <ColumnSchema>(),
                            OtherColumns = new List <ColumnSchema>(),
                            CanBeNull    = true,
                        };

                        thisTable.ForeignKeys.Add(key);
                    }

                    key.ThisColumns.Add(thisColumn);
                    key.OtherColumns.Add(otherColumn);
                }

                #endregion

                var pst = GetProviderSpecificTables(dataConnection, options);

                if (pst != null)
                {
                    tables.AddRange(pst);
                }
            }
            else
            {
                tables = new List <TableSchema>();
            }

            if (options.GetProcedures)
            {
                #region Procedures

                var sqlProvider = dataConnection.DataProvider.CreateSqlBuilder(dataConnection.MappingSchema);
                var procs       = GetProcedures(dataConnection, options);
                var n           = 0;

                if (procs != null)
                {
                    var procParams = GetProcedureParameters(dataConnection, procs, options);

                    procedures =
                        (
                            from sp in procs
                            where
                            (IncludedSchemas.Count == 0 || IncludedSchemas.Contains(sp.SchemaName)) &&
                            (ExcludedSchemas.Count == 0 || !ExcludedSchemas.Contains(sp.SchemaName)) &&
                            (IncludedCatalogs.Count == 0 || IncludedCatalogs.Contains(sp.CatalogName)) &&
                            (ExcludedCatalogs.Count == 0 || !ExcludedCatalogs.Contains(sp.CatalogName))
                            join p  in procParams on sp.ProcedureID equals p.ProcedureID
                            into gr
                            select new ProcedureSchema
                    {
                        CatalogName = sp.CatalogName,
                        SchemaName = sp.SchemaName,
                        ProcedureName = sp.ProcedureName,
                        MemberName = ToValidName(sp.ProcedureName),
                        IsFunction = sp.IsFunction,
                        IsTableFunction = sp.IsTableFunction,
                        IsResultDynamic = sp.IsResultDynamic,
                        IsAggregateFunction = sp.IsAggregateFunction,
                        IsDefaultSchema = sp.IsDefaultSchema,
                        Description = sp.Description,
                        Parameters =
                            (
                                from pr in gr

                                let dt = GetDataType(pr.DataType, options)

                                         let systemType = GetSystemType(pr.DataType, pr.DataTypeExact, dt, pr.Length, pr.Precision, pr.Scale, options)

                                                          orderby pr.Ordinal
                                                          select new ParameterSchema
                        {
                            SchemaName = pr.ParameterName,
                            SchemaType = GetDbType(options, pr.DataType, dt, pr.Length, pr.Precision, pr.Scale, pr.UDTCatalog, pr.UDTSchema, pr.UDTName),
                            IsIn = pr.IsIn,
                            IsOut = pr.IsOut,
                            IsResult = pr.IsResult,
                            Size = pr.Length,
                            ParameterName = ToValidName(pr.ParameterName ?? "par" + ++n),
                            ParameterType = ToTypeName(systemType, true),
                            SystemType = systemType ?? typeof(object),
                            DataType = GetDataType(pr.DataType, pr.DataTypeExact, pr.Length, pr.Precision, pr.Scale),
                            ProviderSpecificType = GetProviderSpecificType(pr.DataType),
                            IsNullable = pr.IsNullable,
                            Description = pr.Description
                        }
                            ).ToList()
                    } into ps
                            select ps
                        ).ToList();

                    var current = 1;

                    var isActiveTransaction = dataConnection.Transaction != null;

                    if (GetProcedureSchemaExecutesProcedure && isActiveTransaction)
                    {
                        throw new LinqToDBException("Cannot read schema with GetSchemaOptions.GetProcedures = true from transaction. Remove transaction or set GetSchemaOptions.GetProcedures to false");
                    }

                    if (!isActiveTransaction)
                    {
                        dataConnection.BeginTransaction();
                    }

                    try
                    {
                        foreach (var procedure in procedures)
                        {
                            if (!procedure.IsResultDynamic && (!procedure.IsFunction || procedure.IsTableFunction) && options.LoadProcedure(procedure))
                            {
                                var commandText = sqlProvider.ConvertTableName(new StringBuilder(),
                                                                               null,
                                                                               procedure.CatalogName,
                                                                               procedure.SchemaName,
                                                                               procedure.ProcedureName,
                                                                               TableOptions.NotSet).ToString();

                                LoadProcedureTableSchema(dataConnection, options, procedure, commandText, tables);
                            }

                            options.ProcedureLoadingProgress(procedures.Count, current++);
                        }
                    }
                    finally
                    {
                        if (!isActiveTransaction)
                        {
                            dataConnection.RollbackTransaction();
                        }
                    }
                }
                else
                {
                    procedures = new List <ProcedureSchema>();
                }

                var psp = GetProviderSpecificProcedures(dataConnection);

                if (psp != null)
                {
                    procedures.AddRange(psp);
                }

                #endregion
            }
            else
            {
                procedures = new List <ProcedureSchema>();
            }

            return(ProcessSchema(new DatabaseSchema
            {
                DataSource = GetDataSourceName(dataConnection),
                Database = GetDatabaseName(dataConnection),
                ServerVersion = dbConnection.ServerVersion,
                Tables = tables,
                Procedures = procedures,
                ProviderSpecificTypeNamespace = GetProviderSpecificTypeNamespace(),
                DataTypesSchema = DataTypesSchema,
            }, options));
        }
        public override DatabaseSchema GetSchema(DataConnection dataConnection, GetSchemaOptions?options = null)
        {
            _majorVersion = int.Parse(dataConnection.Execute <string>("SELECT rdb$get_context('SYSTEM', 'ENGINE_VERSION') from rdb$database").Split('.')[0], CultureInfo.InvariantCulture);

            return(base.GetSchema(dataConnection, options));
        }