ASH84

Software Engineer/Developer, co-founder of Payhere. Ex-Banksalad. Intereseted in iteroperability, bootstrap company, writting.

SQLAlchemy with StoreProcedure

created:2016-08-24
updated:2017-04-03
edit

์ œ๋ชฉ์ž์ฒด๊ฐ€ ์—ญ์„ค์ ์ด์ง€๋งŒ, ์ด๋ ‡๊ฒŒ ์จ์•ผ ํ• ๋•Œ๊ฐ€ ์žˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค๋ฉด, ๋ ˆ๊ฑฐ์‹œ๋ฅผ ๋‹ค์‹œ ์žฌ๊ฐœ๋ฐœํ•˜๋Š” ์ˆ˜์ค€์—์„œ ํŠน์ • ํ”„๋กœ์‹œ์ €๋Š” ๋‹จ์ˆœํžˆ db๋ฅผ ์กฐํšŒํ•˜๋Š” ๊ฒƒ ์ด์ƒ์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ ์ธ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ์„๋•Œ๊ฐ€ ์žˆ๋‹ค. ๋˜ํ•œ, ๋‹ค๋ฅธ ์ด์œ ๋กœ ORM์ธ SQLAlchemy์™€ ์ €์žฅํ”„๋กœ์‹œ์ €๋ฅผ ํ˜ผํ•ฉํ•ด์•ผํ•  ๋•Œ๊ฐ€ ์žˆ๋Š”๋ฐ, ๊ทธ๋Ÿด๋•Œ ๋”ฐ๋กœ ์ €์žฅํ”„๋กœ์‹œ์ €์šฉ ์ฝ”๋“œ๋ฅผ ์“ฐ๊ธฐ ๋ณด๋‹ค๋Š” SQLAlchemy ๋ฅผ ์ด์šฉํ•ด์„œ ํ”„๋กœ์‹œ์ €๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

๊ฐœ์ธ์ ์œผ๋กœ SQLAlchemy๋ฅผ ์จ์„œ ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š” ์ด์œ ๋Š” ์œ ์ง€๋ณด์ˆ˜ ํ•˜๋Š” ์ž…์žฅ์—์„œ DB์—ฐ๊ฒฐ ๋ฌธ์ž์—ด ๋“ฑ๊ณผ ๊ฐ™์€ ๊ด€๋ฆฌํฌ์ธํŠธ๋„ ํ•˜๋‚˜๋กœ ์œ ์ง€ํ•˜๋ฉด ์ข‹๊ณ , ORM ์™ธ์— ๋ช‡๊ฐœ์˜ ํ”„๋กœ์‹œ์ €๋ฅผ ์œ„ํ•ด์„œ DB์—ฐ๊ฒฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์ข…์†์ ์ธ ์ฝ”๋“œ๋ฅผ ์งœ๊ณ  ์‹ถ์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์„ ๋Š๊ผˆ๋‹ค.(์•ˆ๋œ๋‹ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค.)

์œ„์˜ ์ฝ”๋“œ๋Š” Flask-SQLAlchemy ๋กœ ์ง  ์ฝ”๋“œ์ธ๋ฐ, ์ผ๋ฐ˜ SQLAlchemy๋ฅผ ์•„์‹œ๋Š” ๊ฐœ๋ฐœ์ž๋ผ๋ฉด ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ์ด๋‹ค. ๋‹ค๋ฅธ ๊ฒƒ๋ณด๋‹ค๋„ raw_connection() ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•˜์ž๋ฉด ํ”„๋ก์‹œ๋œ DBAPI ์—ฐ๊ฒฐ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ, ์‹ค์ œ DBAPI ์—ฐ๊ฒฐ๊ณผ ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค. ๋‹จ close() ํ•จ์ˆ˜์— ํ•œํ•ด์„œ๋Š” ์˜ˆ์™ธ์ ์œผ๋กœ ์‹ค์ œ๋กœ ์—ฐ๊ฒฐ์ด ๋Š์–ด์ง€๋Š” ๊ฑฐ์ด ์•„๋‹ˆ๋ผ pool๋กœ ๋ฆฌํ„ด๋˜์–ด ์ง„๋‹ค๊ณ  ํ•œ๋‹ค.

์›๋ฌธ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

raw_connection(connection=None)

Return a โ€œrawโ€ DBAPI connection from the connection pool.

The returned object is a proxied version of the DBAPI connection object used by the underlying driver in use. The object will have all the same behavior as the real DBAPI connection, except that its close() method will result in the connection being returned to the pool, rather than being closed for real.

This method provides direct DBAPI connection access for special situations when the API provided by Connection is not needed. When a Connection object is already present, the DBAPI connection is available using the Connection.connection accessor.


#db  #Python  #stored procedure  #์ €์žฅํ”„๋กœ์‹œ์ €  #sqlalchemy