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); }
public T ExecuteScalar <T>() { if (_conn.Trace) { Debug.WriteLine("Executing Query: " + this); } T val = default(T); IntPtr stmt = Prepare(); try { SQLite3.Result r = SQLite3.Step(stmt); if (r == SQLite3.Result.Row) { SQLite3.ColType colType = SQLite3.ColumnType(stmt, 0); val = (T)ReadCol(stmt, 0, colType, typeof(T)); } else if (r == SQLite3.Result.Done) { } else { throw SQLiteException.New(r, SQLite3.GetErrmsg(_conn.Handle)); } } finally { Finalize(stmt); } return(val); }
public T ExecuteScalar <T>() { if (_conn.Trace) { Debug.WriteLine("Executing Query: " + this); } T val = default(T); var stmt = Prepare(); try { var r = SQLite3.Step(stmt); if (r == SQLite3.Result.Row) { var colType = SQLite3.ColumnType(stmt, 0); var colSQLiteType = SQLite3.GetSQLiteType(typeof(T)); val = (T)colSQLiteType.Read(stmt, 0, _conn.StoreDateTimeAsTicks); } else if (r == SQLite3.Result.Done) { } else { throw SQLiteException.New(r, SQLite3.GetErrmsg(_conn.Handle)); } } finally { Finalize(stmt); } return(val); }
private static IEnumerable <ReaderItem> ExecuteReader(this SQLiteCommand sqliteCommand, SQLiteConnection connection) { sqlite3_stmt stmt = Prepare(connection, sqliteCommand.CommandText); try { // We need to manage columns dynamically in order to create columns ColumnLite[] cols = new ColumnLite[SQLite3.ColumnCount(stmt)]; while (SQLite3.Step(stmt) == SQLite3.Result.Row) { ReaderItem obj = new ReaderItem(); for (int i = 0; i < cols.Length; i++) { if (cols[i] == null) { // We try to create column mapping if it's not already created : string name = SQLite3.ColumnName16(stmt, i); cols[i] = new ColumnLite(name, SQLite3.ColumnType(stmt, i).ToType()); } SQLite3.ColType colType = SQLite3.ColumnType(stmt, i); object val = ReadCol(connection, stmt, i, colType, cols[i].ColumnType); obj[cols[i].Name] = val; } yield return(obj); } } finally { SQLite3.Finalize(stmt); } }
/// <summary> /// execute a query</summary> /// <returns> /// return a JArray which contains all objects </returns> /// <param name="query"> contains the query to execute</param> /// <param name="param"> contains the parameters needed to execute the query</param> public JArray execute(String query, object[] param) { JArray array = new JArray(); if (query != null && query.Length > 0) { stmt = SQLite3.Prepare2(this.Handle, query); if (stmt != null) { if (param != null && param.Length > 0) { for (int i = 1; i <= param.Length; i++) { bindValue(param.GetValue(i - 1), i); } } while (SQLite3.Step(stmt) == SQLite3.Result.Row) { int count = SQLite3.ColumnCount(stmt); JObject obj = new JObject(); for (int i = 0; i < count; i++) { string name = SQLite3.ColumnName(stmt, i); SQLite3.ColType colType = SQLite3.ColumnType(stmt, i); switch (colType) { case SQLite3.ColType.Blob: byte[] bytes = SQLite3.ColumnByteArray(stmt, i); obj.Add(name, bytes); break; case SQLite3.ColType.Integer: int intValue = SQLite3.ColumnInt(stmt, i); obj.Add(name, intValue); break; case SQLite3.ColType.Float: double doubleValue = SQLite3.ColumnDouble(stmt, i); obj.Add(name, doubleValue); break; case SQLite3.ColType.Text: string text = SQLite3.ColumnString(stmt, i); obj.Add(name, text); break; case SQLite3.ColType.Null: default: obj.Add(name, null); break; } } array.Add(obj); } } SQLite3.Finalize(stmt); } return(array); }
public IEnumerable <T> ExecuteDeferredQuery <T>(TableMapping map) { if (_conn.Trace) { Debug.WriteLine("Executing Query: " + this); } var stmt = Prepare(); try { var cols = new TableMappingColumn[SQLite3.ColumnCount(stmt)]; for (int i = 0; i < cols.Length; i++) { var name = SQLite3.ColumnName16(stmt, i); cols[i] = map.FindColumn(name); } while (SQLite3.Step(stmt) == SQLite3.Result.Row) { var obj = Activator.CreateInstance(map.MappedType); for (int i = 0; i < cols.Length; i++) { // TODO: Disabling on CanWrite here is temporary until we have // accessing container subtables down if (cols[i] == null) { continue; } if (cols[i] is DirectTableMappingColumn) { var colType = SQLite3.ColumnType(stmt, i); var colSQLiteType = SQLite3.GetSQLiteType(cols[i].TargetType); var val = colSQLiteType.Read(stmt, i, _conn.StoreDateTimeAsTicks); (cols[i] as DirectTableMappingColumn).SetValue(obj, val); } } foreach (IndirectTableMappingColumn column in map.IndirectColumns) { try { column.LoadValueFromDatabase(obj); } catch (Exception e) { Debug.WriteLine(e.ToString()); } } OnInstanceCreated(obj); yield return((T)obj); } } finally { SQLite3.Finalize(stmt); } }
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)); } }
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); } } }
public List <object[]> ExecuteCustomQuery() { if (SQLiteConnection.Trace) { Debug.WriteLine("Executing Query: " + this); } var stmt = Prepare(); try { var colLenght = SQLite3.ColumnCount(stmt); var lstRes = new List <object[]>(); while (SQLite3.Step(stmt) == SQLite3.Result.Row) { var obj = new object[colLenght]; lstRes.Add(obj); for (int i = 0; i < colLenght; i++) { var colType = SQLite3.ColumnType(stmt, i); switch (colType) { case SQLite3.ColType.Blob: obj[i] = SQLite3.ColumnBlob(stmt, i); break; case SQLite3.ColType.Float: obj[i] = SQLite3.ColumnDouble(stmt, i); break; case SQLite3.ColType.Integer: obj[i] = SQLite3.ColumnInt(stmt, i); break; case SQLite3.ColType.Null: obj[i] = null; break; case SQLite3.ColType.Text: obj[i] = SQLite3.ColumnString(stmt, i); break; } } } return(lstRes); } finally { SQLite3.Finalize(stmt); } }
public override object GetDataObject(DataLinkParams dlp) { DbCommand command = CreateCommand(dlp); if (command == null) { return(null); } using (command) { try { var stmt = SQLite3.Prepare2(command.Connection.Handle, command.CommandText); using (stmt) { // setup parameters BindParameters(stmt, command); // build Recordset object Recordset result = new Recordset(); // create columns GetColumns(stmt, out result.Names, out result.Types); for (int i = 0; i < result.Names.Length; i++) { result.AddColumn(); } // fill recordset object[] values = new object[result.Names.Length]; while (SQLite3.Step(stmt) == SQLite3.Result.Row && (dlp.MaxRecords <= 0 || result.Count < dlp.MaxRecords)) { for (int i = 0; i < result.Names.Length; i++) { values[i] = ReadCol(stmt, i, SQLite3.ColumnType(stmt, i)); } result.Add(values); } return(result); } } catch { return(null); } } }
/// <summary> /// Gets the type of the column. /// </summary> /// <param name="colNr"> /// The col nr. /// </param> /// <returns> /// The <see cref="ColType"/>. /// </returns> public SQLite3.ColType GetColumnType(int colNr) { IntPtr statement = IntPtr.Zero; try { statement = this.GetPreparedStatement(); return(SQLite3.ColumnType(statement, colNr)); } finally { if (statement != IntPtr.Zero) { SQLite3.Finalize(statement); } } }
public static IEnumerable <IEnumerable <object> > ExecuteDeferredQuery(this SQLiteCommand command, Table table, SQLiteConnection connection) { if (connection.Trace) { connection.Tracer?.Invoke("Executing Query: " + command); } var items = new List <List <object> >(); var prepareMethod = command.GetType().GetMethod("Prepare", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); if (prepareMethod == null) { throw new MissingMethodException("Could not find Prepare() on SQLiteCommand, are you using a different version of SQLite-Net-PCL?"); } var stmt = prepareMethod.Invoke(command, null); //var stmt = (Sqlite3Statement)prepareMethod.Invoke(command, null); //var stmt = command.Prepare();//If public can avoid the reflection while (SQLite3.Step((SQLitePCL.sqlite3_stmt)stmt) == SQLite3.Result.Row) { var row = new List <object>(); for (int i = 0; i < table.ColumnInfos.Count(); i++) { var colType = SQLite3.ColumnType((SQLitePCL.sqlite3_stmt)stmt, i); var clrType = GetTypeFromColType(colType); var readColMethod = command.GetType().GetMethod("ReadCol", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); if (readColMethod == null) { throw new MissingMethodException("Could not find ReadCol() on SQLiteCommand, are you using a different version of SQLite-Net-PCL?"); } var val = readColMethod.Invoke(command, new object[] { stmt, i, colType, clrType }); //var val = command.ReadCol(stmt, i, colType, clrType);//if public can avoid the reflection row.Add(val); } items.Add(row); } return(items); }
public IEnumerable <T> ExecuteDeferredQuery <T>(TableMapping map) { if (_conn.Trace) { Debug.WriteLine("Executing Query: " + this); } IntPtr stmt = Prepare(); try { TableMapping.Column[] cols = new TableMapping.Column[SQLite3.ColumnCount(stmt)]; for (int i = 0; i < cols.Length; i++) { string name = SQLite3.ColumnName16(stmt, i); cols[i] = map.FindColumn(name); } while (SQLite3.Step(stmt) == SQLite3.Result.Row) { object obj = Activator.CreateInstance(map.MappedType); for (int i = 0; i < cols.Length; i++) { if (cols[i] == null) { continue; } SQLite3.ColType colType = SQLite3.ColumnType(stmt, i); object val = ReadCol(stmt, i, colType, cols[i].ColumnType); cols[i].SetValue(obj, val); } OnInstanceCreated(obj); yield return((T)obj); } } finally { SQLite3.Finalize(stmt); } }
public object ReadCol(IntPtr stmt, int index, Type clrType) { var type = SQLite3.ColumnType(stmt, index); if (type == SQLite3.ColType.Null) { return(null); } if (clrType == typeof(Byte) || clrType == typeof(UInt16) || clrType == typeof(SByte) || clrType == typeof(Int16) || clrType == typeof(Int32)) { return(Convert.ChangeType(SQLite3.ColumnInt(stmt, index), clrType)); } if (clrType == typeof(UInt32) || clrType == typeof(Int64)) { return(Convert.ChangeType(SQLite3.ColumnInt64(stmt, index), clrType)); } if (clrType == typeof(Single) || clrType == typeof(Double) || clrType == typeof(Decimal)) { return(Convert.ChangeType(SQLite3.ColumnDouble(stmt, index), clrType)); } if (clrType == typeof(String)) { return(Convert.ChangeType(SQLite3.ColumnString(stmt, index), clrType)); } if (clrType == typeof(byte[])) { return(SQLite3.ColumnByteArray(stmt, index)); } if (clrType == typeof(DateTime)) { return(ToDateTime(SQLite3.ColumnString(stmt, index))); } if (clrType == typeof(Boolean)) { return(ToBoolean(SQLite3.ColumnString(stmt, index))); } throw new NotSupportedException("Don't know how to read " + clrType); }
public Object ExecuteScalar() { Object retObj = null; var stmt = Prepare(); var cols = new System.Reflection.PropertyInfo[SQLite3.ColumnCount(stmt)]; if (SQLite3.Step(stmt) == SQLite3.Result.Row) { if (cols.Length > 0) { var type = SQLite3.ColumnType(stmt, 0); switch (type) { case SQLite3.ColType.Integer: retObj = SQLite3.ColumnInt64(stmt, 0); break; case SQLite3.ColType.Float: retObj = SQLite3.ColumnDouble(stmt, 0); break; case SQLite3.ColType.Blob: retObj = SQLite3.ColumnByteArray(stmt, 0); break; case SQLite3.ColType.Text: retObj = SQLite3.ColumnString(stmt, 0); break; case SQLite3.ColType.Null: break; } } } SQLite3.Finalize(stmt); return(retObj); }
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); } } }
public IEnumerator <IDictionary <String, Object> > ExecuteSelectQuery(DatabaseDescriptor databaseDescriptor, EntityDescriptor entityDescriptor, String query) { #if XAMARIN SQLitePCL.sqlite3_stmt statement = SQLite3.Prepare2(sqliteDatabase.Handle, query); #elif WINDOWS List <SQLiteQueryRow> statement = null; #endif try { #if XAMARIN statement = SQLite3.Prepare2(sqliteDatabase.Handle, query); #elif WINDOWS statement = sqliteDatabase.Query2(query); #endif } catch (System.Exception exception) { if (sqliteDatabase.IsInTransaction) { sqliteDatabase.Rollback(); } Log.Log.Error(typeof(Database).FullName, "ExecuteSelectQuery", "Exception caught while executing the select query, QUERY: " + query); throw new DatabaseException(typeof(Database).FullName, "ExecuteSelectQuery", exception.Message); } List <Dictionary <String, Object> > tuples = new List <Dictionary <String, Object> >(); #if XAMARIN SQLite3.Result result; while ((result = SQLite3.Step(statement)) == SQLite3.Result.Row) { IDictionary <String, Object> tuple = new Dictionary <String, Object>(); String stmtResult = result.ToString(); //string[] names = SQLite3.ColType.GetNames (); //string[] values = SQLite3.ColType.GetValues (); int columnsCount = SQLite3.ColumnCount(statement); for (int i = 0; i < columnsCount; i++) { String columnName = SQLite3.ColumnName(statement, i); SQLite3.ColType columnType = SQLite3.ColumnType(statement, i); Object columnValue = SQLite3.ColumnText(statement, i); bool isString = false; bool isLong = false; bool isFloat = false; bool isBlob = false; if (columnType == SQLite3.ColType.Text) { isString = true; } else if (columnType == SQLite3.ColType.Integer) { isLong = true; } else if (columnType == SQLite3.ColType.Float) { isFloat = true; } else if (columnType == SQLite3.ColType.Blob) { isBlob = true; } else if (columnType == SQLite3.ColType.Null) { } if (isString) { if (entityDescriptor != null) { EntityDescriptor.Attribute attribute = entityDescriptor.GetAttributeBasedOnColumnName(columnName); if (attribute != null) { if (attribute.GetType().Equals(IDataTypeHandler.CSHARP_STRING_DATA_TYPE, StringComparison.OrdinalIgnoreCase)) { tuple.Add(columnName, (String)columnValue); } else if (attribute.GetType().Equals(IDataTypeHandler.CSHARP_BOOLEAN_PRIMITIVE_DATA_TYPE, StringComparison.OrdinalIgnoreCase)) { tuple.Add(columnName, columnValue.Equals(Boolean.TrueString.ToString()) ? true : false); } else if (attribute.GetType().Equals(IDataTypeHandler.CSHARP_BOOLEAN_DATA_TYPE, StringComparison.OrdinalIgnoreCase)) { tuple.Add(columnName, columnValue.Equals(Boolean.TrueString.ToString()) ? Boolean.TrueString : Boolean.FalseString); } else if (attribute.GetType().Equals(IDataTypeHandler.JAVASCRIPT_STRING_DATA_TYPE, StringComparison.OrdinalIgnoreCase)) { tuple.Add(columnName, (String)columnValue); } } else { tuple.Add(columnName, columnValue); } } else { tuple.Add(columnName, columnValue); } } else if (isLong) { if (entityDescriptor != null) { EntityDescriptor.Attribute attribute = entityDescriptor.GetAttributeBasedOnColumnName(columnName); if (attribute != null) { if (attribute.GetType().Equals(IDataTypeHandler.JAVASCRIPT_NUMBER_DATA_TYPE, StringComparison.OrdinalIgnoreCase)) { tuple.Add(columnName, columnValue); } } else { tuple.Add(columnName, columnValue); } } else { tuple.Add(columnName, columnValue); } } else if (isFloat) { if (entityDescriptor != null) { EntityDescriptor.Attribute attribute = entityDescriptor.GetAttributeBasedOnColumnName(columnName); if (attribute != null) { if (attribute.GetType().Equals(IDataTypeHandler.JAVASCRIPT_NUMBER_DATA_TYPE, StringComparison.OrdinalIgnoreCase)) { tuple.Add(columnName, columnValue); } } else { tuple.Add(columnName, columnValue); } } else { tuple.Add(columnName, columnValue); } tuple.Add(columnName, columnValue); } else if (isBlob) { tuple.Add(columnName, columnValue); } } tuples.Add((Dictionary <String, Object>)tuple); } SQLite3.Finalize(statement); return(tuples.GetEnumerator()); #elif WINDOWS foreach (SQLiteQueryRow cursor in statement) { IDictionary <String, Object> tuple = new Dictionary <String, Object>(); List <SQLiteQueryColumn> columns = cursor.column; if (columns == null || columns.Count <= 0) { continue; } foreach (SQLiteQueryColumn column in columns) { String columnName = column.Key; Object columnValue = column.Value; bool isString = false; bool isLong = false; bool isFloat = false; bool isBlob = false; if (columnValue != null) { if (columnValue.GetType().FullName.Equals(typeof(String).FullName, StringComparison.OrdinalIgnoreCase)) { isString = true; } else if (columnValue.GetType().FullName.Equals(typeof(long).FullName, StringComparison.OrdinalIgnoreCase)) { isLong = true; } else if (columnValue.GetType().FullName.Equals(typeof(float).FullName, StringComparison.OrdinalIgnoreCase)) { isFloat = true; } else if (columnValue.GetType().FullName.Equals(typeof(byte).FullName, StringComparison.OrdinalIgnoreCase)) { isBlob = true; } } else { isString = true; } if (isString) { if (entityDescriptor != null) { EntityDescriptor.Attribute attribute = entityDescriptor.GetAttributeBasedOnColumnName(columnName); if (attribute != null) { if (attribute.GetType().Equals(IDataTypeHandler.CSHARP_STRING_DATA_TYPE, StringComparison.OrdinalIgnoreCase)) { tuple.Add(columnName, (String)columnValue); } else if (attribute.GetType().Equals(IDataTypeHandler.CSHARP_BOOLEAN_PRIMITIVE_DATA_TYPE, StringComparison.OrdinalIgnoreCase)) { tuple.Add(columnName, columnValue.Equals(Boolean.TrueString.ToString()) ? true : false); } else if (attribute.GetType().Equals(IDataTypeHandler.CSHARP_BOOLEAN_DATA_TYPE, StringComparison.OrdinalIgnoreCase)) { tuple.Add(columnName, columnValue.Equals(Boolean.TrueString.ToString()) ? Boolean.TrueString : Boolean.FalseString); } else if (attribute.GetType().Equals(IDataTypeHandler.JAVASCRIPT_STRING_DATA_TYPE, StringComparison.OrdinalIgnoreCase)) { tuple.Add(columnName, (String)columnValue); } } else { tuple.Add(columnName, columnValue); } } else { tuple.Add(columnName, columnValue); } } else if (isLong) { if (entityDescriptor != null) { EntityDescriptor.Attribute attribute = entityDescriptor.GetAttributeBasedOnColumnName(columnName); if (attribute != null) { if (attribute.GetType().Equals(IDataTypeHandler.JAVASCRIPT_NUMBER_DATA_TYPE, StringComparison.OrdinalIgnoreCase)) { tuple.Add(columnName, columnValue); } } else { tuple.Add(columnName, columnValue); } } else { tuple.Add(columnName, columnValue); } } else if (isFloat) { if (entityDescriptor != null) { EntityDescriptor.Attribute attribute = entityDescriptor.GetAttributeBasedOnColumnName(columnName); if (attribute != null) { if (attribute.GetType().Equals(IDataTypeHandler.JAVASCRIPT_NUMBER_DATA_TYPE, StringComparison.OrdinalIgnoreCase)) { tuple.Add(columnName, columnValue); } } else { tuple.Add(columnName, columnValue); } } else { tuple.Add(columnName, columnValue); } tuple.Add(columnName, columnValue); } else if (isBlob) { tuple.Add(columnName, columnValue); } } tuples.Add((Dictionary <String, Object>)tuple); } return(tuples.GetEnumerator()); #endif }