Ejemplo n.º 1
0
 protected void CopyDbOptions(ParameterParser pp)
 {
     Connection      = pp.Connection;
     Transaction     = pp.Transaction;
     Buffered        = pp.Buffered;
     CommandBehavior = pp.CommandBehavior;
 }
Ejemplo n.º 2
0
        public IEnumerable <T> RunStoredProcedure <T>(string spName, params object[] parameters)
        {
            ParameterParser pp = new ParameterParser(spName, IQSqlDirectiveType.StoredProcedure, parameters);

            IDataReader reader = StorageController.RunStoredProcedure(pp.Connection, spName, pp.Parameters, -1, -1, pp.Transaction, pp.CommandBehavior);

            return(reader.MapAll <T>(pp.Buffered));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// When passing type=0, it's never going to try to build a query - only fully formed query SQl will work.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="query"></param>
        /// <param name="parameters"></param>
        protected void ParseQuery(QueryType type, object query, IEnumerable <object> parameters)
        {
            IQuery outputQuery;

            if (query == null)
            {
                throw new IQMissingQueryException();
            }
            else if (query is IQuery)
            {
                ExpectNoParameters(parameters);
                if (type != 0 && ((IQuery)query).QueryType != type)
                {
                    throw new IQException("The query passed was not of type '" + type.ToString() + "' as required for this operation.");
                }
                outputQuery = (IQuery)query;
            }
            else if (type != 0)
            {
                outputQuery = ParseComplexQuery(type, query, parameters);
            }
            else if (query is string)
            {
                string queryString = (string)query;

                ParameterParser pp = new ParameterParser(queryString, parameters);
                CopyDbOptions(pp);

                outputQuery = new SqlQueryRaw(queryString, pp.Parameters);
            }
            else
            {
                throw new IQException("The type of object '" + query.GetType().ToString() + "' passed as a query isn't something I know how to handle.");
            }

            if (outputQuery == null)
            {
                throw new IQMissingQueryException();
            }

            if (outputQuery.QueryType == QueryType.Invalid)
            {
                throw new IQException("Couldn't make a valid query out of the mess you passed me. Check the SQL and the parameters.");
            }

            Query = outputQuery;
        }
Ejemplo n.º 4
0
        public SqlQueryRaw(string sql, IEnumerable<object> parameters)
        {
            IEnumerable<IDataParameter> parms;
            if (parameters is IEnumerable<IDataParameter>)
            {
                parms = (IEnumerable<IDataParameter>)parameters;
                QuerySql = sql;
            }
            else
            {
                ParameterParser pp = new ParameterParser(sql, parameters);
                parms = pp.Parameters;
                QuerySql = pp.Query;
            }

            QueryParameters.AddRange(parms);

            QueryType = ParseQueryType();
        }
Ejemplo n.º 5
0
        public SqlQueryRaw(string sql, IEnumerable <object> parameters)
        {
            IEnumerable <IDataParameter> parms;

            if (parameters is IEnumerable <IDataParameter> )
            {
                parms    = (IEnumerable <IDataParameter>)parameters;
                QuerySql = sql;
            }
            else
            {
                ParameterParser pp = new ParameterParser(sql, parameters);
                parms    = pp.Parameters;
                QuerySql = pp.Query;
            }


            QueryParameters.AddRange(parms);

            QueryType = ParseQueryType();
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
        }