Example #1
0
        /// <summary>
        /// Creates the table.
        /// </summary>
        /// <returns>The table.</returns>
        /// <param name="db">Db.</param>
        /// <param name="type">Type.</param>
        /// <param name="returnQuery">If set to <c>true</c> return query.</param>
        /// <param name="tableAutoGenerated">If set to <c>true</c> table auto generated.</param>
        public string CreateTable(string db, Type type, bool returnQuery = false, bool tableAutoGenerated = false)
        {
            if (!returnQuery)
            {
                CreateFile(db);
            }

            /// table
            TableAttribute table = type.GetAttributeValue <TableAttribute> ();

            if (table != null)
            {
                if (tableAutoGenerated)
                {
                    if (!table.TableAutoGenerated)
                    {
                        return("");
                    }
                }

                string tableName;
                if (table.TableName == "")
                {
                    tableName = type.Name;
                }
                else
                {
                    tableName = table.TableName;
                }

                // columns
                AttributeMappingConfig <ColumnAttribute>[] configs = Reflection.FieldAMCRetrieve <ColumnAttribute> (type);
                if (configs.Length > 0)
                {
                    SQLMaker sql   = new SQLMaker();
                    string   query = sql.GenerateCreateTableSQL(tableName, configs);

                    if (returnQuery)
                    {
                        return(query);
                    }
                    else
                    {
                        ExecuteNonQuery(query);
                    }
                }
            }

            return("");
        }
        /// <summary>
        /// Convert the specified data and flag.
        /// </summary>
        /// <param name="data">Data.</param>
        /// <param name="flag">Flag.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public static object Convert(DataRow row, BindingFlags flag = BindingFlags.NonPublic, Type type = null)
        {
            object           obj        = Activator.CreateInstance(type, null);
            List <FieldInfo> fieldInfos = GetFields(obj.GetType(), flag);

            bool   returnFlag = false;
            string tableName  = Query.GetTableName(type);

            foreach (FieldInfo field in fieldInfos)
            {
                object data = null;
                if (Util.IsValueType(field.FieldType))
                {
                    string key = new SQLMaker().Underline(tableName, field.Name);
                    if (row.ContainsKey(key))
                    {
                        data = row [key];
                        data = ConvertIgnoreData(field, data);
                        if (data == null || data.ToString() == "")
                        {
                            continue;
                        }
                        else
                        {
                            returnFlag = true;
                        }

                        row.Remove(key);
                    }
                }
                else
                {
                    data = Convert(row, flag, field.FieldType);
                }

                if (data != null)
                {
                    field.SetValue(obj, data);
                }
            }

            if (returnFlag)
            {
                return(obj);
            }
            else
            {
                return(null);
            }
        }
Example #3
0
        /// <summary>
        /// Creates the table.
        /// </summary>
        /// <returns>The table.</returns>
        /// <param name="db">Db.</param>
        /// <param name="type">Type.</param>
        /// <param name="returnQuery">If set to <c>true</c> return query.</param>
        /// <param name="tableAutoGenerated">If set to <c>true</c> table auto generated.</param>
        public string CreateTable(string db, Type type, bool returnQuery = false, bool tableAutoGenerated = false)
        {
            if (!returnQuery) {
                CreateFile (db);
            }

            /// table
            TableAttribute table = type.GetAttributeValue<TableAttribute> ();
            if (table != null) {
                if (tableAutoGenerated) {
                    if (!table.TableAutoGenerated) {
                        return "";
                    }
                }

                string tableName;
                if (table.TableName == "") {
                    tableName = type.Name;
                } else {
                    tableName = table.TableName;
                }

                // columns
                AttributeMappingConfig<ColumnAttribute>[] configs = Reflection.FieldAMCRetrieve<ColumnAttribute> (type);
                if (configs.Length > 0) {
                    SQLMaker sql = new SQLMaker ();
                    string query = sql.GenerateCreateTableSQL (tableName, configs);

                    if (returnQuery) {
                        return query;
                    } else {
                        ExecuteNonQuery (query);
                    }
                }
            }

            return "";
        }