internal string CreateSelectByIDStatement(BaseObject baseObject, Guid id) { bool joinForInheritance = false; foreach (object attribute in baseObject.GetType().GetCustomAttributes(typeof(Attributes.Inheritance), false)) { Attributes.Inheritance inheritanceAttribute = (Attributes.Inheritance)attribute; if (inheritanceAttribute.InheritanceType == HJORM.Attributes.InheritanceEnum.TwoTables) { joinForInheritance = true; } } if (joinForInheritance) { return(CreateSelectJoinStatement(baseObject, id)); } else { return(CreateSelectStatement(baseObject.TableName, "ID = '" + id.ToString() + "'", "", 0, 0)); } }
internal string CreateUpdateStatement(BaseObject baseObject) { string sql = ""; Type type = baseObject.GetType(); bool twoTables = false; //1 op 1 relatie in het geval van inheritance bool parentTableInsert = false; foreach (object attribute in type.GetCustomAttributes(typeof(Attributes.Inheritance), false)) { Attributes.Inheritance inheritanceAttribute = (Attributes.Inheritance)attribute; if (inheritanceAttribute.InheritanceType == HJORM.Attributes.InheritanceEnum.TwoTables) { twoTables = true; } else if (inheritanceAttribute.InheritanceType == HJORM.Attributes.InheritanceEnum.ParentTable) { parentTableInsert = true; } } if (twoTables) { //elke in eigen tabel opslaan //dus twee keer een insert doen //base table string baseTableName = type.BaseType.Name; foreach (object persitentAttribute in type.BaseType.GetCustomAttributes(typeof(Attributes.Persistent), false)) { baseTableName = ((Attributes.Persistent)persitentAttribute).DataBaseObject; } string sqlBase = "UPDATE " + baseTableName + " SET "; foreach (PropertyInfo prop in type.BaseType.GetProperties()) { string fieldName; bool useProperty = AnalyseProperty(prop, out fieldName); if (useProperty) { sqlBase += fieldName + " = "; sqlBase += getSqlValue(prop, baseObject); } } sqlBase = sqlBase.Substring(0, sqlBase.Length - 2); sqlBase += " WHERE ID = '" + baseObject.ID + "'"; //id van base en sub zijn gelijk sqlBase += "; "; //child table string sqlSub = "UPDATE " + baseObject.TableName + " SET "; foreach (PropertyInfo prop in type.GetProperties()) { //allen de props als de basetype ze niet heeft if (type.BaseType.GetProperty(prop.Name) == null) { string fieldName; bool useProperty = AnalyseProperty(prop, out fieldName); if (useProperty) { sqlSub += fieldName + " = "; sqlSub += getSqlValue(prop, baseObject); } } } sqlSub = sqlSub.Substring(0, sqlSub.Length - 2); sqlSub += " WHERE ID = '" + baseObject.ID + "'"; sql = sqlBase + sqlSub; } else { sql = "UPDATE " + baseObject.TableName + " SET "; foreach (PropertyInfo prop in type.GetProperties()) { string fieldName; bool useProperty = AnalyseProperty(prop, out fieldName); if (useProperty) { sql += fieldName + " = "; sql += getSqlValue(prop, baseObject); } } sql = sql.Substring(0, sql.Length - 2); sql += " WHERE ID = '" + baseObject.ID + "'"; } return(sql); }
internal virtual string CreateInsertStatement(BaseObject baseObject) { string sql = ""; Type type = baseObject.GetType(); bool twoTableInsert = false; //1 op 1 relatie in het geval van inheritance bool parentTableInsert = false; foreach (object attribute in type.GetCustomAttributes(typeof(Attributes.Inheritance), false)) { Attributes.Inheritance inheritanceAttribute = (Attributes.Inheritance)attribute; if (inheritanceAttribute.InheritanceType == HJORM.Attributes.InheritanceEnum.TwoTables) { twoTableInsert = true; } else if (inheritanceAttribute.InheritanceType == HJORM.Attributes.InheritanceEnum.ParentTable) { parentTableInsert = true; } } if (twoTableInsert) { //elke in eigen tabel opslaan //dus twee keer een insert doen //base table string baseTableName = type.BaseType.Name; foreach (object persitentAttribute in type.BaseType.GetCustomAttributes(typeof(Attributes.Persistent), false)) { baseTableName = ((Attributes.Persistent)persitentAttribute).DataBaseObject; } string sqlBase = "INSERT INTO " + baseTableName + " SET "; foreach (PropertyInfo prop in type.BaseType.GetProperties()) { string fieldName; bool useProperty = AnalyseProperty(prop, out fieldName); if (useProperty) { sqlBase += fieldName + " = "; sqlBase += getSqlValue(prop, baseObject); } } sqlBase = sqlBase.Substring(0, sqlBase.Length - 2); sqlBase += "; "; //child table string sqlSub = "INSERT INTO " + baseObject.TableName + " SET ID = '" + baseObject.ID.ToString() + "', "; foreach (PropertyInfo prop in type.GetProperties()) { //alleen de props als de basetype ze niet heeft, behalve voor site if (type.BaseType.GetProperty(prop.Name) == null || prop.Name == "Site") { string fieldName; bool useProperty = AnalyseProperty(prop, out fieldName); if (useProperty) { sqlSub += fieldName + " = "; sqlSub += getSqlValue(prop, baseObject); } } } sqlSub = sqlSub.Substring(0, sqlSub.Length - 2); sql = sqlBase + sqlSub + "; select last_insert_id();"; } else { string tablename = baseObject.TableName; foreach (object persitentAttribute in type.GetCustomAttributes(typeof(Attributes.Persistent), false)) { tablename = ((Attributes.Persistent)persitentAttribute).DataBaseObject; } if (parentTableInsert) { tablename = type.BaseType.Name; foreach (object persitentAttribute in type.BaseType.GetCustomAttributes(typeof(Attributes.Persistent), false)) { tablename = ((Attributes.Persistent)persitentAttribute).DataBaseObject; } } //geldt voor mySQL, gedaan omdat de volgorde van de eerste keer props (fieldnames) en de tweede keer (values) verschilde sql = "INSERT INTO " + tablename + " SET "; foreach (PropertyInfo prop in type.GetProperties()) { string fieldName; bool useProperty = AnalyseProperty(prop, out fieldName); if (useProperty) { sql += fieldName + " = "; sql += getSqlValue(prop, baseObject); } } sql = sql.Substring(0, sql.Length - 2); sql += "; select last_insert_id();"; } return(sql); }