Example #1
0
        public static bool SQLDeployScript(string path, DBRoles db)
        {
            try
            {
                //Test for directory
                if (Directory.Exists(Path.Combine(path, "dbo\\Deploy\\")) == false)
                {
                    Directory.CreateDirectory(Path.Combine(path, "dbo\\Deploy\\"));
                }

                string dataPath = Path.Combine(path, "dbo\\Deploy\\Script.PostDeployment.sql");

                //Iterate through data names
                using (var sw = new StreamWriter(dataPath))
                {
                    sw.WriteLine("--Auto generated code from XmlSchemaSQLClassGenerator on " + DateTime.Now.ToString("HH:mm dd-MMM-yyyy"));
                    sw.WriteLine("");

                    foreach (DataSet ds in db.Data)
                    {
                        sw.WriteLine(":r ..\\Data\\" + ds.Name + ".sql");
                    }
                }
            }
            catch (Exception ae)
            {
                string s = ae.ToString();
            }
            return(true);
        }
Example #2
0
        public static bool SQLData(string path, DBRoles db)
        {
            try
            {
                //Test for directory
                if (Directory.Exists(Path.Combine(path, "dbo\\Data\\")) == false)
                {
                    Directory.CreateDirectory(Path.Combine(path, "dbo\\Data\\"));
                }

                //Iterate data sets
                foreach (DataSet ds in db.Data)
                {
                    string dataPath = Path.Combine(path, "dbo\\Data\\", ds.Name + ".sql");

                    using (var sw = new StreamWriter(dataPath))
                    {
                        //Create seed data
                        sw.WriteLine("USE [RenameToRequiredDatabase]");
                        sw.WriteLine("GO");
                        sw.WriteLine("");

                        if (ds.IdentityOff == true)
                        {
                            sw.WriteLine("SET IDENTITY_INSERT [dbo].[" + ds.Name + "] ON");
                            sw.WriteLine("GO");
                        }

                        foreach (DataInsert d in ds.Entries)
                        {
                            sw.Write("INSERT [dbo].[" + ds.Name + "] ");
                            sw.Write("([" + ds.Name + "Id], [Value]) ");
                            sw.WriteLine("VALUES (" + (d.Position + 1).ToString() + ", N'" + d.Value + "')");
                            sw.WriteLine("GO");
                        }

                        if (ds.IdentityOff == true)
                        {
                            sw.WriteLine("SET IDENTITY_INSERT [dbo].[" + ds.Name + "] OFF");
                            sw.WriteLine("GO");
                        }
                    }
                }
            }
            catch (Exception ae)
            {
                string s = ae.ToString();
            }
            return(true);
        }
Example #3
0
        public static DBRoles IsNameUnique(DBRoles dbRoles)
        {
            try
            {
                foreach (Table t in dbRoles.Schemas)
                {
                    if (t.Keys.Count > t.Keys.Select(k => k.Name).Distinct().Count())
                    {
                        //Duplicate key name
                        List <string> lNames = t.Keys.Select(k => k.Name).ToList();
                        foreach (string name in lNames)
                        {
                            if (t.Keys.Count(k => k.Name == name) > 1)
                            {
                                List <Key> lDupKeys = t.Keys.Where(k => k.Name == name).ToList();
                                for (int i = 0; i < lDupKeys.Count; i++)
                                {
                                    if (lDupKeys.Count >= 10 && lDupKeys.Count < 100)
                                    {
                                        lDupKeys[i].Name = lDupKeys[i].Name + i.ToString("00");
                                    }
                                    else if (lDupKeys.Count >= 100 && lDupKeys.Count < 1000)
                                    {
                                        lDupKeys[i].Name = lDupKeys[i].Name + i.ToString("000");
                                    }
                                    else if (lDupKeys.Count >= 1000 && lDupKeys.Count < 10000)
                                    {
                                        lDupKeys[i].Name = lDupKeys[i].Name + i.ToString("0000");
                                    }
                                    else if (lDupKeys.Count >= 10000 && lDupKeys.Count < 100000)
                                    {
                                        lDupKeys[i].Name = lDupKeys[i].Name + i.ToString("00000");
                                    }
                                    else
                                    {
                                        lDupKeys[i].Name = lDupKeys[i].Name + (i + 1).ToString();
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ae)
            { string s = ae.ToString(); }

            return(dbRoles);
        }
Example #4
0
        public DBServer this[DBRoles role]
        {
            get
            {
                DBServer server = null;

                for (int i = 0; i < _alServerMember.Count; ++i)
                {
                    if (((DBServer)_alServerMember[i])._sDBRole == role.ToString())
                    {
                        server = (DBServer)_alServerMember[i];
                        break;
                    }
                }

                return server;
            }
        }
Example #5
0
        public static DBRoles All(DBRoles dbRoles)
        {
            try
            {
                //Test for tables without a primary key
                foreach (Table t in dbRoles.Schemas)
                {
                    PrimaryKey.ConfirmPrimaryKeys(t);
                    PrimaryKey.LimitedTextLengths(t);

                    foreach (SQL.Components.Field f in t.Fields)
                    {
                        f.Name = Field.HasValidName(f, t);
                    }
                }
            }
            catch (Exception ae)
            { string s = ae.ToString(); }
            return(dbRoles);
        }
Example #6
0
        public static bool WriteSQLFiles(string path, DBRoles db)
        {
            try
            {
                bool DataCreated   = SQLData(path, db);
                bool DeployCreated = SQLDeployScript(path, db);
                bool TablesCreated = SQLTables(path, db);

                if (TablesCreated == true && DataCreated == true && DeployCreated == true)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception ae)
            {
                string s = ae.ToString();
            }
            return(false);
        }
Example #7
0
        private OdbcConnection _CreateConnectionWorker(DBRoles role)
        {
            // Image server is not using MySQL
            Debug.Assert(DBRoles.Image != role);

            _svr = GetServerForRole(role);

            OdbcConnection odbcconn = CreateConnectionToServer(_svr);

            //The following steps are due to our DB is using GBK encoding
            OdbcCommand cmd = new OdbcCommand("SET NAMES 'gbk';", odbcconn);

            cmd.ExecuteNonQuery();

            return odbcconn;
        }
Example #8
0
        public bool ExecuteNonQuery(DBRoles role, string sQueryTemplate, params string[] rgstr)
        {
            Object obj;

            bool fRet = _QueryWorker(new _DelegateQuery(_ExecuteNonQueryWorker),
                out obj, role, ConnectionFlags.Write, sQueryTemplate, rgstr);

            return fRet;
        }
Example #9
0
        private void WriteSeparateFiles(CodeNamespace cn)
        {
            List <DBRoles> dbRoles = new List <DBRoles>();

            try
            {
                string dirPath = Path.Combine(OutputDirectory, Validation.Namespace.NameIsValid(cn.Name));

                var ccu = new CodeCompileUnit();
                var cns = new CodeNamespace(Validation.Namespace.NameIsValid(cn.Name));

                cns.Imports.AddRange(cn.Imports.Cast <CodeNamespaceImport>().ToArray());
                cns.Comments.AddRange(cn.Comments);
                ccu.Namespaces.Add(cns);

                List <DBRoles> dbLocalRoles = new List <DBRoles>();
                foreach (CodeTypeDeclaration ctd in cn.Types)
                {
                    string path = Path.Combine(dirPath, ctd.Name + ".sql");
                    cns.Types.Clear();
                    cns.Types.Add(ctd);
                    Configuration?.WriteLog(path);

                    Write.Classes c      = new Write.Classes();
                    DBRoles       dbTemp = c.Output(ctd, ccu, path, Configuration);
                    if (dbTemp != null)
                    {
                        dbLocalRoles.Add(dbTemp);
                    }

                    //Add new file to list
                    if (path != "")
                    {
                        WrittenFiles.Add(path);
                    }
                }

                //Consolidate DBRoles for same XSD file into new single DBRole
                DBRoles db = new DBRoles()
                {
                    Name = ccu.Namespaces[0].Name
                };

                foreach (DBRoles d in dbLocalRoles)
                {
                    db.Schemas.AddRange(d.Schemas);
                    db.Data.AddRange(d.Data);
                    db.deployScript = d.deployScript;
                }

                List <bool> WrittenSuccess = new List <bool>();
                WrittenSuccess.Add(SQL.Write.Utils.WriteSQLFiles(dirPath, db));
            }
            catch (Exception ae)
            {
                string s = ae.ToString();
                if (ae.InnerException != null)
                {
                    s = ae.InnerException.Message;
                }
            }
        }
Example #10
0
 public void DeleteDBRoles(DBRoles dBRoles)
 {
     _dBRolesDal.Delete(dBRoles);
 }
Example #11
0
 public DBRoles UpdateDBRoles(DBRoles dBRoles)
 {
     return(_dBRolesDal.Update(dBRoles));
 }
Example #12
0
        private bool _QueryWorker(_DelegateQuery delegatequery, out Object obj,
            DBRoles role, int flags, string sQueryTemplate,
            params string[] rgstr)
        {
            bool fRet = false;
            string sQuery = null;
            DBConnection conn = null;

            Debug.Assert(Util.Boolify(ConnectionFlags._MaskOper_ & flags));
            Debug.Assert((ConnectionFlags._MaskOper_ & flags) != ConnectionFlags._MaskOper_);

            // We might want to split this try/catch into two: get connection
            // and deletegatequery

            try
            {
                conn = _GetConnector(role);

                Util.DbgTimer timer;

                sQuery = GetQueryStringFromStringParams(flags, sQueryTemplate, rgstr);

                DbgAssertKeyUsage(sQuery, conn.Connection);
                DbgAssertQueryRules(sQueryTemplate, sQuery);

                if (Util.Boolify(DbgFlags.AssertMaxQueryTime & s_dbgflags))
                {
                    timer = new Util.DbgTimer(s_dbgiMaxQueryTime);
                }
                else
                {
                    timer = null;
                }

                _err = DBErrors.None;

                fRet = delegatequery(sQuery, conn.Connection, flags, out obj);

                if (Util.Boolify(DbgFlags.AssertMaxQueryTime & s_dbgflags))
                {
                    Debug.Assert(timer.CheckElapsedTime());
                }
            }
            catch (Exception e)
            {
                if (e.Message.IndexOf("Duplicate") > 0)
                {
                    _err = DBErrors.DuplicateRecord;
                }
                else
                {
                    _err = DBErrors.Unknown;
                    Util.DbgNotify();
                    Error.LogEvent(Error.Severities.Error, "Exception \"{0}\"\r\n    for query: \"{1}\"",
                        e.Message, sQuery);
                }

                fRet = false;
                obj = null;
            }
            finally
            {
                if (null != conn)
                {
                    // BUGBUG: Can probably also throw, throwing is fun...
                    conn.Close();
                }
            }

            return fRet;
        }
Example #13
0
 public DBRoles AddDBRoles(DBRoles dBRoles)
 {
     return(_dBRolesDal.Add(dBRoles));
 }
Example #14
0
        public bool ExecuteNonQueryEx(DBRoles role, int flags, out int cRowsAffected,
            string sQueryTemplate, params string[] rgstr)
        {
            Debug.Assert(Util.Boolify(ConnectionFlags.Write & flags));

            Object obj;

            bool fRet = _QueryWorker(new _DelegateQuery(_ExecuteNonQueryWorker),
                out obj, role, flags, sQueryTemplate, rgstr);

            if (fRet)
            {
                cRowsAffected = (int)obj;
            }
            else
            {
                cRowsAffected = 0;
            }

            return fRet;
        }
Example #15
0
        public bool GetDataTableEx(DBRoles role, int flags, out DataTable dt,
            string sQueryTemplate, params string[] rgstr)
        {
            object obj;

            bool fRet = _QueryWorker(new _DelegateQuery(_GetDataTableWorker),
                out obj, role, flags, sQueryTemplate, rgstr);

            if (fRet)
            {
                dt = (DataTable)obj;
            }
            else
            {
                dt = null;
            }

            return fRet;
        }
Example #16
0
        private DBServer GetServerForRole(DBRoles role)
        {
            DBServer svr = null;

            switch (role)
            {
                case DBRoles.ComShop:
                    svr = ServerFarm.GetComShopServer();
                    break;
                case DBRoles.User:
                    svr = ServerFarm.GetUserServer();
                    break;
                case DBRoles.Article:
                    svr = ServerFarm.GetArticleServer();
                    break;
                case DBRoles.Image:
                    svr = ServerFarm.GetImageServer();
                    break;
                case DBRoles.Robot:
                    svr = ServerFarm.GetRobotServer();
                    break;
                case DBRoles.UserGenerate:
                    svr = ServerFarm.GetUserGenerateServer();
                    break;
                case DBRoles.Space:
                    svr = ServerFarm.GetSpaceServer();
                    break;
                case DBRoles.Production:
                    svr = ServerFarm.GetProductionServer();
                    break;
                case DBRoles.ManualHistory:
                    svr = ServerFarm.GetManualHistoryServer();
                    break;
                default:
                    Debug.Assert(false);
                    break;
            }

            return svr;
        }
Example #17
0
        public bool QueryScalarAsString(DBRoles role, out string s,
            string sQueryTemplate, params string[] rgstr)
        {
            Object obj;
            bool fRet = _QueryWorker(new _DelegateQuery(_QueryScalarWorker),
                out obj, role, ConnectionFlags.Read,
                sQueryTemplate, rgstr);

            if (fRet)
            {
                if (obj is System.DBNull)
                {
                    s = null;
                    fRet = false;
                }
                else
                {
                    s = obj.ToString();
                }
            }
            else
            {
                // To make compiler happy and people who don't check return
                // values unhappy
                s = null;
            }

            return fRet;
        }
Example #18
0
        public bool QueryScalar(DBRoles role, int flags,
            out Object obj, string sQueryTemplate, params string[] rgstr)
        {
            Debug.Assert(Util.Boolify(ConnectionFlags.Read & flags));

            bool fRet = _QueryWorker(new _DelegateQuery(_QueryScalarWorker), out obj,
                role, flags, sQueryTemplate, rgstr);

            return fRet;
        }
Example #19
0
        private DBConnection _GetConnector(DBRoles role)
        {
            OdbcConnection odbcconn = _CreateConnectionWorker(role);
            DBConnection conn = new DBConnection(odbcconn);

            return conn;
        }
Example #20
0
        public DBRoles Output(CodeTypeDeclaration ctd, CodeCompileUnit cu, string path, GeneratorConfiguration configuration)
        {
            DBRoles roleNamespace = new DBRoles();

            try
            {
                roleNamespace.Name = cu.Namespaces[0].Name;

                List <CodeAttributeDeclaration> lAttributes      = new List <CodeAttributeDeclaration>();
                List <CodeMemberEvent>          lFieldEvents     = new List <CodeMemberEvent>();
                List <CodeMemberField>          lFieldMembers    = new List <CodeMemberField>();
                List <CodeMemberMethod>         lFieldMethods    = new List <CodeMemberMethod>();
                List <CodeMemberProperty>       lFieldProperties = new List <CodeMemberProperty>();

                List <string> lAttributeNames = new List <string>();

                foreach (CodeAttributeDeclaration at in ctd.CustomAttributes)
                {
                    lAttributes.Add(at);
                    lAttributeNames.Add(at.Name.Substring(0, at.Name.LastIndexOf(".")));
                }
                lAttributeNames = lAttributeNames.Distinct().OrderBy(a => a).ToList();

                for (int i = 0; i < ctd.Members.Count; i++)
                {
                    if (ctd.Members[i].GetType() == typeof(CodeMemberEvent))
                    {
                        lFieldEvents.Add((CodeMemberEvent)ctd.Members[i]);
                    }
                    if (ctd.Members[i].GetType() == typeof(CodeMemberField))
                    {
                        lFieldMembers.Add((CodeMemberField)ctd.Members[i]);
                    }
                    if (ctd.Members[i].GetType() == typeof(CodeMemberMethod))
                    {
                        lFieldMethods.Add((CodeMemberMethod)ctd.Members[i]);
                    }
                    if (ctd.Members[i].GetType() == typeof(CodeMemberProperty))
                    {
                        lFieldProperties.Add((CodeMemberProperty)ctd.Members[i]);
                    }
                }
                lFieldEvents.OrderBy(e => e.Name);
                lFieldMembers.OrderBy(f => f.Name);
                lFieldMethods.OrderBy(m => m.Name);
                lFieldProperties.OrderBy(p => p.Name);

                //Enums must be separated into separate table, populate and linked via foreign key reference
                if (ctd.IsEnum)
                {
                    Table t = Enums.CreateSchema(ctd, cu, lFieldMembers);
                    if (t != null)
                    {
                        roleNamespace.Schemas.Add(t);
                    }

                    DataSet ds = Enums.CreateDataSet(ctd, cu, lFieldMembers);
                    if (ds != null && ds.Entries.Count > 0)
                    {
                        roleNamespace.Data.Add(ds);
                    }

                    if (roleNamespace.deployScript == null)
                    {
                        roleNamespace.deployScript = new DeployScript();
                    }
                    roleNamespace.deployScript.DataTables.Add(t.Name);
                }
                else if (ctd.IsClass)
                {
                    Table t = Tables.CreateSchema(ctd, cu, lFieldMembers);
                    if (t != null)
                    {
                        roleNamespace.Schemas.Add(t);
                    }
                }
                else if (ctd.IsInterface)
                {
                }
                else
                {
                }
            }
            catch (Exception ae)
            {
                string s = ae.ToString();
            }

            return(roleNamespace);
        }