php pdo:是否可以有一个与 DBMS 无关的“描述表”?

发布于 2024-08-24 13:49:33 字数 227 浏览 2 评论 0原文

我有一个脚本可以解析数据库并创建 php 类来使用该数据库。 (zend-db-模型生成器)。有没有办法在 php 中有一个 DBMS agonstic 命令,以便在每种类型的数据库上它都会返回表结构?

因此,如果我对 mysql 使用一次 pdo 驱动程序,对 postgresql 使用一次,这并不重要,它仍然会返回表结构。

我现在看到的唯一解决方案是获取数据库类型并打开每个数据库类型并执行适当的命令。

I have a script that parse the database and create php classes to work with that db. (zend-db-model-generator). is there a way to in php to have a DBMS agonstic commands so on each type of db it will return the table structure ?

so it doesn't matter if i use the pdo driver once for mysql and once for postgresql, it will still return the table structure.

the only solution that i see for now is to get the db type and to switch on each db type and execute the appropriate command.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趴在窗边数星星i 2024-08-31 13:49:33

不幸的是,据我所知,使用相同的 SQL 查询似乎不太可能......

关于您通过数据库提取有关每个表的信息以生成一些 PHP 类的想法,这是 Doctrine(最著名的 PHP ORM)确实如此。

如果您下载它,并查看 Doctrine/Import/[DatabaseType].php 中的类,您会发现每种数据库的执行方式都不同。

例如,对于 MySQL,在 Doctrine_Import_Mysql 中使用以下代码:

'listTableFields' => 'DESCRIBE %s',

,对于 PostgreSQL,在 Doctrine_Import_Pgsql 中使用以下代码:

'listTableColumns'     => "SELECT
                            a.attnum,
                            a.attname AS field,
                            t.typname AS type,
                            format_type(a.atttypid, a.atttypmod) AS complete_type,
                            a.attnotnull AS isnotnull,
                            (SELECT 't'
                              FROM pg_index
                              WHERE c.oid = pg_index.indrelid
                              AND a.attnum = ANY (pg_index.indkey)
                              AND pg_index.indisprimary = 't'
                            ) AS pri,
                            (SELECT pg_attrdef.adsrc
                              FROM pg_attrdef
                              WHERE c.oid = pg_attrdef.adrelid
                              AND pg_attrdef.adnum=a.attnum
                            ) AS default
                      FROM pg_attribute a, pg_class c, pg_type t
                      WHERE c.relname = %s
                            AND a.attnum > 0
                            AND a.attrelid = c.oid
                            AND a.atttypid = t.oid
                      ORDER BY a.attnum",

另一方面 看起来很简单 ^^

而且,在每个类的更下方,有一个名为 listTableColumns 的方法 - 对于每种数据库类型来说,该方法都不相同......

因此,不幸的是,我猜事情不会像您那么简单希望...

但是,作为旁注:也许你可以在项目的这一部分中使用 Doctrine ——可能比重新发明轮子更快;-)

Using the same SQL query, unfortunatly, doesn't seem quite possible, as far as I can tell...

About your idea of going through the database to extract informations about each table to generate some PHP classes, that's one of the things that Doctrine (the most famous PHP ORM) does.

If you download it, and take a look at the classes in Doctrine/Import/[DatabaseType].php, you'll see this is done differently for each kind of database.

For instance, for MySQL, the following piece of code is used in Doctrine_Import_Mysql :

'listTableFields' => 'DESCRIBE %s',

On the other hand, for PostgreSQL, you've got the following, in Doctrine_Import_Pgsql :

'listTableColumns'     => "SELECT
                            a.attnum,
                            a.attname AS field,
                            t.typname AS type,
                            format_type(a.atttypid, a.atttypmod) AS complete_type,
                            a.attnotnull AS isnotnull,
                            (SELECT 't'
                              FROM pg_index
                              WHERE c.oid = pg_index.indrelid
                              AND a.attnum = ANY (pg_index.indkey)
                              AND pg_index.indisprimary = 't'
                            ) AS pri,
                            (SELECT pg_attrdef.adsrc
                              FROM pg_attrdef
                              WHERE c.oid = pg_attrdef.adrelid
                              AND pg_attrdef.adnum=a.attnum
                            ) AS default
                      FROM pg_attribute a, pg_class c, pg_type t
                      WHERE c.relname = %s
                            AND a.attnum > 0
                            AND a.attrelid = c.oid
                            AND a.atttypid = t.oid
                      ORDER BY a.attnum",

Not that easy, it seems ^^

And, farther down each class, there is a method called listTableColumns -- which is not the same for each database type...

So I'm guessing things will, unfortunatly, not be as simple as you hoped...

But, as a sidenote : maybe you could use Doctrine for that part of your project -- might be faster than re-inventing the wheel ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文