Example #1
0
        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());
        }
Example #2
0
        public int SetTo <T>(DataTable result, int resultOffset, ClassDesc desc, T[] objects, int offset, int length)
        {
            int readCount = Math.Min(result.Rows.Count - resultOffset, length);

            if (length < 0)
            {
                readCount = result.Rows.Count - resultOffset;
            }
            for (int i = resultOffset; i < readCount; i++)
            {
                var    row = result.Rows[i + resultOffset];
                object obj = objects[i + offset];

                foreach (var f in desc.FieldDescs)
                {
                    if (row.ContainsKey(f.NameInTable))
                    {
                        f.SetFromDb(obj, row[f.NameInTable]);
                    }
                    else if (row.ContainsKey(f.NameInTable.ToLower()))
                    {
                        f.SetFromDb(obj, row[f.NameInTable.ToLower()]);
                    }
                }
            }
            return(readCount);
        }
Example #3
0
 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) + ";");
 }
Example #4
0
 public static string GenerateSelectSQL <T>(ClassDesc desc, T obj)
 {
     if (desc.KeyField == null)
     {
         throw new Exception("Class " + desc.Name + " hasn't key field");
     }
     return("SELECT * FROM " + desc.Name + " WHERE " + desc.KeyField.NameInTable + " = " + ValueToBlock(desc.KeyField.GetForDb(obj)) + ";");
 }
Example #5
0
        public int ReadTo <T>(T[] objects, int offset, int size, string sql)
        {
            var result = database.ExecuteQuery(sql);

            ClassDesc desc = Registory.GetClassDesc <T>();

            return(ResultReader.SetTo(result, 0, desc, objects, offset, size));
        }
Example #6
0
        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());
        }
Example #7
0
        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());
        }
Example #8
0
 public string GenerateDeleteAllSQL <T>(ClassDesc desc)
 {
     return("DELETE FROM " + desc.Name + ";");
 }
Example #9
0
 public string GenerateSelectAllSQL <T>(ClassDesc desc)
 {
     return("SELECT * FROM " + desc.Name + ";");
 }
Example #10
0
 public ArrayValueConverter(ClassDesc desc)
 {
     this.desc = desc;
 }
Example #11
0
 public ClassValueConverter(ClassDesc desc)
 {
     this.desc = desc;
 }
        public virtual ClassDesc ListUp(Type t)
        {
            ClassDesc classInfo;            // = new ClassDesc(t);

            if (t.IsArray)
            {
                classInfo = new ClassDesc(t.GetElementType());
                if (!converters.ContainsKey(t))
                {
                    converters.Add(t, new ArrayValueConverter(classInfo));
                }
            }
            else
            {
                classInfo = new ClassDesc(t);
                if (!converters.ContainsKey(t))
                {
                    converters.Add(t, new ClassValueConverter(classInfo));
                }
            }
            classInfo.Name = t.Name;

            bool keyFieldIsSetByAttribute = false;

            List <FieldDesc> infoList = new List <FieldDesc>();
            var props = t.GetProperties();

            foreach (var prop in props)
            {
                if (prop.CanRead && prop.CanWrite)
                {
                    FieldDesc info = new FieldDesc();
                    info.Prop           = prop;
                    info.ValueConverter = GetConverter(info.FieldType);

                    if (info.GetAttribute <IgnoreAttribute>() == null)
                    {
                        SetNames(info);

                        infoList.Add(info);

                        var keyAttribute = info.GetAttribute <KeyAttribute>();
                        if (keyAttribute != null)
                        {
                            classInfo.KeyField       = info;
                            keyFieldIsSetByAttribute = true;
                            classInfo.AutoIncrement  = keyAttribute.AutoIncrement;
                        }
                        else if (info.Name.ToLowerInvariant() == "id" && !keyFieldIsSetByAttribute)
                        {
                            classInfo.KeyField      = info;
                            classInfo.AutoIncrement = false;
                        }
                    }
                }
            }
            var fields = t.GetFields();

            foreach (var f in fields)
            {
                if (f.IsPublic && !f.IsStatic && !f.IsInitOnly)
                {
                    FieldDesc info = new FieldDesc();
                    info.Field          = f;
                    info.ValueConverter = GetConverter(info.FieldType);

                    if (info.GetAttribute <IgnoreAttribute>() == null)
                    {
                        SetNames(info);

                        if (info.GetAttribute <KeyAttribute>() != null)
                        {
                            classInfo.KeyField       = info;
                            keyFieldIsSetByAttribute = true;
                        }
                        else if (info.Name.ToLowerInvariant() == "id" && !keyFieldIsSetByAttribute)
                        {
                            classInfo.KeyField = info;
                        }
                        infoList.Add(info);
                    }
                }
            }

            classInfo.FieldDescs = infoList;

            return(classInfo);
        }
Example #13
0
 public int SetTo <T>(DataTable result, ClassDesc desc, T[] objects)
 {
     return(SetTo <T>(result, 0, desc, objects, 0, objects.Length));
 }
Example #14
0
 public T[] Get <T>(DataTable result, ClassDesc desc)
 {
     T[] objects = ReflectionSupport.CreateNewInstances <T>(result.Rows.Count);
     SetTo(result, 0, desc, objects, 0, objects.Length);
     return(objects);
 }