コード例 #1
0
		private SqlTable BuildTable(Type type)
		{
			SqlTable table = null;
			if (!type.IsDefined(typeof(TableAttribute), false))
			{
				if (!type.IsDefined(typeof(SPResultAttribute), false))
					throw new LightException("no TableAttribute or SPResultAttribute found on " + type.FullName);
				else
					table = new SqlTable(type, null, null);
			}
			else
			{
				TableAttribute tableAttr = (TableAttribute) type.GetCustomAttributes(typeof(TableAttribute), false)[0];
				string name = tableAttr.Name;
				string schema = tableAttr.Schema;
				if (name == null || name.Length == 0)
					name = type.Name;
				table = new SqlTable(type, name, schema);
			}
			FindInherited(type, table);
			ProcessFields(type, table);
			ProcessProperties(type, table);
			ProcessMethods(type, table);
			return table;
		}
コード例 #2
0
		private void FindInherited(Type type, SqlTable table)
		{
			if (type.IsDefined(typeof(MapAttribute), false))
			{
				object[] attrs = type.GetCustomAttributes(typeof(MapAttribute), false);
				foreach (MapAttribute attr in attrs)
				{
					IDataBridge data = null;
					BindingFlags flags = BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic;
					MemberInfo member = type.GetProperty(attr.Field, flags);
					if (member != null)
					{
						data = new PropertyBridge((PropertyInfo)member);
					}
					else
					{
						member = type.GetField(attr.Field, flags);
						if (member != null)
							data = new FieldBridge((FieldInfo)member);
						else
							throw new LightException("member " + attr.Field + " not found in class " + type.Name);
					}
					if (attr.Name == null || attr.Name.Length == 0)
						attr.Name = member.Name;
					SqlColumn column = new SqlColumn(table, attr.Name, data);
					if (attr.Alias == null || attr.Alias.Length == 0)
						column.Alias = attr.Field;
					else
						column.Alias = attr.Alias;
					column.IsID = attr.ID;
					column.IsPK = attr.PK;
					table.Add(column);
				}
			}
		}
コード例 #3
0
		public override string GetSql(SqlTable table, ref int offset)
		{
			string name = table.Translate(Column);
			if (name == null || name.Length == 0)
				throw new LightException(string.Format("column {0} not found in table {1}",
				                                       Column, table.Name));
			index = offset;
			IList values = (IList) Value;
			StringBuilder buf = new StringBuilder();
			buf.Append("[").Append(name).Append("] ").Append(Operator).Append(" (");
			if (values.Count > 0)
			{
				offset = offset + values.Count;
				for (int i = index; i < offset; i++)
				{
					if (i > index)
						buf.Append(",");
					string pname = string.Format("@{0}", i);
					buf.Append(pname);
				}
			}
			else
			{
				buf.Append("null");
			}
			buf.Append(")");
			return buf.ToString();
		}
コード例 #4
0
		public override string GetSql(SqlTable table, ref int offset)
		{
			string name = table.Translate(this.Column);
			if (name == null || name.Length == 0)
				throw new LightException(string.Format("column {0} not found in table {1}",
				                                       this.Column, table.Name));
			
			return string.Format("[{0}] {1} null", name, this.Operator);
		}
コード例 #5
0
		public virtual string GetSql(SqlTable table, ref int offset)
		{
			if (HasQuery)
			{
				string sql = query.GetSql(table, ref offset);
				return string.Format("({0})", sql);
			}
			else
			{
				index = offset;
				offset = offset + 1;
				string name = table.Translate(column);
				if (name == null || name.Length == 0)
					//name = column;
					throw new LightException(string.Format("column {0} not found in table {1}", column, table.Name));
				return string.Format("[{0}]{1}@{2}", name, oper, index);
			}
		}
コード例 #6
0
		private void ProcessProperties(Type type, SqlTable table)
		{
			PropertyInfo[] fields = type.GetProperties(
				BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
			foreach (PropertyInfo field in fields)
			{
				if (!field.IsDefined(typeof(ColumnAttribute), false))
					continue;
				ColumnAttribute colAttr = (ColumnAttribute)
					field.GetCustomAttributes(typeof(ColumnAttribute), false)[0];
				PropertyBridge data = new PropertyBridge(field);
				if (colAttr.Name == null || colAttr.Name.Length == 0)
					colAttr.Name = field.Name;
				SqlColumn column = new SqlColumn(table, colAttr.Name, data);
				if (colAttr.Alias == null || colAttr.Alias.Length == 0)
					column.Alias = field.Name;
				else
					column.Alias = colAttr.Alias;
				if (field.IsDefined(typeof(IDAttribute), false))
					column.IsID = true;
				if (field.IsDefined(typeof(PKAttribute), false))
					column.IsPK = true;
				table.Add(column);
			}
		}
コード例 #7
0
		private void ProcessMethods(Type type, SqlTable table)
		{
			MethodInfo[] methods = type.GetMethods(
				BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			foreach (MethodInfo method in methods)
			{
				if (method.IsDefined(typeof(TriggerAttribute), false))
				{
					TriggerAttribute attr = (TriggerAttribute)
						method.GetCustomAttributes(typeof(TriggerAttribute), false)[0];
					SqlTrigger trigger = new SqlTrigger(method, attr.Timing);
					table.AddTrigger(trigger);
				}
			}
		}
コード例 #8
0
        public virtual string GetSql(SqlTable table)
        {
            int offset = 1;

            return(GetSql(table, ref offset));
        }
コード例 #9
0
		public virtual string GetSql(SqlTable table)
		{
			int offset = 1;
			return GetSql(table, ref offset);
		}
コード例 #10
0
		public virtual string GetSql(SqlTable table, ref int offset)
		{
			if (!IsComplete)
				throw new InvalidOperationException("invalid query");
			
			StringBuilder buf = new StringBuilder();
			int sz = constraints.Count;
			if (sz > 0)
				buf.Append("where");
			for (int i = 0; i < sz; i++)
			{
				if (i > 0)
				{
					string op = operators[i-1];
					buf.Append(" ").Append(op);
				}
				SqlConstraint constraint = constraints[i];
				string sql = constraint.GetSql(table, ref offset);
				buf.Append(" ").Append(sql);
			}
			sz = orders.Count;
			if (sz > 0)
			{
				buf.Append(" order by ");
				for (int i = 0; i < sz; i++)
				{
					if (i > 0)
						buf.Append(",");
					SqlOrder order = orders[i];
					string sql = order.GetSql(table);
					buf.Append(sql);
				}
			}
			return buf.ToString();
		}