Example #1
0
        protected IQuery ParseComplexQuery(QueryType type, object query, IEnumerable <object> parms)
        {
            IQuery outputQuery = null;

            // We always want to parse the parameters. But if the thing passed to us as "query" is not a string, then
            // just assume that all the parms are option type parameters and don't pass a query to ParameterParser
            string querySource = query is string?
                                 (string)query:
                                 "";

            ParameterParser pp = new ParameterParser(querySource, parms);

            CopyDbOptions(pp);


            if (Utils.IsNumericType(query))
            {
                // It's a single numeric value - assume it's a primary key

                ExpectNoParameters(pp.Parameters);

                var classInfo = IQ.GetClassInfo <T>();

                ISqlQuery queryPK = classInfo.Query(type);
                queryPK.AddWhereParam(classInfo.PrimaryKey.Name, query);
                outputQuery = queryPK;
            }
            else if (query is string)
            {
                bool isMappable = Utils.IsMappableClass <T>();

                // First check if its a single named field

                if (isMappable)
                {
                    var classInfo = IQ.GetClassInfo <T>();

                    // Try to create a valid raw query.. if it's not valid, assume it's a where
                    if (pp.QueryType == QueryType.Invalid)
                    {
                        ISqlQuery queryPK = classInfo.Query(type);

                        queryPK.AddWhere(pp.Query);
                        queryPK.AddParameter(pp.Parameters);
                        outputQuery = queryPK;
                    }
                    else
                    {
                        outputQuery = pp.GetIQuery();
                    }
                }
                else
                {
                    // it's mapped to a primitive type -
                    outputQuery = pp.GetIQuery();
                }
            }
            if (outputQuery.QueryType != type)
            {
                throw new IQException("Wrong type of query passed to method: was " + outputQuery.ToString() + ", expected " + type.ToString());
            }

            return(outputQuery);
        }
Example #2
0
        /// <summary>
        /// options may include: an IDbConnection, an IDbTransaction, CommandBehavior. Save queries should not
        /// include any other parameters
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public bool Save(object obj, params object[] options)
        {
            ParameterParser pp = new ParameterParser("", options);

            if (pp.Parameters.Count > 0)
            {
                throw new IQException("The only allowed parameters for a Save are IDbConnection, IDbTransaction, and CommandBehavior");
            }

            IDBObjectData dbData = IQ.DBData(obj);
            DBClassInfo   info   = dbData.ClassInfo;

            IQEventType eventType    = IQEventType.Save;
            var         eventHandler = dbData.IQEventHandlerFunc;

            if (eventHandler != null)
            {
                eventHandler(eventType | IQEventType.Before, dbData);
            }


            QueryType queryType = dbData.IsNew() ? QueryType.Insert : QueryType.Update;

            ISqlQuery query = IQ.CreateQuery(queryType);

            query.AddFieldMap(info.FieldNameMap);
            query.TableName = dbData.TableName;

            bool   isDirty = false;
            bool   isNew   = dbData.IsNew();
            string pk      = dbData.ClassInfo.PrimaryKey.Name;

            foreach (var item in dbData.ClassInfo.FieldInfo)
            {
                string name = item.Name;
                if (!item.IsPrimaryKey && !item.IsReadOnly &&
                    (isNew || dbData.IsDirty(name)))
                {
                    query.AddUpdateData(name, info[name].GetValue(obj));
                    isDirty = true;
                }
            }

            bool success = false;



            if (isDirty)
            {
                if (queryType == QueryType.Insert)
                {
                    eventType |= IQEventType.Insert;

                    int newPK = StorageController.RunQueryInsert(pp.Connection, query.GetQuery(), query.Parameters,
                                                                 transaction: pp.Transaction);
                    if (newPK <= 0)
                    {
                        throw new Exception("The record could not be inserted.");
                    }
                    dbData.ClassInfo[pk].SetValue(obj, newPK);
                    success = true;
                }
                else
                {
                    eventType |= IQEventType.Update;

                    query.AddWhereParam(dbData.ClassInfo.PrimaryKey.Name, dbData.ClassInfo.PrimaryKey.GetValue(obj));
                    success = StorageController.RunQueryScalar(pp.Connection, query.GetQuery(), query.Parameters,
                                                               transaction: pp.Transaction) > 0;
                }
            }
            else
            {
                success = false;
            }


            if (eventHandler != null)
            {
                eventHandler(eventType | IQEventType.After, dbData);
            }

            if (success)
            {
                dbData.Clean();
            }

            if (pp.CommandBehavior == CommandBehavior.CloseConnection)
            {
                pp.Connection.Close();
            }
            return(success);
        }