Esempio n. 1
0
//Temp method
func GetPostgresFunctions() error {
	funcName := util.GetCallerName()
	//log.Printf("Calling %s (%d, %d)", funcName, tagId, revisionId)

	db, err := db.GetConnection()
	if err != nil {
		log.Printf("%s Connection err %#v", funcName, err)
		return err
	}
	log.Printf("%s Connected to Postgres", funcName)
	defer db.Close()
	stmt, err := db.Prepare(
		`SELECT  proname, proargnames, pronargs,prosrc
		FROM    pg_catalog.pg_namespace n
		JOIN    pg_catalog.pg_proc p
		ON      pronamespace = n.oid
		WHERE   nspname = 'public'`)

	if err != nil {
		log.Printf("Prepare err %#v", err)
		//stmt.Close()
		//db.Close()
		return err
	}

	result, err := stmt.Query()

	if err != nil {
		log.Printf("Execution problem: %#v", result)
		//result.Close()
		//stmt.Close()
		//db.Close()
		return err
	}
	for result.Next() {
		var name, args, src string
		var numargs int

		if err := result.Scan(&name, &args, &numargs, &src); err != nil {
			log.Printf("Results Error %v", err)
			//result.Close()
			//stmt.Close()
			//db.Close()
			return err
		}
		//		log.Printf("Processing return values for entity relationship ids %d, %d", tagId, revisionId)

		log.Printf("%s Function Name - %s, %s, %d, %s ", funcName, name, args, numargs, src)

		stmt2, err := db.Prepare(
			`SELECT pg_get_function_identity_arguments('` + name + `'::regproc)`)
		if err != nil {
			log.Printf("Prepare err %#v", err)
			//stmt.Close()
			//db.Close()
			return err
		}
		result2, err := stmt2.Query()
		if err != nil {
			log.Printf("Execution problem: %#v", result)
			//result.Close()
			//stmt.Close()
			//db.Close()
			return err
		}
		for result2.Next() {
			var argnameNtype string

			if err := result2.Scan(&argnameNtype); err != nil {
				log.Printf("Result2 Error %v", err)
				//result2.Close()
				//stmt2.Close()
				//db.Close()
				return err
			}
			log.Printf("%s  Function Args - %s %s", funcName, name, argnameNtype)
		}

	}

	//result.Close()
	//stmt.Close()
	//db.Close()
	return nil
}
Esempio n. 2
0
func (tm TagRevisionModel) RetrieveRelationship(revisionId int64, tagId int64) (relationship *dto.EntityRelationship, retCode int, err error) {

	funcName := util.GetCallerName()
	log.Printf("Calling %s (%d, %d)", funcName, tagId, revisionId)

	db, err := db.GetConnection()
	defer db.Close()
	if err != nil {
		log.Printf("%s Connection err %#v", funcName, err)
		return nil, -1, err
	}
	log.Printf("%s Connected to Postgres", funcName)
	defer db.Close()
	stmt, err := db.Prepare(`SELECT * FROM tag_revisiondata WHERE revision_uid =$1 AND tag_uid = $2`)

	if err != nil {
		log.Printf("Prepare err %#v", err)
		//stmt.Close()
		//db.Close()
		return nil, -1, err
	}
	log.Printf("%s Executing ", funcName)
	result, err := stmt.Query(
		revisionId,
		tagId)

	if err != nil {
		log.Printf("%s Execution problem: %#v", funcName, result)
		//result.Close()
		//stmt.Close()
		//db.Close()
		return nil, -1, err
	}
	for result.Next() {
		var status int
		var created, modified time.Time
		if err := result.Scan(&tagId, &revisionId, &status, &created, &modified); err != nil {
			log.Printf("%s Results Error %#v", funcName, err)
			//result.Close()
			//stmt.Close()
			//db.Close()
			return nil, -1, err
		}
		log.Printf("%s Processing return values for entity relationship ids %d, %d", funcName, tagId, revisionId)
		newTRRelationship := &dto.EntityRelationship{
			RelId2:   tagId,
			RelId1:   revisionId,
			RelName1: "dto.Revision",
			RelName2: "dto.Tag",
			RelType2: reflect.TypeOf(dto.Tag{}),
			RelType1: reflect.TypeOf(dto.Revision{}),
			Status:   dto.SetStatus(dto.StatusType(status)),
			Created:  &dto.JsonTime{created, time.RFC3339},
			Modified: &dto.JsonTime{created, time.RFC3339},
		}
		//result.Close()
		//stmt.Close()
		//db.Close()
		log.Printf("%s excution result %v", funcName, newTRRelationship)
		return newTRRelationship, 0, nil
	}

	//result.Close()
	//stmt.Close()
	//db.Close()
	return nil, 1, nil
}
Esempio n. 3
0
func GetPgFuncInfo(name string) (pgfunc *PgFunc, reterr error) {
	funcName := util.GetCallerName()
	// Convert name to all lower case for proper lookup
	name = strings.ToLower(strings.Replace(name, " ", "", -1))
	log.Printf("%s Called with %s", funcName, name)

	conn, err := db.GetConnection()
	if err != nil {
		log.Printf("%s Connection err %#v", funcName, err)
		return nil, err
	}
	log.Printf("%s Connected to Postgres", funcName)
	defer conn.Close()
	stmt, err := conn.Prepare(
		`SELECT  proname, proargnames, pronargs,prosrc,format_type(prorettype, NULL),pg_get_function_identity_arguments('` + name + `'::regproc)
			FROM    pg_catalog.pg_namespace n
			JOIN    pg_catalog.pg_proc p
			ON      pronamespace = n.oid
			WHERE   nspname = 'public' AND p.proname = '` + name + `';`)

	if err != nil {
		log.Printf("Prepare err %#v", err)
		//stmt.Close()
		//conn.Close()
		return nil, err
	}

	result, err := stmt.Query()

	if err != nil {
		log.Printf("%s Execution problem: %#v", funcName, result)
		//result.Close()
		//stmt.Close()
		//conn.Close()
		return nil, err
	}
	for result.Next() {
		var name, args, src, rettype, argtypes string
		var numargs int

		if err := result.Scan(&name, &args, &numargs, &src, &rettype, &argtypes); err != nil {
			log.Printf("%s Results Error %v", funcName, err)
			//result.Close()
			//stmt.Close()
			//conn.Close()
			return nil, err
		}

		//log.Printf("%s  \r\n\tPg Function Name - %s,  \r\n\tNum Args - %d, \r\n\tArgs - %s, \r\n\tArgName,Types - %s, \r\n\tRetType - %s, \r\n\tSrc - \t%s,",
		//	funcName, name, numargs, args, argtypes, rettype, src)
		pgfunc := PgFunc{
			Name:          name,
			NumArgs:       numargs,
			ArgMap:        parsePgFuncArgTypes(numargs, argtypes),
			ReturnTypeStr: rettype,
			Describe:      src,
		}

		log.Printf("%s  \r\n\tPg Func - \r\n\t%v", funcName, pgfunc)
		return &pgfunc, nil
	}

	//result.Close()
	//stmt.Close()
	//conn.Close()
	return nil, nil

}
Esempio n. 4
0
func (pgf *PgFunc) VariadicScan(variadicArgs ...interface{}) (results []map[string]interface{}, retCode int, err error) {
	funcName := util.GetCallerName()

	if pgf == nil {
		log.Printf("%s PgFunc invalid", funcName)
		return nil, -1, nil
	}
	log.Printf("%s  args %#v ", funcName, variadicArgs)

	db, err := db.GetConnection()
	if err != nil {
		log.Printf("%s Connection err %#v", err)
		return nil, -1, err
	}

	query := pgf.generatePgFuncSQL()
	defer db.Close()
	log.Printf("%s Connected to Postgres, %v preparing %s with %v", funcName, pgf, query, variadicArgs)
	stmt, err := db.Prepare(query)
	if err != nil {
		log.Printf("%s Prepare err %#v", err)
		//stmt.Close()
		//db.Close()
		return nil, -1, err
	}
	result, err := stmt.Query(variadicArgs...)
	if err != nil {
		log.Printf("%s Execution problem: %#v", funcName, err)
		//stmt.Close()
		//db.Close()
		return nil, -1, err
	}
	if result == nil {
		log.Printf("%s No results returned: %#v", funcName, result)
		//stmt.Close()
		//db.Close()
		return nil, 1, nil
	}
	columns, err := result.Columns()
	if err != nil {
		log.Printf("%s Colums err %#v", funcName, err)
		//stmt.Close()
		//db.Close()
		return nil, -1, err
	}
	count := len(columns)
	values := make([]interface{}, count)
	valuePtrs := make([]interface{}, count)
	rows := make([]map[string]interface{}, 0)
	for result.Next() {
		retVal := make(map[string]interface{})
		for i, _ := range columns {
			valuePtrs[i] = &values[i]
		}

		if err := result.Scan(valuePtrs...); err != nil {
			log.Printf("%s Scan Error %#v", funcName, err)
			//result.Close()
			//stmt.Close()
			//db.Close()
			return nil, -1, err
		}
		switch pgf.ReturnTypeStr {
		case "void":
			log.Printf("%s Returns %s type", funcName, "void")
			retVal = nil
		case "integer":
			fallthrough
		case "record":
			log.Printf("%s Returns %s type", funcName, pgf.ReturnTypeStr)
			for i, col := range columns {

				var v interface{}

				val := values[i]

				b, ok := val.([]byte)

				if ok {
					v = string(b)
				} else {
					v = val
				}
				retVal[col] = v
				valType := reflect.TypeOf(v)
				log.Printf("%s returning %v [%v]  of type (%s)", funcName, col, v, valType)
			}
		default:
			log.Printf("%s Returns [%s] type", funcName, pgf.ReturnTypeStr)
			for i, col := range columns {

				var v interface{}

				val := values[i]

				b, ok := val.([]byte)

				if ok {
					v = string(b)
				} else {
					v = val
				}
				retVal[col] = v
				valType := reflect.TypeOf(v)
				log.Printf("%s returning %v [%v]  of type (%s)", funcName, col, v, valType)
			}

		}
		rows = append(rows, retVal)
		log.Printf("%s appened %#v", funcName, rows)
	}

	return rows, 0, nil
}