Ejemplo n.º 1
0
        public void CreateClass(string dao_ns, string vo_ns, string common_ns, string table, string dstDir, string skipTablePrefix, bool genValueObject, bool genDAO, bool genXSD, bool genASP, bool genDBHelper)
        {
            TableDef tableDef = this.GetTableDef(table);

            tableDef.OriginalName = table;
            tableDef.Name         = DBHandler.NormalizeClassName(skipTablePrefix, tableDef.Name);
            if (!Directory.Exists(dstDir))
            {
                Directory.CreateDirectory(dstDir);
            }
            if (genValueObject)
            {
                string valueObject = this.GetValueObject(vo_ns, tableDef);
                string path        = Path.Combine(dstDir, vo_ns);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                File.WriteAllText(path + "/" + tableDef.Name + ".cs", valueObject);
            }
            if (genDAO)
            {
                string dao  = this.GetDAO(dao_ns, common_ns, vo_ns, tableDef);
                string path = Path.Combine(dstDir, dao_ns);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                File.WriteAllText(path + "/" + tableDef.Name + "_DAO.cs", dao);
                string str = tableDef.Name + "Impl.cs";
                if (!File.Exists(path + "/" + str))
                {
                    File.WriteAllText(path + "/" + str, this.GetDAOImplClass(dao_ns, tableDef.Name));
                }
                string daoBaseClass = this.GetDAOBaseClass(dao_ns, this._connString);
                File.WriteAllText(path + "/DAOClass.cs", daoBaseClass);
            }
            if (genDBHelper)
            {
                string path = Path.Combine(dstDir, common_ns, "Database");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                File.WriteAllText(path + "/DBConnectionFactory.cs", "using System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Data.Common;\r\nusing System.Data.SqlClient;\r\nusing MySql.Data.MySqlClient;\r\n#if SQLITE\r\nusing System.Data.SQLite;\r\n#endif\r\n\r\n\r\nnamespace %%NS%%.Database\r\n{\r\n    public class DBConnectionFactory\r\n    {\r\n        public static DbConnection GetPgSqlConnection(String connStr)\r\n        {\r\n            DbConnection conn = new Npgsql.NpgsqlConnection(connStr);\r\n            conn.Open();\r\n            return conn;\r\n        }\r\n\r\n        public static DbConnection GetSqlConnection(String connStr)\r\n        {\r\n            DbConnection conn = new SqlConnection(connStr);\r\n            conn.Open();\r\n            return conn;\r\n        }\r\n\r\n        public static DbConnection GetMySqlConnection(String connStr)\r\n        {\r\n            DbConnection conn = new MySqlConnection(connStr);\r\n            conn.Open();\r\n            return conn;\r\n        }\r\n\r\n\r\n        public static DbCommand GetPgSqlCommand(String query, DbConnection conn)\r\n        {\r\n            return new Npgsql.NpgsqlCommand(query, (Npgsql.NpgsqlConnection)conn);\r\n        }\r\n\r\n        public static DbCommand GetSqlCommand(String query, DbConnection conn)\r\n        {\r\n            return new SqlCommand(query, (SqlConnection)conn);\r\n        }\r\n\r\n        public static DbCommand GetMySqlCommand(String query, DbConnection conn)\r\n        {\r\n            return new MySqlCommand(query, (MySqlConnection)conn);\r\n        }\r\n\r\n#if SQLITE\r\n        public static DbCommand GetSqlLiteCommand(String query, DbConnection conn)\r\n        {\r\n            return new SQLiteCommand(query, (SQLiteConnection)conn);\r\n        }\r\n\r\n        public static DbConnection GetSQLiteConnection(String connStr)\r\n        {\r\n            DbConnection conn = new System.Data.SQLite.SQLiteConnection(connStr);\r\n            conn.Open();\r\n            return conn;\r\n        }\r\n#endif\r\n\r\n    }\r\n}\r\n".Replace("%%NS%%", common_ns));
                File.WriteAllText(path + "/DBException.cs", "using System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Text;\r\n\r\nnamespace %%NS%%.Database\r\n{\r\n    public class DBException : Exception \r\n    {\r\n        public DBException(String msg) : base(msg) {}\r\n        public DBException(String msg, Exception baseExeption) : base(msg, baseExeption) { }\r\n    }\r\n}\r\n".Replace("%%NS%%", common_ns));
                File.WriteAllText(path + "/DBTools.cs", "using System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Data.Common;\r\n\r\nnamespace %%NS%%.Database\r\n{\r\n    public class DBTools\r\n    {\r\n        public static Object GetDBValue(DbDataReader reader, String field, Object defaultValue)\r\n        {\r\n            if (reader.IsDBNull(reader.GetOrdinal(field)))\r\n                return defaultValue;\r\n            else\r\n            {\r\n                if (defaultValue is String)\r\n                    return reader.GetString(reader.GetOrdinal(field));\r\n                else if (defaultValue is int)\r\n                    return reader.GetInt32(reader.GetOrdinal(field));\r\n                else if (defaultValue is short)\r\n                    return reader.GetInt16(reader.GetOrdinal(field));\r\n                else if (defaultValue is bool)\r\n                    return reader.GetBoolean(reader.GetOrdinal(field));\r\n                else if (defaultValue is DateTime)\r\n                {\r\n                    try\r\n                    {\r\n                        DateTime tmp = reader.GetDateTime(reader.GetOrdinal(field));\r\n                        return tmp;\r\n                    }\r\n                    catch (Exception)\r\n                    {\r\n                        return null;\r\n                    }\r\n\r\n                }\r\n                //else if (defaultValue is UInt64)\r\n                //    return reader.GetUInt64(reader.GetOrdinal(field));\r\n                else if (defaultValue is long)\r\n                    return reader.GetInt64(reader.GetOrdinal(field));\r\n                else if (defaultValue is double || defaultValue is Double)\r\n                    return reader.GetDouble(reader.GetOrdinal(field));\r\n                else if(defaultValue is Decimal || defaultValue is decimal)\r\n                    return reader.GetDecimal(reader.GetOrdinal(field));\r\n                else\r\n                    return reader.GetValue(reader.GetOrdinal(field));\r\n            }\r\n        }\r\n\r\n        public static bool GetDBBoolValue(DbDataReader reader, String field)\r\n        {\r\n            return (bool)GetDBValue(reader, field, false);\r\n        }\r\n\r\n        public static String GetDBStringValue(DbDataReader reader, String field, String defaultVal)\r\n        {\r\n            return (String)GetDBValue(reader, field, defaultVal);\r\n        }\r\n\r\n        public static String GetDBStringValue(DbDataReader reader, String field)\r\n        {\r\n            return (String)GetDBValue(reader, field, \"\");\r\n        }\r\n\r\n        public static int GetDBIntValue(DbDataReader reader, String field)\r\n        {\r\n            return (int)GetDBValue(reader, field, 0);\r\n        }\r\n\r\n        public static long GetDBLongValue(DbDataReader reader, String field)\r\n        {\r\n            return (long)GetDBValue(reader, field, 0L);\r\n        }\r\n\r\n        public static short GetDBShortValue(DbDataReader reader, String field)\r\n        {\r\n            return (short)GetDBValue(reader, field, (short)0);\r\n        }\r\n\r\n        public static UInt64 GetDBUInt64Value(DbDataReader reader, String field)\r\n        {\r\n            return (UInt64)GetDBValue(reader, field, (UInt64)0);\r\n        }\r\n\r\n        public static Int64 GetDBInt64Value(DbDataReader reader, String field)\r\n        {\r\n            return (Int64)GetDBValue(reader, field, (Int64)0);\r\n        }\r\n\r\n        public static Decimal GetDBDecimalValue(DbDataReader reader, String field)\r\n        {\r\n            return (Decimal)GetDBValue(reader, field, (Decimal)0);\r\n        }\r\n\r\n        public static DateTime GetDBDateTimeValue(DbDataReader reader, String field)\r\n        {\r\n            Object dt = GetDBValue(reader, field, DateTime.MinValue);\r\n            if (dt == null) return DateTime.MinValue;\r\n            return (DateTime)dt;\r\n        }\r\n\r\n        public static double GetDBDoubleValue(DbDataReader reader, String field)\r\n        {\r\n            return (double)GetDBValue(reader, field, 0.0d);\r\n        }\r\n\r\n        public static void AddDbParameter(ref DbCommand cmd, String name, object value, bool obsoleteParamDummy)\r\n        {\r\n            AddDbParameter(ref cmd, name, value);\r\n        }\r\n\r\n        public static void AddDbParameter(ref DbCommand cmd, String name, object value)\r\n        {\r\n            if( cmd.GetType() == typeof(MySql.Data.MySqlClient.MySqlCommand))\r\n            {\r\n                MySql.Data.MySqlClient.MySqlParameter p = new MySql.Data.MySqlClient.MySqlParameter(name.Trim(), value);\r\n                cmd.Parameters.Add(p);\r\n            }\r\n            else if (cmd.GetType() == typeof(Npgsql.NpgsqlCommand))\r\n            {\r\n                Npgsql.NpgsqlParameter p = new Npgsql.NpgsqlParameter(name, value);\r\n                cmd.Parameters.Add(p);\r\n            }\r\n            else\r\n            {\r\n                cmd.Parameters.Insert(0, value);\r\n                cmd.Parameters[0].ParameterName = name.Trim();\r\n            }\r\n        }\r\n\r\n\r\n        public static String GetCSTypeFromDBType(String dbType, double len)\r\n        {\r\n            switch( dbType.ToLower().Trim() )\r\n            {\r\n                case \"varchar\":\r\n                case \"text\":\r\n                case \"tinytext\":\r\n                case \"char\":\r\n                    return \"String\";\r\n                case \"timestamp\":\r\n                case \"date\":\r\n                case \"time\":\r\n                case \"datetime\":\r\n                    return \"DateTime\";\r\n                case \"float\":\r\n                case \"float4\":\r\n                    return \"float\";\r\n                case \"float8\":\r\n                case \"double\":\r\n                    return \"double\";\r\n                case \"int4\":\r\n                case \"int\":\r\n                    return \"int\";\r\n                case \"int8\":\r\n                    return \"long\";\r\n                case \"short\":\r\n                case \"int2\":\r\n                case \"smallint\":\r\n                    return \"short\";\r\n                case \"bool\":\r\n                case \"boolean\":\r\n                case \"tinyint\":\r\n                    return \"bool\";\r\n                case \"bigint\":\r\n                    return \"Int64\";\r\n                case \"decimal\":\r\n                    return \"Decimal\";\r\n                case \"bit\":\r\n                    if (len == 1)\r\n                        return \"bool\";\r\n                    return \"int\";\r\n                default:\r\n                    throw new Exception(\"Unknown datatype: \" + dbType);\r\n            }\r\n        }\r\n\r\n        public static bool IsNullable(String dataType)\r\n        {\r\n            switch (dataType)\r\n            {\r\n                case \"String\":\r\n                    return true;\r\n                case \"int\":\r\n                case \"bool\":\r\n                case \"Decimal\":\r\n                case \"short\":\r\n                case \"long\":\r\n                case \"double\":\r\n                case \"single\":\r\n                case \"float\":\r\n                case \"DateTime\":\r\n                    return false;\r\n            }\r\n            return false;\r\n        }\r\n\r\n\r\n\r\n    }\r\n}\r\n".Replace("%%NS%%", common_ns));
            }
            if (!genXSD)
            {
                return;
            }
            string path1 = Path.Combine(dstDir, "xsd");

            if (!Directory.Exists(path1))
            {
                Directory.CreateDirectory(path1);
            }
            File.WriteAllText(path1 + "/" + tableDef.Name.Trim() + ".xsd", this.GetXSD(tableDef));
        }
Ejemplo n.º 2
0
        private string GetFromDBMapper(TableDef tdef)
        {
            StringBuilder stringBuilder = new StringBuilder();
            string        str1          = "\r\n        protected static @@VALUE@@ MapFromDB(DbDataReader dr) \r\n        {\r\n            @@VALUE@@ item = new @@VALUE@@();\r\n\r\n@@DATA@@\r\n\r\n            return item;\r\n        }\r\n\r\n".Replace("@@VALUE@@", tdef.Name);

            foreach (FieldDef field in tdef.Fields)
            {
                string str2 = DBHandler.NormalizeClassName((string)null, field.Name);
                if (str2 == tdef.Name)
                {
                    str2 += "Value";
                }
                string str3 = !field.CanBeNull || DBTools.IsNullable(field.DataType) ? "" : "Nullable";
                stringBuilder.Append("\t\t\titem." + str2 + " = DBTools.GetDB" + field.DataType.Substring(0, 1).ToUpper().Trim() + field.DataType.Substring(1).Trim() + str3 + "Value( dr, \"" + field.Name + "\");" + DBHandler.NL);
            }
            return(str1.Replace("@@DATA@@", stringBuilder.ToString()));
        }
Ejemplo n.º 3
0
        private string GetToDBMapper(TableDef tdef)
        {
            StringBuilder stringBuilder1 = new StringBuilder();
            StringBuilder stringBuilder2 = new StringBuilder();

            stringBuilder2.Append("\t\t\tif( value == null ) {" + DBHandler.NL);
            stringBuilder2.Append("\t\t\t\tthrow new DBException(\"Value Object is NULL\");" + DBHandler.NL);
            stringBuilder2.Append("\t\t\t}" + DBHandler.NL2);
            string str1 = "\r\n        protected static void MapToDB(ref DbCommand cmd, @@VALUE@@ value) \r\n        {\r\n@@DATA@@\r\n        }\r\n\r\n".Replace("@@VALUE@@", tdef.Name);

            foreach (FieldDef field in tdef.Fields)
            {
                string str2 = field.CanBeNull ? (DBTools.IsNullable(field.DataType) ? field.DataType : field.DataType + "?") : field.DataType;
                string str3 = DBHandler.NormalizeClassName((string)null, field.Name);
                if (str3 == tdef.Name)
                {
                    str3 += "Value";
                }
                stringBuilder1.Append("\t\t\tDBTools.AddDbParameter(ref cmd, \"@" + field.Name + "\", value." + str3 + ");" + DBHandler.NL);
                if (!field.CanBeNull && (DBTools.IsNullable(field.DataType) || str2.EndsWith("?")))
                {
                    stringBuilder2.Append("\t\t\tif( value." + str3 + " == null ) {" + DBHandler.NL);
                    stringBuilder2.Append("\t\t\t\tthrow new DBException(\"Field '" + str3 + "' cannot be null!\");" + DBHandler.NL);
                    stringBuilder2.Append("\t\t\t}" + DBHandler.NL2);
                }
                if (field.DataType == "String" && field.FieldLength <= (int)byte.MaxValue && field.FieldLength > 0)
                {
                    stringBuilder2.Append("\t\t\tif( value." + str3 + " != null && value." + str3 + ".Length > " + (object)field.FieldLength + " ) {" + DBHandler.NL);
                    stringBuilder2.Append("\t\t\t\tthrow new DBException(\"Field '" + str3 + "' cannot be more than '" + (object)field.FieldLength + "' characters!\");" + DBHandler.NL);
                    stringBuilder2.Append("\t\t\t}" + DBHandler.NL2);
                }
            }
            string newValue = stringBuilder2.ToString() + stringBuilder1.ToString();

            return(str1.Replace("@@DATA@@", newValue));
        }