/libraries/dabl/database/propel/model/PropelTypes.php

https://github.com/coleHafner/coleandheather_dabl · PHP · 357 lines · 218 code · 28 blank · 111 comment · 2 complexity · 9cffa0892cd055b0018ab06ab053c53e MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. /**
  10. * A class that maps PropelTypes to PHP native types, PDO types (and Creole types).
  11. *
  12. * @author Hans Lellelid <hans@xmpl.org> (Propel)
  13. * @version $Revision: 2076 $
  14. * @package propel.generator.model
  15. */
  16. class PropelTypes
  17. {
  18. const CHAR = "CHAR";
  19. const VARCHAR = "VARCHAR";
  20. const LONGVARCHAR = "LONGVARCHAR";
  21. const CLOB = "CLOB";
  22. const CLOB_EMU = "CLOB_EMU";
  23. const NUMERIC = "NUMERIC";
  24. const DECIMAL = "DECIMAL";
  25. const TINYINT = "TINYINT";
  26. const SMALLINT = "SMALLINT";
  27. const INTEGER = "INTEGER";
  28. const BIGINT = "BIGINT";
  29. const REAL = "REAL";
  30. const FLOAT = "FLOAT";
  31. const DOUBLE = "DOUBLE";
  32. const BINARY = "BINARY";
  33. const VARBINARY = "VARBINARY";
  34. const LONGVARBINARY = "LONGVARBINARY";
  35. const BLOB = "BLOB";
  36. const DATE = "DATE";
  37. const TIME = "TIME";
  38. const TIMESTAMP = "TIMESTAMP";
  39. const BU_DATE = "BU_DATE";
  40. const BU_TIMESTAMP = "BU_TIMESTAMP";
  41. const BOOLEAN = "BOOLEAN";
  42. const BOOLEAN_EMU = "BOOLEAN_EMU";
  43. const OBJECT = "OBJECT";
  44. const PHP_ARRAY = "ARRAY";
  45. const ENUM = "ENUM";
  46. private static $TEXT_TYPES = array(
  47. self::CHAR, self::VARCHAR, self::LONGVARCHAR, self::CLOB, self::DATE, self::TIME, self::TIMESTAMP, self::BU_DATE, self::BU_TIMESTAMP
  48. );
  49. private static $LOB_TYPES = array(
  50. self::VARBINARY, self::LONGVARBINARY, self::BLOB
  51. );
  52. private static $TEMPORAL_TYPES = array(
  53. self::DATE, self::TIME, self::TIMESTAMP, self::BU_DATE, self::BU_TIMESTAMP
  54. );
  55. private static $NUMERIC_TYPES = array(
  56. self::SMALLINT, self::TINYINT, self::INTEGER, self::BIGINT, self::FLOAT, self::DOUBLE, self::NUMERIC, self::DECIMAL, self::REAL
  57. );
  58. private static $BOOLEAN_TYPES = array(
  59. self::BOOLEAN, self::BOOLEAN_EMU
  60. );
  61. const CHAR_NATIVE_TYPE = "string";
  62. const VARCHAR_NATIVE_TYPE = "string";
  63. const LONGVARCHAR_NATIVE_TYPE = "string";
  64. const CLOB_NATIVE_TYPE = "string";
  65. const CLOB_EMU_NATIVE_TYPE = "resource";
  66. const NUMERIC_NATIVE_TYPE = "string";
  67. const DECIMAL_NATIVE_TYPE = "string";
  68. const TINYINT_NATIVE_TYPE = "int";
  69. const SMALLINT_NATIVE_TYPE = "int";
  70. const INTEGER_NATIVE_TYPE = "int";
  71. const BIGINT_NATIVE_TYPE = "string";
  72. const REAL_NATIVE_TYPE = "double";
  73. const FLOAT_NATIVE_TYPE = "double";
  74. const DOUBLE_NATIVE_TYPE = "double";
  75. const BINARY_NATIVE_TYPE = "string";
  76. const VARBINARY_NATIVE_TYPE = "string";
  77. const LONGVARBINARY_NATIVE_TYPE = "string";
  78. const BLOB_NATIVE_TYPE = "resource";
  79. const BU_DATE_NATIVE_TYPE = "string";
  80. const DATE_NATIVE_TYPE = "string";
  81. const TIME_NATIVE_TYPE = "string";
  82. const TIMESTAMP_NATIVE_TYPE = "string";
  83. const BU_TIMESTAMP_NATIVE_TYPE = "string";
  84. const BOOLEAN_NATIVE_TYPE = "boolean";
  85. const BOOLEAN_EMU_NATIVE_TYPE = "boolean";
  86. const OBJECT_NATIVE_TYPE = "";
  87. const PHP_ARRAY_NATIVE_TYPE = "array";
  88. const ENUM_NATIVE_TYPE = "string";
  89. /**
  90. * Mapping between Propel types and PHP native types.
  91. *
  92. * @var array
  93. */
  94. private static $propelToPHPNativeMap = array(
  95. self::CHAR => self::CHAR_NATIVE_TYPE,
  96. self::VARCHAR => self::VARCHAR_NATIVE_TYPE,
  97. self::LONGVARCHAR => self::LONGVARCHAR_NATIVE_TYPE,
  98. self::CLOB => self::CLOB_NATIVE_TYPE,
  99. self::CLOB_EMU => self::CLOB_EMU_NATIVE_TYPE,
  100. self::NUMERIC => self::NUMERIC_NATIVE_TYPE,
  101. self::DECIMAL => self::DECIMAL_NATIVE_TYPE,
  102. self::TINYINT => self::TINYINT_NATIVE_TYPE,
  103. self::SMALLINT => self::SMALLINT_NATIVE_TYPE,
  104. self::INTEGER => self::INTEGER_NATIVE_TYPE,
  105. self::BIGINT => self::BIGINT_NATIVE_TYPE,
  106. self::REAL => self::REAL_NATIVE_TYPE,
  107. self::FLOAT => self::FLOAT_NATIVE_TYPE,
  108. self::DOUBLE => self::DOUBLE_NATIVE_TYPE,
  109. self::BINARY => self::BINARY_NATIVE_TYPE,
  110. self::VARBINARY => self::VARBINARY_NATIVE_TYPE,
  111. self::LONGVARBINARY => self::LONGVARBINARY_NATIVE_TYPE,
  112. self::BLOB => self::BLOB_NATIVE_TYPE,
  113. self::DATE => self::DATE_NATIVE_TYPE,
  114. self::BU_DATE => self::BU_DATE_NATIVE_TYPE,
  115. self::TIME => self::TIME_NATIVE_TYPE,
  116. self::TIMESTAMP => self::TIMESTAMP_NATIVE_TYPE,
  117. self::BU_TIMESTAMP => self::BU_TIMESTAMP_NATIVE_TYPE,
  118. self::BOOLEAN => self::BOOLEAN_NATIVE_TYPE,
  119. self::BOOLEAN_EMU => self::BOOLEAN_EMU_NATIVE_TYPE,
  120. self::OBJECT => self::OBJECT_NATIVE_TYPE,
  121. self::PHP_ARRAY => self::PHP_ARRAY_NATIVE_TYPE,
  122. self::ENUM => self::ENUM_NATIVE_TYPE,
  123. );
  124. /**
  125. * Mapping between Propel types and Creole types (for rev-eng task)
  126. *
  127. * @var array
  128. */
  129. private static $propelTypeToCreoleTypeMap = array(
  130. self::CHAR => self::CHAR,
  131. self::VARCHAR => self::VARCHAR,
  132. self::LONGVARCHAR => self::LONGVARCHAR,
  133. self::CLOB => self::CLOB,
  134. self::NUMERIC => self::NUMERIC,
  135. self::DECIMAL => self::DECIMAL,
  136. self::TINYINT => self::TINYINT,
  137. self::SMALLINT => self::SMALLINT,
  138. self::INTEGER => self::INTEGER,
  139. self::BIGINT => self::BIGINT,
  140. self::REAL => self::REAL,
  141. self::FLOAT => self::FLOAT,
  142. self::DOUBLE => self::DOUBLE,
  143. self::BINARY => self::BINARY,
  144. self::VARBINARY => self::VARBINARY,
  145. self::LONGVARBINARY => self::LONGVARBINARY,
  146. self::BLOB => self::BLOB,
  147. self::DATE => self::DATE,
  148. self::TIME => self::TIME,
  149. self::TIMESTAMP => self::TIMESTAMP,
  150. self::BOOLEAN => self::BOOLEAN,
  151. self::BOOLEAN_EMU => self::BOOLEAN_EMU,
  152. self::OBJECT => self::OBJECT,
  153. self::PHP_ARRAY => self::PHP_ARRAY,
  154. self::ENUM => self::ENUM,
  155. // These are pre-epoch dates, which we need to map to String type
  156. // since they cannot be properly handled using strtotime() -- or even numeric
  157. // timestamps on Windows.
  158. self::BU_DATE => self::VARCHAR,
  159. self::BU_TIMESTAMP => self::VARCHAR,
  160. );
  161. /**
  162. * Mapping between Propel types and PDO type contants (for prepared statement setting).
  163. *
  164. * @var array
  165. */
  166. private static $propelTypeToPDOTypeMap = array(
  167. self::CHAR => PDO::PARAM_STR,
  168. self::VARCHAR => PDO::PARAM_STR,
  169. self::LONGVARCHAR => PDO::PARAM_STR,
  170. self::CLOB => PDO::PARAM_STR,
  171. self::CLOB_EMU => PDO::PARAM_STR,
  172. self::NUMERIC => PDO::PARAM_INT,
  173. self::DECIMAL => PDO::PARAM_STR,
  174. self::TINYINT => PDO::PARAM_INT,
  175. self::SMALLINT => PDO::PARAM_INT,
  176. self::INTEGER => PDO::PARAM_INT,
  177. self::BIGINT => PDO::PARAM_INT,
  178. self::REAL => PDO::PARAM_STR,
  179. self::FLOAT => PDO::PARAM_STR,
  180. self::DOUBLE => PDO::PARAM_STR,
  181. self::BINARY => PDO::PARAM_STR,
  182. self::VARBINARY => PDO::PARAM_LOB,
  183. self::LONGVARBINARY => PDO::PARAM_LOB,
  184. self::BLOB => PDO::PARAM_LOB,
  185. self::DATE => PDO::PARAM_STR,
  186. self::TIME => PDO::PARAM_STR,
  187. self::TIMESTAMP => PDO::PARAM_STR,
  188. self::BOOLEAN => PDO::PARAM_BOOL,
  189. self::BOOLEAN_EMU => PDO::PARAM_INT,
  190. self::OBJECT => PDO::PARAM_STR,
  191. self::PHP_ARRAY => PDO::PARAM_STR,
  192. self::ENUM => PDO::PARAM_INT,
  193. // These are pre-epoch dates, which we need to map to String type
  194. // since they cannot be properly handled using strtotime() -- or even numeric
  195. // timestamps on Windows.
  196. self::BU_DATE => PDO::PARAM_STR,
  197. self::BU_TIMESTAMP => PDO::PARAM_STR,
  198. );
  199. /**
  200. * Return native PHP type which corresponds to the
  201. * Creole type provided. Use in the base object class generation.
  202. *
  203. * @param $propelType The Propel type name.
  204. * @return string Name of the native PHP type
  205. */
  206. public static function getPhpNative($propelType)
  207. {
  208. return self::$propelToPHPNativeMap[$propelType];
  209. }
  210. /**
  211. * Returns the correct Creole type _name_ for propel added types
  212. *
  213. * @param $type the propel added type.
  214. * @return string Name of the the correct Creole type (e.g. "VARCHAR").
  215. */
  216. public static function getCreoleType($type)
  217. {
  218. return self::$propelTypeToCreoleTypeMap[$type];
  219. }
  220. /**
  221. * Resturns the PDO type (PDO::PARAM_* constant) value.
  222. * @return int
  223. */
  224. public static function getPDOType($type)
  225. {
  226. return self::$propelTypeToPDOTypeMap[$type];
  227. }
  228. /**
  229. * Returns Propel type constant corresponding to Creole type code.
  230. * Used but Propel Creole task.
  231. *
  232. * @param int $sqlType The Creole SQL type constant.
  233. * @return string The Propel type to use or NULL if none found.
  234. */
  235. public static function getPropelType($sqlType)
  236. {
  237. if (isset(self::$creoleToPropelTypeMap[$sqlType])) {
  238. return self::$creoleToPropelTypeMap[$sqlType];
  239. }
  240. }
  241. /**
  242. * Get array of Propel types.
  243. *
  244. * @return array string[]
  245. */
  246. public static function getPropelTypes()
  247. {
  248. return array_keys(self::$propelTypeToCreoleTypeMap);
  249. }
  250. /**
  251. * Whether passed type is a temporal (date/time/timestamp) type.
  252. *
  253. * @param string $type Propel type
  254. * @return boolean
  255. */
  256. public static function isTemporalType($type)
  257. {
  258. return in_array($type, self::$TEMPORAL_TYPES);
  259. }
  260. /**
  261. * Returns true if values for the type need to be quoted.
  262. *
  263. * @param string $type The Propel type to check.
  264. * @return boolean True if values for the type need to be quoted.
  265. */
  266. public static function isTextType($type)
  267. {
  268. return in_array($type, self::$TEXT_TYPES);
  269. }
  270. /**
  271. * Returns true if values for the type are numeric.
  272. *
  273. * @param string $type The Propel type to check.
  274. * @return boolean True if values for the type need to be quoted.
  275. */
  276. public static function isNumericType($type)
  277. {
  278. return in_array($type, self::$NUMERIC_TYPES);
  279. }
  280. /**
  281. * Returns true if values for the type are boolean.
  282. *
  283. * @param string $type The Propel type to check.
  284. * @return boolean True if values for the type need to be quoted.
  285. */
  286. public static function isBooleanType($type)
  287. {
  288. return in_array($type, self::$BOOLEAN_TYPES);
  289. }
  290. /**
  291. * Returns true if type is a LOB type (i.e. would be handled by Blob/Clob class).
  292. * @param string $type Propel type to check.
  293. * @return boolean
  294. */
  295. public static function isLobType($type)
  296. {
  297. return in_array($type, self::$LOB_TYPES);
  298. }
  299. /**
  300. * Convenience method to indicate whether a passed-in PHP type is a primitive.
  301. *
  302. * @param string $phpType The PHP type to check
  303. * @return boolean Whether the PHP type is a primitive (string, int, boolean, float)
  304. */
  305. public static function isPhpPrimitiveType($phpType)
  306. {
  307. return in_array($phpType, array("boolean", "int", "double", "float", "string"));
  308. }
  309. /**
  310. * Convenience method to indicate whether a passed-in PHP type is a numeric primitive.
  311. *
  312. * @param string $phpType The PHP type to check
  313. * @return boolean Whether the PHP type is a primitive (string, int, boolean, float)
  314. */
  315. public static function isPhpPrimitiveNumericType($phpType)
  316. {
  317. return in_array($phpType, array("boolean", "int", "double", "float"));
  318. }
  319. /**
  320. * Convenience method to indicate whether a passed-in PHP type is an object.
  321. *
  322. * @param string $phpType The PHP type to check
  323. * @return boolean Whether the PHP type is a primitive (string, int, boolean, float)
  324. */
  325. public static function isPhpObjectType($phpType)
  326. {
  327. return (!self::isPhpPrimitiveType($phpType) && !in_array($phpType, array("resource", "array")));
  328. }
  329. }