TableCreate.hx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2005-2011, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package sys.db;
  26. import sys.db.SpodInfos;
  27. class TableCreate {
  28. static function autoInc( dbName ) {
  29. // on SQLite, autoincrement is necessary to be primary key as well
  30. return dbName == "SQLite" ? "PRIMARY KEY AUTOINCREMENT" : "AUTO_INCREMENT";
  31. }
  32. public static function getTypeSQL( t : SpodType, dbName : String ) {
  33. return switch( t ) {
  34. case DId: "INTEGER "+autoInc(dbName);
  35. case DUId: "INTEGER UNSIGNED "+autoInc(dbName);
  36. case DInt, DEncoded: "INTEGER";
  37. case DUInt: "INTEGER UNSIGNED";
  38. case DTinyInt: "TINYINT";
  39. case DTinyUInt: "TINYINT UNSIGNED";
  40. case DSmallInt: "SMALLINT";
  41. case DSmallUInt: "SMALLINT UNSIGNED";
  42. case DMediumInt: "MEDIUMINT";
  43. case DMediumUInt: "MEDIUMINT UNSIGNED";
  44. case DSingle: "FLOAT";
  45. case DFloat: "DOUBLE";
  46. case DBool: "TINYINT(1)";
  47. case DString(n): "VARCHAR("+n+")";
  48. case DDate: "DATE";
  49. case DDateTime: "DATETIME";
  50. case DTimeStamp: "TIMESTAMP DEFAULT 0";
  51. case DTinyText: "TINYTEXT";
  52. case DSmallText: "TEXT";
  53. case DText, DSerialized: "MEDIUMTEXT";
  54. case DSmallBinary: "BLOB";
  55. case DBinary, DNekoSerialized: "MEDIUMBLOB";
  56. case DLongBinary: "LONGBLOB";
  57. case DBigInt: "BIGINT";
  58. case DBigId: "BIGINT "+autoInc(dbName);
  59. case DBytes(n): "BINARY(" + n + ")";
  60. case DFlags(fl, auto): getTypeSQL(auto ? (fl.length <= 8 ? DTinyUInt : (fl.length <= 16 ? DSmallUInt : (fl.length <= 24 ? DMediumUInt : DInt))) : DInt, dbName);
  61. case DNull, DInterval: throw "assert";
  62. };
  63. }
  64. public static function create( manager : sys.db.Manager<Dynamic>, ?engine ) {
  65. function quote(v:String):String {
  66. return untyped manager.quoteField(v);
  67. }
  68. var cnx : Connection = untyped manager.getCnx();
  69. if( cnx == null )
  70. throw "SQL Connection not initialized on Manager";
  71. var dbName = cnx.dbName();
  72. var infos = manager.dbInfos();
  73. var sql = "CREATE TABLE " + quote(infos.name) + " (";
  74. var decls = [];
  75. var hasID = false;
  76. for( f in infos.fields ) {
  77. switch( f.t ) {
  78. case DId:
  79. hasID = true;
  80. case DUId, DBigId:
  81. hasID = true;
  82. if( dbName == "SQLite" )
  83. throw "S" + Std.string(f.t).substr(1)+" is not supported by " + dbName + " : use SId instead";
  84. default:
  85. }
  86. decls.push(quote(f.name)+" "+getTypeSQL(f.t,dbName)+(f.isNull ? "" : " NOT NULL"));
  87. }
  88. if( dbName != "SQLite" || !hasID )
  89. decls.push("PRIMARY KEY ("+Lambda.map(infos.key,quote).join(",")+")");
  90. sql += decls.join(",");
  91. sql += ")";
  92. if( engine != null )
  93. sql += "ENGINE="+engine;
  94. cnx.request(sql);
  95. }
  96. public static function exists( manager : sys.db.Manager<Dynamic> ) : Bool {
  97. var cnx : Connection = untyped manager.getCnx();
  98. if( cnx == null )
  99. throw "SQL Connection not initialized on Manager";
  100. try {
  101. cnx.request("SELECT * FROM `"+manager.dbInfos().name+"` LIMIT 1");
  102. return true;
  103. } catch( e : Dynamic ) {
  104. return false;
  105. }
  106. }
  107. }