refactor , sql extern