Browse Source

Formatting and minor adjustments

Erik Winn 6 years ago
parent
commit
711978e9a4
2 changed files with 80 additions and 68 deletions
  1. 78
    66
      examples/test_wmysqlresultset.cpp
  2. 2
    2
      src/wormgen.cpp

+ 78
- 66
examples/test_wmysqlresultset.cpp View File

@@ -3,71 +3,83 @@
3 3
 
4 4
 int main()
5 5
 {
6
-    WSql::WSqlDatabase db(WSql::WMYSQL);
7
-    db.setDatabaseName(std::string("sakila"));
8
-    db.setUserName("root");
9
-    
10
-    if (!db.open()) {
11
-        std::cerr << "Failed to open: " << db.error().text() << std::endl;
12
-        return 1;
13
-    }
14
-    std::vector<std::string>tables = db.tableNames();
15
-    if(db.hasError())
16
-        std::cout << "error: " << db.error().text() << std::endl;
17
-    
18
-    std::vector<std::string>::iterator it = tables.begin();
19
-    WSql::WSqlTable stbl;
20
-    int numflds = 0;
21
-    int row = 0;
22
-    while (it != tables.end()) 
23
-    {
24
-        std::vector<std::string> column_names;
25
-        stbl = db.tableMetaData(*it);
26
-        numflds = stbl.count();
27
-        std::cout << "   ======== Table name: " << stbl.name() 
28
-        << " =========" << std::endl;
29
-        for (int i=0; i < numflds; ++i) 
30
-        {
31
-            WSql::WSqlColumn clm = stbl.column(i);
32
-            column_names.push_back(clm.columnName());
33
-            
34
-            std::cout << "Column " << i << " = " << clm.columnName() << std::endl;
35
-            std::cout << "  * Data type: " << WSql::WSqlDataType::toString(clm.dataType()) << std::endl;
36
-            std::cout << "  * Max Length:  " << clm.maxLength() << std::endl;
37
-            std::cout << "  * Unsigned:  " << (clm.isUnsigned() ? "true" : "false") << std::endl;
38
-            std::cout << "  * Can be null:  " << (clm.canBeNull() ? "true" : "false") << std::endl;
39
-            std::cout << "  * Primary key:  " << (clm.isPrimaryKey() ? "true" : "false") << std::endl;
40
-            std::cout << "  * Autoincrement:  "  << (clm.isAutoIncremented()?"true" : "false") << std::endl;
41
-            std::cout << "  * default value:  "  << clm.defaultValue<std::string>() << std::endl;
42
-        }
43
-        
44
-        std::string sql = "select * from " + *it + " limit 2;";
45
-        if(!db.doQuery(sql))
46
-        {
47
-            std::cout << "Query Failed: " << db.error().text() << std::endl;
48
-            it++;
49
-            continue;
50
-        }else{
51
-            
52
-            WSql::WSqlResult *result = db.getResult();
53
-            WSql::WSqlRecord record = result->fetchFirst();
54
-            while(!record.empty())
55
-            {
56
-                std::cout << "Record Size: " << record.size() << std::endl;
57
-                WSql::WSqlField fld;
58
-                int pt = 0;
59
-                for(;pt < record.size();pt++)
60
-                {
6
+	WSql::WSqlDatabase db ( WSql::WMYSQL );
7
+	db.setDatabaseName ( std::string ( "sakila" ) );
8
+	db.setUserName ( "root" );
9
+
10
+	if ( !db.open() )
11
+	{
12
+		std::cerr << "Failed to open: " << db.error().text() << std::endl;
13
+		return 1;
14
+	}
15
+
16
+	std::vector<std::string>tables = db.tableNames();
17
+
18
+	if ( db.hasError() )
19
+		std::cout << "error: " << db.error().text() << std::endl;
20
+
21
+	std::vector<std::string>::iterator it = tables.begin();
22
+	WSql::WSqlTable stbl;
23
+	int numflds = 0;
24
+	int row = 0;
25
+
26
+	while ( it != tables.end() )
27
+	{
28
+		std::vector<std::string> column_names;
29
+		stbl = db.tableMetaData ( *it );
30
+		numflds = stbl.count();
31
+		std::cout << "   ======== Table name: " << stbl.name()
32
+				  << " =========" << std::endl;
33
+
34
+		for ( int i = 0; i < numflds; ++i )
35
+		{
36
+			WSql::WSqlColumn clm = stbl.column ( i );
37
+			column_names.push_back ( clm.columnName() );
38
+
39
+			std::cout << "Column " << i << " = " << clm.columnName() << std::endl;
40
+			std::cout << "  * Data type: " << WSql::WSqlDataType::toString ( clm.dataType() ) << std::endl;
41
+			std::cout << "  * Max Length:  " << clm.maxLength() << std::endl;
42
+			std::cout << "  * Unsigned:  " << ( clm.isUnsigned() ? "true" : "false" ) << std::endl;
43
+			std::cout << "  * Can be null:  " << ( clm.canBeNull() ? "true" : "false" ) << std::endl;
44
+			std::cout << "  * Primary key:  " << ( clm.isPrimaryKey() ? "true" : "false" ) << std::endl;
45
+			std::cout << "  * Autoincrement:  "  << ( clm.isAutoIncremented() ? "true" : "false" ) << std::endl;
46
+			std::cout << "  * default value:  "  << clm.defaultValue<std::string>() << std::endl;
47
+		}
48
+
49
+		std::string sql = "select * from " + *it + " limit 2;";
50
+
51
+		if ( !db.doQuery ( sql ) )
52
+		{
53
+			std::cout << "Query Failed: " << db.error().text() << std::endl;
54
+			it++;
55
+			continue;
56
+		}
57
+		else
58
+		{
59
+
60
+			WSql::WSqlResult *result = db.getResult();
61
+			WSql::WSqlRecord record = result->fetchFirst();
62
+
63
+			while ( !record.empty() )
64
+			{
65
+				std::cout << "Record Size: " << record.size() << std::endl;
66
+				WSql::WSqlField fld;
67
+				int pt = 0;
68
+
69
+				for ( ; pt < record.size(); pt++ )
70
+				{
61 71
 //                    std::cout << "data for " << column_names[pt] << ": " << record[column_names[pt]] << std::endl;
62
-                    
63
-                    //no danger - if there is nothing there we still get an empty field object 
64
-                    fld = record.field(pt);
65
-                    std::cout << "Field " << fld.name()<< ", Origin Column "<< fld.columnName() << ": " 
66
-                        << fld.data<std::string>() << std::endl;
67
-                }
68
-                record = result->fetchNext();
69
-            }
70
-        }
71
-        it++;
72
-    }
72
+
73
+					//no danger - if there is nothing there we still get an empty field object
74
+					fld = record.field ( pt );
75
+					std::cout << "Field " << fld.name() << ", Origin Column " << fld.columnName() << ": "
76
+							  << fld.data<std::string>() << std::endl;
77
+				}
78
+
79
+				record = result->fetchNext();
80
+			}
81
+		}
82
+
83
+		it++;
84
+	}
73 85
 }

+ 2
- 2
src/wormgen.cpp View File

@@ -125,7 +125,7 @@ int main( int argc, char ** argv )
125 125
     }
126 126
     closedir(dir);
127 127
     if ( dbname.empty() ) {
128
-        std::cerr << PACKAGE << ": Error - no database specified! \n" << std::endl;
128
+        std::cerr << PACKAGE << "FATAL: Error - no database specified! \n" << std::endl;
129 129
         print_help(3);
130 130
     }
131 131
 
@@ -137,7 +137,7 @@ int main( int argc, char ** argv )
137 137
     db.setPassword( password );
138 138
 
139 139
     if ( !db.open() ) {
140
-        std::cerr << "Failed to open: " << db.error().text() << std::endl;
140
+        std::cerr << "FATAL: Failed to open database - error: " << db.error().text() << std::endl;
141 141
         return 4;
142 142
     }
143 143
 

Loading…
Cancel
Save