2 // Mono.Data.Sqlite.SQLiteBase.cs
5 // Robert Simpson (robert@blackcastlesoft.com)
7 // Adapted and modified for the Mono Project by
8 // Marek Habersack (grendello@gmail.com)
11 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
12 // Copyright (C) 2007 Marek Habersack
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 /********************************************************
35 * ADO.NET 2.0 Data Provider for SQLite Version 3.X
36 * Written by Robert Simpson (robert@blackcastlesoft.com)
38 * Released to the public domain, use at your own risk!
39 ********************************************************/
41 namespace Mono
.Data
.Sqlite
45 using System
.Runtime
.InteropServices
;
46 using System
.Collections
.Generic
;
49 /// This internal class provides the foundation of Sqlite support. It defines all the abstract members needed to implement
50 /// a Sqlite data provider, and inherits from SqliteConvert which allows for simple translations of string to and from Sqlite.
52 internal abstract class SqliteBase
: SqliteConvert
, IDisposable
54 internal SqliteBase(SqliteDateFormats fmt
)
58 /// Returns a string representing the active version of Sqlite
60 internal abstract string Version { get; }
62 /// Returns the number of changes the last executing insert/update caused.
64 internal abstract int Changes { get; }
69 /// Implementers should call SqliteFunction.BindFunctions() and save the array after opening a connection
70 /// to bind all attributed user-defined functions and collating sequences to the new connection.
72 /// <param name="strFilename">The filename of the database to open. Sqlite automatically creates it if it doesn't exist.</param>
73 internal abstract void Open(string strFilename
);
75 /// Closes the currently-open database.
78 /// After the database has been closed implemeters should call SqliteFunction.UnbindFunctions() to deallocate all interop allocated
79 /// memory associated with the user-defined functions and collating sequences tied to the closed connection.
81 internal abstract void Close();
83 /// Sets the busy timeout on the connection. SqliteCommand will call this before executing any command.
85 /// <param name="nTimeoutMS">The number of milliseconds to wait before returning SQLITE_BUSY</param>
86 internal abstract void SetTimeout(int nTimeoutMS
);
88 /// Returns the text of the last error issued by Sqlite
90 /// <returns></returns>
91 internal abstract string SqliteLastError();
94 /// Prepares a SQL statement for execution.
96 /// <param name="strSql">The SQL command text to prepare</param>
97 /// <param name="previous">The previous statement in a multi-statement command, or null if no previous statement exists</param>
98 /// <param name="strRemain">The remainder of the statement that was not processed. Each call to prepare parses the
99 /// SQL up to to either the end of the text or to the first semi-colon delimiter. The remaining text is returned
100 /// here for a subsequent call to Prepare() until all the text has been processed.</param>
101 /// <returns>Returns an initialized SqliteStatement.</returns>
102 internal abstract SqliteStatement
Prepare(string strSql
, SqliteStatement previous
, out string strRemain
);
104 /// Steps through a prepared statement.
106 /// <param name="stmt">The SqliteStatement to step through</param>
107 /// <returns>True if a row was returned, False if not.</returns>
108 internal abstract bool Step(SqliteStatement stmt
);
110 /// Finalizes a prepared statement.
112 /// <param name="stmt">The statement to finalize</param>
113 internal abstract void FinalizeStatement(SqliteStatement stmt
);
115 /// Resets a prepared statement so it can be executed again. If the error returned is SQLITE_SCHEMA,
116 /// transparently attempt to rebuild the SQL statement and throw an error if that was not possible.
118 /// <param name="stmt">The statement to reset</param>
119 /// <returns>Returns -1 if the schema changed while resetting, 0 if the reset was sucessful or 6 (SQLITE_LOCKED) if the reset failed due to a lock</returns>
120 internal abstract int Reset(SqliteStatement stmt
);
122 internal abstract void Cancel();
124 internal abstract void Bind_Double(SqliteStatement stmt
, int index
, double value);
125 internal abstract void Bind_Int32(SqliteStatement stmt
, int index
, Int32
value);
126 internal abstract void Bind_Int64(SqliteStatement stmt
, int index
, Int64
value);
127 internal abstract void Bind_Text(SqliteStatement stmt
, int index
, string value);
128 internal abstract void Bind_Blob(SqliteStatement stmt
, int index
, byte[] blobData
);
129 internal abstract void Bind_DateTime(SqliteStatement stmt
, int index
, DateTime dt
);
130 internal abstract void Bind_Null(SqliteStatement stmt
, int index
);
132 internal abstract int Bind_ParamCount(SqliteStatement stmt
);
133 internal abstract string Bind_ParamName(SqliteStatement stmt
, int index
);
134 internal abstract int Bind_ParamIndex(SqliteStatement stmt
, string paramName
);
136 internal abstract int ColumnCount(SqliteStatement stmt
);
137 internal abstract string ColumnName(SqliteStatement stmt
, int index
);
138 internal abstract TypeAffinity
ColumnAffinity(SqliteStatement stmt
, int index
);
139 internal abstract string ColumnType(SqliteStatement stmt
, int index
, out TypeAffinity nAffinity
);
140 internal abstract int ColumnIndex(SqliteStatement stmt
, string columnName
);
141 internal abstract string ColumnOriginalName(SqliteStatement stmt
, int index
);
142 internal abstract string ColumnDatabaseName(SqliteStatement stmt
, int index
);
143 internal abstract string ColumnTableName(SqliteStatement stmt
, int index
);
144 internal abstract void ColumnMetaData(string dataBase
, string table
, string column
, out string dataType
, out string collateSequence
, out bool notNull
, out bool primaryKey
, out bool autoIncrement
);
146 internal abstract double GetDouble(SqliteStatement stmt
, int index
);
147 internal abstract Int32
GetInt32(SqliteStatement stmt
, int index
);
148 internal abstract Int64
GetInt64(SqliteStatement stmt
, int index
);
149 internal abstract string GetText(SqliteStatement stmt
, int index
);
150 internal abstract long GetBytes(SqliteStatement stmt
, int index
, int nDataoffset
, byte[] bDest
, int nStart
, int nLength
);
151 internal abstract long GetChars(SqliteStatement stmt
, int index
, int nDataoffset
, char[] bDest
, int nStart
, int nLength
);
152 internal abstract DateTime
GetDateTime(SqliteStatement stmt
, int index
);
153 internal abstract bool IsNull(SqliteStatement stmt
, int index
);
155 internal abstract IntPtr
CreateCollation(string strCollation
, SqliteCollation func
);
156 internal abstract IntPtr
CreateFunction(string strFunction
, int nArgs
, SqliteCallback func
, SqliteCallback funcstep
, SqliteCallback funcfinal
);
157 internal abstract void FreeFunction(IntPtr cookie
);
159 internal abstract int AggregateCount(IntPtr context
);
160 internal abstract IntPtr
AggregateContext(IntPtr context
);
162 internal abstract long GetParamValueBytes(IntPtr ptr
, int nDataOffset
, byte[] bDest
, int nStart
, int nLength
);
163 internal abstract double GetParamValueDouble(IntPtr ptr
);
164 internal abstract int GetParamValueInt32(IntPtr ptr
);
165 internal abstract Int64
GetParamValueInt64(IntPtr ptr
);
166 internal abstract string GetParamValueText(IntPtr ptr
);
167 internal abstract TypeAffinity
GetParamValueType(IntPtr ptr
);
169 internal abstract void ReturnBlob(IntPtr context
, byte[] value);
170 internal abstract void ReturnDouble(IntPtr context
, double value);
171 internal abstract void ReturnError(IntPtr context
, string value);
172 internal abstract void ReturnInt32(IntPtr context
, Int32
value);
173 internal abstract void ReturnInt64(IntPtr context
, Int64
value);
174 internal abstract void ReturnNull(IntPtr context
);
175 internal abstract void ReturnText(IntPtr context
, string value);
177 internal abstract void SetUpdateHook(SqliteUpdateCallback func
);
178 internal abstract void SetCommitHook(SqliteCommitCallback func
);
179 internal abstract void SetRollbackHook(SqliteRollbackCallback func
);
181 internal abstract int GetLastInsertRowId ();
183 internal abstract object GetValue(SqliteStatement stmt
, int index
, ref SqliteType typ
);
185 protected virtual void Dispose(bool bDisposing
)
189 public void Dispose()
192 GC
.SuppressFinalize(this);