index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. var Classes = Object.create(null);
  2. /**
  3. * Create a new Connection instance.
  4. * @param {object|string} config Configuration or connection string for new MySQL connection
  5. * @return {Connection} A new MySQL connection
  6. * @public
  7. */
  8. exports.createConnection = function createConnection(config) {
  9. var Connection = loadClass('Connection');
  10. var ConnectionConfig = loadClass('ConnectionConfig');
  11. return new Connection({config: new ConnectionConfig(config)});
  12. };
  13. /**
  14. * Create a new Pool instance.
  15. * @param {object|string} config Configuration or connection string for new MySQL connections
  16. * @return {Pool} A new MySQL pool
  17. * @public
  18. */
  19. exports.createPool = function createPool(config) {
  20. var Pool = loadClass('Pool');
  21. var PoolConfig = loadClass('PoolConfig');
  22. return new Pool({config: new PoolConfig(config)});
  23. };
  24. /**
  25. * Create a new PoolCluster instance.
  26. * @param {object} [config] Configuration for pool cluster
  27. * @return {PoolCluster} New MySQL pool cluster
  28. * @public
  29. */
  30. exports.createPoolCluster = function createPoolCluster(config) {
  31. var PoolCluster = loadClass('PoolCluster');
  32. return new PoolCluster(config);
  33. };
  34. /**
  35. * Create a new Query instance.
  36. * @param {string} sql The SQL for the query
  37. * @param {array} [values] Any values to insert into placeholders in sql
  38. * @param {function} [callback] The callback to use when query is complete
  39. * @return {Query} New query object
  40. * @public
  41. */
  42. exports.createQuery = function createQuery(sql, values, callback) {
  43. var Connection = loadClass('Connection');
  44. return Connection.createQuery(sql, values, callback);
  45. };
  46. /**
  47. * Escape a value for SQL.
  48. * @param {*} value The value to escape
  49. * @param {boolean} [stringifyObjects=false] Setting if objects should be stringified
  50. * @param {string} [timeZone=local] Setting for time zone to use for Date conversion
  51. * @return {string} Escaped string value
  52. * @public
  53. */
  54. exports.escape = function escape(value, stringifyObjects, timeZone) {
  55. var SqlString = loadClass('SqlString');
  56. return SqlString.escape(value, stringifyObjects, timeZone);
  57. };
  58. /**
  59. * Escape an identifier for SQL.
  60. * @param {*} value The value to escape
  61. * @param {boolean} [forbidQualified=false] Setting to treat '.' as part of identifier
  62. * @return {string} Escaped string value
  63. * @public
  64. */
  65. exports.escapeId = function escapeId(value, forbidQualified) {
  66. var SqlString = loadClass('SqlString');
  67. return SqlString.escapeId(value, forbidQualified);
  68. };
  69. /**
  70. * Format SQL and replacement values into a SQL string.
  71. * @param {string} sql The SQL for the query
  72. * @param {array} [values] Any values to insert into placeholders in sql
  73. * @param {boolean} [stringifyObjects=false] Setting if objects should be stringified
  74. * @param {string} [timeZone=local] Setting for time zone to use for Date conversion
  75. * @return {string} Formatted SQL string
  76. * @public
  77. */
  78. exports.format = function format(sql, values, stringifyObjects, timeZone) {
  79. var SqlString = loadClass('SqlString');
  80. return SqlString.format(sql, values, stringifyObjects, timeZone);
  81. };
  82. /**
  83. * Wrap raw SQL strings from escape overriding.
  84. * @param {string} sql The raw SQL
  85. * @return {object} Wrapped object
  86. * @public
  87. */
  88. exports.raw = function raw(sql) {
  89. var SqlString = loadClass('SqlString');
  90. return SqlString.raw(sql);
  91. };
  92. /**
  93. * The type constants.
  94. * @public
  95. */
  96. Object.defineProperty(exports, 'Types', {
  97. get: loadClass.bind(null, 'Types')
  98. });
  99. /**
  100. * Load the given class.
  101. * @param {string} className Name of class to default
  102. * @return {function|object} Class constructor or exports
  103. * @private
  104. */
  105. function loadClass(className) {
  106. var Class = Classes[className];
  107. if (Class !== undefined) {
  108. return Class;
  109. }
  110. // This uses a switch for static require analysis
  111. switch (className) {
  112. case 'Connection':
  113. Class = require('./lib/Connection');
  114. break;
  115. case 'ConnectionConfig':
  116. Class = require('./lib/ConnectionConfig');
  117. break;
  118. case 'Pool':
  119. Class = require('./lib/Pool');
  120. break;
  121. case 'PoolCluster':
  122. Class = require('./lib/PoolCluster');
  123. break;
  124. case 'PoolConfig':
  125. Class = require('./lib/PoolConfig');
  126. break;
  127. case 'SqlString':
  128. Class = require('./lib/protocol/SqlString');
  129. break;
  130. case 'Types':
  131. Class = require('./lib/protocol/constants/types');
  132. break;
  133. default:
  134. throw new Error('Cannot find class \'' + className + '\'');
  135. }
  136. // Store to prevent invoking require()
  137. Classes[className] = Class;
  138. return Class;
  139. }