PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/hacked-cadmium/src/main/java/fr/x9c/cadmium/primitives/cadmiumjdbc/Connections.java

http://jhmtasc.googlecode.com/
Java | 1113 lines | 671 code | 51 blank | 391 comment | 11 complexity | 365a20127afc173aae596227441ad492 MD5 | raw file
  1. /*
  2. * This file is part of Cadmium.
  3. * Copyright (C) 2007-2010 Xavier Clerc.
  4. *
  5. * Cadmium is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Cadmium is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. package fr.x9c.cadmium.primitives.cadmiumjdbc;
  19. import java.sql.Connection;
  20. import java.sql.Savepoint;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import fr.x9c.cadmium.kernel.Block;
  24. import fr.x9c.cadmium.kernel.CodeRunner;
  25. import fr.x9c.cadmium.kernel.Fail;
  26. import fr.x9c.cadmium.kernel.Primitive;
  27. import fr.x9c.cadmium.kernel.PrimitiveProvider;
  28. import fr.x9c.cadmium.kernel.Value;
  29. import fr.x9c.cadmium.primitives.cadmium.Cadmium;
  30. /**
  31. * This class provides the primitives related to the <tt>java.sql.Connection</tt> class.
  32. *
  33. * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
  34. * @version 1.1
  35. * @since 1.1
  36. */
  37. @PrimitiveProvider
  38. public final class Connections {
  39. /**
  40. * No instance of this class.
  41. */
  42. private Connections() {
  43. } // end empty constructor
  44. /**
  45. * Clears the warnings for the passed connection.
  46. * @param ctxt context
  47. * @param connection connection
  48. * @return <i>unit</i>
  49. * @throws Fail.Exception if any error occurs
  50. */
  51. @Primitive
  52. public static Value cadmiumjdbc_connection_clear_warnings(final CodeRunner ctxt,
  53. final Value connection)
  54. throws Fail.Exception {
  55. try {
  56. final Connection c = (Connection) connection.asBlock().asCustom();
  57. c.clearWarnings();
  58. return Value.UNIT;
  59. } catch (final Exception e) {
  60. Cadmium.fail(ctxt, e);
  61. return Value.UNIT; // never reached
  62. } // end try/catch
  63. } // end method 'cadmiumjdbc_connection_clear_warnings(CodeRunner, Value)'
  64. /**
  65. * Closes the passed connection.
  66. * @param ctxt context
  67. * @param connection connection
  68. * @return <i>unit</i>
  69. * @throws Fail.Exception if any error occurs
  70. */
  71. @Primitive
  72. public static Value cadmiumjdbc_connection_close(final CodeRunner ctxt,
  73. final Value connection)
  74. throws Fail.Exception {
  75. try {
  76. final Connection c = (Connection) connection.asBlock().asCustom();
  77. c.close();
  78. return Value.UNIT;
  79. } catch (final Exception e) {
  80. Cadmium.fail(ctxt, e);
  81. return Value.UNIT; // never reached
  82. } // end try/catch
  83. } // end method 'cadmiumjdbc_connection_close(CodeRunner, Value)'
  84. /**
  85. * Commits the changes for the passed connection.
  86. * @param ctxt context
  87. * @param connection connection
  88. * @return <i>unit</i>
  89. * @throws Fail.Exception if any error occurs
  90. */
  91. @Primitive
  92. public static Value cadmiumjdbc_connection_commit(final CodeRunner ctxt,
  93. final Value connection)
  94. throws Fail.Exception {
  95. try {
  96. final Connection c = (Connection) connection.asBlock().asCustom();
  97. c.commit();
  98. return Value.UNIT;
  99. } catch (final Exception e) {
  100. Cadmium.fail(ctxt, e);
  101. return Value.UNIT; // never reached
  102. } // end try/catch
  103. } // end method 'cadmiumjdbc_connection_commit(CodeRunner, Value)'
  104. /**
  105. * Creates an array.
  106. * @param ctxt context
  107. * @param connection connection
  108. * @param typeName SQL name for the elements
  109. * @param elements array elements
  110. * @return an array containing the passed elements
  111. * @throws Fail.Exception if any error occurs
  112. */
  113. @Primitive
  114. public static Value cadmiumjdbc_connection_create_array_of(final CodeRunner ctxt,
  115. final Value connection,
  116. final Value typeName,
  117. final Value elements)
  118. throws Fail.Exception {
  119. try {
  120. final Connection c = (Connection) connection.asBlock().asCustom();
  121. final List<Object> elems = new ArrayList<Object>();
  122. Value list = elements;
  123. while (list != Value.EMPTY_LIST) {
  124. final Block b = list.asBlock();
  125. elems.add(b.get(0).asBlock().asCustom());
  126. list = b.get(1);
  127. } // end while
  128. return Cadmium.createObject(c.createArrayOf(typeName.asBlock().asString(),
  129. elems.toArray()));
  130. } catch (final Exception e) {
  131. Cadmium.fail(ctxt, e);
  132. return Value.UNIT; // never reached
  133. } // end try/catch
  134. } // end method 'cadmiumjdbc_connection_create_array_of(CodeRunner, Value, Value, Value)'
  135. /**
  136. * Creates a blob.
  137. * @param ctxt context
  138. * @param connection connection
  139. * @return a new blob
  140. * @throws Fail.Exception if any error occurs
  141. */
  142. @Primitive
  143. public static Value cadmiumjdbc_connection_create_blob(final CodeRunner ctxt,
  144. final Value connection)
  145. throws Fail.Exception {
  146. try {
  147. final Connection c = (Connection) connection.asBlock().asCustom();
  148. return Cadmium.createObject(c.createBlob());
  149. } catch (final Exception e) {
  150. Cadmium.fail(ctxt, e);
  151. return Value.UNIT; // never reached
  152. } // end try/catch
  153. } // end method 'cadmiumjdbc_connection_create_blob(CodeRunner, Value)'
  154. /**
  155. * Creates a clob.
  156. * @param ctxt context
  157. * @param connection connection
  158. * @return a new clob
  159. * @throws Fail.Exception if any error occurs
  160. */
  161. @Primitive
  162. public static Value cadmiumjdbc_connection_create_clob(final CodeRunner ctxt,
  163. final Value connection)
  164. throws Fail.Exception {
  165. try {
  166. final Connection c = (Connection) connection.asBlock().asCustom();
  167. return Cadmium.createObject(c.createClob());
  168. } catch (final Exception e) {
  169. Cadmium.fail(ctxt, e);
  170. return Value.UNIT; // never reached
  171. } // end try/catch
  172. } // end method 'cadmiumjdbc_connection_create_clob(CodeRunner, Value)'
  173. /**
  174. * Creates a nclob.
  175. * @param ctxt context
  176. * @param connection connection
  177. * @return a new nclob
  178. * @throws Fail.Exception if any error occurs
  179. */
  180. @Primitive
  181. public static Value cadmiumjdbc_connection_create_nclob(final CodeRunner ctxt,
  182. final Value connection)
  183. throws Fail.Exception {
  184. try {
  185. final Connection c = (Connection) connection.asBlock().asCustom();
  186. return Cadmium.createObject(c.createNClob());
  187. } catch (final Exception e) {
  188. Cadmium.fail(ctxt, e);
  189. return Value.UNIT; // never reached
  190. } // end try/catch
  191. } // end method 'cadmiumjdbc_connection_create_nclob(CodeRunner, Value)'
  192. /**
  193. * Creates a SQL XML.
  194. * @param ctxt context
  195. * @param connection connection
  196. * @return a new SQL XML
  197. * @throws Fail.Exception if any error occurs
  198. */
  199. @Primitive
  200. public static Value cadmiumjdbc_connection_create_sqlxml(final CodeRunner ctxt,
  201. final Value connection)
  202. throws Fail.Exception {
  203. try {
  204. final Connection c = (Connection) connection.asBlock().asCustom();
  205. return Cadmium.createObject(c.createSQLXML());
  206. } catch (final Exception e) {
  207. Cadmium.fail(ctxt, e);
  208. return Value.UNIT; // never reached
  209. } // end try/catch
  210. } // end method 'cadmiumjdbc_connection_create_sqlxml(CodeRunner, Value)'
  211. /**
  212. * Creates a statement.
  213. * @param ctxt context
  214. * @param connection connection
  215. * @return a new statement
  216. * @throws Fail.Exception if any error occurs
  217. */
  218. @Primitive
  219. public static Value cadmiumjdbc_connection_create_statement(final CodeRunner ctxt,
  220. final Value connection)
  221. throws Fail.Exception {
  222. try {
  223. final Connection c = (Connection) connection.asBlock().asCustom();
  224. return Cadmium.createObject(c.createStatement());
  225. } catch (final Exception e) {
  226. Cadmium.fail(ctxt, e);
  227. return Value.UNIT; // never reached
  228. } // end try/catch
  229. } // end method 'cadmiumjdbc_connection_create_statement(CodeRunner, Value)'
  230. /**
  231. * Creates a statement.
  232. * @param ctxt context
  233. * @param connection connection
  234. * @param type result set type
  235. * @param conc result set concurrency
  236. * @return a new statement
  237. * @throws Fail.Exception if any error occurs
  238. */
  239. @Primitive
  240. public static Value cadmiumjdbc_connection_create_statement_type_conc(final CodeRunner ctxt,
  241. final Value connection,
  242. final Value type,
  243. final Value conc)
  244. throws Fail.Exception {
  245. try {
  246. final Connection c = (Connection) connection.asBlock().asCustom();
  247. return Cadmium.createObject(c.createStatement(Misc.decodeResultSetType(type),
  248. Misc.decodeResultSetConcurrency(conc)));
  249. } catch (final Exception e) {
  250. Cadmium.fail(ctxt, e);
  251. return Value.UNIT; // never reached
  252. } // end try/catch
  253. } // end method 'cadmiumjdbc_connection_create_statement_type_conc(CodeRunner, Value, Value, Value)'
  254. /**
  255. * Creates a statement.
  256. * @param ctxt context
  257. * @param connection connection
  258. * @param type result set type
  259. * @param conc result set concurrency
  260. * @param hold result set holdability
  261. * @return a new statement
  262. * @throws Fail.Exception if any error occurs
  263. */
  264. @Primitive
  265. public static Value cadmiumjdbc_connection_create_statement_type_conc_hold(final CodeRunner ctxt,
  266. final Value connection,
  267. final Value type,
  268. final Value conc,
  269. final Value hold)
  270. throws Fail.Exception {
  271. try {
  272. final Connection c = (Connection) connection.asBlock().asCustom();
  273. return Cadmium.createObject(c.createStatement(Misc.decodeResultSetType(type),
  274. Misc.decodeResultSetConcurrency(conc),
  275. Misc.decodeResultSetHoldability(hold)));
  276. } catch (final Exception e) {
  277. Cadmium.fail(ctxt, e);
  278. return Value.UNIT; // never reached
  279. } // end try/catch
  280. } // end method 'cadmiumjdbc_connection_create_statement_type_conc_hold(CodeRunner, Value, Value, Value, Value)'
  281. /**
  282. * Creates a structure.
  283. * @param ctxt context
  284. * @param connection connection
  285. * @param typeName SQL name for the type of the structure
  286. * @param attributes elements for the structure
  287. * @return a new structure containing the passed elements
  288. * @throws Fail.Exception if any error occurs
  289. */
  290. @Primitive
  291. public static Value cadmiumjdbc_connection_create_struct(final CodeRunner ctxt,
  292. final Value connection,
  293. final Value typeName,
  294. final Value attributes)
  295. throws Fail.Exception {
  296. try {
  297. final Connection c = (Connection) connection.asBlock().asCustom();
  298. final List<Object> attrs = new ArrayList<Object>();
  299. Value list = attributes;
  300. while (list != Value.EMPTY_LIST) {
  301. final Block b = list.asBlock();
  302. attrs.add(b.get(0).asBlock().asCustom());
  303. list = b.get(1);
  304. } // end while
  305. return Cadmium.createObject(c.createStruct(typeName.asBlock().asString(),
  306. attrs.toArray()));
  307. } catch (final Exception e) {
  308. Cadmium.fail(ctxt, e);
  309. return Value.UNIT; // never reached
  310. } // end try/catch
  311. } // end method 'cadmiumjdbc_connection_create_struct(CodeRunner, Value, Value, Value)'
  312. /**
  313. * Tests whether the passed connection has auto-commit mode enabled.
  314. * @param ctxt context
  315. * @param connection connection
  316. * @return <tt>true</tt> if the passed connection has auto-commit mode enabled,
  317. * <tt>false</tt> otherwise
  318. * @throws Fail.Exception if any error occurs
  319. */
  320. @Primitive
  321. public static Value cadmiumjdbc_connection_get_auto_commit(final CodeRunner ctxt,
  322. final Value connection)
  323. throws Fail.Exception {
  324. try {
  325. final Connection c = (Connection) connection.asBlock().asCustom();
  326. return c.getAutoCommit() ? Value.TRUE : Value.FALSE;
  327. } catch (final Exception e) {
  328. Cadmium.fail(ctxt, e);
  329. return Value.UNIT; // never reached
  330. } // end try/catch
  331. } // end method 'cadmiumjdbc_connection_get_auto_commit(CodeRunner, Value)'
  332. /**
  333. * Returns the catalog name of the passed connection.
  334. * @param ctxt context
  335. * @param connection connection
  336. * @return the catalog name of the passed connection
  337. * @throws Fail.Exception if any error occurs
  338. */
  339. @Primitive
  340. public static Value cadmiumjdbc_connection_get_catalog(final CodeRunner ctxt,
  341. final Value connection)
  342. throws Fail.Exception {
  343. try {
  344. final Connection c = (Connection) connection.asBlock().asCustom();
  345. final Block res = Block.createString(c.getCatalog());
  346. return Value.createFromBlock(res);
  347. } catch (final Exception e) {
  348. Cadmium.fail(ctxt, e);
  349. return Value.UNIT; // never reached
  350. } // end try/catch
  351. } // end method 'cadmiumjdbc_connection_get_catalog(CodeRunner, Value)'
  352. /**
  353. * Returns the client info properties for the passed connection.
  354. * @param ctxt context
  355. * @param connection connection
  356. * @return the client info properties for the passed connection
  357. * @throws Fail.Exception if any error occurs
  358. */
  359. @Primitive
  360. public static Value cadmiumjdbc_connection_get_client_infos(final CodeRunner ctxt,
  361. final Value connection)
  362. throws Fail.Exception {
  363. try {
  364. final Connection c = (Connection) connection.asBlock().asCustom();
  365. return Misc.encodeProperties(c.getClientInfo());
  366. } catch (final Exception e) {
  367. Cadmium.fail(ctxt, e);
  368. return Value.UNIT; // never reached
  369. } // end try/catch
  370. } // end method 'cadmiumjdbc_connection_get_client_infos(CodeRunner, Value)'
  371. /**
  372. * Returns the value of a client info property for the passed connection.
  373. * @param ctxt context
  374. * @param connection connection
  375. * @param name property name
  376. * @return the value of a client info property for the passed connection
  377. * @throws Fail.Exception if any error occurs
  378. */
  379. @Primitive
  380. public static Value cadmiumjdbc_connection_get_client_info(final CodeRunner ctxt,
  381. final Value connection,
  382. final Value name)
  383. throws Fail.Exception {
  384. try {
  385. final Connection c = (Connection) connection.asBlock().asCustom();
  386. final Block res = Block.createString(c.getClientInfo(name.asBlock().asString()));
  387. return Value.createFromBlock(res);
  388. } catch (final Exception e) {
  389. Cadmium.fail(ctxt, e);
  390. return Value.UNIT; // never reached
  391. } // end try/catch
  392. } // end method 'cadmiumjdbc_connection_get_client_info(CodeRunner, Value, Value)'
  393. /**
  394. * Returns the holdability for the passed connection.
  395. * @param ctxt context
  396. * @param connection connection
  397. * @return the holdability for the passed connection
  398. * @throws Fail.Exception if any error occurs
  399. */
  400. @Primitive
  401. public static Value cadmiumjdbc_connection_get_holdability(final CodeRunner ctxt,
  402. final Value connection)
  403. throws Fail.Exception {
  404. try {
  405. final Connection c = (Connection) connection.asBlock().asCustom();
  406. return Misc.encodeResultSetHoldability(c.getHoldability());
  407. } catch (final Exception e) {
  408. Cadmium.fail(ctxt, e);
  409. return Value.UNIT; // never reached
  410. } // end try/catch
  411. } // end method 'cadmiumjdbc_connection_get_holdability(CodeRunner, Value)'
  412. /**
  413. * Returns the metadata for the passed connection.
  414. * @param ctxt context
  415. * @param connection connection
  416. * @return the metadata for the passed connection
  417. * @throws Fail.Exception if any error occurs
  418. */
  419. @Primitive
  420. public static Value cadmiumjdbc_connection_get_meta_data(final CodeRunner ctxt,
  421. final Value connection)
  422. throws Fail.Exception {
  423. try {
  424. final Connection c = (Connection) connection.asBlock().asCustom();
  425. return Cadmium.createObject(c.getMetaData());
  426. } catch (final Exception e) {
  427. Cadmium.fail(ctxt, e);
  428. return Value.UNIT; // never reached
  429. } // end try/catch
  430. } // end method 'cadmiumjdbc_connection_get_meta_data(CodeRunner, Value)'
  431. /**
  432. * Returns the transaction isolation for the passed connection.
  433. * @param ctxt context
  434. * @param connection connection
  435. * @return the transaction isolation for the passed connection
  436. * @throws Fail.Exception if any error occurs
  437. */
  438. @Primitive
  439. public static Value cadmiumjdbc_connection_get_transaction_isolation(final CodeRunner ctxt,
  440. final Value connection)
  441. throws Fail.Exception {
  442. try {
  443. final Connection c = (Connection) connection.asBlock().asCustom();
  444. return Misc.encodeTransactionIsolation(c.getTransactionIsolation());
  445. } catch (final Exception e) {
  446. Cadmium.fail(ctxt, e);
  447. return Value.UNIT; // never reached
  448. } // end try/catch
  449. } // end method 'cadmiumjdbc_connection_get_transaction_isolation(CodeRunner, Value)'
  450. /**
  451. * Returns the list of warnings for the passed connection.
  452. * @param ctxt context
  453. * @param connection connection
  454. * @return the list of warnings for the passed connection
  455. * @throws Fail.Exception if any error occurs
  456. */
  457. @Primitive
  458. public static Value cadmiumjdbc_connection_get_warnings(final CodeRunner ctxt,
  459. final Value connection)
  460. throws Fail.Exception {
  461. try {
  462. final Connection c = (Connection) connection.asBlock().asCustom();
  463. return Misc.encodeSQLWarnings(c.getWarnings());
  464. } catch (final Exception e) {
  465. Cadmium.fail(ctxt, e);
  466. return Value.UNIT; // never reached
  467. } // end try/catch
  468. } // end method 'cadmiumjdbc_connection_get_warnings(CodeRunner, Value)'
  469. /**
  470. * Tests whether the passed connection is closed.
  471. * @param ctxt context
  472. * @param connection connection
  473. * @return <tt>true</tt> if the connection is closed,
  474. * <tt>false</tt> otherwise
  475. * @throws Fail.Exception if any error occurs
  476. */
  477. @Primitive
  478. public static Value cadmiumjdbc_connection_is_closed(final CodeRunner ctxt,
  479. final Value connection)
  480. throws Fail.Exception {
  481. try {
  482. final Connection c = (Connection) connection.asBlock().asCustom();
  483. return c.isClosed() ? Value.TRUE : Value.FALSE;
  484. } catch (final Exception e) {
  485. Cadmium.fail(ctxt, e);
  486. return Value.UNIT; // never reached
  487. } // end try/catch
  488. } // end method 'cadmiumjdbc_connection_is_closed(CodeRunner, Value)'
  489. /**
  490. * Tests whether the passed connection is read only.
  491. * @param ctxt context
  492. * @param connection connection
  493. * @return <tt>true</tt> if the connection is read only,
  494. * <tt>false</tt> otherwise
  495. * @throws Fail.Exception if any error occurs
  496. */
  497. @Primitive
  498. public static Value cadmiumjdbc_connection_is_read_only(final CodeRunner ctxt,
  499. final Value connection)
  500. throws Fail.Exception {
  501. try {
  502. final Connection c = (Connection) connection.asBlock().asCustom();
  503. return c.isReadOnly() ? Value.TRUE : Value.FALSE;
  504. } catch (final Exception e) {
  505. Cadmium.fail(ctxt, e);
  506. return Value.UNIT; // never reached
  507. } // end try/catch
  508. } // end method 'cadmiumjdbc_connection_is_read_only(CodeRunner, Value)'
  509. /**
  510. * Tests whether the passed connection is still valid.
  511. * @param ctxt context
  512. * @param connection connection
  513. * @param timeout timeout
  514. * @return <tt>true</tt> if the connection is still valid,
  515. * <tt>false</tt> otherwise
  516. * @throws Fail.Exception if any error occurs
  517. */
  518. @Primitive
  519. public static Value cadmiumjdbc_connection_is_valid(final CodeRunner ctxt,
  520. final Value connection,
  521. final Value timeout)
  522. throws Fail.Exception {
  523. try {
  524. final Connection c = (Connection) connection.asBlock().asCustom();
  525. return c.isValid(timeout.asBlock().asInt32())
  526. ? Value.TRUE
  527. : Value.FALSE;
  528. } catch (final Exception e) {
  529. Cadmium.fail(ctxt, e);
  530. return Value.UNIT; // never reached
  531. } // end try/catch
  532. } // end method 'cadmiumjdbc_connection_is_valid(CodeRunner, Value, Value)'
  533. /**
  534. * Converts the passed SQL statement into the SQL dialect used by the connection.
  535. * @param ctxt context
  536. * @param connection connection
  537. * @param sql SQL statement
  538. * @return the passed SQL statement into the SQL dialect used by the connection
  539. * @throws Fail.Exception if any error occurs
  540. */
  541. @Primitive
  542. public static Value cadmiumjdbc_connection_native_sql(final CodeRunner ctxt,
  543. final Value connection,
  544. final Value sql)
  545. throws Fail.Exception {
  546. try {
  547. final Connection c = (Connection) connection.asBlock().asCustom();
  548. final Block res = Block.createString(c.nativeSQL(sql.asBlock().asString()));
  549. return Value.createFromBlock(res);
  550. } catch (final Exception e) {
  551. Cadmium.fail(ctxt, e);
  552. return Value.UNIT; // never reached
  553. } // end try/catch
  554. } // end method 'cadmiumjdbc_connection_native_sql(CodeRunner, Value, Value)'
  555. /**
  556. * Creates a statement used to call stored procedures.
  557. * @param ctxt context
  558. * @param connection connection
  559. * @param sql SQL statement
  560. * @return a statement used to call stored procedures
  561. * @throws Fail.Exception if any error occurs
  562. */
  563. @Primitive
  564. public static Value cadmiumjdbc_connection_prepare_call(final CodeRunner ctxt,
  565. final Value connection,
  566. final Value sql)
  567. throws Fail.Exception {
  568. try {
  569. final Connection c = (Connection) connection.asBlock().asCustom();
  570. return Cadmium.createObject(c.prepareCall(sql.asBlock().asString()));
  571. } catch (final Exception e) {
  572. Cadmium.fail(ctxt, e);
  573. return Value.UNIT; // never reached
  574. } // end try/catch
  575. } // end method 'cadmiumjdbc_connection_prepare_call(CodeRunner, Value, Value)'
  576. /**
  577. * Creates a statement used to call stored procedures.
  578. * @param ctxt context
  579. * @param connection connection
  580. * @param sql SQL statement
  581. * @param type result set type
  582. * @param conc result set concurrency
  583. * @return a statement used to call stored procedures
  584. * @throws Fail.Exception if any error occurs
  585. */
  586. @Primitive
  587. public static Value cadmiumjdbc_connection_prepare_call_type_conc(final CodeRunner ctxt,
  588. final Value connection,
  589. final Value sql,
  590. final Value type,
  591. final Value conc)
  592. throws Fail.Exception {
  593. try {
  594. final Connection c = (Connection) connection.asBlock().asCustom();
  595. return Cadmium.createObject(c.prepareCall(sql.asBlock().asString(),
  596. Misc.decodeResultSetType(type),
  597. Misc.decodeResultSetConcurrency(conc)));
  598. } catch (final Exception e) {
  599. Cadmium.fail(ctxt, e);
  600. return Value.UNIT; // never reached
  601. } // end try/catch
  602. } // end method 'cadmiumjdbc_connection_prepare_call_type_conc(CodeRunner, Value, Value, Value, Value)'
  603. /**
  604. * Creates a statement used to call stored procedures.
  605. * @param ctxt context
  606. * @param connection connection
  607. * @param sql SQL statement
  608. * @param type result set type
  609. * @param conc result set concurrency
  610. * @param hold result set holdability
  611. * @return a statement used to call stored procedures
  612. * @throws Fail.Exception if any error occurs
  613. */
  614. @Primitive
  615. public static Value cadmiumjdbc_connection_prepare_call_type_conc_hold(final CodeRunner ctxt,
  616. final Value connection,
  617. final Value sql,
  618. final Value type,
  619. final Value conc,
  620. final Value hold)
  621. throws Fail.Exception {
  622. try {
  623. final Connection c = (Connection) connection.asBlock().asCustom();
  624. return Cadmium.createObject(c.prepareCall(sql.asBlock().asString(),
  625. Misc.decodeResultSetType(type),
  626. Misc.decodeResultSetConcurrency(conc),
  627. Misc.decodeResultSetHoldability(hold)));
  628. } catch (final Exception e) {
  629. Cadmium.fail(ctxt, e);
  630. return Value.UNIT; // never reached
  631. } // end try/catch
  632. } // end method 'cadmiumjdbc_connection_prepare_call_type_conc_hold(CodeRunner, Value, Value, Value, Value, Value)'
  633. /**
  634. * Creates a statement used to retrieve autogen keys.
  635. * @param ctxt context
  636. * @param connection connection
  637. * @param sql SQL statement
  638. * @return a statement used to call stored procedures
  639. * @throws Fail.Exception if any error occurs
  640. */
  641. @Primitive
  642. public static Value cadmiumjdbc_connection_prepare_statement(final CodeRunner ctxt,
  643. final Value connection,
  644. final Value sql)
  645. throws Fail.Exception {
  646. try {
  647. final Connection c = (Connection) connection.asBlock().asCustom();
  648. return Cadmium.createObject(c.prepareStatement(sql.asBlock().asString()));
  649. } catch (final Exception e) {
  650. Cadmium.fail(ctxt, e);
  651. return Value.UNIT; // never reached
  652. } // end try/catch
  653. } // end method 'cadmiumjdbc_connection_prepare_statement(CodeRunner, Value, Value)'
  654. /**
  655. * Creates a statement used to retrieve auto generated keys.
  656. * @param ctxt context
  657. * @param connection connection
  658. * @param sql SQL statement
  659. * @param autogenKeys auto generated keys option
  660. * @return a statement used to call stored procedures
  661. * @throws Fail.Exception if any error occurs
  662. */
  663. @Primitive
  664. public static Value cadmiumjdbc_connection_prepare_statement_autogen_keys(final CodeRunner ctxt,
  665. final Value connection,
  666. final Value sql,
  667. final Value autogenKeys)
  668. throws Fail.Exception {
  669. try {
  670. final Connection c = (Connection) connection.asBlock().asCustom();
  671. return Cadmium.createObject(c.prepareStatement(sql.asBlock().asString(),
  672. Misc.decodeStatementAutogenKeys(autogenKeys)));
  673. } catch (final Exception e) {
  674. Cadmium.fail(ctxt, e);
  675. return Value.UNIT; // never reached
  676. } // end try/catch
  677. } // end method 'cadmiumjdbc_connection_prepare_statement_autogen_keys(CodeRunner, Value, Value, Value)'
  678. /**
  679. * Creates a statement used to retrieve auto generated keys.
  680. * @param ctxt context
  681. * @param connection connection
  682. * @param sql SQL statement
  683. * @param indexes indexes of columns that should be returned
  684. * @return a statement used to call stored procedures
  685. * @throws Fail.Exception if any error occurs
  686. */
  687. @Primitive
  688. public static Value cadmiumjdbc_connection_prepare_statement_indexes(final CodeRunner ctxt,
  689. final Value connection,
  690. final Value sql,
  691. final Value indexes)
  692. throws Fail.Exception {
  693. try {
  694. final Connection c = (Connection) connection.asBlock().asCustom();
  695. final List<Integer> columns = new ArrayList<Integer>();
  696. Value list = indexes;
  697. while (list != Value.EMPTY_LIST) {
  698. final Block b = list.asBlock();
  699. columns.add(b.get(0).asLong());
  700. list = b.get(1);
  701. } // end while
  702. final int[] cols = new int[columns.size()];
  703. int i = 0;
  704. for (Integer ii : columns) {
  705. cols[i++] = ii;
  706. } // end for
  707. return Cadmium.createObject(c.prepareStatement(sql.asBlock().asString(),
  708. cols));
  709. } catch (final Exception e) {
  710. Cadmium.fail(ctxt, e);
  711. return Value.UNIT; // never reached
  712. } // end try/catch
  713. } // end method 'cadmiumjdbc_connection_prepare_statement_indexes(CodeRunner, Value, Value, Value)'
  714. /**
  715. * Creates a statement used to retrieve auto generated keys.
  716. * @param ctxt context
  717. * @param connection connection
  718. * @param sql SQL statement
  719. * @param type result set type
  720. * @param conc result set concurrency
  721. * @return a statement used to call stored procedures
  722. * @throws Fail.Exception if any error occurs
  723. */
  724. @Primitive
  725. public static Value cadmiumjdbc_connection_prepare_statement_type_conc(final CodeRunner ctxt,
  726. final Value connection,
  727. final Value sql,
  728. final Value type,
  729. final Value conc)
  730. throws Fail.Exception {
  731. try {
  732. final Connection c = (Connection) connection.asBlock().asCustom();
  733. return Cadmium.createObject(c.prepareStatement(sql.asBlock().asString(),
  734. Misc.decodeResultSetType(type),
  735. Misc.decodeResultSetConcurrency(conc)));
  736. } catch (final Exception e) {
  737. Cadmium.fail(ctxt, e);
  738. return Value.UNIT; // never reached
  739. } // end try/catch
  740. } // end method 'cadmiumjdbc_connection_prepare_statement_type_conc(CodeRunner, Value, Value, Value, Value)'
  741. /**
  742. * Creates a statement used to retrieve auto generated keys.
  743. * @param ctxt context
  744. * @param connection connection
  745. * @param sql SQL statement
  746. * @param type result set type
  747. * @param conc result set concurrency
  748. * @param hold result set holdability
  749. * @return a statement used to call stored procedures
  750. * @throws Fail.Exception if any error occurs
  751. */
  752. @Primitive
  753. public static Value cadmiumjdbc_connection_prepare_statement_type_conc_hold(final CodeRunner ctxt,
  754. final Value connection,
  755. final Value sql,
  756. final Value type,
  757. final Value conc,
  758. final Value hold)
  759. throws Fail.Exception {
  760. try {
  761. final Connection c = (Connection) connection.asBlock().asCustom();
  762. return Cadmium.createObject(c.prepareStatement(sql.asBlock().asString(),
  763. Misc.decodeResultSetType(type),
  764. Misc.decodeResultSetConcurrency(conc),
  765. Misc.decodeResultSetHoldability(hold)));
  766. } catch (final Exception e) {
  767. Cadmium.fail(ctxt, e);
  768. return Value.UNIT; // never reached
  769. } // end try/catch
  770. } // end method 'cadmiumjdbc_connection_prepare_statement_type_conc_hold(CodeRunner, Value, Value, Value, Value, Value)'
  771. /**
  772. * Creates a statement used to retrieve auto generated keys.
  773. * @param ctxt context
  774. * @param connection connection
  775. * @param sql SQL statement
  776. * @param names names of columns that should be returned
  777. * @return a statement used to call stored procedures
  778. * @throws Fail.Exception if any error occurs
  779. */
  780. @Primitive
  781. public static Value cadmiumjdbc_connection_prepare_statement_names(final CodeRunner ctxt,
  782. final Value connection,
  783. final Value sql,
  784. final Value names)
  785. throws Fail.Exception {
  786. try {
  787. final Connection c = (Connection) connection.asBlock().asCustom();
  788. final List<String> columns = new ArrayList<String>();
  789. Value list = names;
  790. while (list != Value.EMPTY_LIST) {
  791. final Block b = list.asBlock();
  792. columns.add(b.get(0).asBlock().asString());
  793. list = b.get(1);
  794. } // end while
  795. return Cadmium.createObject(c.prepareStatement(sql.asBlock().asString(),
  796. columns.toArray(new String[columns.size()])));
  797. } catch (final Exception e) {
  798. Cadmium.fail(ctxt, e);
  799. return Value.UNIT; // never reached
  800. } // end try/catch
  801. } // end method 'cadmiumjdbc_connection_prepare_statement_names(CodeRunner, Value, Value, Value)'
  802. /**
  803. * Release a given savepoint for the passed connection.
  804. * @param ctxt context
  805. * @param connection connection
  806. * @param savepoint savepoint
  807. * @return <i>unit</i>
  808. * @throws Fail.Exception if any error occurs
  809. */
  810. @Primitive
  811. public static Value cadmiumjdbc_connection_release_savepoint(final CodeRunner ctxt,
  812. final Value connection,
  813. final Value savepoint)
  814. throws Fail.Exception {
  815. try {
  816. final Connection c = (Connection) connection.asBlock().asCustom();
  817. c.releaseSavepoint((Savepoint) savepoint.asBlock().asCustom());
  818. return Value.UNIT;
  819. } catch (final Exception e) {
  820. Cadmium.fail(ctxt, e);
  821. return Value.UNIT; // never reached
  822. } // end try/catch
  823. } // end method 'cadmiumjdbc_connection_release_savepoint(CodeRunner, Value, Value)'
  824. /**
  825. * Cancels the changes for the passed connection.
  826. * @param ctxt context
  827. * @param connection connection
  828. * @return <i>unit</i>
  829. * @throws Fail.Exception if any error occurs
  830. */
  831. @Primitive
  832. public static Value cadmiumjdbc_connection_rollback(final CodeRunner ctxt,
  833. final Value connection)
  834. throws Fail.Exception {
  835. try {
  836. final Connection c = (Connection) connection.asBlock().asCustom();
  837. c.rollback();
  838. return Value.UNIT;
  839. } catch (final Exception e) {
  840. Cadmium.fail(ctxt, e);
  841. return Value.UNIT; // never reached
  842. } // end try/catch
  843. } // end method 'cadmiumjdbc_connection_rollback(CodeRunner, Value)'
  844. /**
  845. * Reverts to the state as defined by a given savepoint for the passed connection.
  846. * @param ctxt context
  847. * @param connection connection
  848. * @param savepoint savepoint
  849. * @return <i>unit</i>
  850. * @throws Fail.Exception if any error occurs
  851. */
  852. @Primitive
  853. public static Value cadmiumjdbc_connection_rollback_to(final CodeRunner ctxt,
  854. final Value connection,
  855. final Value savepoint)
  856. throws Fail.Exception {
  857. try {
  858. final Connection c = (Connection) connection.asBlock().asCustom();
  859. c.rollback((Savepoint) savepoint.asBlock().asCustom());
  860. return Value.UNIT;
  861. } catch (final Exception e) {
  862. Cadmium.fail(ctxt, e);
  863. return Value.UNIT; // never reached
  864. } // end try/catch
  865. } // end method 'cadmiumjdbc_connection_rollback_to(CodeRunner, Value, Value)'
  866. /**
  867. * Sets the value of the 'autocommit' property for the passed connection.
  868. * @param ctxt context
  869. * @param connection connection
  870. * @param v new value
  871. * @return <i>unit</i>
  872. * @throws Fail.Exception if any error occurs
  873. */
  874. @Primitive
  875. public static Value cadmiumjdbc_connection_set_auto_commit(final CodeRunner ctxt,
  876. final Value connection,
  877. final Value v)
  878. throws Fail.Exception {
  879. try {
  880. final Connection c = (Connection) connection.asBlock().asCustom();
  881. c.setAutoCommit(v == Value.TRUE);
  882. return Value.UNIT;
  883. } catch (final Exception e) {
  884. Cadmium.fail(ctxt, e);
  885. return Value.UNIT; // never reached
  886. } // end try/catch
  887. } // end method 'cadmiumjdbc_connection_set_auto_commit(CodeRunner, Value, Value)'
  888. /**
  889. * Sets the value of the 'catalog' property for the passed connection.
  890. * @param ctxt context
  891. * @param connection connection
  892. * @param v new value
  893. * @return <i>unit</i>
  894. * @throws Fail.Exception if any error occurs
  895. */
  896. @Primitive
  897. public static Value cadmiumjdbc_connection_set_catalog(final CodeRunner ctxt,
  898. final Value connection,
  899. final Value v)
  900. throws Fail.Exception {
  901. try {
  902. final Connection c = (Connection) connection.asBlock().asCustom();
  903. c.setCatalog(v.asBlock().asString());
  904. return Value.UNIT;
  905. } catch (final Exception e) {
  906. Cadmium.fail(ctxt, e);
  907. return Value.UNIT; // never reached
  908. } // end try/catch
  909. } // end method 'cadmiumjdbc_connection_set_catalog(CodeRunner, Value, Value)'
  910. /**
  911. * Sets the client info properties the passed connection.
  912. * @param ctxt context
  913. * @param connection connection
  914. * @param v new value
  915. * @return <i>unit</i>
  916. * @throws Fail.Exception if any error occurs
  917. */
  918. @Primitive
  919. public static Value cadmiumjdbc_connection_set_client_infos(final CodeRunner ctxt,
  920. final Value connection,
  921. final Value v)
  922. throws Fail.Exception {
  923. try {
  924. final Connection c = (Connection) connection.asBlock().asCustom();
  925. c.setClientInfo(Misc.decodeProperties(v));
  926. return Value.UNIT;
  927. } catch (final Exception e) {
  928. Cadmium.fail(ctxt, e);
  929. return Value.UNIT; // never reached
  930. } // end try/catch
  931. } // end method 'cadmiumjdbc_connection_set_client_infos(CodeRunner, Value, Value)'
  932. /**
  933. * Sets a client info property the passed connection.
  934. * @param ctxt context
  935. * @param connection connection
  936. * @param n property name
  937. * @param v new value
  938. * @return <i>unit</i>
  939. * @throws Fail.Exception if any error occurs
  940. */
  941. @Primitive
  942. public static Value cadmiumjdbc_connection_set_client_info(final CodeRunner ctxt,
  943. final Value connection,
  944. final Value n,
  945. final Value v)
  946. throws Fail.Exception {
  947. try {
  948. final Connection c = (Connection) connection.asBlock().asCustom();
  949. c.setClientInfo(n.asBlock().asString(), v.asBlock().asString());
  950. return Value.UNIT;
  951. } catch (final Exception e) {
  952. Cadmium.fail(ctxt, e);
  953. return Value.UNIT; // never reached
  954. } // end try/catch
  955. } // end method 'cadmiumjdbc_connection_set_client_info(CodeRunner, Value, Value, Value)'
  956. /**
  957. * Sets the value of the 'holdability' property for the passed connection.
  958. * @param ctxt context
  959. * @param connection connection
  960. * @param v new value
  961. * @return <i>unit</i>
  962. * @throws Fail.Exception if any error occurs
  963. */
  964. @Primitive
  965. public static Value cadmiumjdbc_connection_set_holdability(final CodeRunner ctxt,
  966. final Value connection,
  967. final Value v)
  968. throws Fail.Exception {
  969. try {
  970. final Connection c = (Connection) connection.asBlock().asCustom();
  971. c.setHoldability(Misc.decodeResultSetHoldability(v));
  972. return Value.UNIT;
  973. } catch (final Exception e) {
  974. Cadmium.fail(ctxt, e);
  975. return Value.UNIT; // never reached
  976. } // end try/catch
  977. } // end method 'cadmiumjdbc_connection_set_holdability(CodeRunner, Value, Value)'
  978. /**
  979. * Sets the value of the 'read-only' property for the passed connection.
  980. * @param ctxt context
  981. * @param connection connection
  982. * @param v new value
  983. * @return <i>unit</i>
  984. * @throws Fail.Exception if any error occurs
  985. */
  986. @Primitive
  987. public static Value cadmiumjdbc_connection_set_read_only(final CodeRunner ctxt,
  988. final Value connection,
  989. final Value v)
  990. throws Fail.Exception {
  991. try {
  992. final Connection c = (Connection) connection.asBlock().asCustom();
  993. c.setReadOnly(v == Value.TRUE);
  994. return Value.UNIT;
  995. } catch (final Exception e) {
  996. Cadmium.fail(ctxt, e);
  997. return Value.UNIT; // never reached
  998. } // end try/catch
  999. } // end method 'cadmiumjdbc_connection_set_read_only(CodeRunner, Value, Value)'
  1000. /**
  1001. * Creates an unnamed savepoint for the passed connection.
  1002. * @param ctxt context
  1003. * @param connection connection
  1004. * @return a new savepoint
  1005. * @throws Fail.Exception if any error occurs
  1006. */
  1007. @Primitive
  1008. public static Value cadmiumjdbc_connection_set_savepoint(final CodeRunner ctxt,
  1009. final Value connection)
  1010. throws Fail.Exception {
  1011. try {
  1012. final Connection c = (Connection) connection.asBlock().asCustom();
  1013. return Cadmium.createObject(c.setSavepoint());
  1014. } catch (final Exception e) {
  1015. Cadmium.fail(ctxt, e);
  1016. return Value.UNIT; // never reached
  1017. } // end try/catch
  1018. } // end method 'cadmiumjdbc_connection_set_savepoint(CodeRunner, Value)'
  1019. /**
  1020. * Creates a named savepoint for the passed connection.
  1021. * @param ctxt context
  1022. * @param connection connection
  1023. * @param name savepoint name
  1024. * @return a new savepoint
  1025. * @throws Fail.Exception if any error occurs
  1026. */
  1027. @Primitive
  1028. public static Value cadmiumjdbc_connection_set_savepoint_with_name(final CodeRunner ctxt,
  1029. final Value connection,
  1030. final Value name)
  1031. throws Fail.Exception {
  1032. try {
  1033. final Connection c = (Connection) connection.asBlock().asCustom();
  1034. return Cadmium.createObject(c.setSavepoint(name.asBlock().asString()));
  1035. } catch (final Exception e) {
  1036. Cadmium.fail(ctxt, e);
  1037. return Value.UNIT; // never reached
  1038. } // end try/catch
  1039. } // end method 'cadmiumjdbc_connection_set_savepoint_with_name(CodeRunner, Value, Value)'
  1040. /**
  1041. * Sets the value of the 'transaction isolation' property for the passed connection.
  1042. * @param ctxt context
  1043. * @param connection connection
  1044. * @param v new value
  1045. * @return <i>unit</i>
  1046. * @throws Fail.Exception if any error occurs
  1047. */
  1048. @Primitive
  1049. public static Value cadmiumjdbc_connection_set_transaction_isolation(final CodeRunner ctxt,
  1050. final Value connection,
  1051. final Value v)
  1052. throws Fail.Exception {
  1053. try {
  1054. final Connection c = (Connection) connection.asBlock().asCustom();
  1055. c.setTransactionIsolation(Misc.decodeTransactionIsolation(v));
  1056. return Value.UNIT;
  1057. } catch (final Exception e) {
  1058. Cadmium.fail(ctxt, e);
  1059. return Value.UNIT; // never reached
  1060. } // end try/catch
  1061. } // end method 'cadmiumjdbc_connection_set_transaction_isolation(CodeRunner, Value, Value)'
  1062. } // end class 'Connections'