예제 #1
0
        static SQLiteFactory()
        {
#if (SQLITE_STANDARD || USE_INTEROP_DLL || PLATFORM_COMPACTFRAMEWORK) && PRELOAD_NATIVE_LIBRARY
            UnsafeNativeMethods.Initialize();
#endif

#if USE_INTEROP_DLL && INTEROP_LOG
            if (UnsafeNativeMethods.sqlite3_config_log_interop() == SQLiteErrorCode.Ok)
            {
                UnsafeNativeMethods.sqlite3_log(
                    SQLiteErrorCode.Ok, SQLiteConvert.ToUTF8("logging initialized."));
            }
#endif

            SQLiteLog.Initialize();

            string version =
#if NET_40 || NET_45 || NET_451 || NET_452 || NET_46 || NET_461
                "4.0.0.0";
#else
                "3.5.0.0";
#endif

            _dbProviderServicesType = Type.GetType(HelperMethods.StringFormat(CultureInfo.InvariantCulture, "System.Data.Common.DbProviderServices, System.Data.Entity, Version={0}, Culture=neutral, PublicKeyToken=b77a5c561934e089", version), false);
        }
예제 #2
0
        ///////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Creates a <see cref="SQLiteBlob" /> object.  This will not work
        /// for tables that were created WITHOUT ROWID.
        /// </summary>
        /// <param name="connection">
        /// The connection to use when opening the blob object.
        /// </param>
        /// <param name="databaseName">
        /// The name of the database containing the blob object.
        /// </param>
        /// <param name="tableName">
        /// The name of the table containing the blob object.
        /// </param>
        /// <param name="columnName">
        /// The name of the column containing the blob object.
        /// </param>
        /// <param name="rowId">
        /// The integer identifier for the row associated with the desired
        /// blob object.
        /// </param>
        /// <param name="readOnly">
        /// Non-zero to open the blob object for read-only access.
        /// </param>
        /// <returns>
        /// The newly created <see cref="SQLiteBlob" /> instance -OR- null
        /// if an error occurs.
        /// </returns>
        public static SQLiteBlob Create(
            SQLiteConnection connection,
            string databaseName,
            string tableName,
            string columnName,
            long rowId,
            bool readOnly
            )
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            SQLite3 sqlite3 = connection._sql as SQLite3;

            if (sqlite3 == null)
            {
                throw new InvalidOperationException("Connection has no wrapper");
            }

            SQLiteConnectionHandle handle = sqlite3._sql;

            if (handle == null)
            {
                throw new InvalidOperationException("Connection has an invalid handle.");
            }

            SQLiteBlobHandle blob = null;

            try
            {
                // do nothing.
            }
            finally /* NOTE: Thread.Abort() protection. */
            {
                IntPtr ptrBlob = IntPtr.Zero;

                SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3_blob_open(
                    handle, SQLiteConvert.ToUTF8(databaseName),
                    SQLiteConvert.ToUTF8(tableName), SQLiteConvert.ToUTF8(
                        columnName), rowId, readOnly ? 0 : 1, ref ptrBlob);

                if (rc != SQLiteErrorCode.Ok)
                {
                    throw new SQLiteException(rc, null);
                }

                blob = new SQLiteBlobHandle(handle, ptrBlob);
            }

            SQLiteConnection.OnChanged(connection, new ConnectionEventArgs(
                                           SQLiteConnectionEventType.NewCriticalHandle, null, null,
                                           null, null, blob, null, new object[] { typeof(SQLiteBlob),
                                                                                  databaseName, tableName, columnName, rowId, readOnly }));

            return(new SQLiteBlob(sqlite3, blob));
        }
예제 #3
0
파일: SQLite3.cs 프로젝트: bdcliang/BD
        internal override void Bind_Text(SQLiteStatement stmt, int index, string value)
        {
            byte[] buffer    = SQLiteConvert.ToUTF8(value);
            int    errorCode = UnsafeNativeMethods.sqlite3_bind_text((IntPtr)stmt._sqlite_stmt, index, buffer, buffer.Length - 1, (IntPtr)(-1));

            if (errorCode > 0)
            {
                throw new SQLiteException(errorCode, this.SQLiteLastError());
            }
        }
예제 #4
0
파일: SQLite3.cs 프로젝트: bdcliang/BD
        internal override void CreateCollation(string strCollation, SQLiteCollation func, SQLiteCollation func16)
        {
            int errorCode = UnsafeNativeMethods.sqlite3_create_collation((IntPtr)this._sql, SQLiteConvert.ToUTF8(strCollation), 2, IntPtr.Zero, func16);

            if (errorCode == 0)
            {
                UnsafeNativeMethods.sqlite3_create_collation((IntPtr)this._sql, SQLiteConvert.ToUTF8(strCollation), 1, IntPtr.Zero, func);
            }
            if (errorCode > 0)
            {
                throw new SQLiteException(errorCode, this.SQLiteLastError());
            }
        }
예제 #5
0
파일: SQLiteBase.cs 프로젝트: bdcliang/BD
 internal static void ResetConnection(SQLiteConnectionHandle db)
 {
     lock (_lock)
     {
         IntPtr zero = IntPtr.Zero;
         do
         {
             zero = UnsafeNativeMethods.sqlite3_next_stmt((IntPtr)db, zero);
             if (zero != IntPtr.Zero)
             {
                 UnsafeNativeMethods.sqlite3_reset_interop(zero);
             }
         }while (zero != IntPtr.Zero);
         UnsafeNativeMethods.sqlite3_exec((IntPtr)db, SQLiteConvert.ToUTF8("ROLLBACK"), IntPtr.Zero, IntPtr.Zero, out zero);
     }
 }
예제 #6
0
 internal override void Open(string strFilename, SQLiteOpenFlagsEnum flags, int maxPoolSize, bool usePool)
 {
     if (base._sql == null)
     {
         base._usePool = usePool;
         if (usePool)
         {
             base._fileName = strFilename;
             base._sql      = SQLiteConnectionPool.Remove(strFilename, maxPoolSize, out this._poolVersion);
         }
         if (base._sql == null)
         {
             IntPtr ptr;
             int    errorCode = UnsafeNativeMethods.sqlite3_open16_interop(SQLiteConvert.ToUTF8(strFilename), (int)flags, out ptr);
             if (errorCode > 0)
             {
                 throw new SQLiteException(errorCode, null);
             }
             base._sql = ptr;
         }
         base._functionsArray = SQLiteFunction.BindFunctions(this);
     }
 }
예제 #7
0
파일: SQLite3.cs 프로젝트: bdcliang/BD
 internal override int Bind_ParamIndex(SQLiteStatement stmt, string paramName)
 {
     return(UnsafeNativeMethods.sqlite3_bind_parameter_index((IntPtr)stmt._sqlite_stmt, SQLiteConvert.ToUTF8(paramName)));
 }
예제 #8
0
파일: SQLite3.cs 프로젝트: bdcliang/BD
 internal override void ReturnText(IntPtr context, string value)
 {
     byte[] buffer = SQLiteConvert.ToUTF8(value);
     UnsafeNativeMethods.sqlite3_result_text(context, SQLiteConvert.ToUTF8(value), buffer.Length - 1, (IntPtr)(-1));
 }
예제 #9
0
파일: SQLite3.cs 프로젝트: bdcliang/BD
 internal override void ReturnError(IntPtr context, string value)
 {
     UnsafeNativeMethods.sqlite3_result_error(context, SQLiteConvert.ToUTF8(value), value.Length);
 }
예제 #10
0
파일: SQLite3.cs 프로젝트: bdcliang/BD
        internal override SQLiteStatement Prepare(SQLiteConnection cnn, string strSql, SQLiteStatement previous, uint timeoutMS, out string strRemain)
        {
            SQLiteStatement statement2;
            IntPtr          zero      = IntPtr.Zero;
            IntPtr          ptrRemain = IntPtr.Zero;
            int             nRemain   = 0;
            int             errorCode = 0x11;
            int             num3      = 0;

            byte[]          buffer    = SQLiteConvert.ToUTF8(strSql);
            string          typedefs  = null;
            SQLiteStatement statement = null;
            Random          random    = null;
            uint            tickCount = (uint)Environment.TickCount;
            GCHandle        handle    = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            IntPtr          pSql      = handle.AddrOfPinnedObject();

            try
            {
                while ((((errorCode == 0x11) || (errorCode == 6)) || (errorCode == 5)) && (num3 < 3))
                {
                    errorCode = UnsafeNativeMethods.sqlite3_prepare_interop((IntPtr)this._sql, pSql, buffer.Length - 1, out zero, out ptrRemain, out nRemain);
                    if (errorCode == 0x11)
                    {
                        num3++;
                    }
                    else
                    {
                        if (errorCode == 1)
                        {
                            if (string.Compare(this.SQLiteLastError(), "near \"TYPES\": syntax error", StringComparison.OrdinalIgnoreCase) == 0)
                            {
                                int index = strSql.IndexOf(';');
                                if (index == -1)
                                {
                                    index = strSql.Length - 1;
                                }
                                typedefs  = strSql.Substring(0, index + 1);
                                strSql    = strSql.Substring(index + 1);
                                strRemain = "";
                                while ((statement == null) && (strSql.Length > 0))
                                {
                                    statement = this.Prepare(cnn, strSql, previous, timeoutMS, out strRemain);
                                    strSql    = strRemain;
                                }
                                if (statement != null)
                                {
                                    statement.SetTypes(typedefs);
                                }
                                return(statement);
                            }
                            if (this._buildingSchema || (string.Compare(this.SQLiteLastError(), 0, "no such table: TEMP.SCHEMA", 0, 0x1a, StringComparison.OrdinalIgnoreCase) != 0))
                            {
                                continue;
                            }
                            strRemain            = "";
                            this._buildingSchema = true;
                            try
                            {
                                ISQLiteSchemaExtensions service = ((IServiceProvider)SQLiteFactory.Instance).GetService(typeof(ISQLiteSchemaExtensions)) as ISQLiteSchemaExtensions;
                                if (service != null)
                                {
                                    service.BuildTempSchema(cnn);
                                }
                                while ((statement == null) && (strSql.Length > 0))
                                {
                                    statement = this.Prepare(cnn, strSql, previous, timeoutMS, out strRemain);
                                    strSql    = strRemain;
                                }
                                return(statement);
                            }
                            finally
                            {
                                this._buildingSchema = false;
                            }
                        }
                        switch (errorCode)
                        {
                        case 6:
                        case 5:
                            if (random == null)
                            {
                                random = new Random();
                            }
                            if ((Environment.TickCount - tickCount) > timeoutMS)
                            {
                                throw new SQLiteException(errorCode, this.SQLiteLastError());
                            }
                            Thread.Sleep(random.Next(1, 150));
                            break;
                        }
                    }
                }
                if (errorCode > 0)
                {
                    throw new SQLiteException(errorCode, this.SQLiteLastError());
                }
                strRemain = SQLiteConvert.UTF8ToString(ptrRemain, nRemain);
                if (zero != IntPtr.Zero)
                {
                    statement = new SQLiteStatement(this, zero, strSql.Substring(0, strSql.Length - strRemain.Length), previous);
                }
                statement2 = statement;
            }
            finally
            {
                handle.Free();
            }
            return(statement2);
        }
예제 #11
0
파일: SQLite3.cs 프로젝트: bdcliang/BD
        internal override void GetIndexColumnExtendedInfo(string database, string index, string column, out int sortMode, out int onError, out string collationSequence)
        {
            IntPtr ptr;
            int    num;
            int    errorCode = UnsafeNativeMethods.sqlite3_index_column_info_interop((IntPtr)this._sql, SQLiteConvert.ToUTF8(database), SQLiteConvert.ToUTF8(index), SQLiteConvert.ToUTF8(column), out sortMode, out onError, out ptr, out num);

            if (errorCode != 0)
            {
                throw new SQLiteException(errorCode, "");
            }
            collationSequence = SQLiteConvert.UTF8ToString(ptr, num);
        }
예제 #12
0
파일: SQLite3.cs 프로젝트: bdcliang/BD
        internal override void CreateFunction(string strFunction, int nArgs, bool needCollSeq, SQLiteCallback func, SQLiteCallback funcstep, SQLiteFinalCallback funcfinal)
        {
            int errorCode = UnsafeNativeMethods.sqlite3_create_function_interop((IntPtr)this._sql, SQLiteConvert.ToUTF8(strFunction), nArgs, 4, IntPtr.Zero, func, funcstep, funcfinal, needCollSeq ? 1 : 0);

            if (errorCode == 0)
            {
                errorCode = UnsafeNativeMethods.sqlite3_create_function_interop((IntPtr)this._sql, SQLiteConvert.ToUTF8(strFunction), nArgs, 1, IntPtr.Zero, func, funcstep, funcfinal, needCollSeq ? 1 : 0);
            }
            if (errorCode > 0)
            {
                throw new SQLiteException(errorCode, this.SQLiteLastError());
            }
        }
예제 #13
0
파일: SQLite3.cs 프로젝트: bdcliang/BD
        internal override void ColumnMetaData(string dataBase, string table, string column, out string dataType, out string collateSequence, out bool notNull, out bool primaryKey, out bool autoIncrement)
        {
            IntPtr ptr;
            IntPtr ptr2;
            int    num;
            int    num2;
            int    num3;
            int    num5;
            int    num6;
            int    errorCode = UnsafeNativeMethods.sqlite3_table_column_metadata_interop((IntPtr)this._sql, SQLiteConvert.ToUTF8(dataBase), SQLiteConvert.ToUTF8(table), SQLiteConvert.ToUTF8(column), out ptr, out ptr2, out num, out num2, out num3, out num5, out num6);

            if (errorCode > 0)
            {
                throw new SQLiteException(errorCode, this.SQLiteLastError());
            }
            dataType        = SQLiteConvert.UTF8ToString(ptr, num5);
            collateSequence = SQLiteConvert.UTF8ToString(ptr2, num6);
            notNull         = num == 1;
            primaryKey      = num2 == 1;
            autoIncrement   = num3 == 1;
        }
        ///////////////////////////////////////////////////////////////////////////////////////////////

        #region Static "Factory" Methods
        /// <summary>
        /// Creates a <see cref="SQLiteBlob" /> object.  This will not work
        /// for tables that were created WITHOUT ROWID -OR- if the query
        /// does not include the "rowid" column or one of its aliases -OR-
        /// if the <see cref="SQLiteDataReader" /> was not created with the
        /// <see cref="CommandBehavior.KeyInfo" /> flag.
        /// </summary>
        /// <param name="dataReader">
        /// The <see cref="SQLiteDataReader" /> instance with a result set
        /// containing the desired blob column.
        /// </param>
        /// <param name="i">
        /// The index of the blob column.
        /// </param>
        /// <param name="readOnly">
        /// Non-zero to open the blob object for read-only access.
        /// </param>
        /// <returns>
        /// The newly created <see cref="SQLiteBlob" /> instance -OR- null
        /// if an error occurs.
        /// </returns>
        public static SQLiteBlob Create(
            SQLiteDataReader dataReader,
            int i,
            bool readOnly
            )
        {
            SQLiteConnection connection = SQLiteDataReader.GetConnection(
                dataReader);

            if (connection == null)
            {
                throw new InvalidOperationException("Connection not available");
            }

            SQLite3 sqlite3 = connection._sql as SQLite3;

            if (sqlite3 == null)
            {
                throw new InvalidOperationException("Connection has no wrapper");
            }

            SQLiteConnectionHandle handle = sqlite3._sql;

            if (handle == null)
            {
                throw new InvalidOperationException("Connection has an invalid handle.");
            }

            long?rowId = dataReader.GetRowId(i);

            if (rowId == null)
            {
                throw new InvalidOperationException("No RowId is available");
            }

            SQLiteBlobHandle blob = null;

            try
            {
                // do nothing.
            }
            finally /* NOTE: Thread.Abort() protection. */
            {
                IntPtr ptrBlob = IntPtr.Zero;

                SQLiteErrorCode rc = UnsafeNativeMethods.sqlite3_blob_open(
                    sqlite3._sql, SQLiteConvert.ToUTF8(
                        dataReader.GetDatabaseName(i)), SQLiteConvert.ToUTF8(
                        dataReader.GetTableName(i)), SQLiteConvert.ToUTF8(
                        dataReader.GetName(i)), (long)rowId, readOnly ? 0 : 1,
                    ref ptrBlob);

                if (rc != SQLiteErrorCode.Ok)
                {
                    throw new SQLiteException(rc, null);
                }

                blob = new SQLiteBlobHandle(handle, ptrBlob);
            }

            SQLiteConnection.OnChanged(null, new ConnectionEventArgs(
                                           SQLiteConnectionEventType.NewCriticalHandle, null,
                                           null, null, dataReader, blob, null, new object[] {
                typeof(SQLiteBlob), dataReader, i, readOnly
            }));

            return(new SQLiteBlob(sqlite3, blob));
        }