TableCreate.hx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (C)2005-2012 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. class TableCreate {
  25. static function autoInc( dbName ) {
  26. // on SQLite, autoincrement is necessary to be primary key as well
  27. return dbName == "SQLite" ? "PRIMARY KEY AUTOINCREMENT" : "AUTO_INCREMENT";
  28. }
  29. public static function getTypeSQL( t : RecordType, dbName : String ) {
  30. return switch( t ) {
  31. case DId: "INTEGER "+autoInc(dbName);
  32. case DUId: "INTEGER UNSIGNED "+autoInc(dbName);
  33. case DInt, DEncoded: "INTEGER";
  34. case DUInt: "INTEGER UNSIGNED";
  35. case DTinyInt: "TINYINT";
  36. case DTinyUInt, DEnum(_): "TINYINT UNSIGNED";
  37. case DSmallInt: "SMALLINT";
  38. case DSmallUInt: "SMALLINT UNSIGNED";
  39. case DMediumInt: "MEDIUMINT";
  40. case DMediumUInt: "MEDIUMINT UNSIGNED";
  41. case DSingle: "FLOAT";
  42. case DFloat: "DOUBLE";
  43. case DBool: "TINYINT(1)";
  44. case DString(n): "VARCHAR("+n+")";
  45. case DDate: "DATE";
  46. case DDateTime: "DATETIME";
  47. case DTimeStamp: "TIMESTAMP DEFAULT 0";
  48. case DTinyText: "TINYTEXT";
  49. case DSmallText: "TEXT";
  50. case DText, DSerialized: "MEDIUMTEXT";
  51. case DSmallBinary: "BLOB";
  52. case DBinary, DNekoSerialized, DData: "MEDIUMBLOB";
  53. case DLongBinary: "LONGBLOB";
  54. case DBigInt: "BIGINT";
  55. case DBigId: "BIGINT "+autoInc(dbName);
  56. case DBytes(n): "BINARY(" + n + ")";
  57. case DFlags(fl, auto): getTypeSQL(auto ? (fl.length <= 8 ? DTinyUInt : (fl.length <= 16 ? DSmallUInt : (fl.length <= 24 ? DMediumUInt : DInt))) : DInt, dbName);
  58. case DNull, DInterval: throw "assert";
  59. };
  60. }
  61. public static function create( manager : sys.db.Manager<Dynamic>, ?engine ) {
  62. function quote(v:String):String {
  63. return untyped manager.quoteField(v);
  64. }
  65. var cnx : Connection = untyped manager.getCnx();
  66. if( cnx == null )
  67. throw "SQL Connection not initialized on Manager";
  68. var dbName = cnx.dbName();
  69. var infos = manager.dbInfos();
  70. var sql = "CREATE TABLE " + quote(infos.name) + " (";
  71. var decls = [];
  72. var hasID = false;
  73. for( f in infos.fields ) {
  74. switch( f.t ) {
  75. case DId:
  76. hasID = true;
  77. case DUId, DBigId:
  78. hasID = true;
  79. if( dbName == "SQLite" )
  80. throw "S" + Std.string(f.t).substr(1)+" is not supported by " + dbName + " : use SId instead";
  81. default:
  82. }
  83. decls.push(quote(f.name)+" "+getTypeSQL(f.t,dbName)+(f.isNull ? "" : " NOT NULL"));
  84. }
  85. if( dbName != "SQLite" || !hasID )
  86. decls.push("PRIMARY KEY ("+Lambda.map(infos.key,quote).join(",")+")");
  87. sql += decls.join(",");
  88. sql += ")";
  89. if( engine != null )
  90. sql += "ENGINE="+engine;
  91. cnx.request(sql);
  92. }
  93. public static function exists( manager : sys.db.Manager<Dynamic> ) : Bool {
  94. var cnx : Connection = untyped manager.getCnx();
  95. if( cnx == null )
  96. throw "SQL Connection not initialized on Manager";
  97. try {
  98. cnx.request("SELECT * FROM `"+manager.dbInfos().name+"` LIMIT 1");
  99. return true;
  100. } catch( e : Dynamic ) {
  101. return false;
  102. }
  103. }
  104. }