Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup #10

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
267 changes: 151 additions & 116 deletions Assets/Output/SQLMaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,122 +2,157 @@
using System.Text;
using System.Collections.Generic;


namespace UnityORM
{
public class SQLMaker
{

public readonly static DateTime UnixTime = new DateTime(1970,1,1);

public SQLMaker ()
{
}

public string GenerateCreateTableSQL<T>(ClassDesc desc){

var builder = new StringBuilder();
builder.Append("CREATE TABLE IF NOT EXISTS " + desc.Name + "(");
foreach(var f in desc.FieldDescs){
builder.Append(f.NameInTable + " " + ConvertToSQLType(f.FieldType ));
if(f == desc.KeyField){
builder.Append(" PRIMARY KEY");
}
if( desc.AutoIncrement){
builder.Append(" AUTOINCREMENT");
}
builder.Append(",");
}
builder.Remove(builder.Length - 1,1);
builder.Append(");");
return builder.ToString();
}
string ConvertToSQLType(Type t){
if(t == typeof(int) ||
t == typeof(long)){
return "INTEGER";
}else if(t == typeof(double)){
return "FLOAT";
}else if(t == typeof(byte[])){
return "BLOB";
}else if(t == typeof(DateTime)){
return "INTEGER";
}else{
return "TEXT";
}
}

public string GenerateSelectAllSQL<T>(ClassDesc desc){
return "SELECT * FROM " + desc.Name + ";";
}

public string GenerateSelectSQL<T>(ClassDesc desc,object key){
if(desc.KeyField == null) throw new Exception("Class " + desc.Name + " hasn't key field");
return "SELECT * FROM " + desc.Name + " WHERE " + desc.KeyField.NameInTable + " = " + ValueToBlock(key) + ";";
}

public string GenerateDeleteAllSQL<T>(ClassDesc desc){
return "DELETE FROM " + desc.Name + ";";
}
public string GenerateDeleteSQL<T>(ClassDesc desc,object key){
if(desc.KeyField == null) throw new Exception("Class " + desc.Name + " hasn't key field");
return "DELETE FROM " + desc.Name + " WHERE " + desc.KeyField.NameInTable + " = " + ValueToBlock(key) + ";";
}

public string GenerateInsertSQL<T>(ClassDesc desc,T obj){
var builder = new StringBuilder();
builder.Append("INSERT INTO " + desc.Name + " (");

var fields = desc.FieldDescs;
if(desc.AutoIncrement){
fields = new List<UnityORM.FieldDesc>(fields);
fields.Remove(desc.KeyField);
}

foreach( var f in fields){
builder.Append(f.NameInTable + ",");
}
builder.Remove(builder.Length - 1,1);
builder.Append(") VALUES (");

foreach( var f in fields){
object v = f.GetForDb(obj);
builder.Append(ValueToBlock(v) + ",");
}
builder.Remove(builder.Length - 1,1);
builder.Append(");");

return builder.ToString();
}

public string GenerateUpdateSQL<T>(ClassDesc desc,T obj){
if(desc.KeyField == null) throw new Exception("Class " + desc.Name + " hasn't key field");
var builder = new StringBuilder();
builder.Append("UPDATE " + desc.Name + " SET ");
foreach(var f in desc.FieldDescs){
object v = f.GetForDb(obj);
builder.Append(f.NameInTable + "=" + ValueToBlock(v) + ",");
}
builder.Remove(builder.Length -1 ,1);
builder.Append(" WHERE " + desc.KeyField.NameInTable + " = " + ValueToBlock(desc.KeyField.GetForDb(obj)) + ";");

return builder.ToString();
}


string ValueToBlock(object v){
if(v == null){
return "NULL";
}else{
return "'" + Escape(v.ToString()) + "'";
}
}

public string Escape(string str){
return str.Replace("'","''");
}


}
}
public class SQLMaker
{
public readonly static DateTime UnixTime = new DateTime (1970, 1, 1);

public SQLMaker ()
{
}

public string GenerateCreateTableSQL<T> (ClassDesc desc)
{
var builder = new StringBuilder ();
builder.Append ("CREATE TABLE IF NOT EXISTS " + desc.Name + "(");
foreach (var f in desc.FieldDescs)
{
builder.Append (f.nameInTable + " " + ConvertToSQLType (f.FieldType));
if (f == desc.KeyField)
{
builder.Append (" PRIMARY KEY");
}
if (desc.AutoIncrement)
{
builder.Append (" AUTOINCREMENT");
}
builder.Append (",");
}
builder.Remove (builder.Length - 1, 1);
builder.Append (");");
return builder.ToString ();
}

private string ConvertToSQLType (Type t)
{
if (t == typeof(int) ||
t == typeof(long))
{
return "INTEGER";
}
else if (t == typeof(double))
{
return "FLOAT";
}
else if (t == typeof(byte[]))
{
return "BLOB";
}
else if (t == typeof(DateTime))
{
return "INTEGER";
}
else
{
return "TEXT";
}
}

public string GenerateSelectAllSQL<T> (ClassDesc desc)
{
return "SELECT * FROM " + desc.Name + ";";
}

public string GenerateSelectSQL<T> (ClassDesc desc, object key)
{
if (desc.KeyField == null)
{
throw new Exception ("Class " + desc.Name + " hasn't key field");
}
return "SELECT * FROM " + desc.Name + " WHERE " + desc.KeyField.nameInTable + " = " + ValueToBlock (key) + ";";
}

public string GenerateDeleteAllSQL<T> (ClassDesc desc)
{
return "DELETE FROM " + desc.Name + ";";
}

public string GenerateDeleteSQL<T> (ClassDesc desc, object key)
{
if (desc.KeyField == null)
{
throw new Exception ("Class " + desc.Name + " hasn't key field");
}
return "DELETE FROM " + desc.Name + " WHERE " + desc.KeyField.nameInTable + " = " + ValueToBlock (key) + ";";
}

public string GenerateInsertSQL<T> (ClassDesc desc, T obj)
{
var columnsBuilder = new StringBuilder ();
var valuesBuilder = new StringBuilder ();
columnsBuilder.Append ("INSERT INTO " + desc.Name + " (");

var fields = desc.FieldDescs;
if (desc.AutoIncrement)
{
fields = new List<UnityORM.FieldDesc> (fields);
fields.Remove (desc.KeyField);
}

foreach (var f in fields)
{
// columns
columnsBuilder.Append (f.nameInTable + ",");
// values
object v = f.GetForDb (obj);
valuesBuilder.Append (ValueToBlock (v) + ",");
}

columnsBuilder.Remove (columnsBuilder.Length - 1, 1);
columnsBuilder.Append (") VALUES (");

valuesBuilder.Remove (valuesBuilder.Length - 1, 1);
valuesBuilder.Append (");");
columnsBuilder.Append (valuesBuilder);

return columnsBuilder.ToString ();
}

public string GenerateUpdateSQL<T> (ClassDesc desc, T obj)
{
if (desc.KeyField == null)
{
throw new Exception ("Class " + desc.Name + " hasn't key field");
}
var builder = new StringBuilder ();
builder.Append ("UPDATE " + desc.Name + " SET ");
foreach (var f in desc.FieldDescs)
{
object v = f.GetForDb (obj);
builder.Append (f.nameInTable + "=" + ValueToBlock (v) + ",");
}
builder.Remove (builder.Length - 1, 1);
builder.Append (" WHERE " + desc.KeyField.nameInTable + " = " + ValueToBlock (desc.KeyField.GetForDb (obj)) + ";");

return builder.ToString ();
}

private string ValueToBlock (object v)
{
if (v == null)
{
return "NULL";
}
else
{
return "'" + Escape (v.ToString ()) + "'";
}
}

private string Escape (string str)
{
return str.Replace ("'", "''");
}
}
}
Loading