public DbParameter[] GetParameters(SqlStringBuilder sqlStrinbuilder) { IParameterBuilder paramBuilder = GetService <IParameterBuilder>(); Args.ThrowIfNull(paramBuilder, "IParameterBuilder"); return(paramBuilder.GetParameters(sqlStrinbuilder)); }
public static DataTable Where(QiQuery query) { Database db = Db.For(query.cxName); SqlStringBuilder sql = new SqlStringBuilder(); sql .Select(query.table, query.columns) .Where(query); IParameterBuilder parameterBuilder = db.ServiceProvider.Get <IParameterBuilder>(); DbParameter[] parameters = parameterBuilder.GetParameters(sql); return(db.GetDataTable(sql, System.Data.CommandType.Text, parameters)); }
private void ForceUpdateIfExistsInTarget(string uuid, Dao dao) { SqlStringBuilder sql = DatabaseToRestoreTo.ServiceProvider.Get <SqlStringBuilder>(); IParameterBuilder parameterBuilder = DatabaseToRestoreTo.ServiceProvider.Get <IParameterBuilder>(); sql.Select(dao.TableName()).Where(Query.Where("Uuid") == uuid); DataTable table = DatabaseToRestoreTo.GetDataTable(sql.ToString(), CommandType.Text, parameterBuilder.GetParameters(sql)); if (table.Rows.Count == 1) { dao.DataRow = table.Rows[0]; dao.ForceUpdate = true; } else if (table.Rows.Count > 1) { Args.Throw <InvalidOperationException>("More than one instance of the specified Uuid exists in the target database: {0}", uuid); } }
protected internal void CorrectForeignKeys(HashSet <OldToNewIdMapping> oldToNewIdMappings, Database source, Database destination) { Dictionary <string, OldToNewIdMapping> byUuid = oldToNewIdMappings.ToDictionary(otn => otn.Uuid); IParameterBuilder oldParameterBuilder = source.GetService <IParameterBuilder>(); SqlStringBuilder committer = destination.GetService <SqlStringBuilder>(); List <Type> tableTypes = DaoAssembly.GetTypes().Where(type => type.HasCustomAttributeOfType <TableAttribute>()).ToList(); List <PropertyInfo> foreignKeyProperties = new List <PropertyInfo>(); foreach (Type type in tableTypes) { foreignKeyProperties.AddRange(type.GetProperties().Where(prop => prop.HasCustomAttributeOfType <ForeignKeyAttribute>()).ToList()); } // for every foreign key foreach (PropertyInfo prop in foreignKeyProperties) { ForeignKeyAttribute fk = prop.GetCustomAttributeOfType <ForeignKeyAttribute>(); // get the old Dao instances that represents the referenced table Type referencedDaoType = DaoTypes.First(t => { TableAttribute table; t.HasCustomAttributeOfType <TableAttribute>(out table); return(table.TableName.Equals(fk.ReferencedTable)); }); SqlStringBuilder oldSelector = source.GetService <SqlStringBuilder>(); oldSelector.Select(fk.ReferencedTable); List <object> oldReferencedDaos = source.GetDataTable(oldSelector, CommandType.Text, oldParameterBuilder.GetParameters(oldSelector)).ToListOf(referencedDaoType); foreach (object oldReferenced in oldReferencedDaos) { Dao oldReferencedDao = (Dao)oldReferenced; // get the old Dao instances that represent the referencing table where the referencing column value = the old table id Type referencingDaoType = DaoTypes.First(t => { TableAttribute table; t.HasCustomAttributeOfType <TableAttribute>(out table); return(table.TableName.Equals(fk.Table)); }); SqlStringBuilder oldReferencerSelector = source.GetService <SqlStringBuilder>(); oldReferencerSelector.Select(fk.Table).Where(new AssignValue(fk.Name, oldReferencedDao.IdValue)); List <object> oldReferencingDaoInstances = source.GetDataTable(oldReferencerSelector, CommandType.Text, oldParameterBuilder.GetParameters(oldReferencerSelector)).ToListOf(referencingDaoType); if (oldReferencingDaoInstances.Count > 0) { List <string> oldReferencingDaoInstanceUuids = oldReferencingDaoInstances.Select(o => o.Property <string>("Uuid")).ToList(); long oldReferencedId = oldReferencedDao.IdValue.Value; // get the new referenced id long whatItShouldBeNow = byUuid[oldReferencedDao.Property <string>("Uuid")].NewId; // update the new referencing column to match the newId in the destination where the uuids in oldDaoInstances committer.Update(fk.Table, new AssignValue(fk.Name, whatItShouldBeNow)).Where(Query.Where("Uuid").In(oldReferencingDaoInstanceUuids.ToArray())); committer.Go(); } } } destination.ExecuteSql(committer, oldParameterBuilder); }
/// <summary> /// Gets the parameters. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="parameters">The parameters.</param> /// <returns></returns> public List <DbParameter> GetParameters <T>(T parameters) { return(_parameterBuilder.GetParameters(parameters)); }
public virtual void ExecuteSql(SqlStringBuilder builder, IParameterBuilder parameterBuilder) { ExecuteSql(builder, CommandType.Text, parameterBuilder.GetParameters(builder)); }