コード例 #1
0
ファイル: OrmUtil.cs プロジェクト: nust03/xcore
 internal static void SetParameters(IDbCommand cmd, String action, IEntity obj, EntityInfo entityInfo)
 {
     for (int i = 0; i < entityInfo.SavedPropertyList.Count; i++)
     {
         EntityPropertyInfo info = entityInfo.SavedPropertyList[i];
         if (isContinue(action, info, entityInfo)) continue;
         Object paramVal = obj.get(info.Name);
         if (paramVal == null && info.DefaultAttribute != null)
         {
             paramVal = info.DefaultAttribute.Value;
         }
         if (paramVal == null)
         {
             setDefaultValue(cmd, info, entityInfo);
         }
         else if (info.Type.IsSubclassOf(typeof(IEntity)) || MappingClass.Instance.ClassList.Contains(info.Type.FullName))
         {
             setEntityId(cmd, info, paramVal);
         }
         else
         {
             paramVal = DataFactory.SetParameter(cmd, info.ColumnName, paramVal);
             obj.set(info.Name, paramVal);
         }
     }
 }
コード例 #2
0
ファイル: TableBuilderBase.cs プロジェクト: mfz888/xcore
        private List<String> createTable( EntityInfo entity, IDbCommand cmd, List<String> existTables, IDictionary clsList ) {

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat( "Create Table {0} (", getFullName( entity.TableName, entity ) );
            addColumn_PrimaryKey( entity, sb, clsList );

            addColumns( entity, sb );
            String str = sb.ToString().Trim().TrimEnd( new char[] { ',' } ) + " )";

            cmd.CommandText = str;
            logger.Info( "创建表:" + str );
            if (cmd.Connection == null) throw new Exception( "connection is null" );

            if (cmd.Connection.State == ConnectionState.Closed) {
                cmd.Connection.Open();
                OrmHelper.initCount++;
                LogManager.GetLogger("Class:System.Data.TableBuilderBase Method:createTable").Info("数据库连接已开启【" + OrmHelper.initCount + "】");
            }

            cmd.ExecuteNonQuery();

            existTables.Add( entity.TableName );
            logger.Info( LoggerUtil.SqlPrefix + String.Format( "create table {0} ({1})", entity.TableName, entity.FullName ) );

            return existTables;
        }
コード例 #3
0
ファイル: OrmUtil.cs プロジェクト: nust03/xcore
 private static Boolean isContinue(String action, EntityPropertyInfo info, EntityInfo entityInfo)
 {
     if (info.SaveToDB == false) return true;
     if (info.IsList) return true;
     if (info.Name.Equals("Id"))
     {
         if (action.Equals("update")) return true;
         if (action.Equals("insert") && entityInfo.Parent == null) return true;
     }
     return false;
 }
コード例 #4
0
ファイル: TableBuilderBase.cs プロジェクト: nust03/xcore
 protected virtual void addColumn_ByColumnAttribute( EntityInfo entity, StringBuilder sb, EntityPropertyInfo ep, String columnName )
 {
     if (ep.SaveAttribute.Length < 255) {
         addColumn_ShortText( sb, columnName, ep.SaveAttribute.Length );
     }
     else if ((ep.SaveAttribute.Length > 255) && (ep.SaveAttribute.Length < 4000)) {
         addColumn_MiddleText( entity, sb, ep, columnName );
     }
     else {
         addColumn_LongText( entity, sb, columnName );
     }
 }
コード例 #5
0
ファイル: AccessTableBuilder.cs プロジェクト: mfz888/xcore
        protected override void addColumn_Decimal( EntityInfo entity, StringBuilder sb, EntityPropertyInfo ep, String columnName ) {
            if (ep.MoneyAttribute != null) {
                sb.Append( columnName );
                sb.Append( " currency default 0, " );
            }
            else {

                DecimalAttribute da = ep.DecimalAttribute;
                if (da == null) throw new Exception( "DecimalAttribute not found=" + entity.FullName + "_" + ep.Name );

                sb.Append( columnName );
                sb.Append( " decimal(" + da.Precision + "," + da.Scale + ") default 0, " );
            }
        }
コード例 #6
0
ファイル: TableBuilderBase.cs プロジェクト: mfz888/xcore
 private void addColumns( EntityInfo entity, StringBuilder sb ) {
     for (int i = 0; i < entity.SavedPropertyList.Count; i++)
     {
         EntityPropertyInfo ep = entity.SavedPropertyList[i];
         String columnName = getFullName(ep.ColumnName, entity);
         if (ep.Name == "Id")
         {
             String message = String.Format("字段“Id”为系统内置自增长字段,请勿自定义此字段!");
             logger.Info(message);
         }
         else if (ep.SaveToDB && !ep.IsList)
         {
             addColumnSingle(entity, sb, ep, columnName);
         }
     }
 }
コード例 #7
0
ファイル: UniqueAttribute.cs プロジェクト: nust03/xcore
        private static int getCount( String action, IEntity target, EntityInfo entityInfo, EntityPropertyInfo info, Object obj )
        {
            if (obj == null) return 1;

            String usql;
            IDatabaseDialect dialect = entityInfo.Dialect;
            if (action.Equals( "update" )) {
                usql = String.Format( "select count(Id) from {0} where Id<>{3} and {1}={2}", entityInfo.TableName, info.ColumnName, dialect.GetParameter( info.Name ), target.Id );
            }
            else {
                usql = String.Format( "select count(Id) from {0} where {1}={2}", entityInfo.TableName, info.ColumnName, dialect.GetParameter( info.Name ) );
            }

            logger.Info( LoggerUtil.SqlPrefix + " validate unique sql : " + usql );
            IDbCommand cmd = DataFactory.GetCommand( usql, DbContext.getConnection( entityInfo ) );
            DataFactory.SetParameter( cmd, info.ColumnName, obj );
            return cvt.ToInt( cmd.ExecuteScalar() );
        }
コード例 #8
0
ファイル: CacheUtil.cs プロジェクト: mfz888/xcore
        public static void CheckCountCache( String action, IEntity obj, EntityInfo entityInfo ) {

            for (int i = 0; i < entityInfo.SavedPropertyList.Count; i++) {
                IEntity container = null;
                String propertyName = null;
                EntityPropertyInfo info = entityInfo.SavedPropertyList[i];
                if ((info.Name != "Id") && !(info.Name == "OID")) {
                    ICacheAttribute attribute = ReflectionUtil.GetAttribute( info.Property, typeof( CacheCountAttribute ) ) as ICacheAttribute;
                    if (attribute != null) {
                        container = ReflectionUtil.GetPropertyValue( obj, info.Name ) as IEntity;
                        propertyName = attribute.TargetPropertyName.Replace( " ", "" );
                    }
                    if (container != null) {
                        String columnName = Entity.GetInfo( container ).GetColumnName( propertyName );
                        setCountCacheBySql( container, columnName, action );
                    }
                }
            }
        }
コード例 #9
0
ファイル: OrmUtil.cs プロジェクト: nust03/xcore
 private static void setDefaultValue(IDbCommand cmd, EntityPropertyInfo info, EntityInfo entityInfo)
 {
     if (MappingClass.Instance.ClassList.Contains(info.Type.FullName))
     {
         DataFactory.SetParameter(cmd, info.ColumnName, -1);
     }
     else if (info.Type == typeof(DateTime))
     {
         if (entityInfo.DbType == DatabaseType.Access)
         {
             DataFactory.SetParameter(cmd, info.ColumnName, DateTime.Now.ToString());
         }
         else
         {
             DataFactory.SetParameter(cmd, info.ColumnName, DateTime.Now);
         }
     }
     else
     {
         DataFactory.SetParameter(cmd, info.ColumnName, "");
     }
 }
コード例 #10
0
ファイル: TableBuilderBase.cs プロジェクト: mfz888/xcore
        protected virtual void addColumn_LongText( EntityInfo entity, StringBuilder sb, String columnName ) {
            sb.Append( columnName );
            sb.Append( " ntext, " );

        }
コード例 #11
0
ファイル: TableBuilderBase.cs プロジェクト: mfz888/xcore
 protected virtual void addColumn_String( EntityInfo entity, StringBuilder sb, EntityPropertyInfo ep, String columnName ) {
     if (ep.LongTextAttribute != null) {
         addColumn_LongText( entity, sb, columnName );
     }
     else if (ep.SaveAttribute != null) {
         addColumn_ByColumnAttribute( entity, sb, ep, columnName );
     }
     else {
         addColumn_ShortText( sb, columnName, 250 );
     }
 }
コード例 #12
0
 internal static void SetParameters(IDbCommand cmd, String action, IEntity obj, EntityInfo entityInfo)
 {
     for (int i = 0; i < entityInfo.SavedPropertyList.Count; i++)
     {
         EntityPropertyInfo info = entityInfo.SavedPropertyList[i];
         if (isContinue(action, info, entityInfo))
         {
             continue;
         }
         Object paramVal = obj.get(info.Name);
         if (paramVal == null && info.DefaultAttribute != null)
         {
             paramVal = info.DefaultAttribute.Value;
         }
         if (paramVal == null)
         {
             setDefaultValue(cmd, info, entityInfo);
         }
         else if (info.Type.IsSubclassOf(typeof(IEntity)) || MappingClass.Instance.ClassList.Contains(info.Type.FullName))
         {
             setEntityId(cmd, info, paramVal);
         }
         else
         {
             paramVal = DataFactory.SetParameter(cmd, info.ColumnName, paramVal);
             obj.set(info.Name, paramVal);
         }
     }
 }
コード例 #13
0
ファイル: MySqlTableBuilder.cs プロジェクト: nust03/xcore
 protected override void addColumn_Single( EntityInfo entity, StringBuilder sb, string columnName )
 {
     sb.Append( columnName );
     sb.Append( " float default 0, " );
 }
コード例 #14
0
ファイル: MySqlTableBuilder.cs プロジェクト: nust03/xcore
 protected override void addColumn_LongText( EntityInfo entity, StringBuilder sb, String columnName )
 {
     sb.Append( columnName );
     sb.Append( " text, " );
 }
コード例 #15
0
 public Includer(Type t)
 {
     _selectedProperty = "*";
     entityInfo        = Entity.GetInfo(t);
 }
コード例 #16
0
ファイル: TableBuilderBase.cs プロジェクト: mfz888/xcore
 private Boolean isTableCreated( IList existTables, EntityInfo entity ) {
     for (int i = 0; i < existTables.Count; i++) {
         if (string.Compare( existTables[i].ToString(), entity.TableName.Replace( "[", "" ).Replace( "]", "" ), true ) == 0) {
             logger.Info( "映射类 : " + entity.FullName + " => " + existTables[i] );
             return true;
         }
     }
     return false;
 }
コード例 #17
0
ファイル: EntityInfo.cs プロジェクト: mfz888/xcore
        /// <summary>
        /// 根据类型Type,初始化EntityInfo;注意:因为不是从缓存中取,所以速度较慢
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        internal static EntityInfo GetByType(Type t)
        {

            EntityInfo info = new EntityInfo();

            info.Type = t;
            info.Name = t.Name;
            info.FullName = t.FullName;

            info.TableName = AddPrefixToTableName(GetTableName(t));
            info.Database = GetDatabase(t);

            CheckCustomMapping(info);

            info.Label = GetTypeLabel(t);

            IList propertyList = ReflectionUtil.GetPropertyList(t);
            for (int i = 0; i < propertyList.Count; i++)
            {
                PropertyInfo property = propertyList[i] as PropertyInfo;
                EntityPropertyInfo ep = EntityPropertyInfo.Get(property);
                ep.ParentEntityInfo = info;

                if (!(!ep.SaveToDB || ep.IsList))
                {
                    info.SavedPropertyList.Add(ep);
                }
                info.PropertyListAll.Add(ep);
            }

            if (info.SavedPropertyList.Count == 1)
            {
                throw new Exception("class's properties have not been setted '[save]' attribute.");
            }

            return info;
        }
コード例 #18
0
ファイル: EntityInfo.cs プロジェクト: mfz888/xcore
 private static void CheckCustomMapping(EntityInfo info)
 {
     Dictionary<String, MappingInfo> map = DbConfig.Instance.GetMappingInfo();
     if (map.ContainsKey(info.Type.FullName))
     {
         MappingInfo mi = map[info.Type.FullName];
         if (strUtil.HasText(mi.Table)) info.TableName = mi.Table;
         if (strUtil.HasText(mi.Database)) info.Database = mi.Database;
     }
 }
コード例 #19
0
ファイル: MappingClass.cs プロジェクト: mfz888/xcore
 //1029
 private static Boolean updateParentInfo(EntityInfo info)
 {
     if (info.Type.BaseType == typeof(Object)) return false;
     if (OrmHelper.IsEntityBase(info.Type.BaseType)) return false;
     if (info.Type.BaseType.IsAbstract) return false;
     return true;
 }
コード例 #20
0
ファイル: TableBuilderBase.cs プロジェクト: nust03/xcore
 private void addColumns( EntityInfo entity, StringBuilder sb )
 {
     for (int i = 0; i < entity.SavedPropertyList.Count; i++) {
         EntityPropertyInfo ep = entity.SavedPropertyList[i];
         String columnName = getFullName( ep.ColumnName, entity );
         if ((ep.SaveToDB && !ep.IsList) && !(ep.Name == "Id")) {
             addColumnSingle( entity, sb, ep, columnName );
         }
     }
 }
コード例 #21
0
ファイル: DbContext.cs プロジェクト: mfz888/xcore
        /// <summary>
        /// 获取数据库连接,返回的连接已经打开(open);在 mvc 框架中不用关闭,框架会自动关闭连接。
        /// 之所以要传入 EntityInfo,因为 ORM 支持多个数据库,不同的类型有可能映射到不同的数据库。
        /// </summary>
        /// <param name="et"></param>
        /// <returns></returns>
        public static IDbConnection getConnection(EntityInfo et)
        {

            String db = et.Database;
            String connectionString = DbConfig.GetConnectionString(db);

            IDbConnection connection;
            Dictionary<String, IDbConnection> connections = getConnectionAll();
            connections.TryGetValue(db, out connection);
            if (connection == null)
            {
                try
                {
                    connection = DataFactory.GetConnection(connectionString, et.DbType);
                }
                catch(Exception ex)
                {
                    LogManager.GetLogger().Error("数据库连接字符串错误:" + connectionString);
                    throw ex;
                }
                connection.Open();
                OrmHelper.initCount++;
                LogManager.GetLogger("Class:System.Data.DbContext Method:getConnection").Info("数据库连接已开启【" + OrmHelper.initCount + "】");
                setConnection(db, connection);
                if (shouldTransaction())
                {
                    IDbTransaction trans = connection.BeginTransaction();
                    setTransaction(db, trans);
                }
                return connection;
            }
            if (connection.State == ConnectionState.Closed)
            {
                connection.ConnectionString = connectionString;
                connection.Open();
                OrmHelper.initCount++;
                LogManager.GetLogger("Class:System.Data.DbContext Method:getConnection").Info("数据库连接已开启【" + OrmHelper.initCount + "】");
            }
            return connection;
        }
コード例 #22
0
ファイル: TableBuilderBase.cs プロジェクト: mfz888/xcore
        protected virtual void addColumn_MiddleText( EntityInfo entity, StringBuilder sb, EntityPropertyInfo ep, String columnName ) {

            addColumn_ShortText( sb, columnName, ep.SaveAttribute.Length );
        }
コード例 #23
0
ファイル: SqlBuilder.cs プロジェクト: g82tt/xcore
 public SqlBuilder(EntityInfo entityInfo)
 {
     _entityInfo   = entityInfo;
     _propertyList = _entityInfo.SavedPropertyList;
 }
コード例 #24
0
ファイル: TableBuilderBase.cs プロジェクト: mfz888/xcore
        //------------------------------------------------------------------------------------------------------------------------

        private String getFullName( String name, EntityInfo entity ) {
            if (DbConst.SqlKeyWords.Contains( name.ToLower() )) {
                String message = String.Format("'{0}' is reserved word. Entity:{1}, Table:{2}\n您在实体类:'{1}'中使用的字段:'{0}'为系统保留字段,对应的表名为:'{2}',请修改后重试!", name, entity.FullName, entity.TableName);
                logger.Info( message );
                throw new Exception( message );
            }
            return name;
        }
コード例 #25
0
 public Includer(EntityInfo _entityInfo)
 {
     _selectedProperty = "*";
     entityInfo        = _entityInfo;
 }
コード例 #26
0
ファイル: SqlBuilder.cs プロジェクト: g82tt/xcore
 public SqlBuilder(Type type)
 {
     _entityInfo   = Entity.GetInfo(type);
     _propertyList = _entityInfo.SavedPropertyList;
 }
コード例 #27
0
ファイル: TableBuilderBase.cs プロジェクト: mfz888/xcore
        //------------------------------------------------------------------------------------------------------------------------

        protected virtual void addColumn_PrimaryKey( EntityInfo entity, StringBuilder sb, IDictionary clsList ) {
            if (isAddIdentityKey( entity.Type ) == false) {
                sb.Append( " Id int primary key default 0, " );
            }
            else {
                sb.Append( " Id int identity(1,1) primary key, " );
            }
        }
コード例 #28
0
ファイル: TableBuilderBase.cs プロジェクト: mfz888/xcore
 private void addColumnSingle( EntityInfo entity, StringBuilder sb, EntityPropertyInfo ep, String columnName ) {
     if (ep.Type == typeof(int) || ep.Type == typeof(long))
     {
         addColumn_Int(sb, entity, ep, columnName);
     }
     else if (ep.Type == typeof(bool))
     {
         addColumn_Boolean(sb, ep, columnName);
     }
     else if (ep.Type == typeof(DateTime))
     {
         addColumn_Time(sb, columnName);
     }
     else if (ep.Type == typeof(decimal))
     {
         addColumn_Decimal(entity, sb, ep, columnName);
     }
     else if (ep.Type == typeof(double))
     {
         addColumn_Double(entity, sb, columnName);
     }
     else if (ep.Type == typeof(float))
     {
         addColumn_Single(entity, sb, columnName);
     }
     else if (ep.Type == typeof(String))
     {
         addColumn_String(entity, sb, ep, columnName);
     }
     else if (ep.IsEntity)
     {
         addColumn_entity(sb, columnName);
     }
 }
コード例 #29
0
ファイル: TableBuilderBase.cs プロジェクト: mfz888/xcore
 protected virtual void addColumn_Int(StringBuilder sb, EntityInfo entity, EntityPropertyInfo ep, String columnName)
 {
     sb.Append( columnName );
     if (ep.Property.IsDefined( typeof( TinyIntAttribute ), false )) {
         sb.Append(" tinyint default 0, ");
     }
     else if (ep.Property.IsDefined(typeof(LongAttribute), false))
     {
         if (entity.DbType == DatabaseType.Access)
         {
             sb.Append(" double default 0, ");
         }
         else
         {
             sb.Append(" bigint default 0, ");
         }
     }
     else
     {
         sb.Append(" int default 0, ");
     }
 }
コード例 #30
0
ファイル: MySqlTableBuilder.cs プロジェクト: nust03/xcore
 protected override void addColumn_PrimaryKey( EntityInfo entity, StringBuilder sb, IDictionary clsList )
 {
     sb.Append( " Id int unsigned not null auto_increment primary key, " );
 }
コード例 #31
0
ファイル: TableBuilderBase.cs プロジェクト: mfz888/xcore
 protected virtual void addColumn_Single( EntityInfo entity, StringBuilder sb, string columnName ) {
     sb.Append( columnName );
     sb.Append( " real default 0, " );
 }
コード例 #32
0
ファイル: Includer.cs プロジェクト: nust03/xcore
 public Includer( EntityInfo _entityInfo )
 {
     _selectedProperty = "*";
     entityInfo = _entityInfo;
 }
コード例 #33
0
ファイル: DbContext.cs プロジェクト: nust03/xcore
        /// <summary>
        /// ��ȡ���ݿ����ӣ����ص������Ѿ���(open)���� mvc ����в��ùرգ���ܻ��Զ��ر����ӡ�
        /// ֮����Ҫ���� EntityInfo����Ϊ ORM ֧�ֶ�����ݿ⣬��ͬ�������п���ӳ�䵽��ͬ�����ݿ⡣
        /// </summary>
        /// <param name="et"></param>
        /// <returns></returns>
        public static IDbConnection getConnection(EntityInfo et)
        {
            String db = et.Database;
            String connectionString = DbConfig.GetConnectionString(db);

            IDbConnection connection;
            getConnectionAll().TryGetValue(db, out connection);

            if (connection == null)
            {
                connection = DataFactory.GetConnection(connectionString, et.DbType);

                connection.Open();
                setConnection(db, connection);

                if (shouldTransaction())
                {
                    IDbTransaction trans = connection.BeginTransaction();
                    setTransaction(db, trans);
                }

                return connection;
            }
            if (connection.State == ConnectionState.Closed)
            {
                connection.ConnectionString = connectionString;
                connection.Open();
            }
            return connection;
        }
コード例 #34
0
ファイル: AccessTableBuilder.cs プロジェクト: mfz888/xcore
 protected override void addColumn_MiddleText( EntityInfo entity, StringBuilder sb, EntityPropertyInfo temP, string columnName ) {
     addColumn_LongText( entity, sb, columnName );
 }
コード例 #35
0
ファイル: Includer.cs プロジェクト: nust03/xcore
 public Includer( Type t )
 {
     _selectedProperty = "*";
     entityInfo = Entity.GetInfo( t );
 }