Beispiel #1
0
        /// <summary>
        /// Character encoding is a mess here. Code pages mixed with UTF-8 and raw data
        /// </summary>
        private void ReadNamesWithEncodingDetection(SQLiteDataReader r, IDictionary <string, int> field, Encoding encoding)
        {
            byte[] buffer = new byte[100];
            int    len    = (int)r.GetBytes(field["sname"], 0, buffer, 0, buffer.Length);
            int    end    = Array.IndexOf <byte>(buffer, 0, 0, len);

            if (end >= 0)
            {
                len = end;
            }
            this.RawName = new byte[len];
            Array.Copy(buffer, 0, this.RawName, 0, len);
            this.ChangeEncoding(encoding);
        }
Beispiel #2
0
        protected byte[] GetBytes(SQLiteDataReader Reader, int iIndex)
        {
            long kBytesRead, kFieldOffset = 0;

            using (MemoryStream stream = new MemoryStream())
            {
                while ((kBytesRead = Reader.GetBytes(iIndex, kFieldOffset, _abMemoryBuffer, 0, ciMemoryBufferLength)) > 0)
                {
                    stream.Write(_abMemoryBuffer, 0, (int)kBytesRead);
                    kFieldOffset += kBytesRead;
                }
                return(stream.ToArray());
            }
        }
Beispiel #3
0
 private static byte[] GetBytes(SQLiteDataReader reader, int columnIndex)
 {
     using (var stream = new MemoryStream())
     {
         long bytesRead, fieldOffset = 0;
         var  buffer = new byte[(0x2 * 0x400)];
         while ((bytesRead = reader.GetBytes(columnIndex, fieldOffset, buffer, 0, buffer.Length)) > 0)
         {
             stream.Write(buffer, 0, (int)bytesRead);
             fieldOffset += bytesRead;
         }
         return(stream.ToArray());
     }
 }
Beispiel #4
0
 public TrustModel NewItem(SQLiteDataReader reader)
 {
     return(new TrustModel
     {
         TrustId = reader.GetBytes("trustid"),
         Head = new HeadModel
         {
             Version = reader.GetString("version"),
             Script = reader.GetString("script")
         },
         Issuer = new IssuerModel
         {
             Id = reader.GetBytes("issuerid"),
             Signature = reader.GetBytes("issuersignature")
         },
         Server = new ServerModel
         {
             Id = reader.GetBytes("serverid"),
             Signature = reader.GetBytes("serversignature")
         },
         Timestamp = reader.GetString("timestamp").DeserializeObject <TimestampCollection>()
     });
 }
Beispiel #5
0
        public ContentSchema GetContentSchema(long id)
        {
            Prepare(
                "SELECT SCHEMA FROM content_schema WHERE ID = @1;"
                );

            SetParameters(id);
            dataReader = command.ExecuteReader(CommandBehavior.SingleRow);
            dataReader.Read();

            ContentSchema contentSchema = blobReader.BlobToContentSchema(dataReader.GetBytes(0));

            CommitRead();
            return(contentSchema);
        }
Beispiel #6
0
        public static Dictionary <int, PrizeItem> getPrizes()
        {
            if (db.prizes != null)
            {
                return(db.prizes);
            }
            Dictionary <int, PrizeItem> prizes = new Dictionary <int, PrizeItem>();

            using (SQLiteConnection cn = new SQLiteConnection("Data Source = myData"))
            {
                cn.Open();
                SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM prizes", cn);
                using (SQLiteDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        prizes.Add(sdr.GetInt32(0),
                                   new PrizeItem
                        {
                            Id    = sdr.GetInt32(0),
                            IsBad = sdr.GetInt32(4),
                            Type  = sdr.GetInt32(2),
                            Name  = sdr["name"].ToString(),
                        }
                                   );
                        byte[] bytBLOB = new byte[sdr.GetBytes(3, 0, null, 0, int.MaxValue) - 1];
                        sdr.GetBytes(3, 0, bytBLOB, 0, bytBLOB.Length);

                        MemoryStream stmBLOB = new MemoryStream(bytBLOB);
                        prizes[sdr.GetInt32(0)].Image = Image.FromStream(stmBLOB);
                    }
                }
            }
            db.prizes = prizes;
            return(prizes);
        }
 public static byte[] GetBytes(this SQLiteDataReader reader, int col)
 {
     byte[] buffer = new byte[8000];
     using (var ms = new MemoryStream())
     {
         long read   = 0;
         long offset = 0;
         while ((read = reader.GetBytes(col, offset, buffer, 0, buffer.Length)) > 0)
         {
             offset += read;
             ms.Write(buffer, 0, (int)read);
         }
         return(ms.GetBuffer());
     }
 }
        public void Dir_Create(string path)
        {
            if (_stat_failed)
            {
                throw new Exception("操作失败");
            }

            try
            {
                string[] dir = path.Split('/');
                if (dir.Length == 1)
                {
                    return;
                }
                else if (dir.Length == 0)
                {
                    throw new ArgumentException("路径不合法");
                }

                uint cur_fUID = 0;
                for (int i = 1; i < dir.Length; i++)
                {
                    _repo_filesys_cmd.CommandText = "SELECT md5 FROM File WHERE (folderUID=" + cur_fUID + " AND name='" + dir[i] + "')";
                    SQLiteDataReader dr = _repo_filesys_cmd.ExecuteReader();
                    if (!dr.Read())
                    { //找不到路径
                        dr.Close();
                        uint next_fUID = _Generate_Random_ID();
                        _repo_filesys_cmd.CommandText = "INSERT INTO File VALUES(" + cur_fUID + ", '" + dir[i] + "', 0x" + VBUtil.Utils.Others.Hex(VBUtil.Utils.ByteUtils.UIntToByte(next_fUID)) + ", 128)";
                        _repo_filesys_cmd.ExecuteNonQuery();
                        cur_fUID = next_fUID;
                    }
                    else
                    {
                        byte[] buf = new byte[16];
                        dr.GetBytes(0, 0, buf, 0, 16);
                        cur_fUID = VBUtil.Utils.ByteUtils.ByteToUInt(buf);
                        dr.Close();
                    }
                }
            }
            catch (Exception)
            {
                _stat_failed = true;
                Dispose();
                throw;
            }
        }
Beispiel #9
0
        protected MemoryStream getBytesToMemoryStream(SQLiteDataReader reader, int index)
        {
            MemoryStream ms = new MemoryStream();

            byte[] buffer = new byte[2048];
            long   readed;
            long   offset = 0;

            while ((readed = reader.GetBytes(index, offset, buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, (int)readed);
                offset += readed;
            }
            ms.Seek(0, SeekOrigin.Begin);
            return(ms);
        }
Beispiel #10
0
        internal string ReadUtf16(SQLiteDataReader r, int fieldIndex)
        {
            if (r.IsDBNull(fieldIndex))
            {
                return(null);
            }
            byte[] nameBytes = new byte[200];
            int    nameLen   = (int)r.GetBytes(fieldIndex, 0, nameBytes, 0, nameBytes.Length);

            this.encoding ??= AutoDetectUtf16Encoding(nameBytes, nameLen);
            if (this.encoding == null)
            {
                return(string.Empty);
            }

            return(encoding.GetString(nameBytes, 0, nameLen).Replace("\0", "")); // remove trailing \0 characters found in Samsung "_T_..." channel list
        }
        // https://stackoverflow.com/questions/625029/how-do-i-store-and-retrieve-a-blob-from-sqlite
        byte[] GetBytes(SQLiteDataReader reader)
        {
            byte[] inbuffer  = new byte[TSize];
            byte[] outbuffer = new byte[TSize];
            long   bytesRead;
            long   fieldOffset = 0;

            using (MemoryStream stream = new MemoryStream(outbuffer))
            {
                while ((bytesRead = reader.GetBytes(0, fieldOffset, inbuffer, 0, TSize)) > 0)
                {
                    stream.Write(inbuffer, 0, (int)bytesRead);
                    fieldOffset += bytesRead;
                }
                return(outbuffer);
            }
        }
        private byte[] GetBlob(SQLiteDataReader reader, int i)
        {
            MemoryStream ms = new MemoryStream();

            byte[] buffer   = new byte[ReadBufferSize];
            long   offset   = 0;
            int    lastRead = 0;

            do
            {
                lastRead = (int)reader.GetBytes(i, offset, buffer, 0, ReadBufferSize);
                ms.Write(buffer, 0, lastRead);
                offset += lastRead;
            }while (lastRead == ReadBufferSize);

            return(ms.ToArray());
        }
Beispiel #13
0
        private static byte[] ReadBytes(SQLiteDataReader reader, int fieldIndex)
        {
            const int CHUNK_SIZE  = 2 * 1024;
            var       buffer      = new byte[CHUNK_SIZE];
            long      fieldOffset = 0;

            using (var stream = new MemoryStream())
            {
                long bytesRead;
                while ((bytesRead = reader.GetBytes(fieldIndex, fieldOffset, buffer, 0, buffer.Length)) > 0)
                {
                    stream.Write(buffer, 0, (int)bytesRead);
                    fieldOffset += bytesRead;
                }
                return(stream.ToArray());
            }
        }
Beispiel #14
0
        /// <summary>
        /// Read the sound file contents into a <see cref="MemoryStream"/>.
        /// </summary>
        internal static Stream ReadSoundFile(SQLiteDataReader reader)
        {
            const int CHUNK_SIZE = 2 * 1024;

            byte[]       buffer = new byte[CHUNK_SIZE];
            long         bytesRead;
            long         fieldOffset = 0;
            MemoryStream stream      = new MemoryStream();

            while ((bytesRead = reader.GetBytes(3, fieldOffset, buffer, 0, buffer.Length)) > 0)
            {
                stream.Write(buffer, 0, (int)bytesRead);
                fieldOffset += bytesRead;
            }

            return(stream);
        }
Beispiel #15
0
        static byte[] GetBytes(SQLiteDataReader reader)
        {
            const int CHUNK_SIZE = 2 * 1024;

            byte[] buffer = new byte[CHUNK_SIZE];
            long   bytesRead;
            long   fieldOffset = 0;

            using (MemoryStream stream = new MemoryStream())
            {
                while ((bytesRead = reader.GetBytes(0, fieldOffset, buffer, 0, buffer.Length)) > 0)
                {
                    stream.Write(buffer, 0, (int)bytesRead);
                    fieldOffset += bytesRead;
                }
                return(stream.ToArray());
            }
        }
Beispiel #16
0
        public static byte[] GetBytes(this SQLiteDataReader reader, string field)
        {
            const int chunkSize   = 2 * 1024;
            var       buffer      = new byte[chunkSize];
            var       fieldIndex  = reader.GetOrdinal(field);
            long      fieldOffset = 0;

            using (var stream = new MemoryStream())
            {
                long bytesRead;
                while ((bytesRead = reader.GetBytes(fieldIndex, fieldOffset, buffer, 0, buffer.Length)) > 0)
                {
                    stream.Write(buffer, 0, (int)bytesRead);
                    fieldOffset += bytesRead;
                }
                return(stream.ToArray());
            }
        }
Beispiel #17
0
        public static byte[] GetBytes(this SQLiteDataReader reader, int i)
        {
            const int ChunkSize = 2 * 1024;

            byte[] buffer = new byte[ChunkSize];
            long   bytesRead;
            long   fieldOffset = 0;

            using (MemoryStream stream = new MemoryStream())
            {
                while ((bytesRead = reader.GetBytes(i, fieldOffset, buffer, 0, buffer.Length)) > 0)
                {
                    stream.Write(buffer, 0, (int)bytesRead);
                    fieldOffset += bytesRead;
                }
                return(stream.ToArray());
            }
        }
        public bool Dir_Exists(string path)
        {
            if (_stat_failed)
            {
                throw new Exception("操作失败");
            }

            try
            {
                string[] dir = path.Split('/');
                if (dir.Length == 1)
                {
                    return(true);
                }
                else if (dir.Length == 0)
                {
                    throw new ArgumentException("路径不合法");
                }

                uint cur_fUID = 0;
                for (int i = 1; i < dir.Length; i++)
                {
                    _repo_filesys_cmd.CommandText = "SELECT md5 FROM File WHERE (folderUID=" + cur_fUID + " AND name='" + dir[i] + "')";
                    SQLiteDataReader dr = _repo_filesys_cmd.ExecuteReader();
                    if (!dr.Read())
                    {
                        dr.Close();
                        return(false);
                    }
                    byte[] buf = new byte[16];
                    dr.GetBytes(0, 0, buf, 0, 16);
                    cur_fUID = VBUtil.Utils.ByteUtils.ByteToUInt(buf);
                    dr.Close();
                }
                return(true);
            }
            catch (Exception)
            {
                _stat_failed = true;
                Dispose();
                throw;
            }
        }
Beispiel #19
0
        private byte[] GetBytesFrom(SQLiteDataReader reader)
        {
            const int chunkSize = 1024;

            byte[] buffer = new byte[chunkSize];
            using (var stream = new MemoryStream())
            {
                long readedBytes;
                long fieldOffset = 0;
                do
                {
                    readedBytes = reader.GetBytes(0, fieldOffset, buffer, 0, chunkSize);
                    stream.Write(buffer, 0, (int)readedBytes);
                    fieldOffset += readedBytes;
                } while (readedBytes == chunkSize);

                return(stream.GetBuffer());
            }
        }
Beispiel #20
0
        /// <summary>
        /// check inputed master password and registered master password
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool CheckMasterPassword(byte[] password)
        {
            if (this._Connection.State != System.Data.ConnectionState.Open)
            {
                throw new Exception("Database not connected.");
            }

            string query = "";

            query = "SELECT PASSWORD FROM MASTER;";

            try
            {
                using (SQLiteCommand command = new SQLiteCommand(query, this._Connection))
                {
                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        byte[] temp = new byte[password.Length];
                        while (reader.Read())
                        {
                            reader.GetBytes(0, 0, temp, 0, temp.Length);
                        }

                        reader.Close();

                        for (int i = 0; i < password.Length; i++)
                        {
                            if (password[i] != temp[i])
                            {
                                return(false);
                            }
                        }
                        return(true);
                    }
                }
            }
            catch (SQLiteException e)
            {
                MessageBox.Show(e.Message);
            }
            return(false);
        }
Beispiel #21
0
        public static bool GetTextData(long elementId, out string rtf, out string error)
        {
            error = String.Empty;
            rtf   = null;

            try
            {
                using (SQLiteConnection sqlConn = new SQLiteConnection(ConnectionManager.ConnectionStringSQLite))
                {
                    string query = @"select [data] from data where Id=@elementId";
                    sqlConn.Open();
                    using (SQLiteCommand command = new SQLiteCommand(query, sqlConn))
                    {
                        command.Parameters.Add(new SQLiteParameter()
                        {
                            ParameterName = "@elementId", DbType = DbType.Int64, Value = elementId
                        });

                        using (SQLiteDataReader reader = command.ExecuteReader())
                        {
                            reader.Read();
                            if (reader.HasRows && !reader.IsDBNull(0))
                            {
                                Byte[]        data          = new Byte[Convert.ToInt32((reader.GetBytes(0, 0, null, 0, Int32.MaxValue)))];
                                long          bytesReceived = reader.GetBytes(0, 0, data, 0, data.Length);
                                ASCIIEncoding encoding      = new ASCIIEncoding();
                                rtf = encoding.GetString(data, 0, Convert.ToInt32(bytesReceived));
                            }
                            return(true);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return(false);
            }
            return(true);
        }
Beispiel #22
0
        public override object query(string request, AbstractPermission permission = null)
        {
            SQLiteCommand cmd = new SQLiteCommand("SELECT ASSEMBLY_NAME, DOMAIN_OBJECT_SIZE, DOMAIN_OBJECT, QUERY_STRING FROM SITE_" + _src.SiteId.Id +
                                                  " WHERE QUERY_STRING_HASH = '" + StringUtils.getMD5Hash(request) + "';", _cxn);

            connect();
            SQLiteDataReader rdr = cmd.ExecuteReader();

            if (rdr.Read())
            {
                string fullAssemblyName = rdr.GetString(0); // gives us the object type
                Int32  objectSize       = rdr.GetInt32(1);  // gives us the object size in bytes - should have saved this info to database when mocking data
                byte[] buffer           = new byte[objectSize];
                rdr.GetBytes(2, 0, buffer, 0, objectSize);

                return(deserializeObject(buffer));
            }
            else
            {
                return("");
                //throw new exceptions.MdoException("Record not found in mock site " + _src.SiteId.Id);
            }
        }
Beispiel #23
0
        public override object query(string request, AbstractPermission permission = null)
        {
            string sql = "SELECT OBJECT_TYPE, DOMAIN_OBJECT_SIZE, DOMAIN_OBJECT, QUERY_STRING FROM " +
                         _src.SiteId.Id + " WHERE QUERY_STRING_HASH = '" + StringUtils.getMD5Hash(request) + "';";

            SQLiteCommand    cmd = new SQLiteCommand(sql, _cxn);
            SQLiteDataReader rdr = cmd.ExecuteReader();

            if (rdr.Read())
            {
                string fullAssemblyName = rdr.GetString(0); // gives us the object type
                Int32  objectSize       = rdr.GetInt32(1);  // gives us the object size in bytes - should have saved this info to database when mocking data
                byte[] buffer           = new byte[objectSize];
                rdr.GetBytes(2, 0, buffer, 0, objectSize);

                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter deserializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                return(deserializer.Deserialize(new MemoryStream(buffer)));
            }
            else
            {
                throw new exceptions.MdoException(exceptions.MdoExceptionCode.DATA_NO_RECORD_FOR_ID);
            }
        }
Beispiel #24
0
 private async Task <byte[]> GetCachedImageAsync(TileSpecifier spec)
 {
     byte[] buffer = null;
     using (SQLiteCommand command = new SQLiteCommand(SqlStrings.GET_IMAGE, DbConnection))
     {
         command.Parameters.AddWithValue("@latitude", spec.Coordinate.Latitude);
         command.Parameters.AddWithValue("@longitude", spec.Coordinate.Longitude);
         command.Parameters.AddWithValue("@width", spec.Size.Width);
         command.Parameters.AddWithValue("@height", spec.Size.Height);
         command.Parameters.AddWithValue("@zoom", spec.Zoom);
         command.Parameters.AddWithValue("@mapType", spec.MapType);
         using (SQLiteDataReader reader = await Task.Run(() => command.ExecuteReader()))
         {
             if (reader.Read())
             {
                 int length = reader.GetInt32(1);
                 buffer = new byte[length];
                 reader.GetBytes(0, 0, buffer, 0, length);
             }
         }
     }
     return(buffer);
 }
 static byte[] GetBytes(SQLiteDataReader reader)
 {
     try
     {
         const int CHUNK_SIZE  = 2 * 1024;
         byte[]    buffer      = new byte[CHUNK_SIZE];
         long      fieldOffset = 0;
         using (MemoryStream stream = new MemoryStream())
         {
             long bytesRead;
             while ((bytesRead = reader.GetBytes(0, fieldOffset, buffer, 0, buffer.Length)) > 0)
             {
                 stream.Write(buffer, 0, (int)bytesRead);
                 fieldOffset += bytesRead;
             }
             return(stream.ToArray());
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
         return(null);
     }
 }
Beispiel #26
0
        public Dictionary <long, Template> GetTemplates(IList <CardMetadata> metadata)
        {
            var templates = new Dictionary <long, Template>();

            Prepare(
                "SELECT SCHEMAID, TEMPLATE FROM templates " +
                "WHERE ID = @1;"
                );

            for (int i = 0; i < metadata.Count; ++i)
            {
                if (!templates.ContainsKey(metadata[i].TemplateId))
                {
                    SetParameters(metadata[i].TemplateId);
                    dataReader = command.ExecuteReader(CommandBehavior.SingleRow);
                    dataReader.Read();

                    templates.Add(metadata[i].TemplateId, blobReader.BlobToTemplate(dataReader.GetBytes(1)));
                }
            }

            CommitRead();
            return(templates);
        }
Beispiel #27
0
        public object getObject(string dataSourceId, string objectHashString)
        {
            SQLiteCommand cmd = new SQLiteCommand("SELECT ASSEMBLY_NAME, DOMAIN_OBJECT_SIZE, DOMAIN_OBJECT, QUERY_STRING FROM " + TABLE_NAME_PREFIX + dataSourceId +
                                                  " WHERE QUERY_STRING_HASH = '" + objectHashString + "';", _cxn);

            try
            {
                openConnection();

                SQLiteDataReader rdr = cmd.ExecuteReader();

                if (rdr.Read())
                {
                    string assemblyName     = rdr.GetString(0); // probably don't need this since we know how to cast the object type already
                    Int32  domainObjectSize = rdr.GetInt32(1);
                    byte[] domainObject     = new byte[domainObjectSize];
                    rdr.GetBytes(2, 0, domainObject, 0, domainObjectSize);
                    string dbQueryString = rdr.GetString(3);

                    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    return(bf.Deserialize(new MemoryStream(domainObject)));
                }
                else
                {
                    throw new exceptions.MdoException("No record found for that hashcode");
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                closeConnection();
            }
        }
Beispiel #28
0
        public object getObject(string dataSourceId, string objectHashString)
        {
            SQLiteCommand cmd = new SQLiteCommand("SELECT ASSEMBLY_NAME, DOMAIN_OBJECT_SIZE, DOMAIN_OBJECT, QUERY_STRING FROM " + TABLE_NAME_PREFIX + dataSourceId +
                                                  " WHERE QUERY_STRING_HASH = '" + objectHashString + "';", _cxn);

            try
            {
                openConnection();

                SQLiteDataReader rdr = cmd.ExecuteReader();

                if (rdr.Read())
                {
                    string assemblyName     = rdr.GetString(0); // probably don't need this since we know how to cast the object type already
                    Int32  domainObjectSize = rdr.GetInt32(1);
                    byte[] domainObject     = new byte[domainObjectSize];
                    rdr.GetBytes(2, 0, domainObject, 0, domainObjectSize);
                    string dbQueryString = rdr.GetString(3);

                    return(deserializeObject(domainObject));
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                cmd.Dispose();
                closeConnection();
            }
        }
Beispiel #29
0
        public List <LibraryItem> GetLibraryItems(string extension, List <string> fingerprints = null, int MaxRecordCount = 0)
        {
            List <LibraryItem> results             = new List <LibraryItem>();
            List <Exception>   database_corruption = new List <Exception>();

            try
            {
                lock (DBAccessLock.db_access_lock)
                {
                    using (var connection = GetConnection())
                    {
                        connection.Open();

                        string command_string = "SELECT fingerprint, extension, md5, data FROM LibraryItem WHERE 1=1 ";
                        command_string += turnArgumentSetIntoQueryPart("fingerprint", fingerprints);
                        command_string += turnArgumentIntoQueryPart("extension", extension);
                        if (MaxRecordCount > 0)
                        {
                            // http://www.sqlitetutorial.net/sqlite-limit/
                            command_string += " LIMIT @maxnum";
                        }

                        using (var command = new SQLiteCommand(command_string, connection))
                        {
                            //turnArgumentIntoQueryParameter(command, "fingerprint", fingerprint);
                            turnArgumentIntoQueryParameter(command, "extension", extension);
                            if (MaxRecordCount > 0)
                            {
                                command.Parameters.AddWithValue("@maxnum", MaxRecordCount);
                            }

                            using (SQLiteDataReader reader = command.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    LibraryItem result = new LibraryItem();

                                    int field_count = 0;

                                    // Read the record in 2-3 gangs, as there's some DBs out there which have the BLOB as NOT A BLOB but as a STRING type instead:
                                    // this is probably caused by manual editing (using SQLite CLI or other means) of the BLOB record.

                                    // gang 1: load the field count and header fields: these almost never fail.
                                    try
                                    {
                                        field_count = reader.FieldCount;

                                        result.fingerprint = reader.GetString(0);
                                        result.extension   = reader.GetString(1);
                                        result.md5         = reader.GetString(2);
                                    }
                                    catch (Exception ex)
                                    {
                                        string msg = String.Format("LibraryDB::GetLibraryItems: Database record #{4} gang 1 decode failure for DB '{0}': fingerprint={1}, ext={2}, md5={3}, field_count={5}.",
                                                                   library_path,
                                                                   String.IsNullOrEmpty(result.fingerprint) ? "???" : result.fingerprint,
                                                                   String.IsNullOrEmpty(result.extension) ? "???" : result.extension,
                                                                   String.IsNullOrEmpty(result.md5) ? "???" : result.md5,
                                                                   reader.StepCount, // ~= results.Count + database_corruption.Count
                                                                   field_count
                                                                   );
                                        Logging.Error(ex, "{0}", msg);

                                        Exception ex2 = new Exception(msg, ex);

                                        database_corruption.Add(ex2);

                                        // it's no use to try to decode the rest of the DB record: it is lost to us
                                        continue;
                                    }

                                    if (true)
                                    {
                                        Exception ex2 = null;

                                        long total_bytes = 0;

                                        // gang 2: get the BLOB
                                        try
                                        {
                                            total_bytes = reader.GetBytes(3, 0, null, 0, 0);
                                            result.data = new byte[total_bytes];
                                            long total_bytes2 = reader.GetBytes(3, 0, result.data, 0, (int)total_bytes);
                                            if (total_bytes != total_bytes2)
                                            {
                                                throw new Exception("Error reading blob - blob size different on each occasion.");
                                            }

                                            results.Add(result);
                                            continue;
                                        }
                                        catch (Exception ex)
                                        {
                                            string msg = String.Format("LibraryDB::GetLibraryItems: Database record #{4} BLOB decode failure for DB '{0}': fingerprint={1}, ext={2}, md5={3}, BLOB length={5}.",
                                                                       library_path,
                                                                       String.IsNullOrEmpty(result.fingerprint) ? "???" : result.fingerprint,
                                                                       String.IsNullOrEmpty(result.extension) ? "???" : result.extension,
                                                                       String.IsNullOrEmpty(result.md5) ? "???" : result.md5,
                                                                       reader.StepCount, // ~= results.Count + database_corruption.Count
                                                                       total_bytes
                                                                       );

                                            ex2 = new Exception(msg, ex);

                                            // gang 3: get at the BLOB-née-STRING in an indirect way
                                            object[] fields = new object[5];

                                            try
                                            {
                                                reader.GetValues(fields);
                                                byte[] arr = fields[3] as byte[];
                                                if (arr != null)
                                                {
                                                    string blob = Encoding.UTF8.GetString(arr, 0, arr.Length);

                                                    result.data = new byte[arr.Length];
                                                    Array.Copy(arr, result.data, arr.Length);

                                                    results.Add(result);

                                                    Logging.Warn("LibraryDB::GetLibraryItems: Database record #{0} BLOB field is instead decoded as UTF8 STRING, following this RESOLVED ERROR: {1}\n  Decoded STRING content:\n{2}",
                                                                 reader.StepCount, // ~= results.Count + database_corruption.Count
                                                                 ex2.ToStringAllExceptionDetails(),
                                                                 blob);

                                                    continue;
                                                }
                                                else
                                                {
                                                    throw new Exception("Cannot extract BLOB field.");
                                                }
                                            }
                                            catch (Exception ex3)
                                            {
                                                Logging.Error(ex2);
                                                Logging.Error(ex3);

                                                database_corruption.Add(ex2);
                                            }
                                        }
                                    }
                                }

                                reader.Close();
                            }
                        }

                        connection.Close();
                    }

                    //
                    // see SO link above at the `DBAccessLock.db_access_lock` declaration.
                    //
                    // We keep this *inside* the critical section so that we know we'll be the only active SQLite
                    // action which just transpired.
                    // *This* is also the reason why I went with a *global* lock (singleton) for *all* databases,
                    // even while *theoretically* this is *wrong* or rather: *unnecessary* as the databases
                    // i.e. Qiqqa Libraries shouldn't bite one another. I, however, need to ensure that the
                    // added `System.Data.SQLite.SQLiteConnection.ClearAllPools();` statements don't foul up
                    // matters in library B while lib A I/O is getting cleaned up.
                    //
                    // In short: Yuck. + Cave canem.
                    //
                    SQLiteConnection.ClearAllPools();
                }
            }
            catch (Exception ex)
            {
                Logging.Error(ex, "LibraryDB::GetLibraryItems: Database I/O failure for DB '{0}'.", library_path);
                LibraryDB.FurtherDiagnoseDBProblem(ex, database_corruption, library_path);
                throw;
            }

            if (database_corruption.Count > 0)
            {
                // report database corruption: the user may want to recover from this ASAP!
                if (MessageBoxes.AskErrorQuestion(true, "Library '{0}' has some data corruption. Do you want to abort the application to attempt recovery using external tools, e.g. a data restore from backup?\n\nWhen you answer NO, we will continue with what we could recover so far instead.\n\n\nConsult the Qiqqa logfiles to see the individual corruptions reported.",
                                                  library_path))
                {
                    Logging.Warn("User chose to abort the application on database corruption report");
                    Environment.Exit(3);
                }
            }

            return(results);
        }
Beispiel #30
0
        public List <LibraryItem> GetLibraryItems(string fingerprint, string extension, int MaxRecordCount = 0)
        {
            List <LibraryItem> results = new List <LibraryItem>();

            lock (DBAccessLock.db_access_lock)
            {
                using (var connection = GetConnection())
                {
                    connection.Open();

                    string command_string = "SELECT fingerprint, extension, md5, data FROM LibraryItem WHERE 1=1 ";
                    command_string += turnArgumentIntoQueryPart("fingerprint", fingerprint);
                    command_string += turnArgumentIntoQueryPart("extension", extension);
                    if (MaxRecordCount > 0)
                    {
                        // http://www.sqlitetutorial.net/sqlite-limit/
                        command_string += " LIMIT @maxnum";
                    }

                    using (var command = new SQLiteCommand(command_string, connection))
                    {
                        turnArgumentIntoQueryParameter(command, "fingerprint", fingerprint);
                        turnArgumentIntoQueryParameter(command, "extension", extension);
                        if (MaxRecordCount > 0)
                        {
                            command.Parameters.AddWithValue("@maxnum", MaxRecordCount);
                        }

                        using (SQLiteDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                LibraryItem result = new LibraryItem();
                                results.Add(result);

                                result.fingerprint = reader.GetString(0);
                                result.extension   = reader.GetString(1);
                                result.md5         = reader.GetString(2);

                                long total_bytes = reader.GetBytes(3, 0, null, 0, 0);
                                result.data = new byte[total_bytes];
                                long total_bytes2 = reader.GetBytes(3, 0, result.data, 0, (int)total_bytes);
                                if (total_bytes != total_bytes2)
                                {
                                    throw new Exception("Error reading blob - blob size different on each occasion.");
                                }
                            }

                            reader.Close();
                        }
                    }

                    connection.Close();
                }

                //
                // see SO link above at the `DBAccessLock.db_access_lock` declaration.
                //
                // We keep this *inside* the critical section so that we know we'll be the only active SQLite
                // action which just transpired.
                // *This* is also the reason why I went with a *global* lock (singeton) for *all* databases,
                // even while *theoretically* this is *wrong* or rather: *unneccessary* as the databases
                // i.e. Qiqqa Libraries shouldn't bite one another. I, however, need to ensure that the
                // added `System.Data.SQLite.SQLiteConnection.ClearAllPools();` statements don't foul up
                // matters in another library while lib A I/O is getting cleaned up.
                //
                // In short: Yuck. + Cave canem.
                //
                SQLiteConnection.ClearAllPools();
            }

            return(results);
        }