Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace EntityToSQLNS
{
Public enum SQLType
{
Insert,
Delete,
Update,
Select
}
Public interface IEntity
{
}
[AttributeUsage (AttributeTargets. Class, AllowMultiple = false, Inherited = true)]
Public class DTableAttribute: Attribute
{
Public string Name
{
Get;
Set;
}
}
[AttributeUsage (AttributeTargets. Property, AllowMultiple = false, Inherited = true)]
Public class DFiledAttribute: Attribute
{
Public string Name
{
Get;
Set;
}
Public Type FieldType
{
Get;
Set;
}
Public bool Key
{
Get;
Set;
}
}
}
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Reflection;
Using EntityToSQLNS;
Namespace SQLCreate
{
Enum SQLType
{
Insert,
Delete,
Update,
Select
}
Class Program
{
Static void Main (string [] args)
{
User user = new User ();
User. Number = 1;
User. StuNumber = "1234567890 ";
User. PassWord = "123456 ";
Ann ann = new Ann ();
Ann. Number = 3;
Ann. Title = "notification ";
Ann. Content = "No holidays ";
GetSQL gsql = new GetSQL ();
Console. WriteLine (gsql. CreateSQL (ann, SQLType. Select ));
GetSQL <IEntity> gsqlg = new GetSQL <IEntity> ();
Console. WriteLine (gsqlg. CreateSQL (user, SQLType. Insert ));
}
}
Class GetSQL
{
Public string CreateSQL (IEntity entity, SQLType sqlType)
{
String SQL = "";
Switch (sqlType)
{
Case SQLType. Select:
SQL = SelectSQL (entity );
Break;
Case SQLType. Insert:
SQL = InsertSQL (entity );
Break;
Case SQLType. Delete:
SQL = DeleteSQL (entity );
Break;
Case SQLType. Update:
SQL = UpdateSQL (entity );
Break;
}
Return SQL;
}
String InsertSQL (IEntity entity)
{
Type TableType = entity. GetType ();
String TableName = (DTableAttribute) (TableType. GetCustomAttributes (false) [0]). Name;
PropertyInfo [] ProArr = TableType. GetProperties ();
String values = "";
Foreach (PropertyInfo pi in ProArr)
{
Values + = (dfiledattries) (pi. getCustomAttributes (false) [0]). name + "= '" + pi. getValue (entity, null) + "',";
}
Values = values. TrimEnd (',');
Return string. Format ("insert into {0} values ({1})", TableName, values );
}
String SelectSQL (IEntity entity)
{
Type TableType = entity. GetType ();
String TableName = (DTableAttribute) (TableType. GetCustomAttributes (false) [0]). Name;
PropertyInfo [] ProArr = TableType. GetProperties ();
String values = "";
Foreach (PropertyInfo pi in ProArr)
{
DFiledAttribute DF = (dfiledattrites) (pi. GetCustomAttributes (false) [0]);
Values + = DF. Name + ",";
}
Values = values. TrimEnd (',');
Return string. Format ("select {0} from {1}", values, TableName );
}
String DeleteSQL (IEntity entity)
{
Type TableType = entity. GetType ();
String TableName = (DTableAttribute) (TableType. GetCustomAttributes (false) [0]). Name;
PropertyInfo [] ProArr = TableType. GetProperties ();
String condition = "";
Foreach (PropertyInfo pi in ProArr)
{
DFiledAttribute DF = (dfiledattrites) (pi. GetCustomAttributes (false) [0]);
If (DF. Key)
{
Condition = "where" + DF. Name + "= '" + pi. GetValue (entity, null) + "'";
}
}
Return string. Format ("delete {0} {1}", TableName, condition );
}
String UpdateSQL (IEntity entity)
{
Type TableType = entity. GetType ();
String TableName = (DTableAttribute) (TableType. GetCustomAttributes (false) [0]). Name;
PropertyInfo [] ProArr = TableType. GetProperties ();
String values = "";
Foreach (PropertyInfo pi in ProArr)
{
DFiledAttribute DF = (dfiledattrites) (pi. GetCustomAttributes (false) [0]);
Values + = DF. Name + "= '" + pi. GetValue (entity, null) + "',";
}
Values = values. TrimEnd (',');
Return string. Format ("update {0} set {1}", TableName, values );
}
}
Class GetSQL <T>
{
Public string CreateSQL (T entity, SQLType sqlType)
{
String SQL = "";
Switch (sqlType)
{
Case SQLType. Select:
SQL = SelectSQL (entity );
Break;
Case SQLType. Insert:
SQL = InsertSQL (entity );
Break;
Case SQLType. Delete:
SQL = DeleteSQL (entity );
Break;
Case SQLType. Update:
SQL = UpdateSQL (entity );
Break;
}
Return SQL;
}
String InsertSQL (T entity)
{
Type TableType = entity. GetType ();
String TableName = (DTableAttribute) (TableType. GetCustomAttributes (false) [0]). Name;
PropertyInfo [] ProArr = TableType. GetProperties ();
String values = "";
Foreach (PropertyInfo pi in ProArr)
{
Values + = (dfiledattries) (pi. getCustomAttributes (false) [0]). name + "= '" + pi. getValue (entity, null) + "',";
}
Values = values. TrimEnd (',');
Return string. Format ("insert into {0} values ({1})", TableName, values );
}
String SelectSQL (T entity)
{
Type TableType = entity. GetType ();
String TableName = (DTableAttribute) (TableType. GetCustomAttributes (false) [0]). Name;
PropertyInfo [] ProArr = TableType. GetProperties ();
String values = "";
Foreach (PropertyInfo pi in ProArr)
{
DFiledAttribute DF = (dfiledattrites) (pi. GetCustomAttributes (false) [0]);
Values + = DF. Name + ",";
}
Values = values. TrimEnd (',');
Return string. Format ("select {0} from {1}", values, TableName );
}
String DeleteSQL (T entity)
{
Type TableType = entity. GetType ();
String TableName = (DTableAttribute) (TableType. GetCustomAttributes (false) [0]). Name;
PropertyInfo [] ProArr = TableType. GetProperties ();
String condition = "";
Foreach (PropertyInfo pi in ProArr)
{
DFiledAttribute DF = (dfiledattrites) (pi. GetCustomAttributes (false) [0]);
If (DF. Key)
{
Condition = "where" + DF. Name + "= '" + pi. GetValue (entity, null) + "'";
}
}
Return string. Format ("delete {0} {1}", TableName, condition );
}
String UpdateSQL (T entity)
{
Type TableType = entity. GetType ();
String TableName = (DTableAttribute) (TableType. GetCustomAttributes (false) [0]). Name;
PropertyInfo [] ProArr = TableType. GetProperties ();
String values = "";
Foreach (PropertyInfo pi in ProArr)
{
DFiledAttribute DF = (dfiledattrites) (pi. GetCustomAttributes (false) [0]);
Values + = DF. Name + "= '" + pi. GetValue (entity, null) + "',";
}
Values = values. TrimEnd (',');
Return string. Format ("update {0} set {1}", TableName, values );
}
}
Interface IEntity
{
}
[DTable (Name = "StuUsers")]
Class User: IEntity
{
[DFiled (Name = "ID", FieldType = typeof (int), Key = true)]
Public int Number
{
Get;
Set;
}
[DFiled (Name = "StuNum", FieldType = typeof (string), Key = false)]
Public string StuNumber
{
Get;
Set;
}
[DFiled (Name = "Passwd", FieldType = typeof (string), Key = false)]
Public string PassWord
{
Get;
Set;
}
}
[DTable (Name = "Announcement")]
Class Ann: IEntity
{
[DFiled (Name = "ID", FieldType = typeof (int), Key = true)]
Public int Number
{
Get;
Set;
}
[DFiled (Name = "PubTitle", FieldType = typeof (string), Key = false)]
Public string Title
{
Get;
Set;
}
[DFiled (Name = "PubCon", FieldType = typeof (string), Key = false)]
Public string Content
{
Get;
Set;
}
}
}