Beispiel #1
0
 public static SqlQueryParameters WithParser(ISqlProc proc, Action <SqlTableMapper, DbDataReader> propertiesParser = null)
 {
     return(new SqlQueryParameters()
     {
         proc = proc, mapper = new SqlObjTableReader(propertiesParser)
     });
 }
Beispiel #2
0
 public static Func <SqlDataReader> LazyReader(ISqlProc proc
                                               , Action <SqlDataReader> readerGet
                                               , Action <SqlCommand> setupCmd = null
                                               , Action <Exception> onError   = null)
 {
     return(SqlMultiDyn.LazyReader(proc, readerGet, setupCmd, onError));
 }
Beispiel #3
0
        public static Func <SqlDataReader> LazyReader(this ISqlProc proc
                                                      , Action <SqlDataReader> readerGet
                                                      , Action <SqlCommand> setupCmd = null
                                                      , Action <Exception> onError   = null)
        {
            return(new Func <SqlDataReader>(() =>
            {
                SqlDataReader reader = null;
                string errorStr = null;
                try
                {
                    reader = ExecMultiReader(proc, setupCmd, onError: null, progress: null);
                }
                catch (Exception ex)
                {
                    errorStr = String.Format("error in sql {0}: {1}", proc.CmdText ?? "???", ex.Message);
                    System.Diagnostics.Trace.Write(errorStr);
                    if (onError != null)
                    {
                        onError(ex);
                    }
                }

                // System.AppDomain.CurrentDomain.UnhandledException
                if (reader == null || reader.IsClosed || reader.Depth != 0)
                {
                    return null;
                }

                readerGet(reader);
                return reader;
            }));
        }
Beispiel #4
0
        public static IEnumerable <object[]> ExecMulti(this ISqlProc proc, SqlDataReader dataReader,
                                                       IDataMapHelper <object[]> mapper,
                                                       Action <SqlDataReader, SqlField[]> onReadFields = null,
                                                       Action <double> progress = null)
        {
            var helper = mapper;

            helper.GetProperties(dataReader);

            if (onReadFields != null)
            {
                onReadFields(dataReader, helper.GetFields(dataReader));
            }

            do
            {
                object[] objVal = helper.DbRecordArray();
                dataReader.GetValues(objVal);

                object[] val = helper.SetValues(objVal);
                yield return(val);
            }while (dataReader.Read());

            if (progress != null)
            {
                progress(1.0);
            }
        }
Beispiel #5
0
        public static ResultDyn ExecProcResultDyn(this ISqlProc proc
                                                  , Action <SqlCommand> setupCmd = null
                                                  , Action <Exception> onError   = null)
        {
            SqlDataReader reader = null;

            return(DbEnumeratorData.GetResultDyn(SqlMultiDyn.LazyReader(proc, (r) => reader = r, setupCmd, onError)));
        }
Beispiel #6
0
 public MultiResult(ISqlProc proc = null)
 {
     Count = 1;
     // mapper = null;
     this.proc = proc;
     reader    = null;
     mapHelper = null;
 }
Beispiel #7
0
        // public static IEnumerator<ExpandoObject>
        public static DbEnumeratorData <ExpandoObject> ResultDyn(this ISqlProc proc
                                                                 , Action <SqlDataReader> readerGet, Action <SqlCommand> setupCmd = null
                                                                 , Action <Exception> onError = null)
        {
            Func <SqlDataReader> lazyReader = LazyReader(proc, readerGet, setupCmd, onError);
            var dynNumerator = new DbEnumeratorData <ExpandoObject>(lazyReader);

            return(dynNumerator); // as IEnumerator<ExpandoObject>;
        }
Beispiel #8
0
        // in T
        public static bool ExecFill <T>(this ISqlProc proc, IList <T> list,
                                        IDataMapHelper <T> mapper,
                                        Action <double> progress = null) where T : class // , TMap : IDataMapHelper
        {
            Ai.Guard.Check(proc.Connection != null, "proc.Connection null error in ExecFill");
            Ai.Guard.Check(list != null, "list null error in ExecFill");
            if (proc.Connection.State != ConnectionState.Open)
            {
                proc.Connection.Open();
            }

            using (var command = proc.CreateCommand())
            {
                if (progress != null)
                {
                    progress(0.0);
                }

                using (SqlDataReader dataReader = command.ExecuteReader())
                {
                    var results = list as IList <T>;
                    if (!dataReader.Read())
                    {
                        if (progress != null)
                        {
                            progress(1.0);
                        }
                        return(false);
                    }

                    // Task<T> GetFieldValueAsync<T>(int i, CancellationToken cancellationToken);
                    var helper = mapper; //  new DbDataMapHelper<T>();
                    helper.GetProperties(dataReader);

                    do
                    {
                        object[] objVal = helper.DbRecordArray();
                        // int ret =
                        dataReader.GetValues(objVal);
                        T val = helper.SetValues(objVal);
                        results.Add(val);
                    }while (dataReader.Read());

                    if (progress != null)
                    {
                        progress(1.0);
                    }

                    return(true);
                }
            }
        }
Beispiel #9
0
        public bool Prepare(ISqlProc proc, Action <SqlTableMapper, DbDataReader> parser = null, int?commandTimeout = null)
        {
            if (state != StateExec.Init)
            {
                this.Dispose();
            }
            if (parser != null)
            {
                base.propertiesParser = parser;
            }

            LastRow = null;
            var conn = proc.OpenConnection();

            if (conn == null)
            {
                return(false);
            }
            cmd            = proc.CreateCommand();
            cmd.Connection = conn;
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }

            if (commandTimeout.HasValue)
            {
                cmd.CommandTimeout = commandTimeout.Value;
            }

            cmd.Prepare();
            dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection | CommandBehavior.SingleResult);

            state = StateExec.Prepared;

            if (dataReader.Read())
            {
                // First record
                (this as IDataMapHelper <object[]>).GetProperties(dataReader);

                var      helper = this;
                object[] objVal = helper.DbRecordArray();
                // first record array
                dataReader.GetValues(objVal);
                LastRow = helper.SetValues(objVal);
                return(true);
            }

            return(false);
        }
Beispiel #10
0
        private bool ExecProc(ISqlProc proc, int iFrom = 0, int iTake = 0)
        {
            Ai.Guard.Check(proc.CmdText.Length > 0, "SqlTable proc error");
            Ai.Guard.Check(Fields != null, "SqlTable Fields error");

            var fld = this.Fields;

            Action <SqlTableMapper, DbDataReader> parser = (map, dbReader) =>
            {
                fld         = SqlFieldArray.GetArray(dbReader);
                this.Fields = fld;
            };

            bool init = this.queryParam.Prepare(proc, parser, this.Timeout);

            Guard.Check(fld.Length > 0, "fld.Length error");

            return(init);
        }
Beispiel #11
0
        public static SqlDataReader ExecuteWithReconnect(this ISqlProc proc)
        {
            var lastErr = proc.LastError;
            var cmd     = proc.CreateCommand() as SqlCommand;

            var reader = ExecuteReaderRetry(cmd, proc,
                                            retry: () => GetSqlCommandReconnect(proc));

            lastErr = proc.LastError;
            if (lastErr is SqlException)
            {
                throw lastErr;
            }
            else if (reader == null)
            {
                proc.LastError = new Exception(String.Format("procedure error {0}", proc));
                throw proc.LastError;
            }

            return(reader);
        }
Beispiel #12
0
        public static ISqlProc PrepareWithReconnect(this ISqlProc proc, Context db)
        {
            var conn = proc.Connection;

            if (conn == null)
            {
                proc.Connection = proc.OpenConnection();
            }
            conn = proc.Connection;

            try
            {
                var dbName = conn.Database;
                var cmd    = new SqlCommand {
                    CommandText = "SELECT DB_NAME()"
                };                                                              // neutral command
                var result = cmd.ExecuteScalar() as string;
            }
            catch (Exception ex) { proc.LastError = ex; }
            return(proc);
        }
Beispiel #13
0
        public MultiResult <T> Prepare(SqlProc proc
                                       , Action <SqlCommand> setup        = null
                                       , Action <SqlField[]> onReadFields = null
                                       , bool noMoveFirst = false
                                       )
        {
            this.proc = proc;
            var mapper = new DbObject();

            numerator = DbEnumeratorData.GetEnumerator(() =>
            {
                this.reader = SqlMultiDyn.ExecMultiReader(proc, setup, progress: null);
                return(reader);
            });

            if (numerator == null)
            {
                return(null);
            }
            if (reader.IsClosed || reader.Depth != 0)
            {
                return(null);
            }

            if (noMoveFirst)
            {
                return(this);
            }

            numerator.MoveNext();
            var rec = numerator.Current as object[]; // DbDataRecord;

            mapHelper = mapper.GetProperties(reader);

            Fields      = reader.GetFields(); // SqlFieldArray
            FirstRecord = rec;
            // mapHelper.SetValues(mapHelper.DbRecordArray());
            return(this);
        }
Beispiel #14
0
        public static SqlConnection PreparedSqlConnection(this ISqlProc proc)
        {
            if (proc == null)
            {
                return(null);
            }
            if (proc.Connection.IsReady())
            {
                return(proc.Connection as SqlConnection);
            }

            proc.Connection     = proc.OpenConnection();
            proc.CloseOnDispose = true;

            if (proc is ISqlProcContext && string.IsNullOrWhiteSpace(proc.Connection.ConnectionString))
            {
                var procDb = proc as ISqlProcContext;
                proc.Connection = procDb.Context.SqlConnection;
            }

            return(proc.Connection as SqlConnection);
        }
Beispiel #15
0
 public static SqlCommand GetSqlCommandReconnect(this ISqlProc proc)
 {
     proc.Connection     = null;
     proc.CloseOnDispose = true;
     if (proc is SqlProc)
     {
         var sqlProc = proc as SqlProc;
         if (sqlProc.Context != null)
         {
             sqlProc.Context.AssureOpen(withCommand: true);
             sqlProc.Connection = sqlProc.Context.SqlConnection;
         }
         else
         {
             sqlProc.OpenConnection();
         }
     }
     else
     {
         proc.Connection = proc.OpenConnection();
     }
     return(proc.CreateCommand() as SqlCommand);
 }
Beispiel #16
0
        public static IEnumerable <object[]> ExecEnumerable(this ISqlProc proc,
                                                            IDataMapHelper <object[]> mapper,
                                                            Action <double> progress = null, Action <SqlField[]> onReadFields = null)
        {
            Ai.Guard.Check(proc.Connection != null, "proc.Connection null error in ExecFill");

            using (SqlConnection connection = new SqlConnection(proc.ConnectionString()))
            {
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                    if (connection.State != ConnectionState.Open)
                    {
                        yield break;
                    }
                }
                if (connection.Database != proc.DbName)
                {
                    connection.ChangeDatabase(proc.DbName);
                }

                using (var command = proc.CreateCommand())
                {
                    command.Connection = connection;

                    if (progress != null)
                    {
                        progress(0.0);
                    }

                    using (SqlDataReader dataReader = command.ExecuteReader())
                    {
                        if (!dataReader.Read())
                        {
                            if (progress != null)
                            {
                                progress(1.0);
                            }

                            yield break;
                        }

                        // Task<T> GetFieldValueAsync<T>(int i, CancellationToken cancellationToken);
                        var helper = mapper; //  new DbDataMapHelper<T>();
                        helper.GetProperties(dataReader);

                        if (onReadFields != null)
                        {
                            onReadFields(helper.GetFields(dataReader));
                        }

                        do
                        {
                            object[] objVal = helper.DbRecordArray();
                            dataReader.GetValues(objVal);

                            object[] val = helper.SetValues(objVal);
                            yield return(val);
                        }while (dataReader.Read());

                        if (progress != null)
                        {
                            progress(1.0);
                        }
                    }
                }
            }
        }
Beispiel #17
0
        // Unsafe
        public static SqlDataReader ExecMultiReader(this ISqlProc proc,
                                                    Action <SqlCommand> setup  = null,
                                                    Action <Exception> onError = null,
                                                    Action <double> progress   = null)
        {
            var command = proc.CreateCommand();
            //if (command.Connection == null || command.Connection.State != ConnectionState.Open)

            var connStr = proc.ConnectionString();

            Guard.Check(!string.IsNullOrWhiteSpace(connStr), "Exec MultiReader connection error");

            SqlConnection connection = new SqlConnection(connStr);

            if (connection.State != ConnectionState.Open)
            {
                connection.Open();
                if (connection.State != ConnectionState.Open)
                {
                    return(null);
                }
            }
            if (connection.Database != proc.DbName)
            {
                connection.ChangeDatabase(proc.DbName);
            }
            command.Connection = connection;

            if (setup != null)
            {
                setup(command);
            }

            if (progress != null)
            {
                progress(0.0);
            }

            SqlDataReader dataReader = null;

            if (onError != null)
            {
                try
                {
                    dataReader = command.ExecuteReader(behavior: CommandBehavior.CloseConnection);
                }
                catch (Exception ex) { onError(ex); }
            }
            else
            {
                dataReader = command.ExecuteReader(behavior: CommandBehavior.CloseConnection);
            }

            if (dataReader != null && dataReader.IsClosed) // !dataReader.Read())
            {
                if (progress != null)
                {
                    progress(1.0);
                }

                return(null);
            }

            return(dataReader);
        }
Beispiel #18
0
 public bool Prepare(ISqlProc proc, Action <SqlTableMapper, DbDataReader> parser = null, int?commandTimeout = null)
 {
     this.proc = proc;
     return(IsEmpty() || mapper.Prepare(proc, parser, commandTimeout));
 }