TableCreate.hx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (C)2005-2017 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package sys.db;
  23. import sys.db.RecordInfos;
  24. @:deprecated("This class will be removed soon, please install the record-macros library")
  25. class TableCreate {
  26. static function autoInc( dbName ) {
  27. // on SQLite, autoincrement is necessary to be primary key as well
  28. return dbName == "SQLite" ? "PRIMARY KEY AUTOINCREMENT" : "AUTO_INCREMENT";
  29. }
  30. public static function getTypeSQL( t : RecordType, dbName : String ) {
  31. return switch( t ) {
  32. case DId: "INTEGER "+autoInc(dbName);
  33. case DUId: "INTEGER UNSIGNED "+autoInc(dbName);
  34. case DInt, DEncoded: "INTEGER";
  35. case DUInt: "INTEGER UNSIGNED";
  36. case DTinyInt: "TINYINT";
  37. case DTinyUInt, DEnum(_): "TINYINT UNSIGNED";
  38. case DSmallInt: "SMALLINT";
  39. case DSmallUInt: "SMALLINT UNSIGNED";
  40. case DMediumInt: "MEDIUMINT";
  41. case DMediumUInt: "MEDIUMINT UNSIGNED";
  42. case DSingle: "FLOAT";
  43. case DFloat: "DOUBLE";
  44. case DBool: "TINYINT(1)";
  45. case DString(n): "VARCHAR("+n+")";
  46. case DDate: "DATE";
  47. case DDateTime: "DATETIME";
  48. case DTimeStamp: "TIMESTAMP DEFAULT 0";
  49. case DTinyText: "TINYTEXT";
  50. case DSmallText: "TEXT";
  51. case DText, DSerialized: "MEDIUMTEXT";
  52. case DSmallBinary: "BLOB";
  53. case DBinary, DNekoSerialized, DData: "MEDIUMBLOB";
  54. case DLongBinary: "LONGBLOB";
  55. case DBigInt: "BIGINT";
  56. case DBigId: "BIGINT "+autoInc(dbName);
  57. case DBytes(n): "BINARY(" + n + ")";
  58. case DFlags(fl, auto): getTypeSQL(auto ? (fl.length <= 8 ? DTinyUInt : (fl.length <= 16 ? DSmallUInt : (fl.length <= 24 ? DMediumUInt : DInt))) : DInt, dbName);
  59. case DNull, DInterval: throw "assert";
  60. };
  61. }
  62. public static function create( manager : sys.db.Manager<Dynamic>, ?engine ) {
  63. function quote(v:String):String {
  64. return untyped manager.quoteField(v);
  65. }
  66. var cnx : Connection = untyped manager.getCnx();
  67. if( cnx == null )
  68. throw "SQL Connection not initialized on Manager";
  69. var dbName = cnx.dbName();
  70. var infos = manager.dbInfos();
  71. var sql = "CREATE TABLE " + quote(infos.name) + " (";
  72. var decls = [];
  73. var hasID = false;
  74. for( f in infos.fields ) {
  75. switch( f.t ) {
  76. case DId:
  77. hasID = true;
  78. case DUId, DBigId:
  79. hasID = true;
  80. if( dbName == "SQLite" )
  81. throw "S" + Std.string(f.t).substr(1)+" is not supported by " + dbName + " : use SId instead";
  82. default:
  83. }
  84. decls.push(quote(f.name)+" "+getTypeSQL(f.t,dbName)+(f.isNull ? "" : " NOT NULL"));
  85. }
  86. if( dbName != "SQLite" || !hasID )
  87. decls.push("PRIMARY KEY ("+Lambda.map(infos.key,quote).join(",")+")");
  88. sql += decls.join(",");
  89. sql += ")";
  90. if( engine != null )
  91. sql += "ENGINE="+engine;
  92. cnx.request(sql);
  93. }
  94. public static function exists( manager : sys.db.Manager<Dynamic> ) : Bool {
  95. var cnx : Connection = untyped manager.getCnx();
  96. if( cnx == null )
  97. throw "SQL Connection not initialized on Manager";
  98. try {
  99. cnx.request("SELECT * FROM `"+manager.dbInfos().name+"` LIMIT 1");
  100. return true;
  101. } catch( e : Dynamic ) {
  102. return false;
  103. }
  104. }
  105. }