public override string GenerateQuery(Type type) { string tableName = TableAttribute.GetAttributeTable(type); StringBuilder stringBuilder = new StringBuilder($"DELETE FROM {tableName}"); if (WhereSpecifiers.Count > 0) { stringBuilder.Append(" WHERE "); List <string> wheres = new List <string>(); foreach (var specifier in WhereSpecifiers) { PropertyInfo propertyInfo = type.GetProperty(specifier.Key); if (propertyInfo == null) { throw new Exception("Tried to select a non-existing column!"); } if (!SerializerMap.Serializers.TryGetValue(propertyInfo.PropertyType, out var serializer)) { throw new Exception("VoidORM can't serialize the type " + propertyInfo.PropertyType.Name + "!"); } string strValue = serializer.Serialize(specifier.Value, Database.DatabaseDriver); wheres.Add("`" + specifier.Key + "` " + specifier.Comparator + " " + strValue); } stringBuilder.Append(string.Join(" AND ", wheres)); } stringBuilder.Append(";"); return(stringBuilder.ToString()); }
public override string GenerateQuery(Type type) { string tableName = TableAttribute.GetAttributeTable(type); StringBuilder stringBuilder = new StringBuilder($"ALTER TABLE {tableName} "); List <string> columnStrings = new List <string>(); foreach (var specifier in AddColumns) { columnStrings.Add($"ADD COLUMN `{specifier.Name}` {specifier.DataType}"); } stringBuilder.Append(string.Join(", ", columnStrings) + ";"); return(stringBuilder.ToString()); }
public override string GenerateQuery(Type type) { string tableName = TableAttribute.GetAttributeTable(type); StringBuilder stringBuilder = new StringBuilder($"UPDATE {tableName} SET "); List <string> updateStatements = new List <string>(); List <string> primaryKeys = new List <string>(); foreach (var specifier in InsertSpecifiers) { PropertyInfo propertyInfo = type.GetProperty(specifier.Key); if (propertyInfo == null) { throw new Exception("Tried to select a non-existing column!"); } if (!SerializerMap.Serializers.TryGetValue(propertyInfo.PropertyType, out var serializer)) { throw new Exception("VoidORM can't serialize the type " + propertyInfo.PropertyType.Name + "!"); } string strValue = serializer.Serialize(specifier.Value, Database.DatabaseDriver); bool isPrimaryKey = PrimaryKeyAttribute.IsPrimaryKey(propertyInfo); if (isPrimaryKey) { primaryKeys.Add("`" + specifier.Key + "` = " + strValue); } else { updateStatements.Add("`" + specifier.Key + "` = " + strValue); } } stringBuilder.Append(string.Join(", ", updateStatements)); stringBuilder.Append(" WHERE " + string.Join(" AND ", primaryKeys)); stringBuilder.Append(";"); return(stringBuilder.ToString()); }
public override string GenerateQuery(Type type) { string tableName = TableAttribute.GetAttributeTable(type); string insertType = ShouldReplace ? "REPLACE" : "INSERT"; StringBuilder stringBuilder = new StringBuilder($"{insertType} INTO {tableName} ("); List <string> columnNames = new List <string>(); List <string> values = new List <string>(); foreach (var specifier in InsertSpecifiers) { PropertyInfo propertyInfo = type.GetProperty(specifier.Key); if (propertyInfo == null) { throw new Exception("Tried to select a non-existing column!"); } columnNames.Add("`" + specifier.Key + "`"); if (!SerializerMap.Serializers.TryGetValue(propertyInfo.PropertyType, out var serializer)) { throw new Exception("VoidORM can't serialize the type " + propertyInfo.PropertyType.Name + "!"); } string strValue = serializer.Serialize(specifier.Value, Database.DatabaseDriver); values.Add(strValue); } stringBuilder.Append(string.Join(", ", columnNames)); stringBuilder.Append(")"); stringBuilder.Append(" VALUES ("); stringBuilder.Append(string.Join(", ", values)); stringBuilder.Append(");"); return(stringBuilder.ToString()); }
public override string GenerateQuery(Type type) { string tableName = TableAttribute.GetAttributeTable(type); StringBuilder queryBuilder = new StringBuilder($"CREATE TABLE IF NOT EXISTS `{tableName}` ("); List <string> tableFields = new List <string>(); List <string> primaryKeys = new List <string>(); var properties = type.GetProperties(); foreach (var property in properties) { string propertyTypeName = property.PropertyType.Name; string propertyName = property.Name; string sqlType = null; try { sqlType = CSharpTypesToSqlTypesDictionary[propertyTypeName]; } catch (Exception) { throw new NotSupportedException("The C# type " + propertyTypeName + " is not supported for C# to SQL conversion."); } string dataType = sqlType; int?columnLength = ColumnLengthAttribute.GetAttributeColumnLength(property); if (columnLength != null || propertyTypeName == "String") { dataType = string.Format(dataType, columnLength ?? 50); } bool isAutoIncrement = AutoIncrementAttribute.IsAutoIncrement(property); if (isAutoIncrement) { if (Database.DatabaseDriver is MySQLoo) { dataType += " AUTO_INCREMENT"; } } bool isPrimaryKey = PrimaryKeyAttribute.IsPrimaryKey(property); if (isPrimaryKey) { // Check if is SQLite/MySQL if (Database.DatabaseDriver is MySQLoo) { primaryKeys.Add(propertyName); } else if (Database.DatabaseDriver is SQLite) { dataType += " PRIMARY KEY"; } } tableFields.Add($"{propertyName} {dataType}"); } queryBuilder.Append(string.Join(", ", tableFields)); if (primaryKeys.Count > 0) { queryBuilder.Append(", PRIMARY KEY ("); queryBuilder.Append(string.Join(", ", primaryKeys)); queryBuilder.Append(")"); } queryBuilder.Append(");"); return(queryBuilder.ToString()); }
public override string GenerateQuery(Type type) { string tableName = TableAttribute.GetAttributeTable(type); return("DROP TABLE " + tableName + ";"); }