예제 #1
0
        private string getStoredProcedureName(PocoSqlStoredProcedureMap spMap, object obj, SqlBuilderTask.QueryTypeEnum queryType)
        {
            if (spMap == null)
            {
                return(String.Empty);
            }
            if (String.IsNullOrEmpty(spMap.Name))
            {
                return(String.Format("{0}{1}_{2}", Configuration.StoredProceduresPrefix, obj.GetType().Name, queryType.ToString()));
            }

            return(spMap.Name);
        }
예제 #2
0
        //private SqlBuilder buildSqlStatement(object obj, SqlBuilderTask.QueryTypeEnum queryType, string tableName, bool? fullGraph, bool? pluralizeTableNames, IPocoSqlMapping map)
        private SqlBuilder buildSqlStatement(object obj, SqlBuilderTask task)
        {
            if (task.QueryType == SqlBuilderTask.QueryTypeEnum.SelectScopeIdentity)
            {
                task.SqlResult = "select scope_identity()";
                return(this);
            }
            else if (task.QueryType == SqlBuilderTask.QueryTypeEnum.SelectIdentity)
            {
                task.SqlResult = "select @@identity";
                return(this);
            }

            IPocoSqlMapping map       = null;
            string          tableName = null;

            if (Configuration.Comment)
            {
                task.SqlResult = getComment(obj);
            }

            if (task.Map == null)
            {
                map = getMapping(obj);
            }

            if (map != null)
            {
                //
                // Custom query
                //
                var customQuery = getCustomQuery(task.QueryType, map);
                if (!String.IsNullOrEmpty(customQuery))
                {
                    task.SqlResult += customQuery;
                    if (task.SqlResult.EndsWith(";"))
                    {
                        currentTask.EndWithSemicolon = false;
                    }
                    return(this); // if it's a custom query no other thing needs (the user knew in advanced what he wanted) to be done and the result is returned
                }

                //
                // Stored Procedures mappings
                //
                var spMappings = map.GetStoredProceduresMappings();
                if (spMappings != null)
                {
                    PocoSqlStoredProcedureMap spMap = getStoredProcedureMap(spMappings, task.QueryType);
                    if (spMap != null)
                    {
                        string spName = getStoredProcedureName(spMap, obj, task.QueryType);
                        task.SqlResult += String.Format("{0}{1}{2}",
                                                        spMap.Execution ? "exec " : String.Empty,
                                                        spName,
                                                        spMap.Parameters ?  getQueryFields(task.QueryType, map) : String.Empty);
                        if (!spMap.Execution && !spMap.Parameters)
                        {
                            currentTask.EndWithSemicolon = false;
                        }
                        return(this);
                    }
                }

                if (task.QueryType != SqlBuilderTask.QueryTypeEnum.Select && map.GetIsVirtual())
                {
                    throw new CantUpdateVirtualException();
                }

                if (String.IsNullOrEmpty(task.TableName))
                {
                    tableName = map.GetTableName();
                }
            }

            if (String.IsNullOrEmpty(tableName))
            {
                tableName = obj.GetType().Name;
            }

            if ((map == null || (map != null && String.IsNullOrEmpty(map.GetTableName()))) && Configuration.IsPluralizeTableNames)
            {
                tableName = tableName.Pluralize();
            }

            switch (task.QueryType)
            {
            case SqlBuilderTask.QueryTypeEnum.Select:
                task.SqlResult += String.Format("select {0} from {1}", getQueryFields(SqlBuilderTask.QueryTypeEnum.Select, map), tableName);
                break;

            case SqlBuilderTask.QueryTypeEnum.Insert:
                task.SqlResult += String.Format("insert into {0} {1}", tableName, getQueryFields(SqlBuilderTask.QueryTypeEnum.Insert, map));
                break;

            case SqlBuilderTask.QueryTypeEnum.Update:
                task.SqlResult += String.Format("update {0} set {1}", tableName, getQueryFields(SqlBuilderTask.QueryTypeEnum.Update, map));
                break;

            case SqlBuilderTask.QueryTypeEnum.Delete:
                task.SqlResult += String.Format("delete from {0}", tableName);
                string primaryKey       = getPrimaryKey(_obj);
                string deleteWhereValue = "@" + primaryKey;
                if (Configuration.ValuesInQueies)
                {
                    deleteWhereValue = getPropertyValueAsSql(_obj, primaryKey);
                }
                this.Where(String.Format("{0} = {1}", primaryKey, deleteWhereValue));
                break;

            case SqlBuilderTask.QueryTypeEnum.StoredProcedure:
                var queryFields = String.Empty;
                var execCmd     = String.Empty;
                task.SqlResult += String.Format("exec {0} {1}", tableName, getQueryFields(SqlBuilderTask.QueryTypeEnum.StoredProcedure, map));
                break;
            }

            return(this);
        }