Пример #1
0
        public static SQLTableMap GetSQLMap(Type type)
        {
            List <SQLProp> mapprops = new List <SQLProp>();

            PropertyInfo[] props = type.GetProperties();

            SQLProp primaryKey   = null;
            bool    foundPrimary = false;

            foreach (PropertyInfo prop in props)
            {
                object[] attrs = prop.GetCustomAttributes(true);
                foreach (object attr in attrs)
                {
                    SQLColumn ColAttr = attr as SQLColumn;
                    if (ColAttr != null)
                    {
                        //SQLTableProp TableProp = ColAttr as SQLTableProp;
                        //string _cspropName;
                        string _sqlPropName;

                        //if not Provided, we assume the mapped name is the same
                        if (ColAttr.ColName == string.Empty)
                        {
                            _sqlPropName = prop.Name;
                        }
                        else
                        {
                            _sqlPropName = ColAttr.ColName;
                        }
                        //_cspropName = prop.Name;


                        if (ColAttr.Type == SQLColumn.ColType.linkedTable)
                        {
                            SQLLinkedTable linkTable = ColAttr as SQLLinkedTable;

                            mapprops.Add(new SQLTableProp()
                            {
                                CSProp     = prop,
                                SQLColName = _sqlPropName,
                                type       = prop.PropertyType,
                                Table      = GetSQLMap(linkTable.otherTableType),
                                LinkOn     = new SQLProp()
                                {
                                    CSProp     = prop,
                                    SQLColName = _sqlPropName,
                                    type       = prop.PropertyType
                                },
                            });
                        }
                        else
                        {
                            mapprops.Add(new SQLProp()
                            {
                                CSProp     = prop,
                                SQLColName = _sqlPropName,
                                type       = prop.PropertyType
                            });
                        }

                        if (ColAttr.Type == SQLColumn.ColType.PrimaryKey)
                        {
                            if (foundPrimary)
                            {
                                throw new Exception("SQL Mapping: multiple primary keys in one object!");
                            }
                            foundPrimary = true;
                            primaryKey   = new SQLProp()
                            {
                                CSProp     = prop,
                                SQLColName = _sqlPropName,
                                type       = prop.PropertyType
                            };
                        }
                        //_dict.Add(propName, auth);
                    }
                }
            }
            if (!foundPrimary)
            {
                throw new Exception("SQL Mapping: Primary key not Found!");
            }
            return(new SQLTableMap(type.GetTypeInfo().GetCustomAttribute <SQLTable>().SQLTableName, mapprops, primaryKey));
        }
Пример #2
0
 public SQLTableMap(string tableName, IEnumerable <SQLProp> cols, SQLProp primaryKey)
 {
     this.Cols       = cols;
     this.TableName  = tableName;
     this.PrimaryKey = primaryKey;
 }