summaryrefslogtreecommitdiffstats
path: root/kbarcode/sqltables.h
blob: d57adcfbb11b3c72dafa65f1e85475cc06650ba6 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/***************************************************************************
                          sqltables.h  -  description
                             -------------------
    begin                : Son Dez 29 2002
    copyright            : (C) 2002 by Dominik Seichter
    email                : domseichter@web.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef SQLTABLES_H
#define SQLTABLES_H

#include <tqobject.h>
#include <tqwidget.h>
#include <tqstring.h>


/*  Change these values to match your environment: */
#define TABLE_CUSTOMER "customer"
#define TABLE_CUSTOMER_TEXT "customer_text"
#define TABLE_LABEL_DEF "label_def"
#define TABLE_BASIC "barcode_basic"
/* -------- */

// Holds all information
// necessary for connecting
// to a database
struct mysqldata {
    TQString username;
    TQString password;
    TQString hostname;   // localhost
    TQString database;   // kbarcode
    TQString driver;     // QMYSQL3
    bool autoconnect;

    void operator=( const mysqldata & rhs ) {
        username = rhs.username;
        password = rhs.password;
        hostname = rhs.hostname;
        database = rhs.database;
        driver   = rhs.driver;

        autoconnect = rhs.autoconnect;        
    }
};


/**
  * This class describes a SQL database (e.g. MySQL or PostgreSQL)
  * and its features.
  * @author Dominik Seichter
  */
class SqlDescription {
    public:
        /** return the name of the database we can always
          * use to connect to this database.
          */
        virtual const TQString initdb( TQString ) const = 0;

        /** return the auto_increment or serial command used for this db.
          */
        virtual const TQString autoIncrement() const = 0;

        /** return the command to show all fields of table for this db.
          */
        virtual const TQString showColumns( const TQString & table ) const  = 0;
};

class TQSqlDatabase;
class TQSqlQuery;
/**
  * This class is used to access a database, create it and save its settings to the
  * application configuration file. Also the labeldefinitions and exmplate data
  * are imported using this class.
  *
  * @short This singleton is used to access a database in KBarcode.
  * @author Dominik Seichter
  */
class SqlTables : public TQObject {
    TQ_OBJECT
  
    public:
        /** get a mysqldata object which contains the current database settings.
          * this object is for all sql databases, not only for mysql. The name is just a
          * relict from the past ;-)
          * @return mysqldata*
          */
        const mysqldata & getData() const {
            return sqldata;
        }

        /** Set the database settings
         */
        void setData( const mysqldata & data ) {
            sqldata = data;
        }
    
        /** get an instance of this singleton class. Always use this method as there is
          * no public constructor.
          * @return SqlTables*
          */
        static SqlTables* getInstance();

        /** returns true if KBarcode is connected to a database at the moment. Returns false if not.
          * @return bool
          */
        static bool isConnected() {
            return getInstance()->connected;
        }

        const SqlDescription* driver() const;

    public slots:
        const TQString getBarcodeMaxLength( const TQString & name );
        
        void loadConfig();
        void saveConfig();

        /** Connect to a SQL database using breviously configured
          * database settings.
          * @return bool returns true on success
          */
        bool connectMySQL();
        
        bool newTables();
        bool newTables( const TQString & username, const TQString & password, const TQString & hostname, const TQString & database, const TQString & driver );
        void importLabelDef();
        void importExampleData();
        void importData( const TQString & filename, TQSqlDatabase* db );
        bool testSettings( const TQString & username, const TQString & password, const TQString & hostname, const TQString & database, const TQString & driver );

	inline TQSqlDatabase* database() const;

    signals:
        void tablesChanged();
        void connectedSQL();

    private:
        SqlTables( TQObject* parent = 0 );
        ~SqlTables();

        void exec( TQSqlQuery* query, const TQString & text );
        void updateTables();
        
        bool connected;

        TQSqlDatabase* db;
        mysqldata sqldata;

        static SqlTables* instance;
};

inline TQSqlDatabase* SqlTables::database() const
{
    return db;
}

class KComboBox;
class KLineEdit;
class KPushButton;
class TQCheckBox;
/** A widget for configuring a SQL connection.
  * It has child widgets for entering data like the
  * user name, database name, host name, password
  * and the driver. Optionally the user can also
  * test if his settings will work.
  *
  * @author Dominik Seichter
  */
class SqlWidget : public TQWidget {
    TQ_OBJECT
  
    public:
        SqlWidget( bool showlabel, TQWidget* parent = 0, const char* name = 0 );
        ~SqlWidget();

        const TQString username() const;
        const TQString driver() const;
        const TQString database() const;
        const TQString hostname() const;
        const TQString password() const;
        bool autoconnect() const;

        int driverCount() const;

        void save( bool usedb = true );

    signals:
        void databaseWorking( bool b );
        
    private slots:
        void testSettings();
                
    private:
        KComboBox* m_driver;
    
        KLineEdit* m_username;
        KLineEdit* m_database;
        KLineEdit* m_hostname;
        KLineEdit* m_password;

        TQCheckBox* m_autoconnect;

        KPushButton* buttonTest;        
};

#endif