Ejemplo n.º 1
0
        public object Execute(Query query) {
            var cmd = this.NewCommand(query);
            object id = null;

            try {
                this.Connection.Open();
                //cmd.ExecuteNonQuery();
                id = cmd.ExecuteScalar();
                //this.Bubble(query);
            } catch (Exception ex) {
                ex = new DatabaseException(this.DatabaseName, query, "Execute Error-> " + ex.Message, ex);

                //Self.Fork(delegate(object evt) {
                //    this.Source.Bubble((DatabaseEvent)evt);
                //}, new DatabaseEvent(query, TimeSpan.FromMilliseconds(this.ElapsedTS), ex));

                throw ex;
            } finally {
                cmd.Dispose();
                this.Connection.TryClose();
            }


            if (!query.HasOutputs)
                return id;

            var outputs = query.Outputs;
            if (outputs.Count == 1)
                return outputs[0].Value;

            return outputs.Select(x => x.Value).ToList();
        }
Ejemplo n.º 2
0
 public virtual void Bind(DbCommand cmd, Query query) {
     cmd.CommandType = query.Type;
     cmd.CommandText = query.Sql;
     cmd.CommandTimeout = 30;
     foreach (var param in query.Parameters) {
         var p = this.CreateParameter(param);
         //if(param.Direction == ParameterDirection.Output){
         //    p.Size = 16;
         //}
         cmd.Parameters.Add(p);
     }
 }
Ejemplo n.º 3
0
        private static void Append(StringBuilder buffer, Query query = null, string databaseName = null) {
            buffer.AppendLine();
            buffer.AppendLine("-------------------------------------------------------------");
            if(!String.IsNullOrEmpty(databaseName))
                buffer.AppendLine("Database: " + databaseName);
            if (query != null) {
                buffer.AppendLine(query.Sql);
                var parameters = query.Parameters;
                foreach (var parameter in parameters) {
                    if (parameter.Direction == System.Data.ParameterDirection.Input) {
                        buffer.AppendLine(" @" + parameter.Name + " = " + Convert.ToString(parameter.Value));
                    }
                }


                foreach (var parameter in parameters) {
                    if (parameter.Direction != System.Data.ParameterDirection.Input) {
                        buffer.AppendLine(" [" + parameter.Direction.ToString() + "] @" + parameter.Name + " = " + Convert.ToString(parameter.Value));
                    }
                }
                buffer.AppendLine();
            }
        }
Ejemplo n.º 4
0
        public DataSet Select(Query query) {
            var cmd = this.NewCommand(query);
            var adapter = this.Source.Driver.CreateAdapter(cmd);
            DataSet ds = new DataSet();
            try {
                this.Connection.Open();
                
                adapter.Fill(ds);
                
                //this.Bubble(query);
            } catch (Exception ex) {
                ex = new DatabaseException(this.DatabaseName, query, "Select Error-> " + ex.Message, ex);
                //Self.Fork(delegate(object evt) {
                //    this.Source.Bubble((DatabaseEvent)evt);
                //}, new DatabaseEvent(query, TimeSpan.FromMilliseconds(this.ElapsedTS), ex));

                throw ex;
            } finally {
                cmd.Dispose();
                this.Connection.TryClose();
            }
            return ds;
        }
Ejemplo n.º 5
0
 public virtual Query Clone() {
     var query = new Query(this.Sql);
     //query.Parameters.AddRange(this.Parameters);
     return query;
 }
Ejemplo n.º 6
0
        public object ExecuteScalar(Query query) {
            var cmd = this.NewCommand(query);
            Object o = null;
            try {
                this.Connection.Open();
                o = cmd.ExecuteScalar();
                //this.Bubble(query);
            } catch (Exception ex) {
                //ex = new DataException("Database.ExecuteScalar Error-> " + ex.Message, ex.InnerException);
                ex = new DatabaseException(this.DatabaseName, query, "ExecuteScalar Error-> " + ex.Message, ex);

                //Self.Fork(delegate(object evt) {
                //    this.Source.Bubble((DatabaseEvent)evt);
                //}, new DatabaseEvent(query, TimeSpan.FromMilliseconds(this.ElapsedTS), ex));

                throw ex;
            } finally {
                cmd.Dispose();
                this.Connection.TryClose();
            }

            return o;
        }
Ejemplo n.º 7
0
        public DataTable SelectTable(Query query) {
            DataSet ds = this.Select(query);
            DataTable table = (ds.Tables.Count > 0) ? ds.Tables[0] : null;

            return table;
        }
Ejemplo n.º 8
0
 private DbCommand NewCommand(Query query) {
     var cmd = this.Source.Driver.CreateCommand(this.Connection);
     this.Source.Driver.Bind(cmd, query);
     return cmd;
 }
Ejemplo n.º 9
0
        public void ExecuteReader(Query query, Action<RecordReader> recordReader) {
            var cmd = this.NewCommand(query);
            try {
                this.Connection.Open();

                using (RecordReader reader = new RecordReader(cmd.ExecuteReader() as DbDataReader)) {
                    if (reader.HasRows) {
                        while (reader.Read()) {
                            recordReader(reader);
                        }
                    }

                    reader.Destroy();
                }

                //this.Bubble(query);
            } catch (Exception ex) {
                //ex = new DataException("Database.ExecuteReader Error-> " + ex.Message, ex.InnerException);
                ex = new DatabaseException(this.DatabaseName, query, "ExecuteReader Error-> " + ex.Message, ex);
                //Self.Fork(delegate(object evt) {
                //    this.Source.Bubble((DatabaseEvent)evt);
                //}, new DatabaseEvent(query, TimeSpan.FromMilliseconds(this.ElapsedTS), ex));

                throw ex;
            } finally {
                cmd.Dispose();
                this.Connection.TryClose();
            }
        }
Ejemplo n.º 10
0
 private static string Merge(string msg, Query query, string databaseName = null) {
     var buffer = new StringBuilder(msg);
     Append(buffer, query, databaseName);
     return buffer.ToString();
 }
Ejemplo n.º 11
0
 public DatabaseException(string databaseName, Query query, string msg, Exception inner)
     : base(Merge(msg, query, databaseName), inner) {
         this.Query = query;
 }
Ejemplo n.º 12
0
 public DatabaseException(Query query, string msg, Exception inner)
     : base(Merge(msg, query), inner) {
         this.Query = query;
 }
Ejemplo n.º 13
0
 public DatabaseException(Query query, string msg)
     : base(Merge(msg, query)) {
         this.Query = query;
 }