Start on QueryBuilder
Created: - MySql Type Name Map - Added String Extensions
This commit is contained in:
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using MySql.Data.MySqlClient;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
@ -6,6 +7,10 @@ namespace Articulate.Attributes
|
|||||||
{
|
{
|
||||||
public class Column : Attribute
|
public class Column : Attribute
|
||||||
{
|
{
|
||||||
public string Type { get; set; }
|
public MySqlDbType Type { get; set; }
|
||||||
|
|
||||||
|
public int Size { get; set; }
|
||||||
|
|
||||||
|
public bool Primary { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,7 +12,7 @@ namespace Articulate
|
|||||||
public string UserName { get; set; }
|
public string UserName { get; set; }
|
||||||
public string Password { get; set; }
|
public string Password { get; set; }
|
||||||
|
|
||||||
private MySqlConnection Connection { get; set; }
|
public MySqlConnection Connection { get; set; }
|
||||||
|
|
||||||
private static DBConnection _instance = null;
|
private static DBConnection _instance = null;
|
||||||
public static DBConnection Instance()
|
public static DBConnection Instance()
|
||||||
@ -28,14 +28,34 @@ namespace Articulate
|
|||||||
{
|
{
|
||||||
if (String.IsNullOrEmpty(DatabaseName))
|
if (String.IsNullOrEmpty(DatabaseName))
|
||||||
return false;
|
return false;
|
||||||
string connstring = string.Format("Server={0}; database={1}; UID={2}; password={3}", Server, DatabaseName, UserName, Password);
|
string connstring = string.Format("Server={0}; database={1}; UID={2}; password={3}; SSL Mode=none", Server, DatabaseName, UserName, Password);
|
||||||
Connection = new MySqlConnection(connstring);
|
Connection = new MySqlConnection(connstring);
|
||||||
Connection.Open();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SendCommand(MySqlCommand command)
|
||||||
|
{
|
||||||
|
Connection.Open();
|
||||||
|
|
||||||
|
command.ExecuteNonQuery();
|
||||||
|
|
||||||
|
Connection.Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SendQuery(MySqlCommand query)
|
||||||
|
{
|
||||||
|
Connection.Open();
|
||||||
|
|
||||||
|
using (MySqlDataReader reader = query.ExecuteReader())
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Connection.Close();
|
||||||
|
}
|
||||||
|
|
||||||
public void Close()
|
public void Close()
|
||||||
{
|
{
|
||||||
Connection.Close();
|
Connection.Close();
|
||||||
|
|||||||
27
Articulate/Extensions/StringExtension.cs
Normal file
27
Articulate/Extensions/StringExtension.cs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Articulate.Extensions
|
||||||
|
{
|
||||||
|
public static class StringExtension
|
||||||
|
{
|
||||||
|
public static string AddUnderscoresToSentence(this string text, bool preserveAcronyms = false)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(text))
|
||||||
|
return string.Empty;
|
||||||
|
StringBuilder newText = new StringBuilder(text.Length * 2);
|
||||||
|
newText.Append(text[0]);
|
||||||
|
for (int i = 1; i < text.Length; i++)
|
||||||
|
{
|
||||||
|
if (char.IsUpper(text[i]))
|
||||||
|
if ((text[i - 1] != ' ' && !char.IsUpper(text[i - 1])) ||
|
||||||
|
(preserveAcronyms && char.IsUpper(text[i - 1]) &&
|
||||||
|
i < text.Length - 1 && !char.IsUpper(text[i + 1])))
|
||||||
|
newText.Append('_');
|
||||||
|
newText.Append(text[i]);
|
||||||
|
}
|
||||||
|
return newText.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,28 +1,58 @@
|
|||||||
using System;
|
using MySql.Data.MySqlClient;
|
||||||
|
using Pluralize.NET;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace Articulate
|
namespace Articulate
|
||||||
{
|
{
|
||||||
public class Model
|
public abstract class Model
|
||||||
{
|
{
|
||||||
protected string Table = string.Empty;
|
protected virtual string Table => new Pluralizer().Pluralize(this.GetType().Name);
|
||||||
|
|
||||||
protected string PrimaryKey = "id";
|
protected virtual string PrimaryKey => "id";
|
||||||
protected string KeyType = "integer";
|
protected virtual MySqlDbType KeyType => MySqlDbType.Int32;
|
||||||
|
|
||||||
protected bool Incrementing = true;
|
protected bool Incrementing = true;
|
||||||
|
|
||||||
protected bool Timestamps = false;
|
protected bool Timestamps = false;
|
||||||
|
|
||||||
|
public Model()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
public bool Insert()
|
public bool Insert()
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Get()
|
public Model[] Get(params string[][] conditions)
|
||||||
{
|
{
|
||||||
|
string query = QueryBuilder.Select(this, conditions);
|
||||||
|
|
||||||
|
//DBConnection.Instance()
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetTable()
|
||||||
|
{
|
||||||
|
return Table;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetPrimaryKey()
|
||||||
|
{
|
||||||
|
return PrimaryKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MySqlDbType GetKeyType()
|
||||||
|
{
|
||||||
|
return KeyType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Save()
|
||||||
|
{
|
||||||
|
//Insert into database;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
25
Articulate/MysqlTypeNames.cs
Normal file
25
Articulate/MysqlTypeNames.cs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Articulate
|
||||||
|
{
|
||||||
|
public enum MysqlTypeNames
|
||||||
|
{
|
||||||
|
INT = 3,
|
||||||
|
VARCHAR = 253,
|
||||||
|
TEXT = 752,
|
||||||
|
DATE = 10,
|
||||||
|
TINYINT = 1,
|
||||||
|
SMALLINT = 2,
|
||||||
|
BIGINT = 8,
|
||||||
|
FLOAT = 4,
|
||||||
|
DOUBLE = 5,
|
||||||
|
DATETIME = 12,
|
||||||
|
TIMESTAMP = 7,
|
||||||
|
BLOB = 252,
|
||||||
|
MEDIUMBLOB = 250,
|
||||||
|
LONGBLOB = 251
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
170
Articulate/QueryBuilder.cs
Normal file
170
Articulate/QueryBuilder.cs
Normal file
@ -0,0 +1,170 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using Articulate.Attributes;
|
||||||
|
using Articulate.Extensions;
|
||||||
|
using MySql.Data.MySqlClient;
|
||||||
|
|
||||||
|
namespace Articulate
|
||||||
|
{
|
||||||
|
public class QueryBuilder
|
||||||
|
{
|
||||||
|
public static string Select(Model model, params string[][] conditions)
|
||||||
|
{
|
||||||
|
StringBuilder command = new StringBuilder();
|
||||||
|
|
||||||
|
command.Append($"SELECT * FROM `{model.GetTable()}`");
|
||||||
|
|
||||||
|
if (conditions.Length > 0)
|
||||||
|
{
|
||||||
|
command.Append(" WHERE ");
|
||||||
|
|
||||||
|
for (int i = 0; i < conditions.Length; i++)
|
||||||
|
{
|
||||||
|
string[] condition = conditions[i];
|
||||||
|
|
||||||
|
if (i > 0)
|
||||||
|
command.Append(" AND ");
|
||||||
|
|
||||||
|
command.Append(string.Join(" ", condition));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return command.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MySqlCommand InsertCommand(Model model)
|
||||||
|
{
|
||||||
|
using (MySqlCommand cmd = new MySqlCommand("", DBConnection.Instance().Connection))
|
||||||
|
{
|
||||||
|
StringBuilder command = new StringBuilder();
|
||||||
|
|
||||||
|
command.Append($"INSERT INTO `{model.GetTable()}` (");
|
||||||
|
|
||||||
|
PropertyInfo[] columns = model.GetType().GetProperties(BindingFlags.Public |
|
||||||
|
BindingFlags.NonPublic |
|
||||||
|
BindingFlags.Instance).Where(p => Attribute.IsDefined(p, typeof(Column))).ToArray();
|
||||||
|
|
||||||
|
List<string> valRef = new List<string>();
|
||||||
|
|
||||||
|
for (int i = 0; i < columns.Length; i++)
|
||||||
|
{
|
||||||
|
var column = columns[i];
|
||||||
|
|
||||||
|
var name = column.Name.ToLower();
|
||||||
|
var type = column.GetCustomAttribute<Column>().Type;
|
||||||
|
var value = column.GetValue(model);
|
||||||
|
|
||||||
|
cmd.Parameters.Add(new MySqlParameter($"@{name}", type) { Value = value });
|
||||||
|
|
||||||
|
valRef.Add($"@{name}");
|
||||||
|
|
||||||
|
command.Append($"`{name}`");
|
||||||
|
|
||||||
|
if (i + 1 < columns.Length)
|
||||||
|
command.Append(", ");
|
||||||
|
}
|
||||||
|
|
||||||
|
command.Append(") VALUES (");
|
||||||
|
|
||||||
|
for (int i = 0; i < valRef.Count; i++)
|
||||||
|
{
|
||||||
|
var value = valRef[i];
|
||||||
|
|
||||||
|
command.Append($"'{value}'");
|
||||||
|
|
||||||
|
if (i + 1 < valRef.Count)
|
||||||
|
command.Append(", ");
|
||||||
|
}
|
||||||
|
|
||||||
|
command.Append(");");
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string Insert(Model model)
|
||||||
|
{
|
||||||
|
StringBuilder command = new StringBuilder();
|
||||||
|
|
||||||
|
command.Append($"INSERT INTO `{model.GetTable()}` (");
|
||||||
|
|
||||||
|
PropertyInfo[] columns = model.GetType().GetProperties(BindingFlags.Public |
|
||||||
|
BindingFlags.NonPublic |
|
||||||
|
BindingFlags.Instance).Where(p => Attribute.IsDefined(p, typeof(Column))).ToArray();
|
||||||
|
|
||||||
|
List<object> values = new List<object>();
|
||||||
|
|
||||||
|
for (int i = 0; i < columns.Length; i++)
|
||||||
|
{
|
||||||
|
var column = columns[i];
|
||||||
|
|
||||||
|
var name = column.Name.ToLower();
|
||||||
|
var value = column.GetValue(model);
|
||||||
|
|
||||||
|
values.Add(value);
|
||||||
|
|
||||||
|
command.Append($"`{name}`");
|
||||||
|
|
||||||
|
if (i + 1 < columns.Length)
|
||||||
|
command.Append(", ");
|
||||||
|
}
|
||||||
|
|
||||||
|
command.Append(") VALUES (");
|
||||||
|
|
||||||
|
for (int i = 0; i < values.Count; i++)
|
||||||
|
{
|
||||||
|
var value = values[i];
|
||||||
|
|
||||||
|
command.Append($"'{value}'");
|
||||||
|
|
||||||
|
if (i + 1 < values.Count)
|
||||||
|
command.Append(", ");
|
||||||
|
}
|
||||||
|
|
||||||
|
command.Append(");");
|
||||||
|
|
||||||
|
return command.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MySqlCommand CreateTable(Type model)
|
||||||
|
{
|
||||||
|
var modelInstance = (Model)Activator.CreateInstance(model);
|
||||||
|
|
||||||
|
string tableName = modelInstance.GetTable();
|
||||||
|
|
||||||
|
PropertyInfo[] columns = model.GetProperties(BindingFlags.Public |
|
||||||
|
BindingFlags.NonPublic |
|
||||||
|
BindingFlags.Instance).Where(p => Attribute.IsDefined(p, typeof(Column))).ToArray();
|
||||||
|
|
||||||
|
StringBuilder command = new StringBuilder();
|
||||||
|
|
||||||
|
command.Append($"CREATE TABLE `{tableName}` (");
|
||||||
|
|
||||||
|
for (int i = 0; i < columns.Length; i++)
|
||||||
|
{
|
||||||
|
var column = columns[i];
|
||||||
|
Column attr = column.GetCustomAttribute<Column>();
|
||||||
|
|
||||||
|
command.Append($"{column.Name.AddUnderscoresToSentence().ToLower()} ");
|
||||||
|
command.Append($"{(MysqlTypeNames)attr.Type}");
|
||||||
|
|
||||||
|
if (attr.Size > 0)
|
||||||
|
command.Append($"({attr.Size})");
|
||||||
|
|
||||||
|
if (attr.Primary)
|
||||||
|
command.Append(" PRIMARY KEY");
|
||||||
|
|
||||||
|
if (i + 1 < columns.Length)
|
||||||
|
command.Append(", ");
|
||||||
|
}
|
||||||
|
|
||||||
|
command.Append(");");
|
||||||
|
|
||||||
|
return new MySqlCommand(command.ToString(), DBConnection.Instance().Connection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user