DbDriverHelper.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2020-08-04 15:40
  7. */
  8. namespace common\helpers;
  9. use Yii;
  10. class DbDriverHelper
  11. {
  12. public static function isMSSQL()
  13. {
  14. return Yii::$app->getDb()->getDriverName() === 'mssql' || Yii::$app->getDb()->getDriverName() === 'sqlsrv' || Yii::$app->getDb()->getDriverName() === 'dblib';
  15. }
  16. public static function isMySQL()
  17. {
  18. return Yii::$app->getDb()->getDriverName() === 'mysql';
  19. }
  20. public static function isOracle()
  21. {
  22. return Yii::$app->getDb()->getDriverName() === 'oci';
  23. }
  24. public static function isPgSQL()
  25. {
  26. return Yii::$app->getDb()->getDriverName() === 'pgsql';
  27. }
  28. public static function isSqlite()
  29. {
  30. return Yii::$app->getDb()->getDriverName() === 'sqlite';
  31. }
  32. public static function getTableName($tableName, $tablePrefix=null){
  33. if( $tablePrefix === null ){
  34. $tablePrefix = Yii::$app->getDb()->tablePrefix;
  35. }
  36. return preg_replace("/{{%(\w+)}}/isu", $tablePrefix . "$1", $tableName);
  37. }
  38. }