iPhone music to new computer


So I got a new computer and unfortunately lost everything and iTunes music library on my last computer. I have all my music and playlists on my iPhone but to my understanding if you connect your iPhone to a new computer and try to sync with the iTunes program, it will erase all your non purchase music on your iPhone instead of transferring everything over as it is to the new computer and iTunes. Is there a way this can be done? Without having to pay for a third party program to do it, which im not sure I trust.

To note my music library on my iPhone consists oh a mixture of songs purchased on iTunes Store, non purchased music and CD music.