Example #1
0
        private void RefreshSchemaDetails(DataConnection connection, CodeTorch.Core.DataCommand command)
        {
            switch (command.Type.ToString().ToLower())
            {
            case "storedprocedure":
                //get parameters
                DataTable paramDT = GetStoredProcedureParameters(connection, null, command.Text);


                command.Parameters.Clear();
                foreach (DataRow p in paramDT.Rows)
                {
                    CodeTorch.Core.DataCommandParameter param = new CodeTorch.Core.DataCommandParameter();
                    param.Name              = p["ParameterName"].ToString();
                    param.Size              = Convert.ToInt32(p["length"]);
                    param.Type              = GetParameterTypeFromDBType(p["ParameterDBType"].ToString(), Convert.ToBoolean(p["is_user_defined"]), Convert.ToBoolean(p["is_table_type"]));
                    param.Direction         = GetParameterDirectionFromDB(Convert.ToBoolean(p["IsOutParam"]));
                    param.TypeName          = p["ParameterDBType"].ToString();
                    param.IsUserDefinedType = Convert.ToBoolean(p["is_user_defined"]);
                    param.IsTableType       = Convert.ToBoolean(p["is_table_type"]);

                    command.Parameters.Add(param);
                }

                break;

            case "text":
                string[] paramTokens = command.Text.Split('@');
                if (paramTokens.Length > 0)
                {
                    List <string> paramlist = new List <string>();

                    for (int paramIndex = 1; paramIndex < paramTokens.Length; paramIndex++)
                    {
                        string temp          = paramTokens[paramIndex];
                        int    paramEndIndex = temp.Length - 1;

                        for (int i = 0; i < temp.Length; i++)
                        {
                            bool IsValidChar = false;

                            //if character is letter or digit
                            if (Char.IsLetterOrDigit(temp[i]))
                            {
                                IsValidChar = true;
                            }

                            //if character is @
                            if (temp[i] == '@')
                            {
                                IsValidChar = true;
                            }

                            if (temp[i] == '$')
                            {
                                IsValidChar = true;
                            }

                            if (temp[i] == '#')
                            {
                                IsValidChar = true;
                            }

                            if (temp[i] == '_')
                            {
                                IsValidChar = true;
                            }

                            if (!IsValidChar)
                            {
                                paramEndIndex = (i - 1);
                                break;
                            }
                        }

                        string paramitem = string.Empty;
                        //int paramEndIndex = paramTokens[paramIndex].IndexOf(' ');
                        //if (paramEndIndex > 0)
                        //{
                        //    paramitem = paramTokens[paramIndex].Substring(0, paramEndIndex);
                        //}
                        //else
                        //{
                        //    paramitem = paramTokens[paramIndex];
                        //}

                        paramitem = paramTokens[paramIndex].Substring(0, paramEndIndex + 1);

                        if (!paramlist.Contains("@" + paramitem))
                        {
                            paramlist.Add("@" + paramitem);
                        }
                    }


                    command.Parameters.Clear();
                    foreach (string paramItem in paramlist)
                    {
                        CodeTorch.Core.DataCommandParameter param = new CodeTorch.Core.DataCommandParameter();

                        param.Name      = paramItem;
                        param.Size      = 200;
                        param.Type      = DataCommandParameterType.String;
                        param.Direction = DataCommandParameterDirection.In;

                        //param - reset dirty flag

                        command.Parameters.Add(param);
                    }
                }
                break;
            }



            if (command.ReturnType.ToString().ToLower() == "datatable")
            {
                DataColumnCollection columns = this.InferDataCommandResultsetColumns(connection, command);


                command.Columns.Clear();
                foreach (DataColumn col in columns)
                {
                    CodeTorch.Core.DataCommandColumn column = new CodeTorch.Core.DataCommandColumn();

                    column.Name = col.ColumnName;
                    column.Type = col.DataType.Name;

                    command.Columns.Add(column);
                }
            }
        }
Example #2
0
        private void RefreshSchemaDetails(DataConnection connection, DataCommand command)
        {
            //get parameters
            Assembly assembly = null;

            try
            {
                Setting assemblySetting = connection.Settings.Where(i => (i.Name.ToString().ToLower() == "assembly_fullpath")).SingleOrDefault <Setting>();
                Setting classSetting    = connection.Settings.Where(i => (i.Name.ToString().ToLower() == "classname")).SingleOrDefault <Setting>();

                if (assemblySetting != null && classSetting != null)
                {
                    assembly = Assembly.LoadFrom(assemblySetting.Value);

                    if (assembly != null)
                    {
                        Type type = assembly.GetType(classSetting.Value, true, true);

                        if (type != null)
                        {
                            MethodInfo method = type.GetMethod(command.Text);
                            int        index  = 0;

                            command.Parameters.Clear();

                            if (method != null)
                            {
                                while ((method.GetParameters().Length - 1) >= index)
                                {
                                    CodeTorch.Core.DataCommandParameter param = new CodeTorch.Core.DataCommandParameter();
                                    param.Name = "@" + method.GetParameters()[index].Name;
                                    var typeArray = method.GetParameters()[index].ParameterType.ToString().Split('.');
                                    var strtype   = typeArray[typeArray.Count() - 1];

                                    param.Type              = (DataCommandParameterType)Enum.Parse(typeof(DataCommandParameterType), strtype);
                                    param.Size              = 5000;
                                    param.Direction         = DataCommandParameterDirection.In;
                                    param.IsTableType       = false;
                                    param.IsUserDefinedType = false;

                                    command.Parameters.Add(param);
                                    index++;
                                }
                            }

                            if (method.ReturnType.Name.ToLower() != "void")
                            {
                                var entity = Activator.CreateInstance(method.ReturnType, false);
                                command.Columns.Clear();
                                var enumerable = entity as IEnumerable <object>;

                                if (enumerable != null)
                                {
                                    entity = Activator.CreateInstance(enumerable.GetType().GetGenericArguments()[0], false);
                                }

                                foreach (PropertyInfo property in entity?.GetType().GetProperties())
                                {
                                    CodeTorch.Core.DataCommandColumn column = new CodeTorch.Core.DataCommandColumn();

                                    column.Name = property.Name;
                                    column.Type = property.PropertyType.ToString();

                                    command.Columns.Add(column);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (e.InnerException != null)
                {
                    Common.LogException(e, false);
                    throw e.InnerException;
                }
                else
                {
                    Common.LogException(e);
                }
            }
            finally
            {
                if (assembly != null)
                {
                    assembly = null;
                }
            }
        }