예제 #1
0
        public static void Main()
        {
            var ts = new DbList <DbArray <kh.客户, kh.订单, kh.订单明细> >();

            var ts1 = new DbArray <kh.客户, kh.订单, kh.订单明细>();

            ts1.Table1 = new kh.客户[] { new kh.客户 {
                                           客户编号 = 1
                                       }, new kh.客户 {
                                           客户编号 = 2
                                       } };
            //ts.Table2 =
            //ts.Table3 =
            var ts2 = new DbArray <kh.客户, kh.订单, kh.订单明细>(ts1.GetBytes());

            ts.Table1 = new List <DbArray <kh.客户, kh.订单, kh.订单明细> >();
            ts.Table1.Add(ts1);
            ts.Table1.Add(ts2);

            byte[] buff = ts.GetBytes();

            buff.WL();

            RL();
        }
예제 #2
0
        /// <summary>
        /// 根据数据实体(接口方法参数中包含)和带参数的sql语句中包含的参数集合来创建DbParameter集合
        /// </summary>
        /// <param name="entity">数据实体(接口方法参数)</param>
        /// <param name="dbParas">DbParameter集合</param>
        /// <param name="paraNameList">带参数的sql包含的参数集合</param>
        public void GetDbParaListByEntity(object entity, DbList <DbParameter> dbParas, EList <CKeyValue> sqlParaNameList)
        {
            dbParas.Clear();
            if (null == entity)
            {
                return;
            }
            CKeyValue kv          = null;
            object    vObj        = null;
            string    paraTypeStr = "";

            PropertyInfo[] piArr = entity.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo item in piArr)
            {
                kv = sqlParaNameList[item.Name.ToLower()];
                if (null == kv)
                {
                    continue;
                }
                if (string.IsNullOrEmpty(paraTypeStr))
                {
                    paraTypeStr = kv.other.ToString();
                }

                //throw new Exception("未引入与[" + paraTypeStr + "]类型相关的程序集");
                vObj = item.GetValue(entity, null);

                GetDbParaByBaseType(item.PropertyType, paraTypeStr, vObj, kv.Value.ToString(), dbParas);
            }
        }
예제 #3
0
        public void GetDbParaByDataRow(DataRow row, DbList <DbParameter> dbParas, EList <CKeyValue> sqlParaNameList, EList <CKeyValue> tableColumns)
        {
            dbParas.Clear();
            if (null == row)
            {
                return;
            }
            CKeyValue kv          = null;
            Type      type        = null;
            object    vObj        = null;
            string    paraTypeStr = "";

            foreach (var item in sqlParaNameList)
            {
                kv = tableColumns[item.Key];
                if (null == kv)
                {
                    continue;
                }

                if (string.IsNullOrEmpty(paraTypeStr))
                {
                    paraTypeStr = item.other.ToString();
                }

                vObj = row[kv.Value.ToString()];
                type = kv.other as Type;
                type = null == type ? typeof(string) : type;
                GetDbParaByBaseType(type, paraTypeStr, vObj, item.Value.ToString(), dbParas);
            }
        }
예제 #4
0
        public UpdateVerseCommand(VerseViewModel verse, DbList list = null)
        {
            if (verse == null)
                throw new ArgumentNullException("verse", "verse cannot be null.");

            _verse = verse;
            _list = list;
        }
예제 #5
0
 public InsertVerseCommand(DbVerse newVerse, DbList list)
 {
     if (list == null)
         throw new ArgumentNullException("list", "list cannot be null.");
     if (newVerse == null)
         throw new ArgumentNullException("newVerse", "newVerse cannot be null.");
     _newVerse = newVerse;
     _list = list;
 }
예제 #6
0
        string ISqlExpressionProvider.provideSql(DbList <DbParameter> dbParameters, DataOptType dataOptType, PList <Para> paraList, object[] methodParameters)
        {
            string ComNameEn = paraList["ComNameEn", true].ToString();
            string sql       = "select a.* from EmployeeInfo a, WorkInfo b where a.id=b.EmployeeInfoID and b.CompanyNameEn=@CompanyNameEn";

            dbParameters.Add("CompanyNameEn", ComNameEn);
            return(sql);

            throw new NotImplementedException();
        }
        string ISqlExpressionProvider.provideSql(DbList <DbParameter> dbParameters, DataOptType dataOptType, PList <Para> paraList, object[] methodParameters)
        {
            DataEntity <DataElement> dataElements = paraList["dataElements", true].TryObject <DataEntity <DataElement> >();
            string sql = "select a.* from EmployeeInfo a, WorkInfo b where a.id=b.EmployeeInfoID and b.CompanyNameEn=@CompanyNameEn";

            dbParameters.Add("CompanyNameEn", dataElements["CompanyNameEn"].value);
            return(sql);

            throw new NotImplementedException();
        }
예제 #8
0
        private void WriteListToXml(DbList list, XmlWriter xmlWriter)
        {
            switch (list)
            {
            case AlbumList albumList:
                WriteAlbumListToXml(albumList, xmlWriter);
                break;

            case PlayLists playLists:
                WritePlayListToXml(playLists, xmlWriter);
                break;

            case TrackList trackList:
                WriteTrackListToXml(trackList, xmlWriter);
                break;
            }
        }
예제 #9
0
        void GetSqlByProvider(MethodInformation method, DbList <DbParameter> dbParameters, ref string sql)
        {
            string dataProviderNamespace = method.dataProviderNamespace;
            string dataProviderClassName = method.dataProviderClassName;

            if (string.IsNullOrEmpty(dataProviderNamespace) || string.IsNullOrEmpty(dataProviderClassName))
            {
                return;
            }

            AutoCall autoCall = (AutoCall)method.AutoCall;
            ISqlExpressionProvider sqlExpressionProvider = autoCall.GetDataProvider(dataProviderNamespace, dataProviderClassName, autoCall);

            if (null == sqlExpressionProvider)
            {
                return;
            }
            autoCall.GetSqlByDataProvider(sqlExpressionProvider, method.paraList, dbParameters, autoCall, method.dataOptType, ref sql);
        }
예제 #10
0
        /// <summary>
        /// 用数据实体中属于基本类型(int,string,bool等)的属性及其值来创建DbParameter集合
        /// </summary>
        /// <param name="dataType">数据类型(数据实体属性类型)</param>
        /// <param name="dbTag">数据源参数类型(如:sqlserver参数类型为@, mysql参数类型为?, oracle为:)</param>
        /// <param name="data">数据源属性值</param>
        /// <param name="fieldName">sql语句中字段名称(同时也是数据实体属性名称)</param>
        /// <param name="dbParas">DbParameter集合</param>
        public void GetDbParaByBaseType(Type dataType, string dbTag, object data, string fieldName, DbList <DbParameter> dbParas)
        {
            if (!DJTools.IsBaseType(dataType))
            {
                return;
            }

            object val = DJTools.ConvertTo(data, dataType);

            val = null == val ? DBNull.Value : val;
            dbParas.Add(fieldName, val);
        }
예제 #11
0
        /// <summary>
        /// 当执行接口方法时,根据数据提供者获取sql语句表达式(动态创建sql语句)
        /// </summary>
        /// <param name="dataProvider">sql语句数据提供者</param>
        /// <param name="paraList">接口方法参数集合</param>
        /// <param name="dbParameters">sql语句参数集合</param>
        /// <param name="autoCall">AutoCall对象</param>
        /// <param name="dataOptType">数据操作类型select|insert|update|delete</param>
        /// <param name="sql">返回sql语句提供者动态生成的sql语句</param>
        public void GetSqlByDataProvider(ISqlExpressionProvider dataProvider, PList <Para> paraList, DbList <DbParameter> dbParameters, AutoCall autoCall, DataOptType dataOptType, ref string sql)
        {
            if (null != dataProvider)
            {
                int      ncount = paraList.Count;
                object[] paras  = new object[ncount];
                int      n      = 0;
                foreach (Para p in paraList)
                {
                    paras[n] = p.ParaValue;
                    n++;
                }

                MethodInfo m = typeof(ISqlExpressionProvider).GetMethod("provideSql");

                try
                {
                    object v = m.Invoke(dataProvider, new object[] { dbParameters, dataOptType, paraList, paras });
                    if (null != v)
                    {
                        sql = v.ToString();
                    }
                }
                catch (Exception ex)
                {
                    autoCall.ExecuteExcption(typeof(ISqlExpressionProvider), null, "provideSql", paraList, ex);
                }
            }
        }
예제 #12
0
        DbList <DbParameter> GetDbParameters(MethodInformation method, DataEntity <DataElement> dataElements, string sql)
        {
            DbList <DbParameter> dbParameters = new DbList <DbParameter>();

            if (string.IsNullOrEmpty(sql))
            {
                return(dbParameters);
            }
            if (null == dataElements)
            {
                return(dbParameters);
            }

            string msg = "";
            Regex  rg  = DynamicCodeChange.rgParaField;

            if (rg.IsMatch(sql) && 0 < method.paraList.Count)
            {
                if (null == method.paraList[0].ParaValue)
                {
                    msg = "The parameter value cann't be null.";
                    ((AutoCall)method.AutoCall).e(msg);
                    throw new Exception(msg);
                }

                string LeftSign  = "";
                string DbTag     = "";
                string FieldName = "";
                string EndSign   = "";
                object v         = null;
                string sql1      = sql;
                Match  m         = null;
                int    num       = 0;

                //DataEntity<DataElement> dataElements = (DataEntity<DataElement>)method.paraList[0].ParaValue;
                MatchCollection mc = rg.Matches(sql);
                while (rg.IsMatch(sql1) && 1000 > num)
                {
                    if ((method.methodComponent.IsAsync || method.methodComponent.EnabledBuffer) &&
                        0 < method.methodComponent.Interval)
                    {
                        Thread.Sleep(method.methodComponent.Interval);
                    }
                    m        = rg.Match(sql1);
                    LeftSign = m.Groups["LeftSign"].Value;
                    if (!DynamicCodeChange.isEnabledField(LeftSign))
                    {
                        continue;
                    }
                    DbTag     = m.Groups["DbTag"].Value;
                    FieldName = m.Groups["FieldName"].Value;
                    EndSign   = m.Groups["EndSign"].Value;

                    v = dataElements[FieldName].value;
                    dbParameters.Add(FieldName, v);
                    sql1 = sql1.Replace(m.Groups[0].Value, "");
                    sql1 = EndSign + sql1;
                    num++;
                }
            }

            return(dbParameters);
        }
예제 #13
0
        public T Exec <T>(MethodInformation method, DataOptType dataOptType, Action <T> action, string sql)
        {
            T        result   = default(T);
            AutoCall autoCall = (AutoCall)method.AutoCall;

            dynamicWhere(method, ref sql);

            IList <DataEntity <DataElement> > dataElements  = null;
            DataEntity <DataElement>          dataElements1 = null;
            DbList <DbParameter> dbParameters = null;

            if (0 < method.paraList.Count)
            {
                foreach (Para item in method.paraList)
                {
                    if (null != item.ParaValue)
                    {
                        if (null != item.ParaValue as IList <DataEntity <DataElement> > )
                        {
                            dataElements = item.ParaValue as IList <DataEntity <DataElement> >;
                            break;
                        }
                        else if (null != item.ParaValue as DataEntity <DataElement> )
                        {
                            dataElements  = new List <DataEntity <DataElement> >();
                            dataElements1 = item.ParaValue as DataEntity <DataElement>;
                            dataElements.Add(dataElements1);
                            break;
                        }
                    }
                }
            }

            if (null == dataElements)
            {
                dataElements = new List <DataEntity <DataElement> >();
            }

            string err           = "";
            bool   EnabledBuffer = method.methodComponent.EnabledBuffer;

            EnabledBuffer = method.methodComponent.IsAsync ? true : EnabledBuffer;

            if (DataOptType.select == dataOptType ||
                DataOptType.count == dataOptType ||
                DataOptType.procedure == dataOptType)
            {
                if (0 < dataElements.Count)
                {
                    dataElements1 = dataElements[0];
                }
                dbParameters = GetDbParameters(method, dataElements1, sql);
                GetSqlByProvider(method, dbParameters, ref sql);
                dbHelper.query(autoCall, sql, dbParameters, EnabledBuffer, (dt) =>
                {
                    DynamicCodeAutoCall dynamicCodeAutoCall = new DynamicCodeAutoCall();
                    string execClassPath  = "";
                    string execMethodName = "";
                    bool paraExsit        = false;
                    Type returnType       = dynamicCodeAutoCall.GetParaTypeOfResultExecMethod(method, ref execClassPath, ref execMethodName, ref paraExsit);
                    if (paraExsit)
                    {
                        Type resultExecMethodType = execClassPath.GetClassTypeByPath();

                        if (null == resultExecMethodType)
                        {
                            err = "The ClassPath '" + execClassPath + "' is not exist.";
                            autoCall.e(err, ErrorLevels.dangerous);
                            throw new Exception(err);
                        }

                        object clsObj         = null;
                        MethodInfo methodInfo = resultExecMethodType.GetMethod(execMethodName);
                        if (null != methodInfo)
                        {
                            if (methodInfo.ReturnType != typeof(T))
                            {
                                err        = "";
                                string msg = "The return value type of the method '{0}' of the class '{1}' and the method '{2}' of the interface '{3}' are not the same.";
                                method.append(ref err, 0, msg, execMethodName, execClassPath, method.methodInfo.Name, method.methodInfo.DeclaringType.FullName);
                                autoCall.e(err, ErrorLevels.dangerous);
                                throw new Exception(err);
                            }

                            object vData = dataTableTo(method, dt, returnType);
                            try
                            {
                                clsObj = Activator.CreateInstance(resultExecMethodType);
                                result = (T)methodInfo.Invoke(clsObj, new object[] { vData });
                            }
                            catch (Exception ex)
                            {
                                autoCall.e(ex.ToString(), ErrorLevels.dangerous);
                                //throw;
                            }
                        }
                    }
                    else
                    {
                        result = dataTableTo <T>(method, dt);
                    }

                    if (null != action)
                    {
                        action(result);
                    }
                }, ref err);
            }
            else
            {
                int           n          = 0;
                Func <int, T> funcResult = n1 =>
                {
                    object v = n1;
                    if (typeof(bool) == typeof(T))
                    {
                        v = 0 < n1;
                    }
                    return((T)v);
                };

                foreach (DataEntity <DataElement> item in dataElements)
                {
                    dbParameters = GetDbParameters(method, item, sql);
                    switch (dataOptType)
                    {
                    case DataOptType.insert:
                        dbHelper.insert(autoCall, sql, dbParameters, EnabledBuffer, (num) =>
                        {
                            n     += num;
                            result = funcResult(n);
                            if (null != action)
                            {
                                action(result);
                            }
                        }, ref err);
                        break;

                    case DataOptType.update:
                        dbHelper.update(autoCall, sql, dbParameters, EnabledBuffer, (num) =>
                        {
                            n     += num;
                            result = funcResult(n);
                            if (null != action)
                            {
                                action(result);
                            }
                        }, ref err);
                        break;

                    case DataOptType.delete:
                        dbHelper.delete(autoCall, sql, dbParameters, EnabledBuffer, (num) =>
                        {
                            n     += num;
                            result = funcResult(n);
                            if (null != action)
                            {
                                action(result);
                            }
                        }, ref err);
                        break;
                    }
                }

                result = funcResult(n);
            }
            return(result);
        }
예제 #14
0
 public static Database GetById(DatabaseId id)
 {
     return(DbList.First(x => x.Id == id));
 }