protected override void WriteForeignKeys(Type daoType) { Args.ThrowIfNull(daoType, "daoType"); string datasetName = Dao.ConnectionName(daoType); foreach (Type type in daoType.Assembly.GetTypes()) { TableAttribute tableAttr = null; if (type.HasCustomAttributeOfType <TableAttribute>(out tableAttr)) { if (Dao.ConnectionName(type).Equals(datasetName)) { ForeignKeyAttribute[] columns = GetForeignKeys(type); foreach (ForeignKeyAttribute fk in columns) { if (fk != null) { // table1Name, fkName, column1Name, table2Name, column2Name Builder.AppendFormat(AddForeignKeyColumnFormat, GetFormattedTableName(fk.Table), GetFirstThirtyCharacters(fk.ForeignKeyName), GetFirstThirtyCharacters(fk.Name), GetFormattedTableName(fk.ReferencedTable), GetFirstThirtyCharacters(fk.ReferencedKey)); Go(); } } } } } }
/// <summary> /// Causes the current DatabaseInitializer to NOT initialize the /// database for the connection name associated with the specified /// types. /// </summary> /// <param name="types"></param> public void Ignore(params Type[] types) { foreach (Type type in types) { string connectionName = Dao.ConnectionName(type); if (!_ignoreConnectionNames.Contains(connectionName)) { _ignoreConnectionNames.Add(connectionName); } } }
/// <summary> /// Gets the database for the specified type. /// </summary> /// <param name="daoType"></param> /// <returns></returns> public Database this[Type daoType] { get { return(this[Dao.ConnectionName(daoType)]); } internal set { this[Dao.ConnectionName(daoType)] = value; } }
public DaoProxyRegistration(Type daoType) { Args.ThrowIfNull(daoType, "daoType"); Args.ThrowIf <InvalidOperationException>( !daoType.IsSubclassOf(typeof(Dao)), "The specified type ({0}) must be a Dao implementation.", daoType.Name); this.ServiceProvider = new Incubator(); this.Assembly = daoType.Assembly; this.ContextName = Dao.ConnectionName(daoType); Dao.RegisterDaoTypes(daoType, this.ServiceProvider); }
private void ForEachTable(Type type, Action <Type> action) { string connectionName = Dao.ConnectionName(type); foreach (Type t in type.Assembly.GetTypes().Where(t => t.HasCustomAttributeOfType <TableAttribute>(false))) { if (Dao.ConnectionName(t).Equals(connectionName)) { action(t); Go(); } } }
public DaoProxyRegistration(Assembly assembly) { this.ServiceProvider = new Incubator(); this.Assembly = assembly; Type daoType = (from type in assembly.GetTypes() where type.IsSubclassOf(typeof(Dao)) select type).FirstOrDefault(); Args.ThrowIfNull(daoType, "daoType"); this.ContextName = Dao.ConnectionName(daoType); Dao.RegisterDaoTypes(daoType, this.ServiceProvider); }
protected virtual void WriteForeignKeys(Type daoType) { Args.ThrowIfNull(daoType, "daoType"); string datasetName = Dao.ConnectionName(daoType); foreach (Type type in daoType.Assembly.GetTypes()) { TableAttribute tableAttr = null; if (type.HasCustomAttributeOfType <TableAttribute>(out tableAttr)) { if (Dao.ConnectionName(type).Equals(datasetName)) { WriteAddForeignKey(type); } } } }
public static DaoProxyRegistration Register(Type daoType) { string connectionName = Dao.ConnectionName(daoType); if (!Registrations.ContainsKey(connectionName)) { lock (_registerLock) { if (!Registrations.ContainsKey(connectionName)) { DaoProxyRegistration registration = new DaoProxyRegistration(daoType); Registrations.Add(connectionName, registration); } } } return(Registrations[connectionName]); }
public virtual DataSet GetDataSetFromSql <T>(string sqlStatement, CommandType commandType, bool releaseConnection, DbConnection conn, DbTransaction tx, params DbParameter[] dbParamaters) { DbProviderFactory providerFactory = ServiceProvider.Get <DbProviderFactory>(); DataSet set = new DataSet(Dao.ConnectionName <T>().Or(8.RandomLetters())); try { DbCommand command = BuildCommand(sqlStatement, commandType, dbParamaters, providerFactory, conn, tx); FillDataSet(set, command); } finally { if (releaseConnection) { ReleaseConnection(conn); } } return(set); }
/// <summary> /// Register all the Dao types in the current domain that belong to the /// specified connectionName/schema /// </summary> /// <param name="connectionName"></param> /// <param name="retryCount"></param> public static void RegisterConnection(string connectionName, int retryCount = 5) { try { AppDomain.CurrentDomain.GetAssemblies().Each(a => { a.GetTypes() .Where(t => t.IsSubclassOf(typeof(Dao)) && Dao.ConnectionName(t).Equals(connectionName)) .Each(t => Register(t)); }); } catch (Exception ex) { Log.AddEntry("Exception occurred registering connection ({0}): retryCount=({1}): {2}", LogEventType.Warning, connectionName, retryCount.ToString(), ex.Message); if (retryCount > 0) { Thread.Sleep(300); RegisterConnection(connectionName, --retryCount); } } }
public static DaoProxyRegistration Register(Assembly assembly) { Type daoType = (from type in assembly.GetTypes() where type.IsSubclassOf(typeof(Dao)) select type).FirstOrDefault(); string connectionName = Dao.ConnectionName(daoType); if (!Registrations.ContainsKey(connectionName)) { lock (_registerLock) { if (!Registrations.ContainsKey(connectionName)) { DaoProxyRegistration registration = new DaoProxyRegistration(assembly); Registrations.Add(connectionName, registration); } } } return(Registrations[connectionName]); }
/// <summary> /// Creates the tables for the specified type /// </summary> /// <param name="type"></param> public static EnsureSchemaStatus EnsureSchema(Type type, Database database = null) { string name = Dao.ConnectionName(type); Database db = database ?? Db.For(type); EnsureSchemaResult result = new EnsureSchemaResult { Database = db, SchemaName = name }; EnsureSchemaStatus status = _ensureSchemaResults.Where(esr => esr.Equals(result)).Select(esr => esr.Status).FirstOrDefault(); if (status != EnsureSchemaStatus.AlreadyDone || status != EnsureSchemaStatus.Success) { _ensureSchemaResults.Add(result); status = db.TryEnsureSchema(type); } else { status = EnsureSchemaStatus.AlreadyDone; } result.Status = status; return(status); }
/// <summary> /// Creates the tables for the specified type and /// associated sibling tables /// </summary> /// <param name="connectionName"></param> public static void EnsureSchema(string connectionName, Database db = null) { if (string.IsNullOrEmpty(connectionName)) { return; } Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly assembly in assemblies) { Type[] types; if (TryGetTypes(assembly, out types)) { foreach (Type type in types) { if (Dao.ConnectionName(type).Equals(connectionName)) { EnsureSchema(type, db); } } } } }
private static void SetInitializerAndConnectionStringResolver(Type daoType) { SetInitializerAndConnectionStringResolver(Dao.ConnectionName(daoType)); }
private StringBuilder GetBodyAndMeta(Incubator incubator) { StringBuilder script = new StringBuilder(); StringBuilder meta = new StringBuilder(); foreach (string className in incubator.ClassNames) { Type modelType = incubator[className]; if (modelType.IsSubclassOf(typeof(Dao))) { StringBuilder parameters; StringBuilder body; string connectionName = Dao.ConnectionName(modelType); meta.AppendLine("\t\td.tables = d.tables || {};"); meta.AppendFormat("\t\td.tables.{0} = {{}};\r\n", className); meta.AppendFormat("\t\td.tables.{0}.keyColumn = '{1}';\r\n", className, Dao.GetKeyColumnName(modelType)); meta.AppendFormat("\t\td.tables.{0}.cols = [];\r\n", className); meta.AppendFormat("\t\td.tables.{0}.ctx = '{1}';\r\n\r\n", className, connectionName); GetCtorParamsAndBody(modelType, meta, connectionName, out parameters, out body); script.AppendFormat("\t\td.ctors.{0} = function {0}(", className); // -- params script.Append(parameters.ToString()); // -- end params script.AppendLine("){"); // writing meta data PropertyInfo[] modelProps = modelType.GetProperties(); foreach (PropertyInfo prop in modelProps) { ColumnAttribute col; if (prop.HasCustomAttributeOfType <ColumnAttribute>(out col)) { string typeName = prop.PropertyType.Name; if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { typeName = prop.PropertyType.GetGenericArguments()[0].Name; } meta.AppendFormat("\t\td.tables.{0}.cols.push({{name: '{1}', type: '{2}', nullable: {3} }});\r\n", className, col.Name, typeName, col.AllowNull ? "true" : "false"); } ForeignKeyAttribute fk; if (prop.HasCustomAttributeOfType <ForeignKeyAttribute>(out fk)) { meta.AppendFormat("\t\td.fks.push({{ pk: '{0}', pt: '{1}', fk: '{2}', ft: '{3}', nullable: {4} }});\r\n", fk.ReferencedKey, fk.ReferencedTable, fk.Name, fk.Table, fk.AllowNull ? "true" : "false"); } } // -- body script.Append(body.ToString()); // -- end body script.AppendLine("\t\t}"); // -- end writing meta data } } script.AppendLine(meta.ToString()); return(script); }
/// <summary> /// Set and return the Database for the specified type /// </summary> /// <param name="type"></param> /// <param name="database"></param> /// <returns></returns> public static Database For(Type type, Database database) { return(For(Dao.ConnectionName(type), database)); }