Ejemplo n.º 1
0
 private void FindInherited(Type type, OrlTable 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);
         }
     }
 }
Ejemplo n.º 2
0
 public override string GetSql(OrlTable 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();
 }
Ejemplo n.º 3
0
 private OrlTable BuildTable(Type type)
 {
     OrlTable 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 OrlTable(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 OrlTable(type, name, schema);
     }
     FindInherited(type, table);
     ProcessFields(type, table);
     ProcessProperties(type, table);
     ProcessMethods(type, table);
     return table;
 }
Ejemplo n.º 4
0
        public virtual int Delete(Type type, IQuery query)
        {
            ErrorIfClosed();
            int result = 0;

            try
            {
                InternalOpen();
                OrlTable table = TableFor(type);
                if (autocommit)
                {
                    BeginInternal();
                }
                result = table.Delete(this, query);
                if (autocommit)
                {
                    CommitInternal();
                }
            }
            catch (Exception)
            {
                if (autocommit)
                {
                    RollbackInternal();
                }
                throw;
            }
            finally
            {
                InternalClose();
            }
            return(result);
        }
Ejemplo n.º 5
0
        private OrlTable BuildTable(Type type)
        {
            OrlTable 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 OrlTable(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 OrlTable(type, name, schema);
            }
            FindInherited(type, table);
            ProcessFields(type, table);
            ProcessProperties(type, table);
            ProcessMethods(type, table);
            return(table);
        }
Ejemplo n.º 6
0
        public override string GetSql(OrlTable 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());
        }
        public override string GetSql(OrlTable 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);
        }
Ejemplo n.º 8
0
        public OrlTable Build(Type type)
        {
            OrlTable table = cache.Get(type.FullName);

            if (table == null)
            {
                table = BuildTable(type);
                cache.Put(type.FullName, table);
            }
            return(table);
        }
Ejemplo n.º 9
0
        public override string GetSql(OrlTable 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));
        }
Ejemplo n.º 10
0
 protected virtual void DoSelect(Type type, string sql, string[] parameterName, Object[] parameters, IDbTransaction transaction, CommandType commType, IList list)
 {
     ErrorIfClosed();
     try
     {
         InternalOpen();
         OrlTable table = TableFor(type);
         table.Select(this, sql, parameterName, parameters, transaction, commType, list);
     }
     finally
     {
         InternalClose();
     }
 }
Ejemplo n.º 11
0
 protected virtual void DoSelect(Type type, IQuery query, IList list)
 {
     ErrorIfClosed();
     try
     {
         InternalOpen();
         OrlTable table = TableFor(type);
         table.Select(this, query, list);
     }
     finally
     {
         InternalClose();
     }
 }
Ejemplo n.º 12
0
 protected virtual void DoSelect(Type type, string procName, string[] parameterName, object[] parameters, string[] outputParameterName, int?[] outputParameterSize, DbType[] outputParameterType,
                                 ParameterDirection[] outputParameterDirection, out object[] outParameterResult, IList list)
 {
     ErrorIfClosed();
     try
     {
         InternalOpen();
         OrlTable table = TableFor(type);
         table.Select(this, procName, parameterName, parameters, outputParameterName, outputParameterSize, outputParameterType, outputParameterDirection, out outParameterResult, list);
     }
     finally
     {
         InternalClose();
     }
 }
Ejemplo n.º 13
0
 private void ProcessMethods(Type type, OrlTable 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);
         }
     }
 }
Ejemplo n.º 14
0
        public virtual IList Exec(Type type, string procName, string[] parameterName, object[] parameters)
        {
            ErrorIfClosed();
            IList list = new ArrayList();

            try
            {
                InternalOpen();
                OrlTable table = TableFor(type);
                table.Exec(this, procName, parameterName, parameters, list);
            }
            finally
            {
                InternalClose();
            }
            return(list);
        }
Ejemplo n.º 15
0
        public virtual object Find(Type type, object key)
        {
            ErrorIfClosed();
            object objFind = null;

            try
            {
                InternalOpen();
                OrlTable table = TableFor(type);
                objFind = table.Find(this, key);
            }
            finally
            {
                InternalClose();
            }
            return(objFind);
        }
Ejemplo n.º 16
0
 private void FindInherited(Type type, OrlTable 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);
         }
     }
 }
Ejemplo n.º 17
0
		public virtual string GetSql(OrlTable 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);
			}
		}
Ejemplo n.º 18
0
        public virtual string GetSql(OrlTable 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);
                }
                OrlConstraint 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(",");
                    }
                    OrlOrder order = orders[i];
                    string   sql   = order.GetSql(table);
                    buf.Append(sql);
                }
            }
            buf.Append(";");
            return(buf.ToString());
        }
Ejemplo n.º 19
0
 public virtual string GetSql(OrlTable 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));
     }
 }
Ejemplo n.º 20
0
        public virtual int Delete(Type type, ICollection items)
        {
            ErrorIfClosed();
            if (items.Count == 0)
            {
                return(0);
            }
            int result = 0;

            try
            {
                InternalOpen();
                OrlTable table = TableFor(type);
                if (autocommit)
                {
                    BeginInternal();
                }
                result = table.Delete(this, items);
                if (autocommit)
                {
                    CommitInternal();
                }
            }
            catch (Exception)
            {
                if (autocommit)
                {
                    RollbackInternal();
                }
                throw;
            }
            finally
            {
                InternalClose();
            }
            return(result);
        }
Ejemplo n.º 21
0
 private void ProcessFields(Type type, OrlTable table)
 {
     FieldInfo[] fields = type.GetFields(
         BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
     foreach (FieldInfo field in fields)
     {
         if (!field.IsDefined(typeof(ColumnAttribute), false))
         {
             continue;
         }
         ColumnAttribute colAttr = (ColumnAttribute)
                                   field.GetCustomAttributes(typeof(ColumnAttribute), false)[0];
         FieldBridge data = new FieldBridge(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);
     }
 }
Ejemplo n.º 22
0
 private void ProcessMethods(Type type, OrlTable 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);
         }
     }
 }
Ejemplo n.º 23
0
        public virtual string GetSql(OrlTable table)
        {
            int offset = 1;

            return(GetSql(table, ref offset));
        }
Ejemplo n.º 24
0
        public virtual string GetSql(OrlTable 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);
                }
                OrlConstraint 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(",");
                    OrlOrder order = orders[i];
                    string sql = order.GetSql(table);
                    buf.Append(sql);
                }
            }
            buf.Append(";");
            return buf.ToString();
        }
Ejemplo n.º 25
0
 public virtual string GetSql(OrlTable table)
 {
     int offset = 1;
     return GetSql(table, ref offset);
 }
Ejemplo n.º 26
0
 private void ProcessProperties(Type type, OrlTable 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);
     }
 }