PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/dump_provider.php

http://perseph.googlecode.com/
PHP | 134 lines | 104 code | 17 blank | 13 comment | 13 complexity | f457d241d39633411361281f019b7461 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * Produces the provider declaration for a DB.
  4. *
  5. * FEATURE: This is only known to support MySQL for now (other DBs may likely produce invalid results)
  6. */
  7. require_once dirname(__FILE__).'/php_support/error_handling.inc';
  8. require_once 'MDB2.php';
  9. function perror( $msg ) {
  10. file_put_contents( 'php://stderr', $msg );
  11. }
  12. if( $argc < 3 ) {
  13. perror( "Syntax: {$argv[0]} provider_name dsn
  14. DSN Example: mysqli://DBSTestUser:password@localhost/dbs_test
  15. " );
  16. exit(1);
  17. }
  18. function check_error( $res ) {
  19. if( !@PEAR::isError( $res ) )
  20. return true;
  21. perror( $res->getMessage() );
  22. perror( $res->userinfo ); //why is this not part of getMessage?! (TODO: move into the DBScheme adapter for MDB2)
  23. exit(1);
  24. }
  25. $typeMap = array(
  26. 'int' => 'Integer',
  27. 'int2' => 'Integer',
  28. 'int4' => 'Integer',
  29. 'int8' => 'Integer',
  30. 'tinyint' => 'Integer',
  31. 'smallint' => 'Integer',
  32. 'bigint' => 'Integer',
  33. 'char' => 'String',
  34. 'varchar' => 'String',
  35. 'bpchar' => 'String',
  36. 'tinytext' => 'String',
  37. 'text' => 'Text',
  38. 'mediumtext' => 'Text',
  39. 'longtext' => 'Text',
  40. 'date' => 'Date',
  41. 'time' => 'Time',
  42. 'datetime' => 'DateTime',
  43. 'timestamp' => 'DateTime',
  44. 'numeric' => 'Decimal',
  45. 'decimal' => 'Decimal',
  46. 'float8' => 'Float',
  47. 'float' => 'Float',
  48. 'double' => 'Float',
  49. 'bool' => 'Bool',
  50. 'boolean' => 'Bool', //NOTE: MySQL bool is TINYINT -- we can't tell
  51. 'blob' => 'Binary',
  52. 'longblob' => 'Binary',
  53. 'tinyblob' => 'Binary',
  54. 'varbinary' => 'Binary',
  55. 'binary' => 'Binary',
  56. 'mediumblob' => 'Binary',
  57. 'enum' => 'String', //For now we don't fully support enums...
  58. );
  59. $provider = $argv[1];
  60. $dsn = $argv[2];
  61. //turn off all portability to get most correct representation
  62. $mdb = @MDB2::factory( $dsn, array( 'portability' => MDB2_PORTABILITY_NONE ) );
  63. check_error( $mdb );
  64. $mdb->loadModule('Manager');
  65. $mdb->loadModule('Reverse');
  66. print( "/* File generated by dump_provider */\n" );
  67. print( "provider $provider {
  68. definition incomplete;
  69. dbType {$mdb->dbsyntax};
  70. " );
  71. $tables = $mdb->listTables();
  72. check_error( $tables );
  73. $views = $mdb->listViews();
  74. check_error( $views );
  75. $tablesViews = array_merge( $tables, $views );
  76. foreach( $tablesViews as $table ) {
  77. print( "\ttable $table {\n" );
  78. //NOTE: http://pear.php.net/bugs/bug.php?id=15100
  79. $mdb->setOption( 'quote_identifier', true );
  80. $fields = $mdb->listTableFields( $table );
  81. check_error( $fields );
  82. foreach( $fields as $field ) {
  83. //obtain definition
  84. $decl = $mdb->getTableFieldDefinition( $table, $field );
  85. check_error( $decl );
  86. $decl = $decl[0];
  87. //determine native type
  88. if( !array_key_exists( $decl['nativetype'], $typeMap ) ) {
  89. perror( "WARNING: Unknown nativetype: {$decl['nativetype']}\n" );
  90. print( "\t\t/*Omitting $field: Unknown nativetype: {$decl['nativetype']}*/\n" );
  91. continue;
  92. }
  93. $type = $typeMap[$decl['nativetype']];
  94. $ext = '';
  95. //TODO: skip for now since not supported
  96. if( $type == 'Binary' )
  97. {
  98. perror( "WARNING: Omitting $field: Binary fields not yet supported\n" );
  99. print( "\t\t/*Omitting $field: Binary fields not yet supported*/\n" );
  100. continue;
  101. }
  102. //consider Decimals with no fractional part to be Integers
  103. if( $type == 'Decimal' && isset( $decl['length'] ) ) {
  104. $digits = split( ',', $decl['length'] );
  105. if( count( $digits ) < 2 || $digits[1] == 0 )
  106. $type = 'Integer';
  107. }
  108. //consider autincrement fields to be last_insert_id
  109. if( isset($decl['autoincrement']) && $decl['autoincrement'] )
  110. $ext = ' LAST_INSERT_ID';
  111. //write defintion
  112. print( "\t\t$field<$type>$ext;\n" );
  113. }
  114. print( "\t}\n" );
  115. }
  116. print( "}\n" );
  117. ?>