예제 #1
0
        public static bool TryGetValueByName(this Sqlite3Row row, string columnName, out object value)
        {
            value = null;

            SqlTableDefinition tableDefinition = row.Table.GetTableDefinition();

            if (!tableDefinition.TryGetColumn(columnName, out SqlTableColumn column))
            {
                return(false);
            }

            if (row.ColumnData.Length <= column.Ordinal)
            {
                return(false);
            }

            // Special case for row-id
            if (tableDefinition.RowIdColumn != null && columnName.Equals(tableDefinition.RowIdColumn.Name, StringComparison.OrdinalIgnoreCase))
            {
                // Return the row-id
                value = row.RowId;
                return(true);
            }

            value = row.ColumnData[column.Ordinal];
            return(true);
        }
        public void TestIntegerData(int id, long expected)
        {
            using (Sqlite3Database db = new Sqlite3Database(_stream))
            {
                Sqlite3Table tbl = db.GetTable("IntegerTable");
                Sqlite3Row   row = tbl.GetRowById(id);

                Assert.NotNull(row);

                Assert.True(row.TryGetOrdinal(1, out long actual));
                Assert.Equal(expected, actual);
            }
        }
예제 #3
0
        public void TestStringDataContents(SqliteEncoding encoding, int id, string expectedLanguage, string expectedString)
        {
            using (Sqlite3Database db = new Sqlite3Database(Get(encoding)))
            {
                Sqlite3Table tbl = db.GetTable("EncodingTable" + encoding);
                Sqlite3Row   row = tbl.GetRowById(id);

                Assert.NotNull(row);

                Assert.True(row.TryGetOrdinal(1, out string actualLang));
                Assert.True(row.TryGetOrdinal(2, out string actualText));
                Assert.Equal(expectedLanguage, actualLang);
                Assert.Equal(expectedString, actualText);
            }
        }
        public void TestLongText()
        {
            using (Sqlite3Database db = new Sqlite3Database(_stream))
            {
                Sqlite3Table      tbl  = db.GetTable("urls");
                List <Sqlite3Row> rows = tbl.EnumerateRows().ToList();

                Assert.Single(rows);

                Sqlite3Row row = rows.Single();

                Assert.Equal(6014, row.RowId);

                Assert.True(row.TryGetOrdinal(1, out string actual));

                Assert.Equal(_expected, actual);
            }
        }
        public void TestRealData()
        {
            using (Sqlite3Database db = new Sqlite3Database(_stream))
            {
                Sqlite3Table      tbl  = db.GetTable("DataTable");
                List <Sqlite3Row> rows = tbl.EnumerateRows().ToList();

                Assert.Single(rows);

                Sqlite3Row row = rows.Single();

                Assert.Equal(1, row.RowId);

                Assert.True(row.TryGetOrdinal(1, out byte[] actual));

                Assert.Equal(_expected, actual);
            }
        }
예제 #6
0
        public static bool TryGetValueByName <T>(this Sqlite3Row row, string columnName, out T value)
        {
            value = default;

            if (!row.TryGetValueByName(columnName, out object tmp))
            {
                return(false);
            }

            // TODO: Is null a success case?
            if (tmp == null)
            {
                return(false);
            }

            value = (T)Convert.ChangeType(tmp, typeof(T));
            return(true);
        }
        public void TestRealData(int id, long expectedLong)
        {
            using (Sqlite3Database db = new Sqlite3Database(_stream))
            {
                Sqlite3Table tbl = db.GetTable("RealTable");

                Sqlite3Row row = tbl.GetRowById(id);
                Assert.NotNull(row);

                Assert.True(row.TryGetOrdinal(1, out double actual));

                long actualLong = BitConverter.DoubleToInt64Bits(actual);

                // Note: There is a loss of precision in the sqlite3 cli when generating the test database
                // This, and the similar loss of precision in C# / floating points here leads to the code below
                Assert.True(Math.Abs(expectedLong - actualLong) <= 2);
            }
        }
        public void TestIntegerData(int id, long expectedInteger, long?expectedNewInteger)
        {
            using (Sqlite3Database db = new Sqlite3Database(_stream))
            {
                Sqlite3Table tbl = db.GetTable("MyTable");

                Sqlite3Row row = tbl.GetRowById(id);
                Assert.NotNull(row);

                Assert.True(row.TryGetOrdinal(1, out long actual));
                Assert.Equal(expectedInteger, actual);

                if (expectedNewInteger.HasValue)
                {
                    Assert.True(row.TryGetOrdinal(2, out long actualOther));
                    Assert.Equal(expectedNewInteger.Value, actualOther);
                }
                else
                {
                    Assert.False(row.TryGetOrdinal(2, out long _));
                }
            }
        }