Пример #1
0
        public IEnumerable <T> ExecuteQuery <T> () where T : new()
        {
            var stmt = Prepare();

            var props = Orm.GetColumns(typeof(T));
            var cols  = new System.Reflection.PropertyInfo[SQLite3.ColumnCount(stmt)];

            for (int i = 0; i < cols.Length; i++)
            {
                cols[i] = MatchColProp(SQLite3.ColumnName(stmt, i), props);
            }

            while (SQLite3.Step(stmt) == SQLite3.Result.Row)
            {
                var obj = new T();
                for (int i = 0; i < cols.Length; i++)
                {
                    if (cols[i] == null)
                    {
                        continue;
                    }
                    var val = ReadCol(stmt, i, cols[i].PropertyType);
                    cols[i].SetValue(obj, val, null);
                }
                yield return(obj);
            }

            SQLite3.Finalize(stmt);
        }
Пример #2
0
        public int ExecuteNonQuery()
        {
            var r = SQLite3.Result.OK;

            for (int i = 0; i < _conn.MaxExecuteAttempts; i++)
            {
                var stmt = Prepare();
                r = SQLite3.Step(stmt);
                SQLite3.Finalize(stmt);
                if (r == SQLite3.Result.Error)
                {
                    string msg = SQLite3.GetErrmsg(_conn.Handle);
                    throw SQLiteException.New(r, msg);
                }
                else if (r == SQLite3.Result.Done)
                {
                    int rowsAffected = SQLite3.Changes(_conn.Handle);
                    return(rowsAffected);
                }
                else if (r == SQLite3.Result.Busy)
                {
                    // We will retry
                    System.Threading.Thread.Sleep(1000);
                }
                else
                {
                    throw SQLiteException.New(r, r.ToString());
                }
            }
            throw SQLiteException.New(r, r.ToString());
        }
Пример #3
0
        public IEnumerable <object> ExecuteQuery(TableMapping map)
        {
            if (_conn.Trace)
            {
                Console.WriteLine("Executing Query: " + this);
            }

            var stmt = Prepare();

            var cols = new TableMapping.Column[SQLite3.ColumnCount(stmt)];

            for (int i = 0; i < cols.Length; i++)
            {
                var name = Marshal.PtrToStringUni(SQLite3.ColumnName16(stmt, i));
                cols[i] = map.FindColumn(name);
            }

            while (SQLite3.Step(stmt) == SQLite3.Result.Row)
            {
                var obj = Activator.CreateInstance(map.MappedType);
                map.SetConnection(obj, _conn);
                for (int i = 0; i < cols.Length; i++)
                {
                    if (cols[i] == null)
                    {
                        continue;
                    }
                    var val = ReadCol(stmt, i, cols[i].ColumnType);
                    cols[i].SetValue(obj, val);
                }
                yield return(obj);
            }

            SQLite3.Finalize(stmt);
        }
Пример #4
0
        public T ExecuteScalar <T>()
        {
            if (_conn.Trace)
            {
                _conn.InvokeTrace("Executing Query: " + this);
            }
            T result = default(T);

            lock (_conn.SyncObject)
            {
                IntPtr stmt = Prepare();
                try
                {
                    SQLite3.Result result2 = SQLite3.Step(stmt);
                    switch (result2)
                    {
                    case SQLite3.Result.Row:
                    {
                        SQLite3.ColType type = SQLite3.ColumnType(stmt, 0);
                        return((T)ReadCol(stmt, 0, type, typeof(T)));
                    }

                    default:
                        throw SQLiteException.New(result2, SQLite3.GetErrmsg(_conn.Handle));

                    case SQLite3.Result.Done:
                        return(result);
                    }
                }
                finally
                {
                    Finalize(stmt);
                }
            }
        }
Пример #5
0
        public int ExecuteNonQuery()
        {
            if (_conn.Trace)
            {
                _conn.InvokeTrace("Executing: " + this);
            }
            SQLite3.Result result = SQLite3.Result.OK;
            lock (_conn.SyncObject)
            {
                IntPtr stmt = Prepare();
                result = SQLite3.Step(stmt);
                Finalize(stmt);
            }
            switch (result)
            {
            case SQLite3.Result.Done:
                return(SQLite3.Changes(_conn.Handle));

            case SQLite3.Result.Error:
            {
                string errmsg = SQLite3.GetErrmsg(_conn.Handle);
                throw SQLiteException.New(result, errmsg);
            }

            case SQLite3.Result.Constraint:
                if (SQLite3.ExtendedErrCode(_conn.Handle) == SQLite3.ExtendedResult.ConstraintNotNull)
                {
                    throw NotNullConstraintViolationException.New(result, SQLite3.GetErrmsg(_conn.Handle));
                }
                break;
            }
            throw SQLiteException.New(result, result.ToString());
        }
Пример #6
0
        public int ExecuteNonQuery()
        {
            if (_conn.Trace)
            {
                Debug.WriteLine("Executing: " + this);
            }

            var r    = SQLite3.Result.OK;
            var stmt = Prepare();

            r = SQLite3.Step(stmt);
            Finalize(stmt);
            if (r == SQLite3.Result.Done)
            {
                int rowsAffected = SQLite3.Changes(_conn.Handle);
                return(rowsAffected);
            }
            else if (r == SQLite3.Result.Error)
            {
                string msg = SQLite3.GetErrmsg(_conn.Handle);
                throw SQLiteException.New(r, msg);
            }
            else if (r == SQLite3.Result.Constraint)
            {
                if (SQLite3.ExtendedErrCode(_conn.Handle) == SQLite3.ExtendedResult.ConstraintNotNull)
                {
                    throw NotNullConstraintViolationException.New(r, SQLite3.GetErrmsg(_conn.Handle));
                }
            }

            throw SQLiteException.New(r, r.ToString());
        }
Пример #7
0
        public T ExecuteScalar <T>()
        {
            if (_conn.Trace)
            {
                Debug.WriteLine("Executing Query: " + this);
            }

            T val = default(T);

            var stmt = Prepare();

            try {
                var r = SQLite3.Step(stmt);
                if (r == SQLite3.Result.Row)
                {
                    var colType = SQLite3.ColumnType(stmt, 0);
                    val = (T)ReadCol(stmt, 0, colType, typeof(T));
                }
                else if (r == SQLite3.Result.Done)
                {
                }
                else
                {
                    throw SQLiteException.New(r, SQLite3.GetErrmsg(_conn.Handle));
                }
            } finally {
                Finalize(stmt);
            }

            return(val);
        }
Пример #8
0
        /// <summary>
        /// Executes the query.
        /// </summary>
        /// <param name="maxRows">
        /// The maximum rows.
        /// </param>
        /// <returns>
        /// The <see cref="ResultCollection"/>.
        /// </returns>
        public virtual ResultCollection ExecuteQuery(int maxRows)
        {
            ResultCollection results;
            IntPtr           stmt = IntPtr.Zero;

            try
            {
                stmt = this.Prepare();
                var cols = new ColumnInfoExt[SQLite3.ColumnCount(stmt)];

                for (var i = 0; i < cols.Length; i++)
                {
                    cols[i] = new ColumnInfoExt {
                        Name = SQLite3.ColumnName16(stmt, i), ColumnIndex = i
                    };
                }

                results = new ResultCollection(cols);

                var rows = 0;
                while (SQLite3.Step(stmt) == SQLite3.Result.Row)
                {
                    var obj = new ResultRow();
                    for (var i = 0; i < cols.Length; i++)
                    {
                        if (cols[i] == null)
                        {
                            continue;
                        }

                        var colType = SQLite3.ColumnType(stmt, i);
                        cols[i].ColumnMainType = colType;

                        var val = this.ReadCol(stmt, i, colType, cols[i].ColumnClrType);
                        obj[cols[i].ColumnIndex] = val;
                    }

                    results.Add(obj);

                    if (maxRows != 0 && ++rows == maxRows)
                    {
                        break;
                    }
                }
            }
            finally
            {
                if (stmt != IntPtr.Zero)
                {
                    SQLite3.Finalize(stmt);
                }
            }

            return(results);
        }
Пример #9
0
        public T ExecuteScalar <T> ()
        {
            T val = default(T);

            var stmt = Prepare();

            if (SQLite3.Step(stmt) == SQLite3.Result.Row)
            {
                val = (T)ReadCol(stmt, 0, typeof(T));
            }
            SQLite3.Finalize(stmt);

            return(val);
        }
Пример #10
0
        public int ExecuteNonQuery(object[] source)
        {
            if (Connection.Trace)
            {
                Debug.WriteLine("Executing: " + CommandText);
            }

            var r = SQLite3.Result.OK;

            if (!Initialized)
            {
                Statement   = Prepare();
                Initialized = true;
            }

            //bind the values.
            if (source != null)
            {
                for (int i = 0; i < source.Length; i++)
                {
                    SQLiteCommand.BindParameter(Statement, i + 1, source[i], Connection.StoreDateTimeAsTicks);
                }
            }
            r = SQLite3.Step(Statement);

            if (r == SQLite3.Result.Done)
            {
                int rowsAffected = SQLite3.Changes(Connection.Handle);
                SQLite3.Reset(Statement);
                return(rowsAffected);
            }
            else if (r == SQLite3.Result.Error)
            {
                string msg = SQLite3.GetErrmsg(Connection.Handle);
                SQLite3.Reset(Statement);
                throw SQLiteException.New(r, msg);
            }
            else if (r == SQLite3.Result.Constraint && SQLite3.ExtendedErrCode(Connection.Handle) == SQLite3.ExtendedResult.ConstraintNotNull)
            {
                SQLite3.Reset(Statement);
                throw NotNullConstraintViolationException.New(r, SQLite3.GetErrmsg(Connection.Handle));
            }
            else
            {
                SQLite3.Reset(Statement);
                throw SQLiteException.New(r, r.ToString());
            }
        }
Пример #11
0
        public int ExecuteNonQuery(object[] source)
        {
            if (Connection.Trace)
            {
                Connection.InvokeTrace("Executing: " + CommandText);
            }
            SQLite3.Result result = SQLite3.Result.OK;
            if (!Initialized)
            {
                Statement   = Prepare();
                Initialized = true;
            }
            if (source != null)
            {
                for (int i = 0; i < source.Length; i++)
                {
                    SQLiteCommand.BindParameter(Statement, i + 1, source[i], Connection.StoreDateTimeAsTicks);
                }
            }
            result = SQLite3.Step(Statement);
            switch (result)
            {
            case SQLite3.Result.Done:
            {
                int result2 = SQLite3.Changes(Connection.Handle);
                SQLite3.Reset(Statement);
                return(result2);
            }

            case SQLite3.Result.Error:
            {
                string errmsg = SQLite3.GetErrmsg(Connection.Handle);
                SQLite3.Reset(Statement);
                throw SQLiteException.New(result, errmsg);
            }

            case SQLite3.Result.Constraint:
                if (SQLite3.ExtendedErrCode(Connection.Handle) == SQLite3.ExtendedResult.ConstraintNotNull)
                {
                    SQLite3.Reset(Statement);
                    throw NotNullConstraintViolationException.New(result, SQLite3.GetErrmsg(Connection.Handle));
                }
                break;
            }
            SQLite3.Reset(Statement);
            throw SQLiteException.New(result, result.ToString());
        }
Пример #12
0
        private Result InternalExecute(object[] source)
        {
            var r = Result.OK;

            if (!Prepared)
            {
                Statement = Prepare();
                Prepared  = true;
            }

            //bind the values.
            if (source != null)
            {
                for (int i = 0; i < source.Length; i++)
                {
                    BindParameter(Statement, i + 1, source[i], Connection.StoreDateTimeAsTicks);
                }
            }

            r = SQLite3.Step(Statement);

            if (r == Result.Done || r == Result.OK || r == Result.Row)
            {
                return(r);
            }
            else
            {
                var msg = SQLite3.GetErrorMessageUTF8(Connection.Handle);

                if ((ExtendedResult)r == ExtendedResult.ConstraintNotNull)
                {
                    throw new NotNullConstraintViolationException(r, msg, sql: CommandText);
                }

                if ((ExtendedResult)r == ExtendedResult.ConstraintUnique)
                {
                    throw new UniqueConstraintViolationException(r, msg, sql: CommandText);
                }

                throw new SQLiteException(r, msg, sql: CommandText);
            }
        }
Пример #13
0
 public IEnumerable <T> ExecuteDeferredQuery <T>(TableMapping map)
 {
     if (_conn.Trace)
     {
         _conn.InvokeTrace("Executing Query: " + this);
     }
     lock (_conn.SyncObject)
     {
         IntPtr stmt = Prepare();
         try
         {
             TableMapping.Column[] cols = new TableMapping.Column[SQLite3.ColumnCount(stmt)];
             for (int j = 0; j < cols.Length; j++)
             {
                 string name = SQLite3.ColumnName16(stmt, j);
                 cols[j] = map.FindColumn(name);
             }
             while (SQLite3.Step(stmt) == SQLite3.Result.Row)
             {
                 object obj = Activator.CreateInstance(map.MappedType);
                 for (int i = 0; i < cols.Length; i++)
                 {
                     if (cols[i] != null)
                     {
                         SQLite3.ColType colType = SQLite3.ColumnType(stmt, i);
                         object          val     = ReadCol(stmt, i, colType, cols[i].ColumnType);
                         cols[i].SetValue(obj, val);
                     }
                 }
                 OnInstanceCreated(obj);
                 yield return((T)obj);
             }
         }
         finally
         {
             ((_003CExecuteDeferredQuery_003Ec__Iterator3 <T>) /*Error near IL_0234: stateMachine*/)._003C_003E__Finally0();
         }
     }
 }
Пример #14
0
        public IEnumerable <T> ExecuteDeferredQuery <T>(TableMapping map)
        {
            if (_conn.Trace)
            {
                Debug.WriteLine("Executing Query: " + this);
            }

            var stmt = Prepare();

            try {
                var cols = new TableMapping.Column[SQLite3.ColumnCount(stmt)];

                for (int i = 0; i < cols.Length; i++)
                {
                    var name = SQLite3.ColumnName16(stmt, i);
                    cols[i] = map.FindColumn(name);
                }

                while (SQLite3.Step(stmt) == SQLite3.Result.Row)
                {
                    var obj = Activator.CreateInstance(map.MappedType);
                    for (int i = 0; i < cols.Length; i++)
                    {
                        if (cols[i] == null)
                        {
                            continue;
                        }
                        var colType = SQLite3.ColumnType(stmt, i);
                        var val     = ReadCol(stmt, i, colType, cols[i].ColumnType);
                        cols[i].SetValue(obj, val);
                    }
                    OnInstanceCreated(obj);
                    yield return((T)obj);
                }
            } finally {
                SQLite3.Finalize(stmt);
            }
        }
Пример #15
0
        public int ExecuteNonQuery()
        {
            var stmt = Prepare();

            var r = SQLite3.Step(stmt);

            if (r == SQLite3.Result.Error)
            {
                string msg = SQLite3.Errmsg(_db);
                SQLite3.Finalize(stmt);
                throw SQLiteException.New(r, msg);
            }
            else if (r == SQLite3.Result.Done)
            {
                int rowsAffected = SQLite3.Changes(_db);
                SQLite3.Finalize(stmt);
                return(rowsAffected);
            }
            else
            {
                SQLite3.Finalize(stmt);
                throw SQLiteException.New(r, "Unknown error");
            }
        }
Пример #16
0
        public IEnumerable <T> ExecuteQuery <T>(TableMapping map, object[] source)
        {
            CheckDisposed();
            Log(nameof(ExecuteQuery), source);
            OnExecutionStarted();

            var sw = Stopwatch.StartNew();

            try
            {
                var r = Result.OK;

                if (!Prepared)
                {
                    Statement = Prepare();
                    Prepared  = true;
                }

                //bind the values.
                if (source != null)
                {
                    for (int i = 0; i < source.Length; i++)
                    {
                        BindParameter(Statement, i + 1, source[i], Connection.StoreDateTimeAsTicks);
                    }
                }

                var cols = new TableColumn[SQLite3.ColumnCount(Statement)];

                for (int i = 0; i < cols.Length; i++)
                {
                    var name = SQLite3.ColumnNameUTF8(Statement, i);
                    cols[i] = map.FindColumn(name);
                }

                while (SQLite3.Step(Statement) == Result.Row)
                {
                    var obj = map.CreateInstance();
                    for (int i = 0; i < cols.Length; i++)
                    {
                        if (cols[i] == null)
                        {
                            continue;
                        }
                        var colType = SQLite3.ColumnType(Statement, i);
                        var val     = ReadCol(Statement, i, colType, cols[i].ColumnType, cols[i].PropertyDefaultValue);
                        cols[i].SetValue(obj, val);
                    }
                    OnInstanceCreated(obj);
                    yield return((T)obj);
                }

                if (r == Result.Done || r == Result.OK)
                {
                }
                else
                {
                    var msg = SQLite3.GetErrorMessageUTF8(Connection.Handle);
                    var ex  = new SQLiteException(r, msg, sql: CommandText);
                    ex.PopulateColumnFromTableMapping(map);

                    throw ex;
                }
            }
            finally
            {
                if (Statement != null)
                {
                    SQLite3.Reset(Statement);
                }

                sw.Stop();
                Log(sw);
                OnExecutionEnded();
            }
        }