/lib/sqlstore/databases/oracle.js

https://github.com/oberhamsi/ringo-sqlstore · JavaScript · 126 lines · 69 code · 11 blank · 46 comment · 4 complexity · 240cbed4bf01f1df8d4d97029846cb4c MD5 · raw file

  1. var {ColumnType} = require("../types");
  2. var {Types} = java.sql;
  3. var BaseDialect = require("../basedialect").BaseDialect;
  4. /**
  5. * Database Dialect for Oracle databases
  6. * @constructor
  7. * @returns
  8. */
  9. var Dialect = function() {
  10. this.registerColumnType("integer", new ColumnType(Types.INTEGER, "number(10,0)"));
  11. this.registerColumnType("long", new ColumnType(Types.BIGINT, "number(19,0)"));
  12. this.registerColumnType("short", new ColumnType(Types.SMALLINT, "number(5,0)"));
  13. this.registerColumnType("float", new ColumnType(Types.FLOAT, "float"));
  14. this.registerColumnType("double", new ColumnType(Types.DOUBLE, "double precision"));
  15. this.registerColumnType("character", new ColumnType(Types.CHAR, "char(1 char)"));
  16. this.registerColumnType("string", new ColumnType(Types.VARCHAR, "varchar2", {
  17. "length": 4000
  18. }));
  19. this.registerColumnType("byte", new ColumnType(Types.TINYINT, "number(3,0)"));
  20. this.registerColumnType("boolean", new ColumnType(Types.BIT, "number(1,0)"));
  21. this.registerColumnType("date", new ColumnType(Types.DATE, "date"));
  22. this.registerColumnType("time", new ColumnType(Types.TIME, "date"));
  23. this.registerColumnType("timestamp", new ColumnType(Types.TIMESTAMP, "timestamp"));
  24. this.registerColumnType("binary", new ColumnType(Types.BINARY, "blob"));
  25. this.registerColumnType("text", new ColumnType(Types.LONGVARCHAR, "clob"));
  26. return this;
  27. };
  28. // extend BaseDialect
  29. Dialect.prototype = new BaseDialect();
  30. Dialect.prototype.constructor = Dialect;
  31. /** @ignore */
  32. Dialect.prototype.toString = function() {
  33. return "[Dialect Oracle]";
  34. };
  35. /**
  36. * Returns true
  37. * @returns True
  38. * @type Boolean
  39. */
  40. Dialect.prototype.hasSequenceSupport = function() {
  41. return true;
  42. };
  43. /**
  44. * Returns the SQL statement for retrieving the next value of a sequence
  45. * @param {String} sequenceName The name of the sequence
  46. * @returns The SQL statement
  47. * @type String
  48. */
  49. Dialect.prototype.getSqlNextSequenceValue = function(sequenceName) {
  50. return "SELECT " + this.quote(sequenceName) + ".NEXTVAL FROM DUAL";
  51. };
  52. /**
  53. * Extends the SQL statement passed as argument with a limit restriction
  54. * @param {Array} sqlBuf The SQL statement
  55. * @param {Limit} limit The limit
  56. */
  57. Dialect.prototype.addSqlLimit = function(sqlBuf, limit) {
  58. sqlBuf.unshift("SELECT * FROM ( ");
  59. sqlBuf.push(") WHERE ROWNUM <= ", limit.toString());
  60. };
  61. /**
  62. * Extends the SQL statement passed as argument with an offset restriction
  63. * @param {Array} sqlBuf The SQL statement
  64. * @param {Number} offset The offset
  65. */
  66. Dialect.prototype.addSqlOffset = function(sqlBuf, offset) {
  67. sqlBuf.unshift("SELECT * FROM (SELECT r.*, ROWNUM rnum FROM (");
  68. sqlBuf.push(") r ) where rnum > ", offset.toString());
  69. };
  70. /**
  71. * Extends the SQL statement passed as argument with a range restriction
  72. * @param {Array} sqlBuf The SQL statement
  73. * @param {Number} offset The offset
  74. * @param {Limit} limit The limit
  75. */
  76. Dialect.prototype.addSqlRange = function(sqlBuf, offset, limit) {
  77. sqlBuf.unshift("SELECT * FROM (SELECT r.*, ROWNUM rnum FROM (");
  78. sqlBuf.push(") r WHERE ROWNUM <= ", (offset + limit).toString());
  79. sqlBuf.push(") WHERE rnum > ", offset.toString());
  80. };
  81. /**
  82. * Returns the default schema for the connection passed as argument
  83. * @param {java.sql.Connection} conn The connection to use
  84. * @returns The name of the default schema
  85. * @type String
  86. */
  87. Dialect.prototype.getDefaultSchema = function(conn) {
  88. var metaData = conn.getMetaData();
  89. var userName = metaData.getUserName();
  90. var schemas = null;
  91. try {
  92. schemas = metaData.getSchemas(null, userName);
  93. if (schemas.next()) {
  94. return schemas.getString(1);
  95. }
  96. } finally {
  97. if (schemas != null) {
  98. schemas.close();
  99. }
  100. }
  101. return null;
  102. };
  103. /**
  104. * Returns the boolean value for the value passed as argument
  105. * @param {Object} value The value
  106. * @returns The boolean value
  107. * @type Boolean
  108. */
  109. Dialect.prototype.getBooleanValue = function(value) {
  110. if (!!value === true) {
  111. return 1;
  112. };
  113. return 0;
  114. };
  115. module.exports = new Dialect();