public static double Avg(EntityDescriptor entityDescriptor, String column, String whereClause, IEnumerator<String> groupBys, String having) { Siminov.IsActive(); DatabaseDescriptor databaseDescriptor = GetDatabaseDescriptor(entityDescriptor.GetClassName()); DatabaseBundle databaseBundle = resourceManager.GetDatabaseBundle(databaseDescriptor.GetDatabaseName()); IDatabaseImpl database = databaseBundle.GetDatabase(); IQueryBuilder queryBuilder = databaseBundle.GetQueryBuilder(); if (database == null) { Log.Log.Error(typeof(DatabaseHelper).FullName, "Avg(" + column + ")", "No Database Instance Found For ENTITY-DESCRIPTOR: " + entityDescriptor.GetClassName()); throw new DeploymentException(typeof(DatabaseHelper).FullName, "Avg(" + column + ")", "No Database Instance Found For ENTITY-DESCRIPTOR: " + entityDescriptor.GetClassName()); } IDictionary<String, Object> parameters = new Dictionary<String, Object>(); parameters.Add(IQueryBuilder.FORM_AVG_QUERY_TABLE_NAME_PARAMETER, entityDescriptor.GetTableName()); parameters.Add(IQueryBuilder.FORM_AVG_QUERY_COLUMN_PARAMETER, column); parameters.Add(IQueryBuilder.FORM_AVG_QUERY_WHERE_CLAUSE_PARAMETER, whereClause); parameters.Add(IQueryBuilder.FORM_AVG_QUERY_GROUP_BYS_PARAMETER, groupBys); parameters.Add(IQueryBuilder.FORM_AVG_QUERY_HAVING_PARAMETER, having); String query = queryBuilder.FormAvgQuery(parameters); IEnumerator<IDictionary<String, Object>> datas = database.ExecuteSelectQuery(GetDatabaseDescriptor(entityDescriptor.GetClassName()), entityDescriptor, query); while (datas.MoveNext()) { IDictionary<String, Object> data = datas.Current; ICollection<Object> parse = data.Values; IEnumerator<Object> values = parse.GetEnumerator(); while (values.MoveNext()) { Object value = values.Current; if (value.GetType().FullName.Equals(typeof(String).FullName, StringComparison.OrdinalIgnoreCase)) { return int.Parse((String)value); } } } return 0; }
/// <summary> /// Remove entity descriptor object based on entity descriptor object. /// </summary> /// <param name="entityDescriptor">Entity descriptor object which needs to be removed</param> public void RemoveEntityDescriptor(EntityDescriptor entityDescriptor) { RemoveEntityDescriptorBasedOnClassName(entityDescriptor.GetClassName()); }
/// <summary> /// Add entity descriptor object in respect to entity descriptor path. /// </summary> /// <param name="entityDescriptorPath">Entity Descriptor Path</param> /// <param name="entityDescriptor">Entity Descriptor object</param> public void AddEntityDescriptor(String entityDescriptorPath, EntityDescriptor entityDescriptor) { this.entityDescriptorsBasedOnPath.Add(entityDescriptorPath, entityDescriptor); this.entityDescriptorsBasedOnTableName.Add(entityDescriptor.GetTableName(), entityDescriptor); this.entityDescriptorsBasedOnClassName.Add(entityDescriptor.GetClassName(), entityDescriptor); }
/// <summary> /// Is used to drop a index on a table in database /// <para> /// Create Index On Book table /// <code> /// String indexName = "BOOK_INDEX_BASED_ON_AUTHOR"; /// EntityDescriptor entityDescriptor = new Book().GetEntityDescriptor(); /// /// try { /// Database.DropIndex(entityDescriptor, indexName); /// } catch(DatabaseException databaseException) { /// //Log It. /// } /// </code> /// </para> /// </summary> /// <param name="entityDescriptor">Entity Descriptor object which defines the structure of table</param> /// <param name="indexName">Name of a index needs to be drop</param> /// <exception cref="Siminov.Core.Exception.DatabaseException">If not able to drop index on table</exception> public static void DropIndex(EntityDescriptor entityDescriptor, String indexName) { Siminov.IsActive(); DatabaseDescriptor databaseDescriptor = GetDatabaseDescriptor(entityDescriptor.GetClassName()); DatabaseBundle databaseBundle = resourceManager.GetDatabaseBundle(databaseDescriptor.GetDatabaseName()); IDatabaseImpl database = databaseBundle.GetDatabase(); IQueryBuilder queryBuilder = databaseBundle.GetQueryBuilder(); if (database == null) { Log.Log.Error(typeof(DatabaseHelper).FullName, "DropIndex", "No Database Instance Found For ENTITY-DESCRIPTOR: " + entityDescriptor.GetClassName() + ", " + " INDEX-NAME: " + indexName); throw new DeploymentException(typeof(DatabaseHelper).FullName, "DropIndex", "No Database Instance Found For ENTITY-DESCRIPTOR: " + entityDescriptor.GetClassName() + ", " + " INDEX-NAME: " + indexName); } IDictionary<String, Object> parameters = new Dictionary<String, Object>(); parameters.Add(IQueryBuilder.FORM_DROP_INDEX_QUERY_TABLE_NAME_PARAMETER, entityDescriptor.GetTableName()); parameters.Add(IQueryBuilder.FORM_DROP_INDEX_QUERY_INDEX_NAME_PARAMETER, indexName); String query = queryBuilder.FormDropIndexQuery(parameters); database.ExecuteQuery(GetDatabaseDescriptor(entityDescriptor.GetClassName()), entityDescriptor, query); IDatabaseEvents databaseEventHandler = resourceManager.GetDatabaseEventHandler(); if (databaseEventHandler != null) { databaseEventHandler.OnIndexDropped(GetDatabaseDescriptor(entityDescriptor.GetClassName()), entityDescriptor, entityDescriptor.GetIndex(indexName)); } }
/// <summary> /// Is used to create a new index on a table in database /// <para> /// Create Index On Book table /// <code> /// String indexName = "BOOK_INDEX_BASED_ON_AUTHOR"; /// boolean isUnique = true; /// /// Collection<String> columnNames = new ArrayList<String>(); /// columnNames.Add("LINK"); /// /// try { /// new Book().CreateIndex(indexName, columnNames.GetEnumerator(), isUnique); /// } catch(DatabaseException databaseException) { /// //Log It. /// } /// </code> /// </para> /// </summary> /// <param name="entityDescriptor">Entity Descriptor object which defines the structure of table</param> /// <param name="indexName">Name of index</param> /// <param name="columnNames">Iterator over column names</param> /// <param name="isUnique">true/false whether index needs to be unique or not. (A unique index guarantees that the index key contains no duplicate values and therefore every row in the table is in some way unique</param> /// <exception cref="Siminov.Core.Exception.DatabaseException">If not able to create index on table</exception> public static void CreateIndex(EntityDescriptor entityDescriptor, String indexName, IEnumerator<String> columnNames, bool isUnique) { DatabaseDescriptor databaseDescriptor = GetDatabaseDescriptor(entityDescriptor.GetClassName()); DatabaseBundle databaseBundle = resourceManager.GetDatabaseBundle(databaseDescriptor.GetDatabaseName()); IDatabaseImpl database = databaseBundle.GetDatabase(); IQueryBuilder queryBuilder = databaseBundle.GetQueryBuilder(); if (database == null) { Log.Log.Error(typeof(DatabaseHelper).FullName, "CreateIndex", "No Database Instance Found For ENTITY-DESCRIPTOR: " + entityDescriptor.GetClassName()); throw new DeploymentException(typeof(DatabaseHelper).FullName, "CreateIndex", "No Database Instance Found For ENTITY-DESCRIPTOR: " + entityDescriptor.GetClassName()); } ICollection<String> columnNamesCollection = new List<String>(); while (columnNames.MoveNext()) { columnNamesCollection.Add(columnNames.Current); } IDictionary<String, Object> parameters = new Dictionary<String, Object>(); parameters.Add(IQueryBuilder.FORM_CREATE_INDEX_QUERY_INDEX_NAME_PARAMETER, indexName); parameters.Add(IQueryBuilder.FORM_CREATE_INDEX_QUERY_TABLE_NAME_PARAMETER, entityDescriptor.GetTableName()); parameters.Add(IQueryBuilder.FORM_CREATE_INDEX_QUERY_COLUMN_NAMES_PARAMETER, columnNamesCollection.GetEnumerator()); parameters.Add(IQueryBuilder.FORM_CREATE_INDEX_QUERY_IS_UNIQUE_PARAMETER, isUnique); String query = queryBuilder.FormCreateIndexQuery(parameters); database.ExecuteQuery(GetDatabaseDescriptor(entityDescriptor.GetClassName()), entityDescriptor, query); IDatabaseEvents databaseEventHandler = resourceManager.GetDatabaseEventHandler(); if (databaseEventHandler != null) { EntityDescriptor.Index index = new EntityDescriptor.Index(); index.SetName(indexName); index.SetUnique(isUnique); IEnumerator<String> columnNamesIterator = columnNamesCollection.GetEnumerator(); while (columnNamesIterator.MoveNext()) { index.AddColumn(columnNamesIterator.Current); } databaseEventHandler.OnIndexCreated(GetDatabaseDescriptor(entityDescriptor.GetClassName()), entityDescriptor, index); } }
/// <summary> /// Is used to create a new table in an database /// Manually creating table structure using Entity Descriptor mapped class. /// Example: /// <para> /// <code> /// Book book = new Book(); /// /// try { /// Database.CreateTables(book.GetEntityDescriptor()); /// } catch(DatabaseException databaseException) { /// //Log It. /// } /// </code> /// </para> /// </summary> /// <param name="entityDescriptor">Entity Descriptor object which defines the structure of table</param> /// <exception cref="Siminov.Core.Exception.DatabaseException">If not able to create table in SQLite</exception> public static void CreateTable(EntityDescriptor entityDescriptor) { /* * 1. Get IDatabase with respect to current entity descriptor class name. * 2. Get Table Name, and all columns. * 3. Get all attributes and properties from entity descriptor. * LIKE(COLUMN NAMES, COLUMN TYPES, PRIMARY KEYS, UNIQUE's, NOT NULL, DEFAULT VALUES, CHECKS, ). * * 4. If current version of OS is lower then 8 (FROYO) then we have to create triggers for all foreign keys defined, * because Android OS Version lower then 8 (FROYO) does not support FOREIGN KEY SYNTAX. * Else get foreign keys. * * 5. Call QueryBuilder.formCreateTableQuery, get query to create table. * After forming create table query call executeQuery method to create table in database. * * 6. Create all triggers. * 7. Create Index for table if its defined. * Get all attributes and properties of index. * LIKE(INDEX NAME, IS UNIQUE INDEX, COLUMN NAMES). * After forming index query call executeQuery method to create index. * */ DatabaseDescriptor databaseDescriptor = GetDatabaseDescriptor(entityDescriptor.GetClassName()); DatabaseBundle databaseBundle = resourceManager.GetDatabaseBundle(databaseDescriptor.GetDatabaseName()); IDatabaseImpl database = databaseBundle.GetDatabase(); IQueryBuilder queryBuilder = databaseBundle.GetQueryBuilder(); IDataTypeHandler dataTypeHandler = databaseBundle.GetDataTypeHandler(); if (database == null) { Log.Log.Error(typeof(DatabaseHelper).FullName, "CreateTable", "No Database Instance Found For ENITTY-DESCRIPTOR: " + entityDescriptor.GetClassName()); throw new DeploymentException(typeof(DatabaseHelper).FullName, "CreateTable", "No Database Instance Found For ENTITY-DESCRIPTOR: " + entityDescriptor.GetClassName()); } String tableName = entityDescriptor.GetTableName(); /* * Get all attributes and properties from entity descriptor. * LIKE(COLUMN NAMES, COLUMN TYPES, DEFAULT VALUES, CHECKS, NOT NULL, PRIMARY KEYS, UNIQUE's ). */ ICollection<String> columnNames = new LinkedList<String>(); ICollection<String> columnTypes = new LinkedList<String>(); ICollection<String> defaultValues = new LinkedList<String>(); ICollection<String> checks = new LinkedList<String>(); ICollection<Boolean> isNotNull = new LinkedList<Boolean>(); ICollection<String> primaryKeys = new LinkedList<String>(); ICollection<String> uniqueKeys = new LinkedList<String>(); IEnumerator<EntityDescriptor.Attribute> attributes = entityDescriptor.GetAttributes(); while (attributes.MoveNext()) { EntityDescriptor.Attribute attribute = attributes.Current; columnNames.Add(attribute.GetColumnName()); columnTypes.Add(dataTypeHandler.Convert(attribute.GetType())); isNotNull.Add(attribute.IsNotNull()); defaultValues.Add(attribute.GetDefaultValue()); checks.Add(attribute.GetCheck()); bool isPrimary = attribute.IsPrimaryKey(); bool isUnique = attribute.IsUnique(); if (isPrimary) { primaryKeys.Add(attribute.GetColumnName()); } if (isUnique) { uniqueKeys.Add(attribute.GetColumnName()); } } /* * Add ONE-TO-MANY And MANY-TO-MANY Relationship Columns. */ IEnumerator<EntityDescriptor.Relationship> oneToManyRelationships = entityDescriptor.GetManyToOneRelationships(); IEnumerator<EntityDescriptor.Relationship> manyToManyRelationships = entityDescriptor.GetManyToManyRelationships(); while (oneToManyRelationships.MoveNext()) { EntityDescriptor.Relationship oneToManyRelationship = oneToManyRelationships.Current; EntityDescriptor referedEntityDescriptor = oneToManyRelationship.GetReferedEntityDescriptor(); if (referedEntityDescriptor == null) { referedEntityDescriptor = GetEntityDescriptor(oneToManyRelationship.GetReferTo()); oneToManyRelationship.SetReferedEntityDescriptor(referedEntityDescriptor); } ICollection<EntityDescriptor.Attribute> foreignAttributes = GetForeignKeys(referedEntityDescriptor); IEnumerator<EntityDescriptor.Attribute> foreignAttributesIterator = foreignAttributes.GetEnumerator(); while (foreignAttributesIterator.MoveNext()) { EntityDescriptor.Attribute foreignAttribute = foreignAttributesIterator.Current; columnNames.Add(foreignAttribute.GetColumnName()); columnTypes.Add(dataTypeHandler.Convert(foreignAttribute.GetType())); isNotNull.Add(foreignAttribute.IsNotNull()); defaultValues.Add(foreignAttribute.GetDefaultValue()); checks.Add(foreignAttribute.GetCheck()); bool isPrimary = foreignAttribute.IsPrimaryKey(); if (isPrimary) { primaryKeys.Add(foreignAttribute.GetColumnName()); } bool isUnique = foreignAttribute.IsUnique(); if (isUnique) { uniqueKeys.Add(foreignAttribute.GetColumnName()); } } } while (manyToManyRelationships.MoveNext()) { EntityDescriptor.Relationship manyToManyRelationship = manyToManyRelationships.Current; EntityDescriptor parentEntityDescriptor = manyToManyRelationship.GetReferedEntityDescriptor(); IEnumerator<EntityDescriptor.Attribute> parentAttributes = parentEntityDescriptor.GetAttributes(); while (parentAttributes.MoveNext()) { EntityDescriptor.Attribute attribute = parentAttributes.Current; bool isPrimary = attribute.IsPrimaryKey(); if (isPrimary) { columnNames.Add(attribute.GetColumnName()); columnTypes.Add(dataTypeHandler.Convert(attribute.GetType())); isNotNull.Add(attribute.IsNotNull()); defaultValues.Add(attribute.GetDefaultValue()); checks.Add(attribute.GetCheck()); if (isPrimary) { primaryKeys.Add(attribute.GetColumnName()); } bool isUnique = attribute.IsUnique(); if (isUnique) { uniqueKeys.Add(attribute.GetColumnName()); } } } } /* * If current version of OS is lower then 8 (FROYO) then we have to create triggers for all foreign keys defined, * because Android OS Version lower then 8 (FROYO) does not support FOREIGN KEY SYNTAX. * Else get foreign keys. */ String foreignKeys = ""; IDictionary<String, Object> parameters = new Dictionary<String, Object>(); parameters.Add(IQueryBuilder.FORM_FOREIGN_KEYS_DATABASE_DESCRIPTOR_PARAMETER, entityDescriptor); foreignKeys = queryBuilder.FormForeignKeyQuery(parameters); /* * Call QueryBuilder.formCreateTableQuery, get query to create table. * After forming create table query call executeQuery method to create table in database. */ parameters = new Dictionary<String, Object>(); parameters.Add(IQueryBuilder.FORM_CREATE_TABLE_QUERY_TABLE_NAME_PARAMETER, tableName); parameters.Add(IQueryBuilder.FORM_CREATE_TABLE_QUERY_COLUMN_NAMES_PARAMETER, columnNames.GetEnumerator()); parameters.Add(IQueryBuilder.FORM_CREATE_TABLE_QUERY_COLUMN_TYPES_PARAMETER, columnTypes.GetEnumerator()); parameters.Add(IQueryBuilder.FORM_CREATE_TABLE_QUERY_DEFAULT_VALUES_PARAMETER, defaultValues.GetEnumerator()); parameters.Add(IQueryBuilder.FORM_CREATE_TABLE_QUERY_CHECKS_PARAMETER, checks.GetEnumerator()); parameters.Add(IQueryBuilder.FORM_CREATE_TABLE_QUERY_PRIMARY_KEYS_PARAMETER, primaryKeys.GetEnumerator()); parameters.Add(IQueryBuilder.FORM_CREATE_TABLE_QUERY_NOT_NULLS_PARAMETER, isNotNull.GetEnumerator()); parameters.Add(IQueryBuilder.FORM_CREATE_TABLE_QUERY_UNIQUE_COLUMNS_PARAMETER, uniqueKeys.GetEnumerator()); parameters.Add(IQueryBuilder.FORM_CREATE_TABLE_QUERY_FOREIGN_KEYS_PARAMETER, foreignKeys); String query = queryBuilder.FormCreateTableQuery(parameters); database.ExecuteQuery(GetDatabaseDescriptor(entityDescriptor.GetClassName()), entityDescriptor, query); /* * Create Index for table if its defined. */ IEnumerator<EntityDescriptor.Index> indexes = entityDescriptor.GetIndexes(); while (indexes.MoveNext()) { /* * Get all attributes and properties of index. * LIKE(INDEX NAME, IS UNIQUE INDEX, COLUMN NAMES). * * After forming index query call executeQuery method to create index. */ CreateIndex(entityDescriptor, indexes.Current); } IDatabaseEvents databaseEventHandler = resourceManager.GetDatabaseEventHandler(); if (databaseEventHandler != null) { databaseEventHandler.OnTableCreated(GetDatabaseDescriptor(entityDescriptor.GetClassName()), entityDescriptor); } }
/// <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(); }
/// <summary> /// Upgrade Table /// </summary> /// <param name="entityDescriptor">Entity Descriptor object related to table</param> /// <exception cref="Siminov.Core.Exception.DatabaseException">If any exception thrown while upgrading table</exception> public static void UpgradeTable(EntityDescriptor entityDescriptor) { DatabaseDescriptor databaseDescriptor = GetDatabaseDescriptor(entityDescriptor.GetClassName()); DatabaseBundle databaseBundle = resourceManager.GetDatabaseBundle(databaseDescriptor.GetDatabaseName()); IDatabaseImpl database = databaseBundle.GetDatabase(); IQueryBuilder queryBuilder = databaseBundle.GetQueryBuilder(); if (database == null) { Log.Log.Error(typeof(DatabaseHelper).FullName, "UpgradeTable", "No Database Instance Found For, TABLE-NAME: " + entityDescriptor.GetTableName()); throw new DatabaseException(typeof(DatabaseHelper).FullName, "UpgradeTable", "No Database Instance Found For, TABLE-NAME: " + entityDescriptor.GetTableName()); } IDictionary<String, Object> parameters = new Dictionary<String, Object>(); parameters.Add(IQueryBuilder.FORM_TABLE_INFO_QUERY_TABLE_NAME_PARAMETER, entityDescriptor.GetTableName()); String tableInfoQuery = queryBuilder.FormTableInfoQuery(parameters); Log.Log.Debug(typeof(DatabaseHelper).FullName, "UpgradeTable", "Table Info Query: " + tableInfoQuery); ICollection<EntityDescriptor.Attribute> newAttributes = new List<EntityDescriptor.Attribute>(); ICollection<String> oldAttributes = new List<String>(); IEnumerator<EntityDescriptor.Attribute> attributes = entityDescriptor.GetAttributes(); IEnumerator<IDictionary<String, Object>> datas = database.ExecuteSelectQuery(null, null, tableInfoQuery); while (datas.MoveNext()) { IDictionary<String, Object> data = datas.Current; IEnumerator<String> keys = data.Keys.GetEnumerator(); while (keys.MoveNext()) { String key = keys.Current; if (key.Equals(Constants.FORM_TABLE_INFO_QUERY_NAME, StringComparison.OrdinalIgnoreCase)) { oldAttributes.Add((String)data[key]); } } } while (attributes.MoveNext()) { EntityDescriptor.Attribute attribute = attributes.Current; bool contain = false; foreach (String oldColumn in oldAttributes) { if (oldColumn.Equals(attribute.GetColumnName())) { contain = true; break; } } if (!contain) { newAttributes.Add(attribute); } } foreach (EntityDescriptor.Attribute column in newAttributes) { String columnName = column.GetColumnName(); parameters = new Dictionary<String, Object>(); parameters.Add(IQueryBuilder.FORM_ALTER_ADD_COLUMN_QUERY_TABLE_NAME_PARAMETER, entityDescriptor.GetTableName()); parameters.Add(IQueryBuilder.FORM_ALTER_ADD_COLUMN_QUERY_COLUMN_NAME_PARAMETER, columnName); String addColumnQuery = queryBuilder.FormAlterAddColumnQuery(parameters); Log.Log.Debug(typeof(DatabaseHelper).FullName, "UpgradeTable", "Add New Column Query: " + addColumnQuery); database.ExecuteQuery(null, null, addColumnQuery); } }
public static String GroupConcat(EntityDescriptor entityDescriptor, String column, String delimiter, String whereClause, IEnumerator<String> groupBys, String having) { Siminov.IsActive(); DatabaseDescriptor databaseDescriptor = GetDatabaseDescriptor(entityDescriptor.GetClassName()); DatabaseBundle databaseBundle = resourceManager.GetDatabaseBundle(databaseDescriptor.GetDatabaseName()); IDatabaseImpl database = databaseBundle.GetDatabase(); IQueryBuilder queryBuilder = databaseBundle.GetQueryBuilder(); if (database == null) { Log.Log.Error(typeof(DatabaseHelper).FullName, "GroupConcat", "No Database Instance Found For ENTITY-DESCRIPTOR: " + entityDescriptor.GetClassName()); throw new DeploymentException(typeof(DatabaseHelper).FullName, "GroupConcat", "No Database Instance Found For ENTITY-DESCRIPTOR: " + entityDescriptor.GetClassName()); } IDictionary<String, Object> parameters = new Dictionary<String, Object>(); parameters.Add(IQueryBuilder.FORM_GROUP_CONCAT_QUERY_TABLE_NAME_PARAMETER, entityDescriptor.GetTableName()); parameters.Add(IQueryBuilder.FORM_GROUP_CONCAT_QUERY_COLUMN_PARAMETER, column); parameters.Add(IQueryBuilder.FORM_GROUP_CONCAT_QUERY_WHERE_CLAUSE_PARAMETER, delimiter); parameters.Add(IQueryBuilder.FORM_GROUP_CONCAT_QUERY_GROUP_BYS_PARAMETER, whereClause); parameters.Add(IQueryBuilder.FORM_GROUP_CONCAT_QUERY_HAVING_PARAMETER, groupBys); parameters.Add(IQueryBuilder.FORM_GROUP_CONCAT_QUERY_DELIMITER_PARAMETER, having); String query = queryBuilder.FormGroupConcatQuery(parameters); IEnumerator<IDictionary<String, Object>> datas = database.ExecuteSelectQuery(GetDatabaseDescriptor(entityDescriptor.GetClassName()), entityDescriptor, query); while (datas.MoveNext()) { IDictionary<String, Object> data = datas.Current; ICollection<Object> parse = data.Values; IEnumerator<Object> values = parse.GetEnumerator(); while (values.MoveNext()) { return ((String)values.Current); } } return null; }
static Object[] LazyFetch(EntityDescriptor entityDescriptor, bool distinct, String whereClause, IEnumerator<String> columnNames, IEnumerator<String> groupBy, String having, IEnumerator<String> orderBy, String whichOrderBy, String limit) { /* * 1. Get entity descriptor object for mapped invoked class object. * 2. Traverse group by's and form a single string. * 3. Traverse order by'z and form a single string. * 4. Pass all parameters to executeFetchQuery and get cursor. * 5. Pass got cursor and mapped entity descriptor object for invoked class object, and pass it parseCursor method which will return all tuples in form of actual objects. * 6. Check for relationship's if any, IF EXISTS: process it, ELSE: return all objects. */ Siminov.IsActive(); /* * 1. Get entity descriptor object for mapped invoked class object. */ DatabaseDescriptor databaseDescriptor = GetDatabaseDescriptor(entityDescriptor.GetClassName()); DatabaseBundle databaseBundle = resourceManager.GetDatabaseBundle(databaseDescriptor.GetDatabaseName()); IDatabaseImpl database = databaseBundle.GetDatabase(); IQueryBuilder queryBuilder = databaseBundle.GetQueryBuilder(); if (database == null) { Log.Log.Error(typeof(DatabaseHelper).FullName, "LazyFetch", "No Database Instance Found For ENTITY-DESCRIPTOR: " + entityDescriptor.GetClassName()); throw new DeploymentException(typeof(DatabaseHelper).FullName, "LazyFetch", "No Database Instance Found For ENTITY-DESCRIPTOR: " + entityDescriptor.GetClassName()); } /* * 4. Pass all parameters to executeFetchQuery and get cursor. */ IDictionary<String, Object> parameters = new Dictionary<String, Object>(); parameters.Add(IQueryBuilder.FORM_SELECT_QUERY_TABLE_NAME_PARAMETER, entityDescriptor.GetTableName()); parameters.Add(IQueryBuilder.FORM_SELECT_QUERY_DISTINCT_PARAMETER, distinct); parameters.Add(IQueryBuilder.FORM_SELECT_QUERY_WHERE_CLAUSE_PARAMETER, whereClause); parameters.Add(IQueryBuilder.FORM_SELECT_QUERY_COLUMN_NAMES_PARAMETER, columnNames); parameters.Add(IQueryBuilder.FORM_SELECT_QUERY_GROUP_BYS_PARAMETER, groupBy); parameters.Add(IQueryBuilder.FORM_SELECT_QUERY_HAVING_PARAMETER, having); parameters.Add(IQueryBuilder.FORM_SELECT_QUERY_ORDER_BYS_PARAMETER, orderBy); parameters.Add(IQueryBuilder.FORM_SELECT_QUERY_WHICH_ORDER_BY_PARAMETER, whichOrderBy); parameters.Add(IQueryBuilder.FORM_SELECT_QUERY_LIMIT_PARAMETER, limit); String query = queryBuilder.FormSelectQuery(parameters); IEnumerator<Object> tuples = ParseAndInflateData(entityDescriptor, database.ExecuteSelectQuery(GetDatabaseDescriptor(entityDescriptor.GetClassName()), entityDescriptor, query)); /* * 5. Pass got cursor and mapped entity descriptor object for invoked class object, and pass it parseCursor method which will return all tuples in form of actual objects. */ ICollection<Object> tuplesCollection = new LinkedList<Object>(); while (tuples.MoveNext()) { Object tuple = tuples.Current; tuplesCollection.Add(tuple); } Type classObject = null; try { classObject = ClassUtils.CreateClass(entityDescriptor.GetClassName()); } catch (System.Exception exception) { Log.Log.Error(typeof(DatabaseHelper).FullName, "LazyFetch", "Exception caught while making class object for return type, ENTITY-DESCRIPTOR: " + entityDescriptor.GetClassName() + ", " + exception.Message); throw new DatabaseException(typeof(DatabaseHelper).FullName, "LazyFetch", "Exception caught while making class object for return type, ENTITY-DESCRIPTOR: " + entityDescriptor.GetClassName() + ", " + exception.Message); } Object returnType = Array.CreateInstance(classObject, tuplesCollection.Count); IEnumerator<Object> tuplesIterator = tuplesCollection.GetEnumerator(); int index = 0; while (tuplesIterator.MoveNext()) { ((Object[])returnType)[index++] = tuplesIterator.Current; } Object[] returnTypes = (Object[])returnType; return returnTypes; }
/// <summary> /// Remove entity descriptor object based on table name. /// </summary> /// <param name="tableName">Name of table</param> public void RemoveEntityDescriptorBasedOnTableName(String tableName) { EntityDescriptor entityDescriptor = this.entityDescriptorsBasedOnTableName[tableName]; RemoveEntityDescriptorBasedOnClassName(entityDescriptor.GetClassName()); }