Ejemplo n.º 1
0
        public override void Open()
        {
            VerifyNotDisposed();
            if (State != ConnectionState.Closed)
            {
                throw new InvalidOperationException("Cannot Open when State is {0}.".FormatInvariant(State));
            }

            var connectionStringBuilder = new SQLiteConnectionStringBuilder {
                ConnectionString = ConnectionString
            };

            m_dataSource = connectionStringBuilder.DataSource;
            if (string.IsNullOrEmpty(m_dataSource))
            {
                throw new InvalidOperationException("Connection String Data Source must be set.");
            }

            SQLiteOpenFlags openFlags = connectionStringBuilder.ReadOnly ? SQLiteOpenFlags.ReadOnly : SQLiteOpenFlags.ReadWrite;

            if (!connectionStringBuilder.FailIfMissing && !connectionStringBuilder.ReadOnly)
            {
                openFlags |= SQLiteOpenFlags.Create;
            }

            SetState(ConnectionState.Connecting);

            Match  m         = s_vfsRegex.Match(m_dataSource);
            string fileName  = m.Groups["fileName"].Value;
            string vfsName   = m.Groups["vfsName"].Value;
            var    errorCode = NativeMethods.sqlite3_open_v2(ToNullTerminatedUtf8(fileName), out m_db, openFlags, string.IsNullOrEmpty(vfsName) ? null : ToNullTerminatedUtf8(vfsName));

            bool success = false;

            try
            {
                if (errorCode != SQLiteErrorCode.Ok)
                {
                    SetState(ConnectionState.Broken);
                    throw new SQLiteException(errorCode, m_db);
                }

                if (!string.IsNullOrEmpty(connectionStringBuilder.Password))
                {
                    byte[] passwordBytes = Encoding.UTF8.GetBytes(connectionStringBuilder.Password);
                    errorCode = NativeMethods.sqlite3_key(m_db, passwordBytes, passwordBytes.Length);
                    if (errorCode != SQLiteErrorCode.Ok)
                    {
                        throw new SQLiteException(errorCode, m_db);
                    }
                }

                bool allowOpenReadOnly = true;
#if MONOANDROID
                // opening read-only throws "EntryPointNotFoundException: sqlite3_db_readonly" on Android API 15 and below (JellyBean is API 16)
                allowOpenReadOnly = Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.JellyBean;
#endif
                if (allowOpenReadOnly)
                {
                    int isReadOnly = NativeMethods.sqlite3_db_readonly(m_db, "main");
                    if (isReadOnly == 1 && !connectionStringBuilder.ReadOnly)
                    {
                        throw new SQLiteException(SQLiteErrorCode.ReadOnly);
                    }
                }

                // wait up to ten seconds (in native code) when there is DB contention, but still give managed code a
                // chance to respond to cancellation periodically
                NativeMethods.sqlite3_busy_timeout(m_db, 10000);

                if (connectionStringBuilder.CacheSize != 0)
                {
                    this.ExecuteNonQuery("pragma cache_size={0}".FormatInvariant(connectionStringBuilder.CacheSize));
                }

                if (connectionStringBuilder.PageSize != 0)
                {
                    this.ExecuteNonQuery("pragma page_size={0}".FormatInvariant(connectionStringBuilder.PageSize));
                }

                if (connectionStringBuilder.ContainsKey(SQLiteConnectionStringBuilder.MmapSizeKey))
                {
                    this.ExecuteNonQuery("pragma mmap_size={0}".FormatInvariant(connectionStringBuilder.MmapSize));
                }

                if (connectionStringBuilder.ForeignKeys)
                {
                    this.ExecuteNonQuery("pragma foreign_keys = on");
                }

                if (connectionStringBuilder.JournalMode != SQLiteJournalModeEnum.Default)
                {
                    this.ExecuteNonQuery("pragma journal_mode={0}".FormatInvariant(connectionStringBuilder.JournalMode));
                }

                if (connectionStringBuilder.ContainsKey(SQLiteConnectionStringBuilder.SynchronousKey))
                {
                    this.ExecuteNonQuery("pragma synchronous={0}".FormatInvariant(connectionStringBuilder.SyncMode));
                }

                if (connectionStringBuilder.TempStore != SQLiteTemporaryStore.Default)
                {
                    this.ExecuteNonQuery("pragma temp_store={0}".FormatInvariant(connectionStringBuilder.TempStore));
                }

                if (m_statementCompleted != null)
                {
                    SetProfileCallback(s_profileCallback);
                }

                SetState(ConnectionState.Open);
                success = true;
            }
            finally
            {
                if (!success)
                {
                    Utility.Dispose(ref m_db);
                }
            }
        }
        public override void Open()
        {
            VerifyNotDisposed();
            if (State != ConnectionState.Closed)
            {
                throw new InvalidOperationException("Cannot Open when State is {0}.".FormatInvariant(State));
            }

            var connectionStringBuilder = new SQLiteConnectionStringBuilder {
                ConnectionString = ConnectionString
            };

            m_dataSource = connectionStringBuilder.DataSource;
            if (string.IsNullOrEmpty(m_dataSource))
            {
                throw new InvalidOperationException("Connection String Data Source must be set.");
            }

            SQLiteOpenFlags openFlags = connectionStringBuilder.ReadOnly ? SQLiteOpenFlags.ReadOnly : SQLiteOpenFlags.ReadWrite;

            if (!connectionStringBuilder.FailIfMissing && !connectionStringBuilder.ReadOnly)
            {
                openFlags |= SQLiteOpenFlags.Create;
            }

            SetState(ConnectionState.Connecting);

            Match  m         = s_vfsRegex.Match(m_dataSource);
            string fileName  = m.Groups["fileName"].Value;
            string vfsName   = m.Groups["vfsName"].Value;
            var    errorCode = NativeMethods.sqlite3_open_v2(ToNullTerminatedUtf8(fileName), out m_db, openFlags, string.IsNullOrEmpty(vfsName) ? null : ToNullTerminatedUtf8(vfsName));

            bool success = false;

            try
            {
                if (errorCode != SQLiteErrorCode.Ok)
                {
                    SetState(ConnectionState.Broken);
                    errorCode.ThrowOnError();
                }

                if (!string.IsNullOrEmpty(connectionStringBuilder.Password))
                {
                    byte[] passwordBytes = Encoding.UTF8.GetBytes(connectionStringBuilder.Password);
                    NativeMethods.sqlite3_key(m_db, passwordBytes, passwordBytes.Length).ThrowOnError();
                }

                int isReadOnly = NativeMethods.sqlite3_db_readonly(m_db, "main");
                if (isReadOnly == 1 && !connectionStringBuilder.ReadOnly)
                {
                    throw new SQLiteException(SQLiteErrorCode.ReadOnly);
                }

                if (connectionStringBuilder.CacheSize != 0)
                {
                    this.ExecuteNonQuery("pragma cache_size={0}".FormatInvariant(connectionStringBuilder.CacheSize));
                }

                if (connectionStringBuilder.PageSize != 0)
                {
                    this.ExecuteNonQuery("pragma page_size={0}".FormatInvariant(connectionStringBuilder.PageSize));
                }

                if (connectionStringBuilder.ContainsKey(SQLiteConnectionStringBuilder.MmapSizeKey))
                {
                    this.ExecuteNonQuery("pragma mmap_size={0}".FormatInvariant(connectionStringBuilder.MmapSize));
                }

                if (connectionStringBuilder.ForeignKeys)
                {
                    this.ExecuteNonQuery("pragma foreign_keys = on");
                }

                if (connectionStringBuilder.JournalMode != SQLiteJournalModeEnum.Default)
                {
                    this.ExecuteNonQuery("pragma journal_mode={0}".FormatInvariant(connectionStringBuilder.JournalMode));
                }

                if (connectionStringBuilder.ContainsKey(SQLiteConnectionStringBuilder.SynchronousKey))
                {
                    this.ExecuteNonQuery("pragma synchronous={0}".FormatInvariant(connectionStringBuilder.SyncMode));
                }

                if (connectionStringBuilder.TempStore != SQLiteTemporaryStore.Default)
                {
                    this.ExecuteNonQuery("pragma temp_store={0}".FormatInvariant(connectionStringBuilder.TempStore));
                }

                if (m_statementCompleted != null)
                {
                    NativeMethods.sqlite3_profile(m_db, m_profileCallback, IntPtr.Zero);
                }

                SetState(ConnectionState.Open);
                success = true;
            }
            finally
            {
                if (!success)
                {
                    Utility.Dispose(ref m_db);
                }
            }
        }
		public override void Open()
		{
			VerifyNotDisposed();
			if (State != ConnectionState.Closed)
				throw new InvalidOperationException("Cannot Open when State is {0}.".FormatInvariant(State));

			var connectionStringBuilder = new SQLiteConnectionStringBuilder { ConnectionString = ConnectionString };
			m_dataSource = connectionStringBuilder.DataSource;
			if (string.IsNullOrEmpty(m_dataSource))
				throw new InvalidOperationException("Connection String Data Source must be set.");

			SQLiteOpenFlags openFlags = connectionStringBuilder.ReadOnly ? SQLiteOpenFlags.ReadOnly : SQLiteOpenFlags.ReadWrite;
			if (!connectionStringBuilder.FailIfMissing && !connectionStringBuilder.ReadOnly)
				openFlags |= SQLiteOpenFlags.Create;

			SetState(ConnectionState.Connecting);

			Match m = s_vfsRegex.Match(m_dataSource);
			string fileName = m.Groups["fileName"].Value;
			string vfsName = m.Groups["vfsName"].Value;
			var errorCode = NativeMethods.sqlite3_open_v2(ToNullTerminatedUtf8(fileName), out m_db, openFlags, string.IsNullOrEmpty(vfsName) ? null : ToNullTerminatedUtf8(vfsName));

			bool success = false;
			try
			{
				if (errorCode != SQLiteErrorCode.Ok)
				{
					SetState(ConnectionState.Broken);
					errorCode.ThrowOnError();
				}

				if (!string.IsNullOrEmpty(connectionStringBuilder.Password))
				{
					byte[] passwordBytes = Encoding.UTF8.GetBytes(connectionStringBuilder.Password);
					NativeMethods.sqlite3_key(m_db, passwordBytes, passwordBytes.Length).ThrowOnError();
				}

				bool allowOpenReadOnly = true;
#if MONOANDROID
				// opening read-only throws "EntryPointNotFoundException: sqlite3_db_readonly" on Android API 15 and below (JellyBean is API 16)
				allowOpenReadOnly = Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.JellyBean;
#endif
				if (allowOpenReadOnly)
				{
					int isReadOnly = NativeMethods.sqlite3_db_readonly(m_db, "main");
					if (isReadOnly == 1 && !connectionStringBuilder.ReadOnly)
						throw new SQLiteException(SQLiteErrorCode.ReadOnly);
				}

				if (connectionStringBuilder.CacheSize != 0)
					this.ExecuteNonQuery("pragma cache_size={0}".FormatInvariant(connectionStringBuilder.CacheSize));

				if (connectionStringBuilder.PageSize != 0)
					this.ExecuteNonQuery("pragma page_size={0}".FormatInvariant(connectionStringBuilder.PageSize));

				if (connectionStringBuilder.ContainsKey(SQLiteConnectionStringBuilder.MmapSizeKey))
					this.ExecuteNonQuery("pragma mmap_size={0}".FormatInvariant(connectionStringBuilder.MmapSize));

				if (connectionStringBuilder.ForeignKeys)
					this.ExecuteNonQuery("pragma foreign_keys = on");

				if (connectionStringBuilder.JournalMode != SQLiteJournalModeEnum.Default)
					this.ExecuteNonQuery("pragma journal_mode={0}".FormatInvariant(connectionStringBuilder.JournalMode));

				if (connectionStringBuilder.ContainsKey(SQLiteConnectionStringBuilder.SynchronousKey))
					this.ExecuteNonQuery("pragma synchronous={0}".FormatInvariant(connectionStringBuilder.SyncMode));

				if (connectionStringBuilder.TempStore != SQLiteTemporaryStore.Default)
					this.ExecuteNonQuery("pragma temp_store={0}".FormatInvariant(connectionStringBuilder.TempStore));

				if (m_statementCompleted != null)
					SetProfileCallback(s_profileCallback);

				SetState(ConnectionState.Open);
				success = true;
			}
			finally
			{
				if (!success)
					Utility.Dispose(ref m_db);
			}
		}