private void BindParameters(SQLitePCL.sqlite3_stmt stmt, DbCommand command)
 {
     for (int i = 0; i < command.Parameters.Count; i++)
     {
         BindParameter(stmt, i + 1, command.Parameters[0].Value, command.Connection.StoreDateTimeAsTicks);
     }
 }
Example #2
0
        public List <JsonData> GetEvents(string appID, int maxAllowed)
        {
            List <JsonData> eventList = new List <JsonData>();

            string sqlCommand = string.Format(CultureInfo.InvariantCulture, "SELECT * FROM {0} WHERE {1}  = ? ORDER BY {2},   ROWID LIMIT {3} ", TABLE_NAME, MA_APP_ID_COLUMN_NAME, EVENT_DELIVERY_ATTEMPT_COUNT_COLUMN_NAME, maxAllowed);

            lock (_lock)
            {
                Sqlite3Statement stmt = null;
                try
                {
                    stmt = ExecuteQuery(sqlCommand, appID);
                    while (Sqlite3.sqlite3_step(stmt) == Sqlite3.SQLITE_ROW)
                    {
                        JsonData data = new JsonData();
                        data["id"]    = (string)GetColumnValue(stmt, typeof(string), EVENT_ID_COLUMN_NAME);
                        data["event"] = (string)GetColumnValue(stmt, typeof(string), EVENT_COLUMN_NAME);
                        data["appID"] = (string)GetColumnValue(stmt, typeof(string), MA_APP_ID_COLUMN_NAME);
                        eventList.Add(data);
                    }
                }
                finally
                {
                    if (stmt != null)
                    {
                        Sqlite3.sqlite3_finalize(stmt);
                    }
                }
            }
            return(eventList);
        }
Example #3
0
        public static IDictionary <string, object> ParseSQLiteLinha(this SQLitePCL.sqlite3_stmt stQuery, string[] colunas)
        {
            var linha = new Dictionary <string, object>();

            for (int i = 0; i < colunas.Length; i++)
            {
                var coluna     = colunas[i];
                var tipoColuna = SQLite3.ColumnType(stQuery, i);
                switch (tipoColuna)
                {
                case SQLite3.ColType.Blob:

                    linha.Add(coluna, SQLite3.ColumnBlob(stQuery, i));
                    break;

                case SQLite3.ColType.Float:
                    linha.Add(coluna, SQLite3.ColumnDouble(stQuery, i));
                    break;

                case SQLite3.ColType.Integer:
                    linha.Add(coluna, SQLite3.ColumnInt(stQuery, i));
                    break;

                case SQLite3.ColType.Null:
                    linha.Add(coluna, null);
                    break;

                case SQLite3.ColType.Text:
                    linha.Add(coluna, SQLite3.ColumnString(stQuery, i));
                    break;
                }
            }

            return(linha);
        }
        private void Execute(string query, params object[] parameters)
        {
            Sqlite3Statement statement = null;

            try
            {
                Result r = (Result)Enum.Parse(typeof(Result), Sqlite3.sqlite3_prepare_v2(Handle, query, out statement).ToString());
                if (r != Result.OK && r != Result.Done && r != Result.Row)
                {
                    throw Sqlite3Exception.New(r, string.Format("Error executing statement {0}", r));
                }
                BindData(statement, parameters);
                r = (Result)Enum.Parse(typeof(Result), Sqlite3.sqlite3_step(statement).ToString());
                if (r != Result.OK && r != Result.Done && r != Result.Row)
                {
                    throw Sqlite3Exception.New(r, string.Format("Error executing statement {0}", r));
                }
            }
            finally
            {
                if (statement != null)
                {
                    Sqlite3.sqlite3_finalize(statement);
                }
            }
        }
Example #5
0
        public long NumberOfEvents(string appID)
        {
            long count = 0;

            string sqlCommand = string.Format(CultureInfo.InvariantCulture, "SELECT COUNT(*) C FROM {0} where {1} = ?", TABLE_NAME, MA_APP_ID_COLUMN_NAME);

            Sqlite3Statement stmt = null;

            try
            {
                stmt = ExecuteQuery(sqlCommand, appID);

                while (Sqlite3.sqlite3_step(stmt) == Sqlite3.SQLITE_ROW)
                {
                    count = Sqlite3.sqlite3_column_int(stmt, 0);
                }
            }
            finally
            {
                if (stmt != null)
                {
                    Sqlite3.sqlite3_finalize(stmt);
                }
            }
            return(count);
        }
Example #6
0
        public static int BindBlob(Sqlite3Statement stmt, int index, byte[] val, int n, IntPtr free)
        {
#if USE_WP8_NATIVE_SQLITE
            return(Sqlite3.sqlite3_bind_blob(stmt, index, val, n));
#elif USE_SQLITEPCL_RAW
            return(Sqlite3.sqlite3_bind_blob(stmt, index, val));
#else
            return(Sqlite3.sqlite3_bind_blob(stmt, index, val, n, null));
#endif
        }
Example #7
0
        public static int BindText(Sqlite3Statement stmt, int index, string val, int n, IntPtr free)
        {
#if USE_WP8_NATIVE_SQLITE
            return(Sqlite3.sqlite3_bind_text(stmt, index, val, n));
#elif USE_SQLITEPCL_RAW
            return(Sqlite3.sqlite3_bind_text(stmt, index, val));
#else
            return(Sqlite3.sqlite3_bind_text(stmt, index, val, n, null));
#endif
        }
Example #8
0
        public static byte[] ColumnByteArray(Sqlite3Statement stmt, int index)
        {
            int length = Sqlite3.sqlite3_column_bytes(stmt, index);

            if (length > 0)
            {
                return(Sqlite3.sqlite3_column_blob(stmt, index).ToArray());
            }

            return(new byte[0]);
        }
        private DatasetMetadata SqliteStmtToDatasetMetadata(Sqlite3Statement stmt)
        {
            string   datasetName    = (string)GetColumnValue(stmt, typeof(string), RecordColumns.DATASET_NAME);
            DateTime creationTime   = (DateTime)GetColumnValue(stmt, typeof(DateTime), DatasetColumns.CREATION_TIMESTAMP);
            DateTime lastModified   = (DateTime)GetColumnValue(stmt, typeof(DateTime), DatasetColumns.LAST_MODIFIED_TIMESTAMP);
            string   lastModifiedBy = (string)GetColumnValue(stmt, typeof(string), DatasetColumns.LAST_MODIFIED_BY);
            int      storageSize    = (int)GetColumnValue(stmt, typeof(int), DatasetColumns.STORAGE_SIZE_BYTES);
            int      recordCount    = (int)GetColumnValue(stmt, typeof(int), DatasetColumns.RECORD_COUNT);

            return(new DatasetMetadata(datasetName, creationTime, lastModified, lastModifiedBy, storageSize, recordCount));
        }
Example #10
0
        public static string[] GetColunasSQLite(this SQLitePCL.sqlite3_stmt stQuery)
        {
            var qtdColunas = SQLite3.ColumnCount(stQuery);
            var colunas    = new string[qtdColunas];

            for (int i = 0; i < qtdColunas; i++)
            {
                colunas[i] = SQLite3.ColumnName(stQuery, i);
            }

            return(colunas);
        }
        internal void UpdateOrInsertRecord(string identityId, string datasetName, Record record)
        {
            lock (sqlite_lock)
            {
                string checkRecordExistsQuery = "SELECT count(*) FROM " + SQLiteLocalStorage.TABLE_RECORDS + " WHERE " +
                                                RecordColumns.IDENTITY_ID + " = @whereIdentityId AND " +
                                                RecordColumns.DATASET_NAME + " = @whereDatasetName AND " +
                                                RecordColumns.KEY + " = @whereKey ";

                bool             recordsFound = false;
                Sqlite3Statement stmt         = null;
                try
                {
                    stmt = ExecuteQuery(checkRecordExistsQuery, identityId, datasetName, record.Key);
                    while (Sqlite3.sqlite3_step(stmt) == Sqlite3.SQLITE_ROW)
                    {
                        recordsFound = Sqlite3.sqlite3_column_int(stmt, 0) > 0;
                    }
                }
                finally
                {
                    if (stmt != null)
                    {
                        Sqlite3.sqlite3_finalize(stmt);
                    }
                }

                if (recordsFound)
                {
                    string updateRecordQuery =
                        RecordColumns.BuildUpdate(
                            new string[] {
                        RecordColumns.VALUE,
                        RecordColumns.SYNC_COUNT,
                        RecordColumns.MODIFIED,
                        RecordColumns.LAST_MODIFIED_TIMESTAMP,
                        RecordColumns.LAST_MODIFIED_BY,
                        RecordColumns.DEVICE_LAST_MODIFIED_TIMESTAMP
                    },
                            RecordColumns.IDENTITY_ID + " = @whereIdentityId AND " +
                            RecordColumns.DATASET_NAME + " = @whereDatasetName AND " +
                            RecordColumns.KEY + " = @whereKey "
                            );

                    Execute(updateRecordQuery, record.Value, record.SyncCount, record.IsModified ? 1 : 0, record.LastModifiedDate, record.LastModifiedBy, record.DeviceLastModifiedDate, identityId, datasetName, record.Key);
                }
                else
                {
                    string insertRecord = RecordColumns.BuildInsert();
                    Execute(insertRecord, identityId, datasetName, record.Key, record.Value, record.SyncCount, record.LastModifiedDate, record.LastModifiedBy, record.DeviceLastModifiedDate, record.IsModified ? 1 : 0);
                }
            }
        }
        private void GetColumns(SQLitePCL.sqlite3_stmt stmt, out string[] names, out Type[] types)
        {
            int columnCount = SQLite3.ColumnCount(stmt);

            types = new Type[columnCount];
            names = new string[columnCount];
            for (int i = 0; i < columnCount; i++)
            {
                names[i] = CheckName(names, i, SQLite3.ColumnName(stmt, i));
                types[i] = SQLiteColumnTypeToType(SQLite3.ColumnType(stmt, i));
            }
        }
Example #13
0
 public void ResetStatement()
 {
     try
     {
         SQLite3.Finalize(Statement);
     }
     finally
     {
         Statement = NullStatement;
         Prepared  = false;
     }
 }
Example #14
0
 private void Dispose(bool disposing)
 {
     if (Statement != NullStatement)
     {
         try {
             SQLite3.Finalize(Statement);
         } finally {
             Statement  = NullStatement;
             Connection = null;
         }
     }
 }
Example #15
0
        private object[] RunSql(string sqlString)
        {
            SQLitePCL.sqlite3_stmt stQuery = null;
            try
            {
                stQuery = SQLite3.Prepare2(database.DatabaseNotAsync.Handle, sqlString);
                var colLenght = SQLite3.ColumnCount(stQuery);
                while (SQLite3.Step(stQuery) == SQLite3.Result.Row)
                {
                    var obj = new object[colLenght];
                    for (int i = 0; i < colLenght; i++)
                    {
                        var colType = SQLite3.ColumnType(stQuery, i);
                        switch (colType)
                        {
                        case SQLite3.ColType.Blob:
                            obj[i] = SQLite3.ColumnBlob(stQuery, i);
                            break;

                        case SQLite3.ColType.Float:
                            obj[i] = SQLite3.ColumnDouble(stQuery, i);
                            break;

                        case SQLite3.ColType.Integer:
                            obj[i] = SQLite3.ColumnInt(stQuery, i);
                            break;

                        case SQLite3.ColType.Null:
                            obj[i] = null;
                            break;

                        case SQLite3.ColType.Text:
                            obj[i] = SQLite3.ColumnString(stQuery, i);
                            break;
                        }
                    }
                    return(obj);
                }
                return(null);
            }
            catch (Exception)
            {
                return(null);
            }
            finally
            {
                if (stQuery != null)
                {
                    SQLite3.Finalize(stQuery);
                }
            }
        }
        private void BindData(Sqlite3Statement statement, params object[] parameters)
        {
            if (parameters != null)
            {
                for (int i = 1; i <= parameters.Length; i++)
                {
                    object o = parameters[i - 1];
                    if (o == null)
                    {
                        Sqlite3.sqlite3_bind_null(statement, i);
                        continue;
                    }

                    var type = o.GetType();
                    var dt   = o as DateTime?;
                    if (dt.HasValue)
                    {
                        string ticks = dt.Value.Ticks.ToString();
                        Sqlite3.sqlite3_bind_text(statement, i, ticks);
                    }
                    else if (type == typeof(string))
                    {
                        Sqlite3.sqlite3_bind_text(statement, i, (string)o);
                    }
                    else if ((typeof(Int32) == type) ||
                             (typeof(Boolean) == type) ||
                             (typeof(Byte) == type) ||
                             (typeof(UInt16) == type) ||
                             (typeof(Int16) == type) ||
                             (typeof(sbyte) == type) ||
                             (typeof(Int64) == type) ||
                             (typeof(long) == type) ||
                             (typeof(UInt32) == type))
                    {
                        Sqlite3.sqlite3_bind_int64(statement, i, (Int64)Convert.ChangeType(o, typeof(Int64)));
                    }
                    else if ((typeof(double) == type) ||
                             (typeof(float) == type) ||
                             (typeof(decimal) == type))
                    {
                        Sqlite3.sqlite3_bind_double(statement, i, (double)o);
                    }
                    else if (type == typeof(byte[]))
                    {
                        Sqlite3.sqlite3_bind_blob(statement, i, (byte[])o);
                    }
                }
            }
        }
Example #17
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());
            }
        }
Example #18
0
        public static Sqlite3Statement Prepare2(Sqlite3DatabaseHandle db, string query)
        {
            Sqlite3Statement stmt = default(Sqlite3Statement);

#if USE_WP8_NATIVE_SQLITE || USE_SQLITEPCL_RAW
            var r = Sqlite3.sqlite3_prepare_v2(db, query, out stmt);
#else
            stmt = new Sqlite3Statement();
            var r = Sqlite3.sqlite3_prepare_v2(db, query, -1, ref stmt, 0);
#endif
            if (r != 0)
            {
                throw SQLiteException.New((Result)r, GetErrmsg(db));
            }
            return(stmt);
        }
Example #19
0
        public static void ExecutaSql(SQLiteConnection conn, string sqlString, Func <IDictionary <string, object>, bool> action)
        {
            SQLitePCL.sqlite3_stmt stQuery = null;
            using (stQuery = SQLite3.Prepare2(conn.Handle, sqlString))
            {
                var colunas = stQuery.GetColunasSQLite();

                while (SQLite3.Step(stQuery) == SQLite3.Result.Row)
                {
                    var linha = ParseSQLiteLinha(stQuery, colunas);
                    if (!action(linha))
                    {
                        break;
                    }
                }
            }
        }
        private Record SqliteStmtToRecord(Sqlite3Statement stmt)
        {
            string   key            = (string)GetColumnValue(stmt, typeof(string), RecordColumns.KEY);
            string   value          = (string)GetColumnValue(stmt, typeof(string), RecordColumns.VALUE);
            int      syncCount      = (int)GetColumnValue(stmt, typeof(int), RecordColumns.SYNC_COUNT);
            DateTime lastModified   = (DateTime)GetColumnValue(stmt, typeof(DateTime), RecordColumns.LAST_MODIFIED_TIMESTAMP);
            string   lastModifiedBy = (string)GetColumnValue(stmt, typeof(string), RecordColumns.LAST_MODIFIED_BY);
            DateTime deviceLastModifiedTimestamp = (DateTime)GetColumnValue(stmt, typeof(DateTime), RecordColumns.DEVICE_LAST_MODIFIED_TIMESTAMP);
            bool     modified = (int)GetColumnValue(stmt, typeof(int), RecordColumns.MODIFIED) == 1;

            return(new Record(key,
                              value,
                              syncCount,
                              lastModified,
                              lastModifiedBy,
                              deviceLastModifiedTimestamp,
                              modified));
        }
Example #21
0
        void BindAll(Sqlite3Statement stmt)
        {
            int nextIdx = 1;

            foreach (var b in _bindings)
            {
                if (b.Name != null)
                {
                    b.Index = SQLite3.BindParameterIndex(stmt, b.Name);
                }
                else
                {
                    b.Index = nextIdx++;
                }

                BindParameter(stmt, b.Index, b.Value, _conn.StoreDateTimeAsTicks);
            }
        }
Example #22
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);
            }
        }
 private bool CheckQuery(DbCommand command)
 {
     SQLitePCL.sqlite3_stmt stmt = null;
     try
     {
         stmt = SQLite3.Prepare2(command.Connection.Handle, command.CommandText);
         return(true);
     }
     catch
     {
         return(false);
     }
     finally
     {
         if (stmt != null)
         {
             SQLite3.Finalize(stmt);
         }
     }
 }
        private object ReadCol(SQLitePCL.sqlite3_stmt stmt, int index, SQLite3.ColType type)
        {
            switch (type)
            {
            case SQLite3.ColType.Blob:
                return(SQLite3.ColumnByteArray(stmt, index));

            case SQLite3.ColType.Float:
                return(SQLite3.ColumnDouble(stmt, index));

            case SQLite3.ColType.Integer:
                return(SQLite3.ColumnInt64(stmt, index));

            case SQLite3.ColType.Null:
                return(null);

            case SQLite3.ColType.Text:
                return(SQLite3.ColumnString(stmt, index));
            }
            return(null);
        }
        internal long GetLastSyncCountHelper(string query, params string[] parameters)
        {
            long             lastSyncCount = 0;
            Sqlite3Statement stmt          = null;

            try
            {
                stmt = ExecuteQuery(query, parameters);
                while (Sqlite3.sqlite3_step(stmt) == Sqlite3.SQLITE_ROW)
                {
                    lastSyncCount = (Int64)GetColumnValue(stmt, typeof(Int64), DatasetColumns.LAST_SYNC_COUNT);
                }
            }
            finally
            {
                if (stmt != null)
                {
                    Sqlite3.sqlite3_finalize(stmt);
                }
            }
            return(lastSyncCount);
        }
        internal List <Record> GetModifiedRecordsHelper(string query, params object[] parameters)
        {
            List <Record>    records = new List <Record>();
            Sqlite3Statement stmt    = null;

            try
            {
                stmt = ExecuteQuery(query, parameters);
                while (Sqlite3.sqlite3_step(stmt) == Sqlite3.SQLITE_ROW)
                {
                    records.Add(SqliteStmtToRecord(stmt));
                }
            }
            finally
            {
                if (stmt != null)
                {
                    Sqlite3.sqlite3_finalize(stmt);
                }
            }

            return(records);
        }
Example #27
0
        public static IList <IDictionary <string, object> > ExecutarSql(this SQLiteConnection connection, string sqlString)
        {
            var result = new List <IDictionary <string, object> >();

            SQLitePCL.sqlite3_stmt stQuery = null;

            using (connection)
            {
                using (stQuery = SQLite3.Prepare2(connection.Handle, sqlString))
                {
                    var colunas = stQuery.GetColunasSQLite();

                    while (SQLite3.Step(stQuery) == SQLite3.Result.Row)
                    {
                        var linha = stQuery.ParseSQLiteLinha(colunas);

                        result.Add(linha);
                    }
                }
            }

            return(result);
        }
        internal List <DatasetMetadata> GetDatasetMetadataHelper(string query, params string[] parameters)
        {
            List <DatasetMetadata> datasetMetadataList = new List <DatasetMetadata>();

            Sqlite3Statement stmt = null;

            try
            {
                stmt = ExecuteQuery(query, parameters);
                while (Sqlite3.sqlite3_step(stmt) == Sqlite3.SQLITE_ROW)
                {
                    datasetMetadataList.Add(SqliteStmtToDatasetMetadata(stmt));
                }
            }
            finally
            {
                if (stmt != null)
                {
                    Sqlite3.sqlite3_finalize(stmt);
                }
            }

            return(datasetMetadataList);
        }
        internal Record GetRecordHelper(string query, params string[] parameters)
        {
            Record           record = null;
            Sqlite3Statement stmt   = null;

            try
            {
                stmt = ExecuteQuery(query, parameters);

                while (Sqlite3.sqlite3_step(stmt) == Sqlite3.SQLITE_ROW)
                {
                    record = SqliteStmtToRecord(stmt);
                }
            }
            finally
            {
                if (stmt != null)
                {
                    Sqlite3.sqlite3_finalize(stmt);
                }
            }

            return(record);
        }
Example #30
0
        private List <object[]> RunSql(string sqlString, bool includeColumnNamesAsFirstRow)
        {
            var lstRes = new List <object[]>();

            SQLitePCL.sqlite3_stmt stQuery = null;
            try
            {
                stQuery = SQLite3.Prepare2(App.Database.DatabaseNotAsync.Handle, sqlString);
                var colLenght = SQLite3.ColumnCount(stQuery);

                if (includeColumnNamesAsFirstRow)
                {
                    var obj = new object[colLenght];
                    lstRes.Add(obj);
                    for (int i = 0; i < colLenght; i++)
                    {
                        obj[i] = SQLite3.ColumnName(stQuery, i);
                    }
                }

                while (SQLite3.Step(stQuery) == SQLite3.Result.Row)
                {
                    var obj = new object[colLenght];
                    lstRes.Add(obj);
                    for (int i = 0; i < colLenght; i++)
                    {
                        var colType = SQLite3.ColumnType(stQuery, i);
                        switch (colType)
                        {
                        case SQLite3.ColType.Blob:
                            obj[i] = SQLite3.ColumnBlob(stQuery, i);
                            break;

                        case SQLite3.ColType.Float:
                            obj[i] = SQLite3.ColumnDouble(stQuery, i);
                            break;

                        case SQLite3.ColType.Integer:
                            obj[i] = SQLite3.ColumnInt(stQuery, i);
                            break;

                        case SQLite3.ColType.Null:
                            obj[i] = null;
                            break;

                        case SQLite3.ColType.Text:
                            obj[i] = SQLite3.ColumnString(stQuery, i);
                            break;
                        }
                    }
                }
                return(lstRes);
            }
            catch (Exception)
            {
                return(null);
            }
            finally
            {
                if (stQuery != null)
                {
                    SQLite3.Finalize(stQuery);
                }
            }
        }
Example #31
0
 private Record SqliteStmtToRecord(Sqlite3Statement stmt)
 {
     string key = (string)GetColumnValue(stmt, typeof(string), RecordColumns.KEY);
     string value = (string)GetColumnValue(stmt, typeof(string), RecordColumns.VALUE);
     int syncCount = (int)GetColumnValue(stmt, typeof(int), RecordColumns.SYNC_COUNT);
     DateTime lastModified = (DateTime)GetColumnValue(stmt, typeof(DateTime), RecordColumns.LAST_MODIFIED_TIMESTAMP);
     string lastModifiedBy = (string)GetColumnValue(stmt, typeof(string), RecordColumns.LAST_MODIFIED_BY);
     DateTime deviceLastModifiedTimestamp = (DateTime)GetColumnValue(stmt, typeof(DateTime), RecordColumns.DEVICE_LAST_MODIFIED_TIMESTAMP);
     bool modified = (int)GetColumnValue(stmt, typeof(int), RecordColumns.MODIFIED) == 1;
     return new Record(key, 
         value, 
         syncCount,
         lastModified,
         lastModifiedBy,
         deviceLastModifiedTimestamp,
         modified);
 }
Example #32
0
        private DatasetMetadata SqliteStmtToDatasetMetadata(Sqlite3Statement stmt)
        {
            string datasetName = (string)GetColumnValue(stmt, typeof(string), RecordColumns.DATASET_NAME);
            DateTime creationTime = (DateTime)GetColumnValue(stmt, typeof(DateTime), DatasetColumns.CREATION_TIMESTAMP);
            DateTime lastModified = (DateTime)GetColumnValue(stmt, typeof(DateTime), DatasetColumns.LAST_MODIFIED_TIMESTAMP);
            string lastModifiedBy = (string)GetColumnValue(stmt, typeof(string), DatasetColumns.LAST_MODIFIED_BY);
            int storageSize = (int)GetColumnValue(stmt, typeof(int), DatasetColumns.STORAGE_SIZE_BYTES);
            int recordCount = (int)GetColumnValue(stmt, typeof(int), DatasetColumns.RECORD_COUNT);

            return new DatasetMetadata(datasetName, creationTime, lastModified, lastModifiedBy, storageSize, recordCount);
        }
Example #33
0
        private object GetColumnValue(Sqlite3Statement stmt, Type t, string columnName)
        {
            int columnCount = Sqlite3.sqlite3_column_count(stmt);
            int columnIndex = -1;
            int columnType = -1;
            for(int i = 0;i <columnCount; i++)
            {
                string colName = Sqlite3.sqlite3_column_name(stmt, i);
                if (colName.Equals(columnName, StringComparison.OrdinalIgnoreCase))
                {
                    columnIndex = i;
                    columnType = Sqlite3.sqlite3_column_type(stmt, i);
                    break;
                }
            }

            if(t == typeof(string))
            {
                return Sqlite3.sqlite3_column_text(stmt, columnIndex);
            }else if ((typeof(Int32) == t)
                    || (typeof(Boolean) == t)
                    || (typeof(Byte) == t)
                    || (typeof(UInt16) == t)
                    || (typeof(Int16) == t)
                    || (typeof(sbyte) == t))
            {
                return Convert.ChangeType(Sqlite3.sqlite3_column_int(stmt, columnIndex), t);
            }
            else if ((typeof(double) == t)
                   || (typeof(float) == t))
            {
                return Convert.ChangeType(Sqlite3.sqlite3_column_double(stmt, columnIndex), t);
            }
            else if (typeof(DateTime) == t)
            {
                string time = Sqlite3.sqlite3_column_text(stmt, columnIndex);
                return new DateTime(long.Parse(time, CultureInfo.InvariantCulture.NumberFormat), DateTimeKind.Utc);
            }
            else if (
                       (typeof(Int64) == t)
                    || (typeof(UInt32) == t)
                    )
            {
                return Convert.ChangeType(Sqlite3.sqlite3_column_int64(stmt, columnIndex), t, null);
            }
            else if (typeof(System.Nullable<long>) == t)
            {
                if (columnType == Sqlite3.SQLITE_NULL)
                {
                    return null;
                }
                else
                {
                    long? x = Sqlite3.sqlite3_column_int64(stmt, columnIndex);
                    return x;
                }
            }
            else if (typeof(System.Nullable<double>) == t)
            {
                if (columnType == Sqlite3.SQLITE_NULL)
                {
                    return null;
                }
                else
                {
                    double? x = Sqlite3.sqlite3_column_double(stmt, columnIndex);
                    return x;
                }
            }
            else if (typeof(System.Nullable<int>) == t)
            {
                if (columnType == Sqlite3.SQLITE_NULL)
                {
                    return null;
                }
                else
                {
                    int? x = Sqlite3.sqlite3_column_int(stmt, columnIndex);
                    return x;
                }
            }
            else if (typeof(decimal) == t)
            {
                return (decimal)Convert.ChangeType(Sqlite3.sqlite3_column_double(stmt, columnIndex), t);
            }
            else if (typeof(byte[]) == t)
            {
                return Sqlite3.sqlite3_column_blob(stmt, columnIndex);
            }
            else
            {
                throw new NotSupportedException("Invalid type conversion " + t.FullName);
            }
        }
Example #34
0
        private void BindData(Sqlite3Statement statement, params object[] parameters)
        {
            if (parameters != null)
            {
                for (int i = 1; i <= parameters.Length; i++)
                {
                    object o = parameters[i - 1];
                    if(o == null)
                    {
                        Sqlite3.sqlite3_bind_null(statement, i);
                        continue;
                    }

                    var type = o.GetType();
                    var dt = o as DateTime?;
                    if (dt.HasValue)
                    {
                        string ticks = dt.Value.Ticks.ToString();
                        Sqlite3.sqlite3_bind_text(statement, i, ticks);
                    }
                    else if (type == typeof(string))
                    {
                        Sqlite3.sqlite3_bind_text(statement, i, (string)o);
                    }
                    else if ((typeof(Int32) == type)
                            || (typeof(Boolean) == type)
                            || (typeof(Byte) == type)
                            || (typeof(UInt16) == type)
                            || (typeof(Int16) == type)
                            || (typeof(sbyte) == type)
                            || (typeof(Int64) == type)
                            || (typeof(long) == type)
                            || (typeof(UInt32) == type))
                    {
                        Sqlite3.sqlite3_bind_int64(statement, i,(Int64) Convert.ChangeType(o, typeof(Int64)));
                    }
                    else if ((typeof(double) == type)
                            || (typeof(float) == type)
                            || (typeof(decimal) == type))
                    {
                        Sqlite3.sqlite3_bind_double(statement, i, (double)o);
                    }
                    else if(type == typeof(byte[]))
                    {
                        Sqlite3.sqlite3_bind_blob(statement, i, (byte[])o);
                    }
                }
            }
        }