⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 databaseloader.cs

📁 oracle dal gen,生成.NET ORACLE DAL层代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
                }

                idxInput++;
            }

            /*
             *  Adding output id parameter(s)
             */
            int idxOutputId = 0;
            foreach (PropertyInfo pi in propsInfo)
            {
                DbColumn bt = GetCustomAttribute<IdColumn>(pi);
                if (bt != null)
                    op.Add(CreateParameter(pi.GetValue(dataObject, null), bt.ColumnName, ParameterDirection.Output)); ;

                idxOutputId++;
            }

            /*
             *  Saving object
             */
            object[] retValues = DBUtils.Save(cmd.Text, op, cmd.Type, connection);

            /*
             *  Picking up the return parameters from the databae
             */
            int idxRetId = 0;
            foreach (PropertyInfo pi in propsInfo)
            {
                if (retValues.Length == idxRetId)
                    break;

                pi.SetValue(dataObject, retValues[idxRetId], null);

                idxRetId++;
            }

            /*
             *  Saving list columns
             */
            foreach (PropertyInfo pi in propsInfo)
            {
                ListColumn bt = GetCustomAttribute<ListColumn>(pi);

                if (bt != null)
                {
                    IList objectList = (IList)pi.GetValue(dataObject, null);
                    for (int i = 0; i < objectList.Count; i++)
                    {
                        object o = objectList[i];
                        
                        List<Param> saveParams = new List<Param>();
                        foreach(string str in bt.ColumnName.Split(','))
                            saveParams.Add(new Param(str, GetColumnValue(dataObject, str)));

                        objectList[i] = Save(bt.ChildType, o, saveParams.ToArray(), GetClassDatabaseType(bt.ChildType, Method.Merge), connection);
                    }
                }
            }

            TimeSpan ts = DateTime.Now - startTime;
            Console.WriteLine("Saved in: " + ts.TotalMilliseconds);

            return dataObject;
        }

        #endregion

        #region Delete
        public bool DeleteSingle<T>(Param[] paramArray, OraConnection connection)
        {
            return (bool)Delete(typeof(T), paramArray, GetClassDatabaseType(typeof(T), Method.Delete), connection);
        }

        public bool DeleteAll<T>(OraConnection connection)
        {
            return (bool)Delete(typeof(T), null, GetClassDatabaseType(typeof(T), Method.Delete), connection);
        }

        protected object Delete(Type type, Param[] paramArray, ClassDatabaseType cmd, OraConnection connection)
        {
            List<OracleParameter> op = new List<OracleParameter>();
            /*
             *  Adding id parameter(s)
             */
            PropertyInfo[] propsInfo = type.GetProperties();
            int idx = 0;
            foreach (PropertyInfo pi in propsInfo)
            {
                DbColumn bt = GetCustomAttribute<IdColumn>(pi);
                if (bt != null)
                {
                    object propertyValue = null;
                    if (paramArray != null)
                        propertyValue = paramArray[idx].Value;

                    op.Add(CreateParameter(propertyValue, bt.ColumnName, ParameterDirection.Input));
                }

                idx++;
            }

            /*
             *  Deleting data from the database
             */
            return DBUtils.Execute(cmd.Text, op, cmd.Type, connection);
        }
        #endregion

        #region Notification
        public void RegisterNotification<T>(OraConnection connection)
        {
            IList<ClassDatabaseType> list = GetCustomAttributes<ClassDatabaseType>(typeof(T));
            foreach (ClassDatabaseType cdt in list)
            {
                if (cdt.Method == Method.Notification)
                    RegisterNotification(cdt.Text, cdt.Type, connection);
            }
        }

        public void RegisterNotification(string sql, CommandType cmdType, OraConnection connection)
        {
            if (ntfConnection == null)
                ntfConnection = new OracleConnection(connection.ConnectionString);

            if (ntfConnection.State != ConnectionState.Open)
                ntfConnection.Open();

            OracleCommand cmd = new OracleCommand(sql, ntfConnection);
            cmd.CommandType = cmdType;

            if (dep == null)
                dep = new OracleDependency();

            dep.AddCommandDependency(cmd);
            dep.OnChange += new OnChangeEventHandler(dep_OnChange);

            //cmd.Notification.IsNotifiedOnce = false;


            cmd.ExecuteNonQuery();
        }

        private void dep_OnChange(object sender, OracleNotificationEventArgs eventArgs)
        {
            if (ObjectChanged != null)
                ObjectChanged(sender, eventArgs);
        }
        #endregion

        #region Support methods
        private OracleParameter CreateParameter(object value, string parameterName, ParameterDirection direction)
        {
            OracleParameter p = new OracleParameter();
            if (direction == ParameterDirection.Input)
                p.ParameterName = inPrefix + "_" + parameterName;
            else if (direction == ParameterDirection.Output)
            {
                p.ParameterName = outPrefix + "_" + parameterName;
                if (value == null)
                    p.OracleDbType = OracleDbType.Decimal;
            }
            else
                p.ParameterName = parameterName;

            p.Value = value;
            p.Direction = direction;

            /*
             *  Bug work around. Even though a procedure says varchar2, you need to pass the parameter as clob.
             */
            if (p.OracleDbType == OracleDbType.Varchar2)
                p.OracleDbType = OracleDbType.Clob;

            return p;
        }

        private object GetColumnValue(object o, string columnName)
        {
            PropertyInfo[] props = o.GetType().GetProperties();

            object retValue = null;
            List<object> values = new List<object>();
            foreach (PropertyInfo pi in props)
            {
                DbColumn dt = GetCustomAttribute<DbColumn>(pi);
                if (dt.ColumnName.Equals(columnName.Trim()))
                {
                    retValue = pi.GetValue(o, null);
                    break;
                }
            }

            return retValue;
        }

        private OracleDbType GetOracleType(string type)
        {
            type = type.ToLower();
            if (type.Equals("varchar2"))
                return OracleDbType.Varchar2;
            else if (type.Equals("number"))
                return OracleDbType.Decimal;
            else
                return OracleDbType.Varchar2;
        }

        private IList<K> GetCustomAttributes<K>(Type type)
        {
            object[] attrs = type.GetCustomAttributes(typeof(K), true);
            List<K> list = new List<K>();
            foreach(object o in attrs) 
                list.Add((K)o);

            PropertyInfo[] propsInfo = type.GetProperties();
            foreach (PropertyInfo pi in propsInfo)
            {
                object o = GetCustomAttribute<K>(pi);
                if (o != null)
                    list.Add((K)o);
            }

            return list;
        }

        private K GetCustomAttribute<K>(PropertyInfo pi)
        {
            K ic = default(K);

            object[] o = pi.GetCustomAttributes(typeof(K), true);
            if (o != null && o.Length > 0 && o[0] is K)
                ic = (K)o[0];

            return (K)ic;
        }

        private ClassDatabaseType GetClassDatabaseType(Type type, Method method)
        {
            /*
             *  Getting the pl/sql mapping from dao class
             */            
            IList<ClassDatabaseType> customAttributes = GetCustomAttributes<ClassDatabaseType>(type);
            foreach (ClassDatabaseType cbt in customAttributes)
            {
                if (cbt.Method == method)
                    return cbt;
            }

            throw new Exception(string.Format("Missing pl/sql {0} mapping for class {1}.", method.ToString(), type.ToString()));
        }
        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (ntfConnection != null)
            {
                ntfConnection.Close();
                ntfConnection.Dispose();
            }
        }

        #endregion
    }

    public class Param
    {
        private string columnName;
        private object value;

        public Param(string columnName, object value)
        {
            this.columnName = columnName;
            this.value = value;
        }

        public string ColumnName
        {
            get { return columnName; }
            set { columnName = value; }
        }

        public object Value
        {
            get { return value; }
            set { this.value = value; }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -