Beispiel #1
0
        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

        }
Beispiel #2
0
        /// <summary>
        /// Iterates the provided cursor, and returns tuples in form of actual objects.
        /// </summary>
        /// <param name="entityDescriptor">Entity Descriptor Object</param>
        /// <param name="values">Values</param>
        /// <returns></returns>
        private static IEnumerator<Object> ParseAndInflateData(EntityDescriptor entityDescriptor, IEnumerator<IDictionary<String, Object>> values)
        {
            Siminov.IsActive();

            ICollection<Object> tuples = new LinkedList<Object>();
            while (values.MoveNext())
            {

                IDictionary<String, Object> value = values.Current;

                ICollection<String> columnNames = value.Keys;
                IEnumerator<String> columnNamesIterate = columnNames.GetEnumerator();

                IDictionary<String, Object> data = new Dictionary<String, Object>();
                while (columnNamesIterate.MoveNext())
                {
                    String columnName = columnNamesIterate.Current;

                    if (entityDescriptor.ContainsAttributeBasedOnColumnName(columnName))
                    {
                        data.Add(entityDescriptor.GetAttributeBasedOnColumnName(columnName).GetSetterMethodName(), value[columnName]);
                    }
                }

                Object model = null;

                try
                {
                    model = ClassUtils.CreateAndInflateObject(entityDescriptor.GetClassName(), data);
                }
                catch (SiminovException siminovException)
                {
                    Log.Log.Error(typeof(DatabaseHelper).FullName, "ParseAndInflateData", "SiminovException caught while create and inflate object through reflection, CLASS-NAME: " + entityDescriptor.GetClassName() + ", " + siminovException.GetMessage());
                    throw new DatabaseException(typeof(DatabaseHelper).FullName, "ParseAndInflateData", siminovException.GetMessage());
                }

                tuples.Add(model);
            }

            return tuples.GetEnumerator();
        }