public IEnumerable <T> ExecuteQuery <T>() where T : new()
        {
            var stmt = Prepare();

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

            for (int i = 0; i < cols.Length; i++)
            {
                cols[i] = MatchColProp(SQLite3.ColumnName16(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);
        }
        /// <summary>
        /// Loads the column information.
        /// </summary>
        private void LoadColumnInfo()
        {
            var stmt = this.GetPreparedStatement();

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

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

                this.Columns = cols;
                if (this.Columns != null)
                {
                    this.ColumnNames = this.Columns.Select(c => c.Name).ToList();
                }
            }
            catch (Exception ex)
            {
                Logger?.LogError(ex);
            }
            finally
            {
                SQLite3.Finalize(stmt);
            }
        }
        private static IEnumerable <ReaderItem> ExecuteReader(this SQLiteCommand sqliteCommand, SQLiteConnection connection)
        {
            sqlite3_stmt stmt = Prepare(connection, sqliteCommand.CommandText);

            try
            {
                // We need to manage columns dynamically in order to create columns
                ColumnLite[] cols = new ColumnLite[SQLite3.ColumnCount(stmt)];

                while (SQLite3.Step(stmt) == SQLite3.Result.Row)
                {
                    ReaderItem obj = new ReaderItem();
                    for (int i = 0; i < cols.Length; i++)
                    {
                        if (cols[i] == null)
                        {
                            // We try to create column mapping if it's not already created :
                            string name = SQLite3.ColumnName16(stmt, i);
                            cols[i] = new ColumnLite(name, SQLite3.ColumnType(stmt, i).ToType());
                        }
                        SQLite3.ColType colType = SQLite3.ColumnType(stmt, i);
                        object          val     = ReadCol(connection, stmt, i, colType, cols[i].ColumnType);
                        obj[cols[i].Name] = val;
                    }
                    yield return(obj);
                }
            }
            finally
            {
                SQLite3.Finalize(stmt);
            }
        }
示例#4
0
        public IEnumerable <T> ExecuteDeferredQuery <T>(TableMapping map)
        {
            if (_conn.Trace)
            {
                Debug.WriteLine("Executing Query: " + this);
            }

            var stmt = Prepare();

            try
            {
                var cols = new TableMappingColumn[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++)
                    {
                        // TODO: Disabling on CanWrite here is temporary until we have
                        // accessing container subtables down
                        if (cols[i] == null)
                        {
                            continue;
                        }
                        if (cols[i] is DirectTableMappingColumn)
                        {
                            var colType       = SQLite3.ColumnType(stmt, i);
                            var colSQLiteType = SQLite3.GetSQLiteType(cols[i].TargetType);
                            var val           = colSQLiteType.Read(stmt, i, _conn.StoreDateTimeAsTicks);
                            (cols[i] as DirectTableMappingColumn).SetValue(obj, val);
                        }
                    }
                    foreach (IndirectTableMappingColumn column in map.IndirectColumns)
                    {
                        try
                        {
                            column.LoadValueFromDatabase(obj);
                        }
                        catch (Exception e)
                        {
                            Debug.WriteLine(e.ToString());
                        }
                    }
                    OnInstanceCreated(obj);
                    yield return((T)obj);
                }
            }
            finally
            {
                SQLite3.Finalize(stmt);
            }
        }
示例#5
0
        public bool Read()
        {
            if (SQLite3.Step(_vd) == SQLite3.Result.Row)
            {
                _columnNameList.Clear();

                int count = FieldCount;
                for (int i = 0; i < count; i++)
                {
                    string n = SQLite3.ColumnName16(_vd, i);
                    _columnNameList.Add(n);
                }

                return(true);
            }
            return(false);
        }
        /// <summary>
        /// Gets the name of the column.
        /// </summary>
        /// <param name="colNr">
        /// The col nr.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public string GetColumnName(int colNr)
        {
            IntPtr statement = IntPtr.Zero;

            try
            {
                statement = this.GetPreparedStatement();
                return(SQLite3.ColumnName16(this.GetPreparedStatement(), colNr));
            }
            finally
            {
                if (statement != IntPtr.Zero)
                {
                    SQLite3.Finalize(statement);
                }
            }
        }
示例#7
0
        public IEnumerable <T> ExecuteDeferredQuery <T>(TableMapping map)
        {
            if (_conn.Trace)
            {
                Debug.WriteLine("Executing Query: " + this);
            }

            IntPtr stmt = Prepare();

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

                for (int i = 0; i < cols.Length; i++)
                {
                    string name = SQLite3.ColumnName16(stmt, i);
                    cols[i] = 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)
                        {
                            continue;
                        }
                        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 {
                SQLite3.Finalize(stmt);
            }
        }
        public bool selectAllInto(JArray results, string query)
        {
            if (_connection != null)
            {
                try
                {
                    var cmd  = _connection.CreateCommand(query);
                    var stmt = SQLite3.Prepare2(_connection.Handle, cmd.CommandText);

                    try
                    {
                        while (SQLite3.Step(stmt) == SQLite3.Result.Row)
                        {
                            int colCount = SQLite3.ColumnCount(stmt);
                            if (colCount <= 0)
                            {
                                return(false);
                            }

                            JObject jsonObj = new JObject();

                            for (int i = 0; i < colCount; i++)
                            {
                                string columnName = SQLite3.ColumnName16(stmt, i);

                                if (columnName.Equals(FIELD_JSON))
                                {
                                    MemoryStream ms = new MemoryStream(SQLite3.ColumnByteArray(stmt, i));
                                    using (BsonReader reader = new BsonReader(ms))
                                    {
                                        jsonObj.Add(FIELD_JSON, JToken.ReadFrom(reader));
                                    }
                                }
                                else if (columnName.Equals(FIELD_ID))
                                {
                                    jsonObj.Add(FIELD_ID, Int32.Parse(SQLite3.ColumnString(stmt, i)));
                                }
                                else
                                {
                                    if (isJSONCreatedColumn(columnName))
                                    {
                                        jsonObj.Add(columnName, SQLite3.ColumnString(stmt, i));
                                    }
                                    else
                                    {
                                        jsonObj.Add(columnName.Replace('_', '.'), SQLite3.ColumnString(stmt, i));
                                    }
                                }
                            }
                            results.Add(jsonObj);
                        }
                    }
                    finally
                    {
                        SQLite3.Finalize(stmt);
                    }
                    return(true);
                }
                catch (Exception e)
                {
                    lastErrorMsg = e.ToString();
                }
            }
            return(false);
        }