コード例 #1
0
ファイル: OrmMapExtension.cs プロジェクト: ciker/simple_orm
        public static string BuildInsertQuery(this OrmMap map, string argValues)
        {
            string insertTemplate = "INSERT INTO {0}({1}) VALUES(" + argValues + ")";
            string argCols        = MapArgColumnList(map);

            return(String.Format(insertTemplate, map.TableName, argCols));
        }
コード例 #2
0
ファイル: OrmMapExtension.cs プロジェクト: ciker/simple_orm
        public static string BuildSelectWhereQuery(this OrmMap map, string whereSection)
        {
            string selectTemplate = "SELECT {0} FROM {1} WHERE " + whereSection;
            string argCols        = MapArgColumnList(map);

            return(String.Format(selectTemplate, argCols, map.TableName));
        }
コード例 #3
0
ファイル: OrmMapExtension.cs プロジェクト: ciker/simple_orm
        public static string BuildSelectAllQuery(this OrmMap map)
        {
            string selectTemplate = "SELECT {0} FROM {1}";
            string argCols        = MapArgColumnList(map);

            return(String.Format(selectTemplate, argCols, map.TableName));
        }
コード例 #4
0
ファイル: OrmMap.cs プロジェクト: ciker/simple_orm
        public static OrmMap FromType(Type type)
        {
            TableAttribute attr = (TableAttribute)type.GetCustomAttributes(typeof(TableAttribute), true).SingleOrDefault();

            if (attr == null)
            {
                throw new Exception("this type have no TableAttribute");
            }
            OrmMap map = new OrmMap();

            map.TableName = attr.TableName;

            foreach (PropertyInfo p in type.GetProperties())
            {
                ColumnAttribute ca = (ColumnAttribute)p.GetCustomAttributes(typeof(ColumnAttribute), true).SingleOrDefault();
                if (ca != null)
                {
                    map[ca.ColumnName] = p;
                    if (ca is IdAttribute)
                    {
                        map.ID = ca.ColumnName;
                    }
                }
            }
            return(map);
        }
コード例 #5
0
ファイル: OrmMapExtension.cs プロジェクト: ciker/simple_orm
        public static string MapArgColumnList(this OrmMap map)
        {
            StringBuilder b = new StringBuilder();

            foreach (string col in map.Columns)
            {
                b.Append(col);
                b.Append(',');
            }
            b.Remove(b.Length - 1, 1);
            return(b.ToString());
        }
コード例 #6
0
ファイル: MyORM.cs プロジェクト: ciker/simple_orm
        public void RegisterType(Type type)
        {
            OrmMap map = null;

            if (!mappingPool.ContainsKey(type))
            {
                map = OrmMap.FromType(type);
                mappingPool[type] = map;
            }
            else
            {
                map = mappingPool[type];
            }
        }
コード例 #7
0
ファイル: MyORM.cs プロジェクト: ciker/simple_orm
        public ICollection <T> SelectAll <T>() where T : class, new()
        {
            OrmMap map         = mappingPool[typeof(T)];
            string selectQuery = map.BuildSelectAllQuery();

            using (DbConnection connection = GetOpenConnection())
            {
                DbCommand command = connection.CreateCommand();
                command.CommandText = map.BuildSelectAllQuery();

                DbDataReader    reader  = command.ExecuteReader();
                DbReaderAdapter adapter = new DbReaderAdapter(reader, map);
                return(adapter.GetMultipleResult <T>());
            }
        }
コード例 #8
0
ファイル: MyORM.cs プロジェクト: ciker/simple_orm
        public int Delete <T>(object id) where T : class, new()
        {
            OrmMap map            = mappingPool[typeof(T)];
            string whereStatement = String.Format("{0}=@id", map.ID);

            string deleteQuery = map.BuildDeleteQuery(whereStatement);

            using (DbConnection connection = GetOpenConnection())
            {
                DbCommand command = connection.CreateCommand();
                command.CommandText = deleteQuery;

                DbParameter param = command.CreateParameter();
                param.DbType        = map.GetDbType(map.ID);
                param.ParameterName = "@id";
                param.Value         = id;
                command.Parameters.Add(param);

                return(command.ExecuteNonQuery());
            }
        }
コード例 #9
0
ファイル: MyORM.cs プロジェクト: ciker/simple_orm
        public int Insert(object o)
        {
            Type          type           = o.GetType();
            OrmMap        map            = mappingPool[type];
            StringBuilder argListBuilder = new StringBuilder();
            List <KeyValuePair <string, string> > colNameToNamedParam = new List <KeyValuePair <string, string> >();

            foreach (string col in map.Columns)
            {
                string namedParam = '@' + col;
                colNameToNamedParam.Add(new KeyValuePair <string, string>(col, namedParam));
                argListBuilder.Append(namedParam);
                argListBuilder.Append(',');
            }
            argListBuilder.Remove(argListBuilder.Length - 1, 1);

            string insertQuery = map.BuildInsertQuery(argListBuilder.ToString());

            using (DbConnection connection = GetOpenConnection())
            {
                DbCommand command = connection.CreateCommand();
                command.CommandText = insertQuery;
                foreach (var pair in colNameToNamedParam)
                {
                    DbParameter param = command.CreateParameter();
                    param.DbType        = map.GetDbType(pair.Key);
                    param.ParameterName = pair.Value;
                    param.Value         = map[pair.Key].GetValue(o) ?? DBNull.Value;
                    command.Parameters.Add(param);
                }
                try
                {
                    return(command.ExecuteNonQuery());
                }
                catch (Exception e)
                {
                    return(0);
                }
            }
        }
コード例 #10
0
ファイル: MyORM.cs プロジェクト: ciker/simple_orm
        public T SelectById <T>(object id) where T : class, new()
        {
            OrmMap map            = mappingPool[typeof(T)];
            string whereStatement = String.Format("{0}=@id", map.ID);
            string selectQuery    = map.BuildSelectWhereQuery(whereStatement);

            using (DbConnection connection = GetOpenConnection())
            {
                DbCommand command = connection.CreateCommand();
                command.CommandText = selectQuery;

                DbParameter param = command.CreateParameter();
                param.DbType        = map.GetDbType(map.ID);
                param.ParameterName = "@id";
                param.Value         = id;
                command.Parameters.Add(param);

                DbDataReader    reader  = command.ExecuteReader();
                DbReaderAdapter adapter = new DbReaderAdapter(reader, map);
                return(adapter.GetSingleResult <T>());
            }
        }
コード例 #11
0
ファイル: OrmMap.cs プロジェクト: ciker/simple_orm
        public static OrmMap FromType(Type type)
        {
            TableAttribute attr = (TableAttribute)type.GetCustomAttributes(typeof(TableAttribute), true).SingleOrDefault();
            if (attr == null)
            {
                throw new Exception("this type have no TableAttribute");
            }
            OrmMap map = new OrmMap();
            map.TableName = attr.TableName;

            foreach (PropertyInfo p in type.GetProperties())
            {
                ColumnAttribute ca = (ColumnAttribute)p.GetCustomAttributes(typeof(ColumnAttribute), true).SingleOrDefault();
                if (ca != null)
                {
                    map[ca.ColumnName] = p;
                    if (ca is IdAttribute)
                    {
                        map.ID = ca.ColumnName;
                    }
                }
            }
            return map;
        }
コード例 #12
0
ファイル: OrmMapExtension.cs プロジェクト: ciker/simple_orm
        public static string BuildDeleteQuery(this OrmMap map, string whereSection)
        {
            string deleteTemplate = "DELETE FROM {0} WHERE " + whereSection;

            return(String.Format(deleteTemplate, map.TableName));
        }
コード例 #13
0
 public DbReaderAdapter(DbDataReader reader, OrmMap map)
 {
     this.reader = reader;
     this.map    = map;
 }