File: util.cpp

package info (click to toggle)
qdjango 0.6.2-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, trixie
  • size: 908 kB
  • sloc: cpp: 7,818; sh: 32; makefile: 12; javascript: 1
file content (69 lines) | stat: -rw-r--r-- 2,097 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * Copyright (C) 2010-2015 Jeremy Lainé
 * Contact: https://github.com/jlaine/qdjango
 *
 * This file is part of the QDjango Library.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

#include <cstdlib>

#include "QDjango.h"

#include "util.h"

bool initialiseDatabase()
{
    char *p;

    // enable SQL debugging
    if ((p = getenv("QDJANGO_DB_DEBUG")) != 0)
        QDjango::setDebugEnabled(true);

    // open database
    QString databaseDriver = "QSQLITE";
    if ((p = getenv("QDJANGO_DB_DRIVER")) != 0)
        databaseDriver = QString::fromLocal8Bit(p);
    QSqlDatabase db = QSqlDatabase::addDatabase(databaseDriver);

    if ((p = getenv("QDJANGO_DB_NAME")) != 0)
        db.setDatabaseName(QString::fromLocal8Bit(p));
    else if (databaseDriver == "QSQLITE")
        db.setDatabaseName(":memory:");

    if ((p = getenv("QDJANGO_DB_USER")) != 0)
        db.setUserName(QString::fromLocal8Bit(p));

    if ((p = getenv("QDJANGO_DB_PASSWORD")) != 0)
        db.setPassword(QString::fromLocal8Bit(p));

    if ((p = getenv("QDJANGO_DB_HOST")) != 0)
        db.setHostName(QString::fromLocal8Bit(p));

    if (db.open()) {
        QDjango::setDatabase(db);
        return true;
    } else {
        return false;
    }
}

QString normalizeSql(const QSqlDatabase &db, const QString &sql)
{
    QDjangoDatabase::DatabaseType databaseType = QDjangoDatabase::databaseType(db);
    QString modSql(sql);
    if (databaseType == QDjangoDatabase::MySqlServer)
        modSql.replace("`", "\"");
    else if (databaseType == QDjangoDatabase::SQLite)
        modSql.replace("? ESCAPE '\\'", "?");
    return modSql;
}