public virtual EnsureSchemaStatus TryEnsureSchema(Type type, bool force, out Exception ex, ILogger logger = null) { EnsureSchemaStatus result = EnsureSchemaStatus.Invalid; ex = null; try { string schemaName = Dao.RealConnectionName(type); if (!SchemaNames.Contains(schemaName) || force) { _schemaNames.Add(schemaName); SchemaWriter schema = ServiceProvider.Get <SchemaWriter>(); schema.WriteSchemaScript(type); ExecuteSql(schema, ServiceProvider.Get <IParameterBuilder>()); result = EnsureSchemaStatus.Success; } else { result = EnsureSchemaStatus.AlreadyDone; } } catch (Exception e) { ex = e; result = EnsureSchemaStatus.Error; logger = logger ?? Log.Default; logger.AddEntry("Non fatal error occurred trying to write schema for type {0}: {1}", LogEventType.Warning, ex, type.Name, ex.Message); } return(result); }
/// <summary> /// Recursively search the given schema for named schemas and adds them to the given container /// </summary> /// <param name="schema">schema object to search</param> /// <param name="names">list of named schemas</param> protected virtual void addName(Schema schema, SchemaNames names) { NamedSchema ns = schema as NamedSchema; if (null != ns) { if (names.Contains(ns.SchemaName)) { return; } } switch (schema.Tag) { case Schema.Type.Null: case Schema.Type.Boolean: case Schema.Type.Int: case Schema.Type.Long: case Schema.Type.Float: case Schema.Type.Double: case Schema.Type.Bytes: case Schema.Type.String: break; case Schema.Type.Enumeration: case Schema.Type.Fixed: names.Add(ns); break; case Schema.Type.Record: case Schema.Type.Error: var rs = schema as RecordSchema; names.Add(rs); foreach (Field field in rs.Fields) { addName(field.Schema, names); } break; case Schema.Type.Array: var asc = schema as ArraySchema; addName(asc.ItemSchema, names); break; case Schema.Type.Map: var ms = schema as MapSchema; addName(ms.ValueSchema, names); break; case Schema.Type.Union: var us = schema as UnionSchema; foreach (Schema usc in us.Schemas) { addName(usc, names); } break; default: throw new CodeGenException("Unable to add name for " + schema.Name + " type " + schema.Tag); } }